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(vModel): fix v-model being reset in updated #8639

Closed
wants to merge 16 commits into from
Closed
Changes from all commits
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
13 changes: 8 additions & 5 deletions packages/runtime-dom/src/directives/vModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,16 @@ function setChecked(
{ value, oldValue }: DirectiveBinding,
vnode: VNode,
) {
// store the v-model value on the element so it can be accessed by the
// change listener.
// #8638
if (value === oldValue)
Copy link
Contributor

Choose a reason for hiding this comment

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

Returning early like this won't work if the value is an Array or Set that's been modified. e.g.:

Running this same example against main works as expected:

return // store the v-model value on the element so it can be accessed by the
// change listener.
Comment on lines +160 to +161
Copy link
Contributor

Choose a reason for hiding this comment

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

Prettier has moved this comment, so it's now in the wrong place. I think you'll need to add braces around the if body to get the comment to stay where it's meant to be.

;(el as any)._modelValue = value
if (isArray(value)) {
el.checked = looseIndexOf(value, vnode.props!.value) > -1
} else if (isSet(value)) {
el.checked = value.has(vnode.props!.value)
} else if (value !== oldValue) {
} else {
el.checked = looseEqual(value, getCheckboxValue(el, true))
}
}
Expand Down Expand Up @@ -216,8 +218,9 @@ export const vModelSelect: ModelDirective<HTMLSelectElement> = {
beforeUpdate(el, _binding, vnode) {
el[assignKey] = getModelAssigner(vnode)
},
updated(el, { value, modifiers: { number } }) {
if (!el._assigning) {
updated(el, { value, oldValue, modifiers: { number } }) {
// #8579
if (!el._assigning && value !== oldValue) {
setSelected(el, value, number)
}
},
Expand Down