2018-03-19 10:16:27 +08:00
|
|
|
|
|
|
|
import Checkbox from './Checkbox'
|
2018-01-12 16:10:41 +08:00
|
|
|
import hasProp from '../_util/props-util'
|
2017-10-26 15:18:08 +08:00
|
|
|
export default {
|
2018-04-08 21:17:20 +08:00
|
|
|
name: 'ACheckboxGroup',
|
2017-10-26 15:18:08 +08:00
|
|
|
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 {
|
2018-01-16 15:41:00 +08:00
|
|
|
sValue: value || defaultValue,
|
2017-11-02 11:48:38 +08:00
|
|
|
}
|
|
|
|
},
|
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
|
2018-01-16 15:41:00 +08:00
|
|
|
const { sValue } = this
|
2017-10-26 15:18:08 +08:00
|
|
|
let newVal = []
|
|
|
|
if (checked) {
|
2018-01-16 15:41:00 +08:00
|
|
|
newVal = [...sValue, targetValue]
|
2017-10-26 15:18:08 +08:00
|
|
|
} else {
|
2018-01-16 15:41:00 +08:00
|
|
|
newVal = [...sValue]
|
2017-11-02 11:48:38 +08:00
|
|
|
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)]
|
2017-12-27 18:15:11 +08:00
|
|
|
if (!hasProp(this, 'value')) {
|
2018-01-16 15:41:00 +08:00
|
|
|
this.sValue = newVal
|
2017-11-02 11:48:38 +08:00
|
|
|
}
|
|
|
|
this.$emit('input', newVal)
|
|
|
|
this.$emit('change', newVal)
|
2017-10-26 15:18:08 +08:00
|
|
|
},
|
2018-01-16 15:41:00 +08:00
|
|
|
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)
|
|
|
|
},
|
2017-10-26 15:18:08 +08:00
|
|
|
},
|
2018-01-16 15:41:00 +08:00
|
|
|
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>
|
|
|
|
)
|
2017-10-26 15:18:08 +08:00
|
|
|
},
|
|
|
|
watch: {
|
|
|
|
value (val) {
|
2018-01-16 15:41:00 +08:00
|
|
|
this.sValue = val
|
2017-10-26 15:18:08 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2018-03-19 10:16:27 +08:00
|
|
|
|