Skip to content

Commit 71066b5

Browse files
authored
fix(v-model): fix case where .trim and .number modifiers are used together (#5842)
fix #5839
1 parent a388129 commit 71066b5

File tree

4 files changed

+60
-3
lines changed

4 files changed

+60
-3
lines changed

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

+31
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,37 @@ describe('component: emit', () => {
355355
expect(fn2).toHaveBeenCalledWith('two')
356356
})
357357

358+
test('.trim and .number modifiers should work with v-model on component', () => {
359+
const Foo = defineComponent({
360+
render() {},
361+
created() {
362+
this.$emit('update:modelValue', ' +01.2 ')
363+
this.$emit('update:foo', ' 1 ')
364+
}
365+
})
366+
367+
const fn1 = jest.fn()
368+
const fn2 = jest.fn()
369+
370+
const Comp = () =>
371+
h(Foo, {
372+
modelValue: null,
373+
modelModifiers: { trim: true, number: true },
374+
'onUpdate:modelValue': fn1,
375+
376+
foo: null,
377+
fooModifiers: { trim: true, number: true },
378+
'onUpdate:foo': fn2
379+
})
380+
381+
render(h(Comp), nodeOps.createElement('div'))
382+
383+
expect(fn1).toHaveBeenCalledTimes(1)
384+
expect(fn1).toHaveBeenCalledWith(1.2)
385+
expect(fn2).toHaveBeenCalledTimes(1)
386+
expect(fn2).toHaveBeenCalledWith(1)
387+
})
388+
358389
test('isEmitListener', () => {
359390
const options = {
360391
click: null,

packages/runtime-core/src/componentEmits.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ export function emit(
122122
const { number, trim } = props[modifiersKey] || EMPTY_OBJ
123123
if (trim) {
124124
args = rawArgs.map(a => a.trim())
125-
} else if (number) {
125+
}
126+
if (number) {
126127
args = rawArgs.map(toNumber)
127128
}
128129
}

packages/runtime-dom/__tests__/directives/vModel.spec.ts

+25-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ describe('vModel', () => {
201201
it('should support modifiers', async () => {
202202
const component = defineComponent({
203203
data() {
204-
return { number: null, trim: null, lazy: null }
204+
return { number: null, trim: null, lazy: null, trimNumber: null }
205205
},
206206
render() {
207207
return [
@@ -229,6 +229,19 @@ describe('vModel', () => {
229229
trim: true
230230
}
231231
),
232+
withVModel(
233+
h('input', {
234+
class: 'trim-number',
235+
'onUpdate:modelValue': (val: any) => {
236+
this.trimNumber = val
237+
}
238+
}),
239+
this.trimNumber,
240+
{
241+
trim: true,
242+
number: true
243+
}
244+
),
232245
withVModel(
233246
h('input', {
234247
class: 'lazy',
@@ -248,6 +261,7 @@ describe('vModel', () => {
248261

249262
const number = root.querySelector('.number')
250263
const trim = root.querySelector('.trim')
264+
const trimNumber = root.querySelector('.trim-number')
251265
const lazy = root.querySelector('.lazy')
252266
const data = root._vnode.component.data
253267

@@ -261,6 +275,16 @@ describe('vModel', () => {
261275
await nextTick()
262276
expect(data.trim).toEqual('hello, world')
263277

278+
trimNumber.value = ' 1 '
279+
triggerEvent('input', trimNumber)
280+
await nextTick()
281+
expect(data.trimNumber).toEqual(1)
282+
283+
trimNumber.value = ' +01.2 '
284+
triggerEvent('input', trimNumber)
285+
await nextTick()
286+
expect(data.trimNumber).toEqual(1.2)
287+
264288
lazy.value = 'foo'
265289
triggerEvent('change', lazy)
266290
await nextTick()

packages/runtime-dom/src/directives/vModel.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ export const vModelText: ModelDirective<
5252
let domValue: string | number = el.value
5353
if (trim) {
5454
domValue = domValue.trim()
55-
} else if (castToNumber) {
55+
}
56+
if (castToNumber) {
5657
domValue = toNumber(domValue)
5758
}
5859
el._assign(domValue)

0 commit comments

Comments
 (0)