Skip to content

Commit

Permalink
fix(runtime-dom): force update v-model number with leading 0 (vuejs#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
OnlyWick authored and lynxlangya committed May 30, 2024
1 parent 865ac7f commit 964912f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
37 changes: 37 additions & 0 deletions packages/runtime-dom/__tests__/directives/vModel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
})
5 changes: 3 additions & 2 deletions packages/runtime-dom/src/directives/vModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 964912f

Please sign in to comment.