refactor: adjust event options handling to be JSX friendly

This commit is contained in:
Evan You
2020-07-14 13:20:59 -04:00
parent e3d30ba26f
commit 00ab9e2e85
6 changed files with 33 additions and 37 deletions

View File

@@ -174,7 +174,7 @@ describe('component: emit', () => {
const fn = jest.fn()
render(
h(Foo, {
'onFoo.once': fn
onFooOnce: fn
}),
nodeOps.createElement('div')
)
@@ -213,8 +213,8 @@ describe('component: emit', () => {
test('.once listeners', () => {
const def2 = { emits: { click: null } }
expect(isEmitListener(def2, 'onClick.once')).toBe(true)
expect(isEmitListener(def2, 'onclick.once')).toBe(false)
expect(isEmitListener(def2, 'onClickOnce')).toBe(true)
expect(isEmitListener(def2, 'onclickOnce')).toBe(false)
})
})
})

View File

@@ -76,7 +76,7 @@ export function emit(
handler = props[handlerName]
}
if (!handler) {
handler = props[handlerName + `.once`]
handler = props[handlerName + `Once`]
if (!instance.emitted) {
;(instance.emitted = {} as Record<string, boolean>)[handlerName] = true
} else if (instance.emitted[handlerName]) {
@@ -136,7 +136,7 @@ export function isEmitListener(comp: Component, key: string): boolean {
if (!isOn(key) || !(emits = normalizeEmitsOptions(comp))) {
return false
}
key = key.replace(/\.once$/, '')
key = key.replace(/Once$/, '')
return (
hasOwn(emits, key[2].toLowerCase() + key.slice(3)) ||
hasOwn(emits, key.slice(2))