Skip to content

Commit

Permalink
fix(v-model): consistent nullish value handling with 2.x (#1530)
Browse files Browse the repository at this point in the history
fix #1528
  • Loading branch information
underfin authored Jul 6, 2020
1 parent 441c236 commit 425335c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
5 changes: 5 additions & 0 deletions packages/runtime-dom/__tests__/directives/vModel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/runtime-dom/src/directives/vModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down Expand Up @@ -85,7 +85,7 @@ export const vModelText: ModelDirective<
return
}
}
el.value = value
el.value = value == null ? '' : value
}
}

Expand Down

0 comments on commit 425335c

Please sign in to comment.