Skip to content

Commit

Permalink
fix(runtime-dom): prevent unnecessary updates in v-model checkbox whe…
Browse files Browse the repository at this point in the history
…n value is unchanged
  • Loading branch information
jh-leong committed Oct 10, 2024
1 parent 35785f3 commit 5494731
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions packages/runtime-dom/src/directives/vModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,24 +160,26 @@ export const vModelCheckbox: ModelDirective<HTMLInputElement> = {

function setChecked(
el: HTMLInputElement,
{ value }: DirectiveBinding,
{ value, oldValue }: DirectiveBinding,
vnode: VNode,
) {
// store the v-model value on the element so it can be accessed by the
// change listener.
;(el as any)._modelValue = value
let checked: boolean
let isSimpleValue = false

if (isArray(value)) {
checked = looseIndexOf(value, vnode.props!.value) > -1
} else if (isSet(value)) {
checked = value.has(vnode.props!.value)
} else {
isSimpleValue = true
checked = looseEqual(value, getCheckboxValue(el, true))
}

// Only update if the checked state has changed
if (el.checked !== checked) {
if (el.checked !== checked && (!isSimpleValue || value !== oldValue)) {
el.checked = checked
}
}
Expand Down

0 comments on commit 5494731

Please sign in to comment.