Skip to content

Commit 018b850

Browse files
fix(v-model): fix trim modifier on events with non-string args ( (#5770)
fix #5765
1 parent bb06819 commit 018b850

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

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

+22
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,28 @@ describe('component: emit', () => {
385385
expect(fn2).toHaveBeenCalledTimes(1)
386386
expect(fn2).toHaveBeenCalledWith(1)
387387
})
388+
389+
test('only trim string parameter when work with v-model on component', () => {
390+
const Foo = defineComponent({
391+
render() {},
392+
created() {
393+
this.$emit('update:modelValue', ' foo ', { bar: ' bar ' })
394+
}
395+
})
396+
397+
const fn = jest.fn()
398+
const Comp = () =>
399+
h(Foo, {
400+
modelValue: null,
401+
modelModifiers: { trim: true },
402+
'onUpdate:modelValue': fn
403+
})
404+
405+
render(h(Comp), nodeOps.createElement('div'))
406+
407+
expect(fn).toHaveBeenCalledTimes(1)
408+
expect(fn).toHaveBeenCalledWith('foo', { bar: ' bar ' })
409+
})
388410

389411
test('isEmitListener', () => {
390412
const options = {

packages/runtime-core/src/componentEmits.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
isArray,
99
isFunction,
1010
isObject,
11+
isString,
1112
isOn,
1213
toNumber,
1314
UnionToIntersection
@@ -122,7 +123,7 @@ export function emit(
122123
}Modifiers`
123124
const { number, trim } = props[modifiersKey] || EMPTY_OBJ
124125
if (trim) {
125-
args = rawArgs.map(a => a.trim())
126+
args = rawArgs.map(a => isString(a) ? a.trim() : a)
126127
}
127128
if (number) {
128129
args = rawArgs.map(toNumber)

0 commit comments

Comments
 (0)