Skip to content

Commit

Permalink
fix: checkbox v-model="array" ignore false-value (#6180)
Browse files Browse the repository at this point in the history
close #6178
  • Loading branch information
nickmessing authored and yyx990803 committed Jul 21, 2017
1 parent 3036551 commit 3d14e85
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/platforms/web/compiler/directives/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function genCheckboxModel (
'if(Array.isArray($$a)){' +
`var $$v=${number ? '_n(' + valueBinding + ')' : valueBinding},` +
'$$i=_i($$a,$$v);' +
`if($$c){$$i<0&&(${value}=$$a.concat($$v))}` +
`if($$el.checked){$$i<0&&(${value}=$$a.concat($$v))}` +
`else{$$i>-1&&(${value}=$$a.slice(0,$$i).concat($$a.slice($$i+1)))}` +
`}else{${genAssignmentCode(value, '$$c')}}`,
null, true
Expand Down
28 changes: 28 additions & 0 deletions test/unit/features/directives/model-checkbox.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,34 @@ describe('Directive v-model checkbox', () => {
}).then(done)
})

it('bind to Array value ignores false-value', done => {
const vm = new Vue({
data: {
test: ['1']
},
template: `
<div>
<input type="checkbox" v-model="test" value="1" :false-value="true">
<input type="checkbox" v-model="test" value="2" :false-value="true">
</div>
`
}).$mount()
document.body.appendChild(vm.$el)
expect(vm.$el.children[0].checked).toBe(true)
expect(vm.$el.children[1].checked).toBe(false)
vm.$el.children[0].click()
expect(vm.test.length).toBe(0)
vm.$el.children[1].click()
expect(vm.test).toEqual(['2'])
vm.$el.children[0].click()
expect(vm.test).toEqual(['2', '1'])
vm.test = ['1']
waitForUpdate(() => {
expect(vm.$el.children[0].checked).toBe(true)
expect(vm.$el.children[1].checked).toBe(false)
}).then(done)
})

it('bind to Array value with value bindings', done => {
const vm = new Vue({
data: {
Expand Down

0 comments on commit 3d14e85

Please sign in to comment.