From 018b8503994c2dae3c42dd03ea50d062956be970 Mon Sep 17 00:00:00 2001 From: shadowings-zy Date: Wed, 26 Oct 2022 17:47:05 +0800 Subject: [PATCH] fix(v-model): fix trim modifier on events with non-string args ( (#5770) fix #5765 --- .../__tests__/componentEmits.spec.ts | 22 +++++++++++++++++++ packages/runtime-core/src/componentEmits.ts | 3 ++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/packages/runtime-core/__tests__/componentEmits.spec.ts b/packages/runtime-core/__tests__/componentEmits.spec.ts index f22be2de0c1..0c196c763e0 100644 --- a/packages/runtime-core/__tests__/componentEmits.spec.ts +++ b/packages/runtime-core/__tests__/componentEmits.spec.ts @@ -385,6 +385,28 @@ describe('component: emit', () => { expect(fn2).toHaveBeenCalledTimes(1) expect(fn2).toHaveBeenCalledWith(1) }) + + 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 = { diff --git a/packages/runtime-core/src/componentEmits.ts b/packages/runtime-core/src/componentEmits.ts index b113eaa5357..03835b53f20 100644 --- a/packages/runtime-core/src/componentEmits.ts +++ b/packages/runtime-core/src/componentEmits.ts @@ -8,6 +8,7 @@ import { isArray, isFunction, isObject, + isString, isOn, toNumber, UnionToIntersection @@ -122,7 +123,7 @@ export function emit( }Modifiers` const { number, trim } = props[modifiersKey] || EMPTY_OBJ if (trim) { - args = rawArgs.map(a => a.trim()) + args = rawArgs.map(a => isString(a) ? a.trim() : a) } if (number) { args = rawArgs.map(toNumber)