Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(v-model): keep the same behavior as v-model with undefinedvalue with 2.x #1530

Merged
merged 2 commits into from
Jul 6, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 @@ -47,7 +47,7 @@ export const vModelText: ModelDirective<
HTMLInputElement | HTMLTextAreaElement
> = {
beforeMount(el, { value, modifiers: { lazy, trim, number } }, vnode) {
el.value = value
if (value !== undefined && value !== null) el.value = value
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should also be able to do

Suggested change
if (value !== undefined && value !== null) el.value = value
if (value != null) el.value = value

el._assign = getModelAssigner(vnode)
const castToNumber = number || el.type === 'number'
addEventListener(el, lazy ? 'change' : 'input', e => {
Expand Down Expand Up @@ -85,6 +85,7 @@ export const vModelText: ModelDirective<
return
}
}
if (value === undefined || value === null) value = ''

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Literally assigning NULL to native input value should has same effect. Probably checks value === null and value !== null is not required.

Check out example:

  1. Open: https://codesandbox.io/s/native-input-value-nwjoz?file=/index.js
  2. See console logs

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above:

Suggested change
if (value === undefined || value === null) value = ''
if (value == null) value = ''

el.value = value
}
}
Expand Down