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.
117 lines
2.6 KiB
117 lines
2.6 KiB
7 years ago
|
|
||
|
import Checkbox from './Checkbox'
|
||
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 {
|
||
7 years ago
|
sValue: value || defaultValue,
|
||
7 years ago
|
}
|
||
|
},
|
||
7 years ago
|
methods: {
|
||
|
handleChange (event) {
|
||
|
const target = event.target
|
||
7 years ago
|
const { value: targetValue, checked } = target
|
||
7 years ago
|
const { sValue } = this
|
||
7 years ago
|
let newVal = []
|
||
|
if (checked) {
|
||
7 years ago
|
newVal = [...sValue, targetValue]
|
||
7 years ago
|
} else {
|
||
7 years ago
|
newVal = [...sValue]
|
||
7 years ago
|
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.sValue = newVal
|
||
7 years ago
|
}
|
||
|
this.$emit('input', newVal)
|
||
|
this.$emit('change', newVal)
|
||
7 years ago
|
},
|
||
7 years ago
|
getOptions () {
|
||
|
const { options } = this.$props
|
||
|
return options.map(option => {
|
||
|
if (typeof option === 'string') {
|
||
|
return {
|
||
|
label: option,
|
||
|
value: option,
|
||
|
}
|
||
|
}
|
||
|
return option
|
||
|
})
|
||
|
},
|
||
|
toggleOption (option) {
|
||
|
const optionIndex = this.sValue.indexOf(option.value)
|
||
|
const value = [...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)
|
||
|
},
|
||
7 years ago
|
},
|
||
7 years ago
|
render () {
|
||
|
const { $props: props, $data: state, $slots } = this
|
||
|
const { prefixCls, options } = props
|
||
|
let children = $slots.default
|
||
|
if (options && options.length > 0) {
|
||
|
children = this.getOptions().map(option => (
|
||
|
<Checkbox
|
||
|
key={option.value}
|
||
|
disabled={'disabled' in option ? option.disabled : props.disabled}
|
||
|
value={option.value}
|
||
|
checked={state.sValue.indexOf(option.value) !== -1}
|
||
|
onChange={() => this.toggleOption(option)}
|
||
|
class={`${prefixCls}-item`}
|
||
|
>
|
||
|
{option.label}
|
||
|
</Checkbox>
|
||
|
))
|
||
|
}
|
||
|
return (
|
||
|
<div class={prefixCls}>
|
||
|
{children}
|
||
|
</div>
|
||
|
)
|
||
7 years ago
|
},
|
||
|
watch: {
|
||
|
value (val) {
|
||
7 years ago
|
this.sValue = val
|
||
7 years ago
|
},
|
||
|
},
|
||
|
}
|
||
7 years ago
|
|