From 15ffe8f2c954359770c57e4d9e589b0b622e4a60 Mon Sep 17 00:00:00 2001 From: Wick Date: Mon, 15 Apr 2024 22:02:57 +0800 Subject: [PATCH] fix(runtime-dom): force update v-model number with leading 0 (#10506) close #10503 close #10615 --- .../__tests__/directives/vModel.spec.ts | 37 +++++++++++++++++++ packages/runtime-dom/src/directives/vModel.ts | 5 ++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/packages/runtime-dom/__tests__/directives/vModel.spec.ts b/packages/runtime-dom/__tests__/directives/vModel.spec.ts index d6398f37321..66b57b68964 100644 --- a/packages/runtime-dom/__tests__/directives/vModel.spec.ts +++ b/packages/runtime-dom/__tests__/directives/vModel.spec.ts @@ -1269,4 +1269,41 @@ describe('vModel', () => { expect(foo.selected).toEqual(true) expect(bar.selected).toEqual(true) }) + + // #10503 + test('equal value with a leading 0 should trigger update.', async () => { + const setNum = function (this: any, value: any) { + this.num = value + } + const component = defineComponent({ + data() { + return { num: 0 } + }, + render() { + return [ + withVModel( + h('input', { + id: 'input_num1', + type: 'number', + 'onUpdate:modelValue': setNum.bind(this), + }), + this.num, + ), + ] + }, + }) + + render(h(component), root) + const data = root._vnode.component.data + + const inputNum1 = root.querySelector('#input_num1')! + expect(inputNum1.value).toBe('0') + + inputNum1.value = '01' + triggerEvent('input', inputNum1) + await nextTick() + expect(data.num).toBe(1) + + expect(inputNum1.value).toBe('1') + }) }) diff --git a/packages/runtime-dom/src/directives/vModel.ts b/packages/runtime-dom/src/directives/vModel.ts index e002e2e10da..b0ea41728bd 100644 --- a/packages/runtime-dom/src/directives/vModel.ts +++ b/packages/runtime-dom/src/directives/vModel.ts @@ -86,9 +86,10 @@ export const vModelText: ModelDirective< el[assignKey] = getModelAssigner(vnode) // avoid clearing unresolved text. #2302 if ((el as any).composing) return - const elValue = - number || el.type === 'number' ? looseToNumber(el.value) : el.value + (number || el.type === 'number') && !/^0\d/.test(el.value) + ? looseToNumber(el.value) + : el.value const newValue = value == null ? '' : value if (elValue === newValue) {