fix(v-model): consistent nullish value handling with 2.x (#1530)

fix #1528
This commit is contained in:
underfin 2020-07-07 07:02:33 +08:00 committed by GitHub
parent 441c23602f
commit 425335c28b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 2 deletions

View File

@ -48,6 +48,7 @@ describe('vModel', () => {
const input = root.querySelector('input')!
const data = root._vnode.component.data
expect(input.value).toEqual('')
input.value = 'foo'
triggerEvent('input', input)
@ -57,6 +58,10 @@ describe('vModel', () => {
data.value = 'bar'
await nextTick()
expect(input.value).toEqual('bar')
data.value = undefined
await nextTick()
expect(input.value).toEqual('')
})
it('should work with multiple listeners', async () => {

View File

@ -47,7 +47,7 @@ export const vModelText: ModelDirective<
HTMLInputElement | HTMLTextAreaElement
> = {
beforeMount(el, { value, modifiers: { lazy, trim, number } }, vnode) {
el.value = value
el.value = value == null ? '' : value
el._assign = getModelAssigner(vnode)
const castToNumber = number || el.type === 'number'
addEventListener(el, lazy ? 'change' : 'input', e => {
@ -85,7 +85,7 @@ export const vModelText: ModelDirective<
return
}
}
el.value = value
el.value = value == null ? '' : value
}
}