fix(v-model): properly detect input type=number

fix #3813
This commit is contained in:
Evan You 2021-07-19 17:46:04 -04:00
parent 93a950d60d
commit 3056e9b3dc
2 changed files with 33 additions and 1 deletions

View File

@ -70,6 +70,37 @@ describe('vModel', () => {
expect(input.value).toEqual('')
})
it('should work with number input', async () => {
const component = defineComponent({
data() {
return { value: null }
},
render() {
return [
withVModel(
h('input', {
type: 'number',
'onUpdate:modelValue': setValue.bind(this)
}),
this.value
)
]
}
})
render(h(component), root)
const input = root.querySelector('input')!
const data = root._vnode.component.data
expect(input.value).toEqual('')
expect(input.type).toEqual('number')
input.value = 1
triggerEvent('input', input)
await nextTick()
expect(typeof data.value).toEqual('number')
expect(data.value).toEqual(1)
})
it('should work with multiple listeners', async () => {
const spy = jest.fn()
const component = defineComponent({

View File

@ -49,7 +49,8 @@ export const vModelText: ModelDirective<
> = {
created(el, { modifiers: { lazy, trim, number } }, vnode) {
el._assign = getModelAssigner(vnode)
const castToNumber = number || el.type === 'number'
const castToNumber =
number || (vnode.props && vnode.props.type === 'number')
addEventListener(el, lazy ? 'change' : 'input', e => {
if ((e.target as any).composing) return
let domValue: string | number = el.value