ant-design-vue/components/checkbox/Group.vue

108 lines
2.7 KiB
Vue
Raw Normal View History

2017-10-26 15:18:08 +08:00
<template>
<div :class="`${prefixCls}`">
2017-11-03 18:46:18 +08:00
<Checkbox v-for="item in checkOptions" :key="item.value" :checked="checkedStatus.has(item.value)"
2017-10-26 15:18:08 +08:00
:value="item.value" :disabled="item.disabled" @change="handleChange">{{item.label}}</Checkbox>
<slot v-if="options.length === 0"></slot>
</div>
</template>
<script>
2017-10-27 14:04:48 +08:00
import Checkbox from './Checkbox.vue'
2017-10-26 15:18:08 +08:00
export default {
name: 'CheckboxGroup',
props: {
prefixCls: {
default: 'ant-checkbox-group',
type: String,
},
2017-11-02 11:48:38 +08:00
defaultValue: {
2017-10-26 15:18:08 +08:00
default: () => [],
type: Array,
},
2017-11-02 11:48:38 +08:00
value: {
default: undefined,
type: Array,
},
2017-10-26 15:18:08 +08:00
options: {
default: () => [],
type: Array,
},
2017-11-03 18:46:18 +08:00
disabled: Boolean,
2017-10-26 15:18:08 +08:00
},
model: {
prop: 'value',
},
2017-11-02 11:48:38 +08:00
data () {
const { value, defaultValue } = this
return {
stateValue: value || defaultValue,
}
},
2017-10-26 15:18:08 +08:00
computed: {
checkedStatus () {
2017-11-02 11:48:38 +08:00
return new Set(this.stateValue)
2017-10-26 15:18:08 +08:00
},
2017-11-03 18:46:18 +08:00
checkOptions () {
const { disabled } = this
return this.options.map(option => {
return typeof option === 'string'
? { label: option, value: option }
: { ...option, disabled: option.disabled === undefined ? disabled : option.disabled }
})
},
2017-10-26 15:18:08 +08:00
},
created () {
this.setChildCheckbox(this.$slots.default)
},
methods: {
handleChange (event) {
const target = event.target
2017-11-02 11:48:38 +08:00
const { value: targetValue, checked } = target
const { stateValue, value } = this
2017-10-26 15:18:08 +08:00
let newVal = []
if (checked) {
2017-11-02 11:48:38 +08:00
newVal = [...stateValue, targetValue]
2017-10-26 15:18:08 +08:00
} else {
2017-11-02 11:48:38 +08:00
newVal = [...stateValue]
const index = newVal.indexOf(targetValue)
2017-10-26 15:18:08 +08:00
index >= 0 && newVal.splice(index, 1)
}
2017-11-02 11:48:38 +08:00
newVal = [...new Set(newVal)]
if (value === undefined) {
this.stateValue = newVal
}
this.$emit('input', newVal)
this.$emit('change', newVal)
2017-10-26 15:18:08 +08:00
},
setChildCheckbox (children = []) {
const { options, $slots, checkedStatus } = this
if (options.length === 0 && $slots.default) {
children.forEach(({ componentOptions = {}, children: newChildren }) => {
2017-10-27 14:56:23 +08:00
const { Ctor, propsData } = componentOptions
if (Ctor && Ctor.options.name === 'Checkbox') {
2017-10-26 15:18:08 +08:00
propsData.isGroup = true
propsData.onGroupChange = this.handleChange
propsData.checked = checkedStatus.has(propsData.value)
} else {
this.setChildCheckbox(newChildren)
}
}, this)
}
},
},
mounted () {
},
beforeUpdate () {
this.setChildCheckbox(this.$slots.default)
},
watch: {
value (val) {
2017-11-02 11:48:38 +08:00
this.stateValue = val
2017-10-26 15:18:08 +08:00
this.setChildCheckbox(this.$slots.default)
},
},
components: {
Checkbox,
},
}
</script>