From fa8f257a8a496bbd2b0174153203e6768202fa4e Mon Sep 17 00:00:00 2001 From: OnlyWick Date: Wed, 13 Mar 2024 15:26:05 +0800 Subject: [PATCH 1/4] fix(runtime-dom): equal value with a leading 0 do not trigger update fix #10503 --- packages/runtime-dom/src/directives/vModel.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/runtime-dom/src/directives/vModel.ts b/packages/runtime-dom/src/directives/vModel.ts index 9e94810d8cd..9f9744c1770 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' && el.value[0] !== '0') + ? looseToNumber(el.value) + : el.value const newValue = value == null ? '' : value if (elValue === newValue) { From 41589339456aa46c920a18adc84c41122de90e66 Mon Sep 17 00:00:00 2001 From: OnlyWick Date: Wed, 13 Mar 2024 16:36:49 +0800 Subject: [PATCH 2/4] test(runtime-dom): add test --- .../__tests__/directives/vModel.spec.ts | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/packages/runtime-dom/__tests__/directives/vModel.spec.ts b/packages/runtime-dom/__tests__/directives/vModel.spec.ts index 6cc7b53e2c2..7db55680f87 100644 --- a/packages/runtime-dom/__tests__/directives/vModel.spec.ts +++ b/packages/runtime-dom/__tests__/directives/vModel.spec.ts @@ -1237,4 +1237,41 @@ describe('vModel', () => { await nextTick() expect(data.value).toEqual('使用拼音输入') }) + + // #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') + }) }) From 3e89a710ef46acb655dd9cec2078a4cda93e884f Mon Sep 17 00:00:00 2001 From: Evan You Date: Mon, 15 Apr 2024 21:52:07 +0800 Subject: [PATCH 3/4] Update vModel.ts --- packages/runtime-dom/src/directives/vModel.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/runtime-dom/src/directives/vModel.ts b/packages/runtime-dom/src/directives/vModel.ts index 9f9744c1770..17155a04b35 100644 --- a/packages/runtime-dom/src/directives/vModel.ts +++ b/packages/runtime-dom/src/directives/vModel.ts @@ -87,7 +87,7 @@ export const vModelText: ModelDirective< // avoid clearing unresolved text. #2302 if ((el as any).composing) return const elValue = - number || (el.type === 'number' && el.value[0] !== '0') + (number || el.type === 'number') && !/^0\d/.test(el.value) ? looseToNumber(el.value) : el.value const newValue = value == null ? '' : value From 881a437d8781548af98ac5a0f2b8ef9b6f7938c4 Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Mon, 15 Apr 2024 13:57:09 +0000 Subject: [PATCH 4/4] [autofix.ci] apply automated fixes --- packages/runtime-dom/__tests__/directives/vModel.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/runtime-dom/__tests__/directives/vModel.spec.ts b/packages/runtime-dom/__tests__/directives/vModel.spec.ts index d03364ba1b6..66b57b68964 100644 --- a/packages/runtime-dom/__tests__/directives/vModel.spec.ts +++ b/packages/runtime-dom/__tests__/directives/vModel.spec.ts @@ -1237,7 +1237,7 @@ describe('vModel', () => { await nextTick() expect(data.value).toEqual('使用拼音输入') }) - + it('multiple select (model is number, option value is string)', async () => { const component = defineComponent({ data() {