Skip to content

Commit

Permalink
fix(v-model): properly detect input type=number
Browse files Browse the repository at this point in the history
fix #3813
  • Loading branch information
yyx990803 committed Jul 19, 2021
1 parent 93a950d commit 3056e9b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
31 changes: 31 additions & 0 deletions packages/runtime-dom/__tests__/directives/vModel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
3 changes: 2 additions & 1 deletion packages/runtime-dom/src/directives/vModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3056e9b

Please sign in to comment.