iview/src/components/checkbox/checkbox-group.vue

79 lines
2.2 KiB
Vue
Raw Normal View History

2016-09-09 14:29:19 +08:00
<template>
<div :class="classes">
<slot></slot>
</div>
</template>
<script>
2017-08-24 17:25:21 +08:00
import { findComponentsDownward, oneOf } from '../../utils/assist';
import Emitter from '../../mixins/emitter';
2018-01-21 01:17:38 +01:00
2016-09-09 14:29:19 +08:00
const prefixCls = 'ivu-checkbox-group';
export default {
name: 'CheckboxGroup',
mixins: [ Emitter ],
2016-09-09 14:29:19 +08:00
props: {
2017-03-01 17:58:40 +08:00
value: {
2016-09-09 14:29:19 +08:00
type: Array,
default () {
2016-12-25 22:49:42 +08:00
return [];
}
2017-08-24 17:25:21 +08:00
},
size: {
validator (value) {
return oneOf(value, ['small', 'large', 'default']);
2018-06-28 14:19:49 +08:00
},
default () {
2018-08-07 16:35:27 +08:00
return !this.$IVIEW || this.$IVIEW.size === '' ? 'default' : this.$IVIEW.size;
2017-08-24 17:25:21 +08:00
}
2016-09-09 14:29:19 +08:00
}
},
2017-03-01 17:58:40 +08:00
data () {
return {
2017-03-15 17:56:50 +08:00
currentValue: this.value,
childrens: []
2017-03-01 17:58:40 +08:00
};
},
2016-09-09 14:29:19 +08:00
computed: {
classes () {
2017-08-24 17:25:21 +08:00
return [
`${prefixCls}`,
{
[`ivu-checkbox-${this.size}`]: !!this.size
}
];
2016-09-09 14:29:19 +08:00
}
},
2017-03-01 17:58:40 +08:00
mounted () {
2016-09-09 14:29:19 +08:00
this.updateModel(true);
},
methods: {
updateModel (update) {
2017-03-15 17:56:50 +08:00
this.childrens = findComponentsDownward(this, 'Checkbox');
if (this.childrens) {
2018-01-21 01:17:38 +01:00
const { value } = this;
2017-03-15 17:56:50 +08:00
this.childrens.forEach(child => {
child.model = value;
2016-09-09 14:29:19 +08:00
2017-03-15 17:56:50 +08:00
if (update) {
child.currentValue = value.indexOf(child.label) >= 0;
child.group = true;
}
});
}
2016-09-09 14:29:19 +08:00
},
change (data) {
2017-03-01 17:58:40 +08:00
this.currentValue = data;
this.$emit('input', data);
2016-09-09 14:29:19 +08:00
this.$emit('on-change', data);
this.dispatch('FormItem', 'on-form-change', data);
2016-09-09 14:29:19 +08:00
}
},
watch: {
2017-03-01 17:58:40 +08:00
value () {
this.updateModel(true);
2016-09-09 14:29:19 +08:00
}
}
2016-12-25 22:49:42 +08:00
};
</script>