Skip to content

Commit b302cbb

Browse files
fix: kebab-case events are attached correctly on web components, see #2841 (#2847)
1 parent 1cc8712 commit b302cbb

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

packages/runtime-dom/__tests__/patchEvents.spec.ts

+26
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,30 @@ describe(`runtime-dom: events patching`, () => {
153153
expect(prevFn).toHaveBeenCalledTimes(2)
154154
expect(nextFn).toHaveBeenCalledTimes(4)
155155
})
156+
157+
// #2841
158+
test('should patch event correctly in web-components', async () => {
159+
class TestElement extends HTMLElement {
160+
constructor() {
161+
super()
162+
}
163+
}
164+
window.customElements.define('test-element', TestElement)
165+
const testElement = document.createElement('test-element', {
166+
is: 'test-element'
167+
})
168+
const fn1 = jest.fn()
169+
const fn2 = jest.fn()
170+
171+
// in webComponents, @foo-bar will patch prop 'onFooBar'
172+
// and @foobar will patch prop 'onFoobar'
173+
174+
patchProp(testElement, 'onFooBar', null, fn1)
175+
testElement.dispatchEvent(new CustomEvent('foo-bar'))
176+
expect(fn1).toHaveBeenCalledTimes(1)
177+
178+
patchProp(testElement, 'onFoobar', null, fn2)
179+
testElement.dispatchEvent(new CustomEvent('foobar'))
180+
expect(fn2).toHaveBeenCalledTimes(1)
181+
})
156182
})

packages/runtime-dom/src/modules/events.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isArray } from '@vue/shared'
1+
import { hyphenate, isArray } from '@vue/shared'
22
import {
33
ComponentInternalInstance,
44
callWithAsyncErrorHandling
@@ -96,7 +96,7 @@ function parseName(name: string): [string, EventListenerOptions | undefined] {
9696
options
9797
}
9898
}
99-
return [name.slice(2).toLowerCase(), options]
99+
return [hyphenate(name.slice(2)), options]
100100
}
101101

102102
function createInvoker(

0 commit comments

Comments
 (0)