Skip to content

Commit 3532b2b

Browse files
fix(runtime-core): fix emit listener check on kebab-case events (#2542)
fix #2540
1 parent 2ab8c41 commit 3532b2b

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

packages/runtime-core/__tests__/componentEmits.spec.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -283,12 +283,23 @@ describe('component: emit', () => {
283283
})
284284

285285
test('isEmitListener', () => {
286-
const options = { click: null }
286+
const options = {
287+
click: null,
288+
'test-event': null,
289+
fooBar: null,
290+
FooBaz: null
291+
}
287292
expect(isEmitListener(options, 'onClick')).toBe(true)
288293
expect(isEmitListener(options, 'onclick')).toBe(false)
289294
expect(isEmitListener(options, 'onBlick')).toBe(false)
290295
// .once listeners
291296
expect(isEmitListener(options, 'onClickOnce')).toBe(true)
292297
expect(isEmitListener(options, 'onclickOnce')).toBe(false)
298+
// kebab-case option
299+
expect(isEmitListener(options, 'onTestEvent')).toBe(true)
300+
// camelCase option
301+
expect(isEmitListener(options, 'onFooBar')).toBe(true)
302+
// PascalCase option
303+
expect(isEmitListener(options, 'onFooBaz')).toBe(true)
293304
})
294305
})

packages/runtime-core/src/componentEmits.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,10 @@ export function isEmitListener(
201201
if (!options || !isOn(key)) {
202202
return false
203203
}
204-
key = key.replace(/Once$/, '')
204+
key = key.slice(2).replace(/Once$/, '')
205205
return (
206-
hasOwn(options, key[2].toLowerCase() + key.slice(3)) ||
207-
hasOwn(options, key.slice(2))
206+
hasOwn(options, key[0].toLowerCase() + key.slice(1)) ||
207+
hasOwn(options, hyphenate(key)) ||
208+
hasOwn(options, key)
208209
)
209210
}

0 commit comments

Comments
 (0)