You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ant-design-vue/components/checkbox/Group.vue

93 lines
2.0 KiB

7 years ago
<template>
<div :class="`${prefixCls}`">
7 years ago
<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>
7 years ago
</div>
</template>
<script>
7 years ago
import Checkbox from './Checkbox.vue'
7 years ago
import hasProp from '../_util/props-util'
7 years ago
export default {
name: 'CheckboxGroup',
props: {
prefixCls: {
default: 'ant-checkbox-group',
type: String,
},
7 years ago
defaultValue: {
7 years ago
default: () => [],
type: Array,
},
7 years ago
value: {
default: undefined,
type: Array,
},
7 years ago
options: {
default: () => [],
type: Array,
},
7 years ago
disabled: Boolean,
7 years ago
},
model: {
prop: 'value',
},
7 years ago
provide () {
return {
7 years ago
checkboxGroupContext: this,
7 years ago
}
},
7 years ago
data () {
const { value, defaultValue } = this
return {
stateValue: value || defaultValue,
}
},
7 years ago
computed: {
checkedStatus () {
7 years ago
return new Set(this.stateValue)
7 years ago
},
7 years ago
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 }
})
},
7 years ago
},
methods: {
handleChange (event) {
const target = event.target
7 years ago
const { value: targetValue, checked } = target
7 years ago
const { stateValue } = this
7 years ago
let newVal = []
if (checked) {
7 years ago
newVal = [...stateValue, targetValue]
7 years ago
} else {
7 years ago
newVal = [...stateValue]
const index = newVal.indexOf(targetValue)
7 years ago
index >= 0 && newVal.splice(index, 1)
}
7 years ago
newVal = [...new Set(newVal)]
7 years ago
if (!hasProp(this, 'value')) {
7 years ago
this.stateValue = newVal
}
this.$emit('input', newVal)
this.$emit('change', newVal)
7 years ago
},
},
mounted () {
},
watch: {
value (val) {
7 years ago
this.stateValue = val
7 years ago
},
},
components: {
Checkbox,
},
}
</script>