Skip to content

Commit

Permalink
feat(mdc-checkbox): Support array as v-model.
Browse files Browse the repository at this point in the history
  • Loading branch information
pgbross authored and pgbross committed Apr 6, 2018
1 parent bfce806 commit 1c8ca1a
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions components/checkbox/mdc-checkbox.vue
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ export default {
event: 'change',
},
props: {
checked: Boolean,
checked: [Boolean, Array],
indeterminate: Boolean,
disabled: Boolean,
label: String,
'align-end': Boolean,
value: {
type: String,
type: [String, Number],
default() {
return 'on';
},
Expand All @@ -68,9 +68,7 @@ export default {
},
},
watch: {
checked(value) {
this.foundation.setChecked(value);
},
checked: 'setChecked',
disabled(value) {
this.foundation.setDisabled(value);
},
Expand Down Expand Up @@ -139,7 +137,7 @@ export default {
this.foundation.init();
this.ripple.init();
this.formField.init();
this.foundation.setChecked(this.checked);
this.setChecked(this.checked);
this.foundation.setDisabled(this.disabled);
this.foundation.setIndeterminate(this.indeterminate);
},
Expand All @@ -149,9 +147,30 @@ export default {
this.foundation.destroy();
},
methods: {
setChecked(checked) {
this.foundation.setChecked(
Array.isArray(checked) ? checked.indexOf(this.value) > -1 : checked,
);
},
onChange() {
this.$emit('update:indeterminate', this.foundation.isIndeterminate());
this.$emit('change', this.foundation.isChecked());
const isChecked = this.foundation.isChecked();
if (Array.isArray(this.checked)) {
const idx = this.checked.indexOf(this.value);
if (isChecked) {
idx < 0 && this.$emit('change', this.checked.concat(this.value));
} else {
idx > -1 &&
this.$emit(
'change',
this.checked.slice(0, idx).concat(this.checked.slice(idx + 1)),
);
}
} else {
this.$emit('change', isChecked);
}
},
},
};
Expand Down

0 comments on commit 1c8ca1a

Please sign in to comment.