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'
|
2018-11-30 22:58:46 +08:00
|
|
|
function noop () {}
|
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: {
|
2018-09-05 21:28:54 +08:00
|
|
|
default: 'ant-checkbox',
|
2017-10-26 15:18:08 +08:00
|
|
|
type: String,
|
|
|
|
},
|
2017-11-02 11:48:38 +08:00
|
|
|
defaultValue: {
|
2018-08-09 09:41:54 +08:00
|
|
|
default: undefined,
|
2017-10-26 15:18:08 +08:00
|
|
|
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-08-09 09:41:54 +08:00
|
|
|
sValue: value || defaultValue || [],
|
2017-11-02 11:48:38 +08:00
|
|
|
}
|
|
|
|
},
|
2018-09-05 21:28:54 +08:00
|
|
|
watch: {
|
|
|
|
value (val) {
|
|
|
|
this.sValue = val
|
2017-10-26 15:18:08 +08:00
|
|
|
},
|
2018-09-05 21:28:54 +08:00
|
|
|
},
|
|
|
|
methods: {
|
2018-01-16 15:41:00 +08:00
|
|
|
getOptions () {
|
2019-01-02 20:21:01 +08:00
|
|
|
const { options, $scopedSlots } = this
|
2018-01-16 15:41:00 +08:00
|
|
|
return options.map(option => {
|
|
|
|
if (typeof option === 'string') {
|
|
|
|
return {
|
|
|
|
label: option,
|
|
|
|
value: option,
|
|
|
|
}
|
|
|
|
}
|
2019-01-02 20:21:01 +08:00
|
|
|
let label = option.label
|
|
|
|
if (label === undefined && $scopedSlots.label) {
|
|
|
|
label = $scopedSlots.label(option)
|
|
|
|
}
|
|
|
|
return { ...option, label }
|
2018-01-16 15:41:00 +08:00
|
|
|
})
|
|
|
|
},
|
|
|
|
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
|
2018-09-05 21:28:54 +08:00
|
|
|
const groupPrefixCls = `${prefixCls}-group`
|
2018-01-16 15:41:00 +08:00
|
|
|
if (options && options.length > 0) {
|
|
|
|
children = this.getOptions().map(option => (
|
|
|
|
<Checkbox
|
2018-09-05 21:28:54 +08:00
|
|
|
prefixCls={prefixCls}
|
|
|
|
key={option.value.toString()}
|
2018-01-16 15:41:00 +08:00
|
|
|
disabled={'disabled' in option ? option.disabled : props.disabled}
|
|
|
|
value={option.value}
|
|
|
|
checked={state.sValue.indexOf(option.value) !== -1}
|
2018-11-30 22:58:46 +08:00
|
|
|
onChange={option.onChange || noop}
|
2018-09-05 21:28:54 +08:00
|
|
|
class={`${groupPrefixCls}-item`}
|
2018-01-16 15:41:00 +08:00
|
|
|
>
|
|
|
|
{option.label}
|
|
|
|
</Checkbox>
|
|
|
|
))
|
|
|
|
}
|
|
|
|
return (
|
2018-09-05 21:28:54 +08:00
|
|
|
<div class={groupPrefixCls}>
|
2018-01-16 15:41:00 +08:00
|
|
|
{children}
|
|
|
|
</div>
|
|
|
|
)
|
2017-10-26 15:18:08 +08:00
|
|
|
},
|
|
|
|
}
|
2018-03-19 10:16:27 +08:00
|
|
|
|