]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(v-model): fix trim modifier on events with non-string args ( (#5770)
authorshadowings-zy <shadowingszy@outlook.com>
Wed, 26 Oct 2022 09:47:05 +0000 (17:47 +0800)
committerGitHub <noreply@github.com>
Wed, 26 Oct 2022 09:47:05 +0000 (05:47 -0400)
fix #5765

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

index f22be2de0c1239b819c542c22fbdebcead2a2f94..0c196c763e054c0b032c35db35228f3cc1360c2a 100644 (file)
@@ -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 = {
index b113eaa53573489ea75873d0d7d0d16b2d896ca5..03835b53f20940c207e380956be3eb8f010d81b0 100644 (file)
@@ -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)