ant-design-vue
Version:
An enterprise-class UI design language and Vue-based implementation
121 lines (113 loc) • 2.8 kB
JavaScript
import _toConsumableArray from 'babel-runtime/helpers/toConsumableArray';
import Checkbox from './Checkbox';
import hasProp from '../_util/props-util';
export default {
name: 'ACheckboxGroup',
props: {
prefixCls: {
'default': 'ant-checkbox',
type: String
},
defaultValue: {
'default': undefined,
type: Array
},
value: {
'default': undefined,
type: Array
},
options: {
'default': function _default() {
return [];
},
type: Array
},
disabled: Boolean
},
model: {
prop: 'value'
},
provide: function provide() {
return {
checkboxGroupContext: this
};
},
data: function data() {
var value = this.value,
defaultValue = this.defaultValue;
return {
sValue: value || defaultValue || []
};
},
watch: {
value: function value(val) {
this.sValue = val;
}
},
methods: {
getOptions: function getOptions() {
var options = this.$props.options;
return options.map(function (option) {
if (typeof option === 'string') {
return {
label: option,
value: option
};
}
return option;
});
},
toggleOption: function toggleOption(option) {
var optionIndex = this.sValue.indexOf(option.value);
var value = [].concat(_toConsumableArray(this.sValue));
if (optionIndex === -1) {
value.push(option.value);
} else {
value.splice(optionIndex, 1);
}
if (!hasProp(this, 'value')) {
this.sValue = value;
}
this.$emit('input', value);
this.$emit('change', value);
}
},
render: function render() {
var _this = this;
var h = arguments[0];
var props = this.$props,
state = this.$data,
$slots = this.$slots;
var prefixCls = props.prefixCls,
options = props.options;
var children = $slots['default'];
var groupPrefixCls = prefixCls + '-group';
if (options && options.length > 0) {
children = this.getOptions().map(function (option) {
return h(
Checkbox,
{
attrs: {
prefixCls: prefixCls,
disabled: 'disabled' in option ? option.disabled : props.disabled,
value: option.value,
checked: state.sValue.indexOf(option.value) !== -1
},
key: option.value.toString(), on: {
'change': function change() {
return _this.toggleOption(option);
}
},
'class': groupPrefixCls + '-item'
},
[option.label]
);
});
}
return h(
'div',
{ 'class': groupPrefixCls },
[children]
);
}
};