Skip to content

Commit d6e6f1d

Browse files
nickmessingyyx990803
authored andcommitted
fix(v-model): Allow using array value with array v-model in checkboxes (#6220)
fix #6219
1 parent d77b953 commit d6e6f1d

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/platforms/web/compiler/directives/model.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ function genCheckboxModel (
9393
'if(Array.isArray($$a)){' +
9494
`var $$v=${number ? '_n(' + valueBinding + ')' : valueBinding},` +
9595
'$$i=_i($$a,$$v);' +
96-
`if($$el.checked){$$i<0&&(${value}=$$a.concat($$v))}` +
96+
`if($$el.checked){$$i<0&&(${value}=$$a.concat([$$v]))}` +
9797
`else{$$i>-1&&(${value}=$$a.slice(0,$$i).concat($$a.slice($$i+1)))}` +
9898
`}else{${genAssignmentCode(value, '$$c')}}`,
9999
null, true

test/unit/features/directives/model-checkbox.spec.js

+28
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,34 @@ describe('Directive v-model checkbox', () => {
161161
}).then(done)
162162
})
163163

164+
it('bind to Array value with array value bindings (object loose equal)', done => {
165+
const vm = new Vue({
166+
data: {
167+
test: [{ a: 1 }]
168+
},
169+
template: `
170+
<div>
171+
<input type="checkbox" v-model="test" :value="{ a: 1 }">
172+
<input type="checkbox" v-model="test" :value="[2]">
173+
</div>
174+
`
175+
}).$mount()
176+
document.body.appendChild(vm.$el)
177+
expect(vm.$el.children[0].checked).toBe(true)
178+
expect(vm.$el.children[1].checked).toBe(false)
179+
vm.$el.children[0].click()
180+
expect(vm.test.length).toBe(0)
181+
vm.$el.children[1].click()
182+
expect(vm.test).toEqual([[2]])
183+
vm.$el.children[0].click()
184+
expect(vm.test).toEqual([[2], { a: 1 }])
185+
vm.test = [{ a: 1 }]
186+
waitForUpdate(() => {
187+
expect(vm.$el.children[0].checked).toBe(true)
188+
expect(vm.$el.children[1].checked).toBe(false)
189+
}).then(done)
190+
})
191+
164192
it('.number modifier', () => {
165193
const vm = new Vue({
166194
data: {

0 commit comments

Comments
 (0)