element-plus/packages/popover/src/directive.ts
jeremywu 2280dd5c5e
fix(project): fix/exporting-fix-for-volar (#849)
- Using `defineComponent` to wrap component up for Volar support, this should close #841
- Also made changes for some typing
- Removed `merge.ts` since `Object.assign` are now supported natively
2020-12-06 23:52:47 +08:00

31 lines
907 B
TypeScript

import type { DirectiveBinding, VNode, ObjectDirective } from 'vue'
import { on } from '@element-plus/utils/dom'
interface PopoverInstance {
events: Record<string, EventListenerOrEventListenerObject>
triggerRef: HTMLElement
}
const attachEvents = (el: HTMLElement, binding: DirectiveBinding, vnode: VNode) => {
const _ref = binding.arg || binding.value
const popover = vnode.dirs[0].instance.$refs[_ref] as PopoverInstance
if (popover) {
popover.triggerRef = el
// because v-popover cannot modify the vnode itself due to it has already been
Object.entries(popover.events).map(([eventName, e]) => {
on(el, eventName.toLowerCase().slice(2), e)
})
}
}
export default {
mounted(el, binding, vnode) {
attachEvents(el, binding, vnode)
},
updated(el, binding, vnode) {
attachEvents(el, binding, vnode)
},
} as ObjectDirective
export const VPopover = 'popover'