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