fix(popover): event listener will be removed before called

This commit is contained in:
07akioni 2019-10-30 17:53:09 +08:00
parent d347cf8ebf
commit 18c6d47992
3 changed files with 42 additions and 8 deletions

View File

@ -1,6 +1,6 @@
# Custom Action
```html
<n-popconfirm :controller="controller">
<n-popconfirm>
<template v-slot:activator>
<n-button>Quit</n-button>
</template>

View File

@ -133,16 +133,44 @@ export default {
registerListeners () {
const el = this.$el
if (el) {
el.removeEventListener('click', this.handleClick)
el.removeEventListener('mouseenter', this.handleMouseEnter)
el.removeEventListener('mouseleave', this.handleMouseLeave)
if (!el['@n-popover-context']) el['@n-popover-context'] = {}
const elContext = el['@n-popover-context']
if (this.triggeredByClick) {
/// console.log('register click')
if (!(elContext.handleClick === this.handleClick)) {
el.removeEventListener('click', elContext.handleClick)
el.addEventListener('click', this.handleClick)
elContext.handleClick = this.handleClick
}
if (elContext.handleMouseEnter) {
el.removeEventListener('mouseenter', elContext.handleMouseEnter)
el.removeEventListener('mouseenter', this.handleMouseEnter)
elContext.handleMouseEnter = null
}
if (elContext.handleMouseLeave) {
el.removeEventListener('mouseenter', elContext.handleMouseLeave)
el.removeEventListener('mouseleave', this.handleMouseLeave)
elContext.handleMouseLeave = null
}
// console.log('register click')
// debugger
el.addEventListener('click', this.handleClick)
} else if (this.triggeredByHover) {
// console.log('register hover')
el.addEventListener('mouseenter', this.handleMouseEnter)
el.addEventListener('mouseleave', this.handleMouseLeave)
if (!(elContext.handleMouseLeave === this.handleMouseLeave)) {
el.removeEventListener('mouseleave', elContext.handleMouseLeave)
el.addEventListener('mouseleave', this.handleMouseLeave)
elContext.handleMouseLeave = this.handleMouseLeave
}
if (!(elContext.handleMouseEnter === this.handleMouseEnter)) {
el.removeEventListener('mouseenter', elContext.handleMouseEnter)
el.addEventListener('mouseenter', this.handleMouseEnter)
elContext.handleMouseEnter = this.handleMouseEnter
}
if (elContext.handleClick) {
el.removeEventListener('click', elContext.handleMouseEnter)
el.removeEventListener('click', this.handleMouseEnter)
elContext.handleMouseEnter = null
}
}
}
}

View File

@ -66,6 +66,12 @@ export default {
const slots = context.scopedSlots
const defaultSlot = slots.default && slots.default()
const activatorSlot = slots.activator && slots.activator()
let activatorVNode = activatorSlot[0]
if (!activatorVNode.tag) {
activatorVNode = h('span', {
staticClass: 'n-popover-text-wrapper'
}, [activatorVNode])
}
const id = genId()
const props = context.props
const listeners = context.listeners
@ -77,7 +83,7 @@ export default {
controller,
id
}
}, [activatorSlot[0]]),
}, [activatorVNode]),
h(NBasePortal, {}, [
h(NPopoverContent, {
props: {