2018-05-05 17:39:56 +08:00
|
|
|
import PropTypes from '../_util/vue-types'
|
2018-09-05 21:28:54 +08:00
|
|
|
import classNames from 'classnames'
|
|
|
|
import VcCheckbox from '../vc-checkbox'
|
|
|
|
import { getOptionProps, getAttrs } from '../_util/props-util'
|
|
|
|
function noop () {}
|
|
|
|
|
2017-10-26 15:18:08 +08:00
|
|
|
export default {
|
2018-05-05 21:38:03 +08:00
|
|
|
inheritAttrs: false,
|
2018-04-08 21:17:20 +08:00
|
|
|
name: 'ACheckbox',
|
2017-10-26 15:18:08 +08:00
|
|
|
props: {
|
|
|
|
prefixCls: {
|
|
|
|
default: 'ant-checkbox',
|
|
|
|
type: String,
|
|
|
|
},
|
2018-06-08 18:37:03 +08:00
|
|
|
defaultChecked: PropTypes.bool,
|
|
|
|
checked: PropTypes.bool,
|
|
|
|
disabled: PropTypes.bool,
|
2017-10-26 15:18:08 +08:00
|
|
|
isGroup: Boolean,
|
2018-09-05 21:28:54 +08:00
|
|
|
value: PropTypes.any,
|
2017-10-26 15:18:08 +08:00
|
|
|
name: String,
|
2018-06-01 21:25:16 +08:00
|
|
|
id: String,
|
2017-10-26 15:18:08 +08:00
|
|
|
indeterminate: Boolean,
|
2018-05-05 17:39:56 +08:00
|
|
|
type: PropTypes.string.def('checkbox'),
|
2018-05-20 21:41:40 +08:00
|
|
|
autoFocus: Boolean,
|
2017-10-26 15:18:08 +08:00
|
|
|
},
|
|
|
|
model: {
|
|
|
|
prop: 'checked',
|
|
|
|
},
|
2017-11-06 17:46:08 +08:00
|
|
|
inject: {
|
2018-01-16 15:41:00 +08:00
|
|
|
checkboxGroupContext: { default: null },
|
2017-11-06 17:46:08 +08:00
|
|
|
},
|
2017-10-26 15:18:08 +08:00
|
|
|
methods: {
|
|
|
|
handleChange (event) {
|
2017-11-02 11:48:38 +08:00
|
|
|
const targetChecked = event.target.checked
|
|
|
|
this.$emit('input', targetChecked)
|
2018-09-05 21:28:54 +08:00
|
|
|
this.$emit('change', event)
|
2018-01-16 15:41:00 +08:00
|
|
|
},
|
|
|
|
focus () {
|
2018-09-05 21:28:54 +08:00
|
|
|
this.$refs.vcCheckbox.focus()
|
2018-01-16 15:41:00 +08:00
|
|
|
},
|
|
|
|
blur () {
|
2018-09-05 21:28:54 +08:00
|
|
|
this.$refs.vcCheckbox.blur()
|
2017-11-06 17:46:08 +08:00
|
|
|
},
|
2017-11-02 11:48:38 +08:00
|
|
|
},
|
2018-09-05 21:28:54 +08:00
|
|
|
|
2018-01-16 15:41:00 +08:00
|
|
|
render () {
|
2018-09-05 21:28:54 +08:00
|
|
|
const { checkboxGroupContext: checkboxGroup, $listeners, $slots } = this
|
|
|
|
const props = getOptionProps(this)
|
|
|
|
const children = $slots.default
|
|
|
|
const { mouseenter = noop, mouseleave = noop, ...restListeners } = $listeners
|
2018-01-16 15:41:00 +08:00
|
|
|
const {
|
|
|
|
prefixCls,
|
2018-09-05 21:28:54 +08:00
|
|
|
indeterminate,
|
|
|
|
...restProps
|
2018-01-16 15:41:00 +08:00
|
|
|
} = props
|
2018-09-05 21:28:54 +08:00
|
|
|
const checkboxProps = { props: { ...restProps, prefixCls }, on: restListeners, attrs: getAttrs(this) }
|
|
|
|
if (checkboxGroup) {
|
2018-11-30 22:58:46 +08:00
|
|
|
checkboxProps.on.change = (...args) => {
|
|
|
|
this.$emit('change', ...args)
|
|
|
|
checkboxGroup.toggleOption({ label: children, value: props.value })
|
|
|
|
}
|
2018-09-05 21:28:54 +08:00
|
|
|
checkboxProps.props.checked = checkboxGroup.sValue.indexOf(props.value) !== -1
|
|
|
|
checkboxProps.props.disabled = props.disabled || checkboxGroup.disabled
|
|
|
|
} else {
|
|
|
|
checkboxProps.on.change = this.handleChange
|
2018-01-16 15:41:00 +08:00
|
|
|
}
|
2018-09-05 21:28:54 +08:00
|
|
|
const classString = classNames({
|
2018-05-05 21:38:03 +08:00
|
|
|
[`${prefixCls}-wrapper`]: true,
|
2018-12-13 21:26:21 +08:00
|
|
|
[`${prefixCls}-wrapper-checked`]: checkboxProps.props.checked,
|
|
|
|
[`${prefixCls}-wrapper-disabled`]: checkboxProps.props.disabled,
|
2018-05-05 21:38:03 +08:00
|
|
|
})
|
2018-09-05 21:28:54 +08:00
|
|
|
const checkboxClass = classNames({
|
|
|
|
[`${prefixCls}-indeterminate`]: indeterminate,
|
|
|
|
})
|
2018-01-16 15:41:00 +08:00
|
|
|
return (
|
|
|
|
<label
|
2018-05-05 21:38:03 +08:00
|
|
|
class={classString}
|
2018-09-05 21:28:54 +08:00
|
|
|
onMouseenter={mouseenter}
|
|
|
|
onMouseleave={mouseleave}
|
2018-01-16 15:41:00 +08:00
|
|
|
>
|
2018-09-05 21:28:54 +08:00
|
|
|
<VcCheckbox
|
|
|
|
{...checkboxProps}
|
|
|
|
class={checkboxClass}
|
|
|
|
ref='vcCheckbox'
|
|
|
|
/>
|
2018-12-13 21:26:21 +08:00
|
|
|
{children !== undefined && <span>{children}</span>}
|
2018-01-16 15:41:00 +08:00
|
|
|
</label>
|
|
|
|
)
|
|
|
|
},
|
2017-10-26 15:18:08 +08:00
|
|
|
}
|