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

92 lines
2.0 KiB
Vue
Raw Normal View History

2017-10-26 15:18:08 +08:00
<template>
<div :class="`${prefixCls}`">
2017-11-07 11:58:47 +08:00
<Checkbox v-for="item in checkOptions" :key="item.value" :value="item.value" :disabled="item.disabled">
{{item.label}}
</Checkbox>
<slot v-if="checkOptions.length === 0"></slot>
2017-10-26 15:18:08 +08:00
</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-06 17:46:08 +08:00
provide () {
return {
2017-11-07 11:58:47 +08:00
checkboxGroupContext: this,
2017-11-06 17:46:08 +08:00
}
},
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
},
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
},
},
mounted () {
},
watch: {
value (val) {
2017-11-02 11:48:38 +08:00
this.stateValue = val
2017-10-26 15:18:08 +08:00
},
},
components: {
Checkbox,
},
}
</script>