From aeb8072ce53b48ddc316be02eff78659523fe163 Mon Sep 17 00:00:00 2001 From: zhaozhongyu Date: Mon, 25 Oct 2021 17:50:27 +0800 Subject: [PATCH 1/2] fix(runtime-core): v-model Modifiers trim/number did not work if use hyphenate string in props and camelize in emits --- packages/runtime-core/src/componentEmits.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/runtime-core/src/componentEmits.ts b/packages/runtime-core/src/componentEmits.ts index 390c6350b8c..f746a123c61 100644 --- a/packages/runtime-core/src/componentEmits.ts +++ b/packages/runtime-core/src/componentEmits.ts @@ -113,7 +113,8 @@ export function emit( const isModelListener = event.startsWith('update:') // for v-model update:xxx events, apply modifiers on args - const modelArg = isModelListener && event.slice(7) + const eventArg = isModelListener && event.slice(7) + const modelArg = eventArg && (hyphenate(eventArg) in props ? hyphenate(eventArg) : camelize(eventArg)) if (modelArg && modelArg in props) { const modifiersKey = `${ modelArg === 'modelValue' ? 'model' : modelArg From 9813e0ecad3199b18d1e191530e9ce2e2d032fe1 Mon Sep 17 00:00:00 2001 From: zhaozhongyu Date: Mon, 25 Oct 2021 18:54:19 +0800 Subject: [PATCH 2/2] update testcases --- .../__tests__/componentEmits.spec.ts | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/packages/runtime-core/__tests__/componentEmits.spec.ts b/packages/runtime-core/__tests__/componentEmits.spec.ts index d88981a43d4..41dcf66a662 100644 --- a/packages/runtime-core/__tests__/componentEmits.spec.ts +++ b/packages/runtime-core/__tests__/componentEmits.spec.ts @@ -353,6 +353,54 @@ describe('component: emit', () => { expect(fn2).toHaveBeenCalledTimes(1) expect(fn2).toHaveBeenCalledWith('two') }) + // for hyphenate props and camelize emit + test('.trim modifier should work with v-model on component for hyphenate props and camelize emit', () => { + const Foo = defineComponent({ + render() {}, + created() { + this.$emit('update:firstName', ' one ') + } + }) + + const fn1 = jest.fn() + + const Comp = () => + h(Foo, { + 'first-name': null, + 'first-nameModifiers': { trim: true }, + 'onUpdate:first-name': fn1, + }) + + render(h(Comp), nodeOps.createElement('div')) + + expect(fn1).toHaveBeenCalledTimes(1) + expect(fn1).toHaveBeenCalledWith('one') + }) + + // for camelize props and hyphenate emit + test('.trim modifier should work with v-model on component for camelize props and hyphenate emit', () => { + const Foo = defineComponent({ + render() {}, + created() { + this.$emit('update:first-name', ' one ') + } + }) + + const fn1 = jest.fn() + + const Comp = () => + h(Foo, { + firstName: null, + firstNameModifiers: { trim: true }, + 'onUpdate:firstName': fn1, + + }) + + render(h(Comp), nodeOps.createElement('div')) + + expect(fn1).toHaveBeenCalledTimes(1) + expect(fn1).toHaveBeenCalledWith('one') + }) test('isEmitListener', () => { const options = {