From d7e859f2be2c107f62eb04a65876120e21594443 Mon Sep 17 00:00:00 2001 From: shadowings-zy Date: Thu, 21 Apr 2022 16:21:12 +0800 Subject: [PATCH] fix(runtime-core): only trim string parameter when work with v-model on component (#5765) --- .../__tests__/componentEmits.spec.ts | 22 +++++++++++++++++++ packages/runtime-core/src/componentEmits.ts | 2 +- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/packages/runtime-core/__tests__/componentEmits.spec.ts b/packages/runtime-core/__tests__/componentEmits.spec.ts index 8963bda2649..cc275450935 100644 --- a/packages/runtime-core/__tests__/componentEmits.spec.ts +++ b/packages/runtime-core/__tests__/componentEmits.spec.ts @@ -355,6 +355,28 @@ describe('component: emit', () => { expect(fn2).toHaveBeenCalledWith('two') }) + test('only trim string parameter when work with v-model on component', () => { + const Foo = defineComponent({ + render() {}, + created() { + this.$emit('update:modelValue', ' foo ', { bar: ' bar ' }) + } + }) + + const fn = jest.fn() + const Comp = () => + h(Foo, { + modelValue: null, + modelModifiers: { trim: true }, + 'onUpdate:modelValue': fn + }) + + render(h(Comp), nodeOps.createElement('div')) + + expect(fn).toHaveBeenCalledTimes(1) + expect(fn).toHaveBeenCalledWith('foo', { bar: ' bar ' }) + }) + test('isEmitListener', () => { const options = { click: null, diff --git a/packages/runtime-core/src/componentEmits.ts b/packages/runtime-core/src/componentEmits.ts index 3983e5885b3..7786371eff7 100644 --- a/packages/runtime-core/src/componentEmits.ts +++ b/packages/runtime-core/src/componentEmits.ts @@ -121,7 +121,7 @@ export function emit( }Modifiers` const { number, trim } = props[modifiersKey] || EMPTY_OBJ if (trim) { - args = rawArgs.map(a => a.trim()) + args = rawArgs.map(a => (typeof a === 'string' ? a.trim() : a)) } else if (number) { args = rawArgs.map(toNumber) }