Skip to content

Commit 3056e9b

Browse files
committed
fix(v-model): properly detect input type=number
fix #3813
1 parent 93a950d commit 3056e9b

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

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

+31
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,37 @@ describe('vModel', () => {
7070
expect(input.value).toEqual('')
7171
})
7272

73+
it('should work with number input', async () => {
74+
const component = defineComponent({
75+
data() {
76+
return { value: null }
77+
},
78+
render() {
79+
return [
80+
withVModel(
81+
h('input', {
82+
type: 'number',
83+
'onUpdate:modelValue': setValue.bind(this)
84+
}),
85+
this.value
86+
)
87+
]
88+
}
89+
})
90+
render(h(component), root)
91+
92+
const input = root.querySelector('input')!
93+
const data = root._vnode.component.data
94+
expect(input.value).toEqual('')
95+
expect(input.type).toEqual('number')
96+
97+
input.value = 1
98+
triggerEvent('input', input)
99+
await nextTick()
100+
expect(typeof data.value).toEqual('number')
101+
expect(data.value).toEqual(1)
102+
})
103+
73104
it('should work with multiple listeners', async () => {
74105
const spy = jest.fn()
75106
const component = defineComponent({

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ export const vModelText: ModelDirective<
4949
> = {
5050
created(el, { modifiers: { lazy, trim, number } }, vnode) {
5151
el._assign = getModelAssigner(vnode)
52-
const castToNumber = number || el.type === 'number'
52+
const castToNumber =
53+
number || (vnode.props && vnode.props.type === 'number')
5354
addEventListener(el, lazy ? 'change' : 'input', e => {
5455
if ((e.target as any).composing) return
5556
let domValue: string | number = el.value

0 commit comments

Comments
 (0)