Skip to content

Commit 425335c

Browse files
authored
fix(v-model): consistent nullish value handling with 2.x (#1530)
fix #1528
1 parent 441c236 commit 425335c

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

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

+5
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ describe('vModel', () => {
4848

4949
const input = root.querySelector('input')!
5050
const data = root._vnode.component.data
51+
expect(input.value).toEqual('')
5152

5253
input.value = 'foo'
5354
triggerEvent('input', input)
@@ -57,6 +58,10 @@ describe('vModel', () => {
5758
data.value = 'bar'
5859
await nextTick()
5960
expect(input.value).toEqual('bar')
61+
62+
data.value = undefined
63+
await nextTick()
64+
expect(input.value).toEqual('')
6065
})
6166

6267
it('should work with multiple listeners', async () => {

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export const vModelText: ModelDirective<
4747
HTMLInputElement | HTMLTextAreaElement
4848
> = {
4949
beforeMount(el, { value, modifiers: { lazy, trim, number } }, vnode) {
50-
el.value = value
50+
el.value = value == null ? '' : value
5151
el._assign = getModelAssigner(vnode)
5252
const castToNumber = number || el.type === 'number'
5353
addEventListener(el, lazy ? 'change' : 'input', e => {
@@ -85,7 +85,7 @@ export const vModelText: ModelDirective<
8585
return
8686
}
8787
}
88-
el.value = value
88+
el.value = value == null ? '' : value
8989
}
9090
}
9191

0 commit comments

Comments
 (0)