Skip to content

Commit

Permalink
fix: also listen for original case
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Nov 11, 2022
1 parent 3670f0f commit b7b934d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
24 changes: 19 additions & 5 deletions packages/runtime-dom/__tests__/customElement.spec.ts
Expand Up @@ -234,7 +234,9 @@ describe('defineCustomElement', () => {
h('div', {
onClick: () => {
emit('my-click', 1)
emit('myClick', 1) // validate hypenization
},
onMousedown: () => {
emit('myEvent', 1) // validate hypenization
}
})
}
Expand All @@ -255,13 +257,25 @@ describe('defineCustomElement', () => {
const spy = jest.fn()
e.addEventListener('my-click', spy)
e.shadowRoot!.childNodes[0].dispatchEvent(new CustomEvent('click'))
expect(spy).toHaveBeenCalledTimes(2)
expect(spy).toHaveBeenCalledTimes(1)
expect(spy.mock.calls[0][0]).toMatchObject({
detail: [1]
})
expect(spy.mock.calls[1][0]).toMatchObject({
detail: [1]
})
})

// #5373
test('case transform for camelCase event', () => {
container.innerHTML = `<my-el-emits></my-el-emits>`
const e = container.childNodes[0] as VueElement
const spy1 = jest.fn()
e.addEventListener('myEvent', spy1)
const spy2 = jest.fn()
// emitting myEvent, but listening for my-event. This happens when
// using the custom element in a Vue template
e.addEventListener('my-event', spy2)
e.shadowRoot!.childNodes[0].dispatchEvent(new CustomEvent('mousedown'))
expect(spy1).toHaveBeenCalledTimes(1)
expect(spy2).toHaveBeenCalledTimes(1)
})
})

Expand Down
15 changes: 12 additions & 3 deletions packages/runtime-dom/src/apiCustomElement.ts
Expand Up @@ -351,15 +351,24 @@ export class VueElement extends BaseClass {
}
}

// intercept emit
instance.emit = (event: string, ...args: any[]) => {
const dispatch = (event: string, args: any[]) => {
this.dispatchEvent(
new CustomEvent(hyphenate(event), {
new CustomEvent(event, {
detail: args
})
)
}

// intercept emit
instance.emit = (event: string, ...args: any[]) => {
// dispatch both the raw and hyphenated versions of an event
// to match Vue behavior
dispatch(event, args)
if (hyphenate(event) !== event) {
dispatch(hyphenate(event), args)
}
}

// locate nearest Vue custom element parent for provide/inject
let parent: Node | null = this
while (
Expand Down

0 comments on commit b7b934d

Please sign in to comment.