ant-design-vue/components/checkbox/Checkbox.jsx

126 lines
3.3 KiB
React
Raw Normal View History

2018-03-19 10:16:27 +08:00
2018-01-12 16:10:41 +08:00
import hasProp from '../_util/props-util'
2018-05-05 17:39:56 +08:00
import PropTypes from '../_util/vue-types'
2017-10-26 15:18:08 +08:00
export default {
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,
},
2017-11-02 11:48:38 +08:00
defaultChecked: Boolean,
checked: { type: Boolean, default: undefined },
2017-10-26 15:18:08 +08:00
disabled: Boolean,
isGroup: Boolean,
value: [String, Number, Boolean],
name: String,
indeterminate: Boolean,
2018-05-05 17:39:56 +08:00
type: PropTypes.string.def('checkbox'),
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-11-02 11:48:38 +08:00
data () {
2017-11-07 11:58:47 +08:00
const { checkboxGroupContext, checked, defaultChecked, value } = this
2018-01-16 15:41:00 +08:00
let sChecked
if (checkboxGroupContext) {
sChecked = checkboxGroupContext.sValue.indexOf(value) !== -1
} else {
sChecked = !hasProp(this, 'checked') ? defaultChecked : checked
2017-11-06 17:46:08 +08:00
}
2017-11-02 11:48:38 +08:00
return {
2018-01-16 15:41:00 +08:00
sChecked,
2017-11-02 11:48:38 +08:00
}
},
2017-10-26 15:18:08 +08:00
computed: {
checkboxClass () {
2018-01-16 15:41:00 +08:00
const { prefixCls, indeterminate, sChecked, $props, checkboxGroupContext } = this
let disabled = $props.disabled
if (checkboxGroupContext) {
disabled = disabled || checkboxGroupContext.disabled
}
2017-10-26 15:18:08 +08:00
return {
[`${prefixCls}`]: true,
2018-01-16 15:41:00 +08:00
[`${prefixCls}-checked`]: sChecked,
2017-10-26 15:18:08 +08:00
[`${prefixCls}-disabled`]: disabled,
[`${prefixCls}-indeterminate`]: indeterminate,
}
},
},
methods: {
handleChange (event) {
2017-11-02 11:48:38 +08:00
const targetChecked = event.target.checked
this.$emit('input', targetChecked)
2018-05-05 17:42:33 +08:00
const { checked, checkboxGroupContext } = this
2018-01-16 15:41:00 +08:00
if ((checked === undefined && !checkboxGroupContext) || (checkboxGroupContext && checkboxGroupContext.sValue === undefined)) {
this.sChecked = targetChecked
2017-11-02 11:48:38 +08:00
}
2017-10-26 15:18:08 +08:00
const target = {
2018-05-05 17:42:33 +08:00
...this.$props,
2018-05-05 17:39:56 +08:00
checked: targetChecked,
2017-10-26 15:18:08 +08:00
}
2018-01-16 15:41:00 +08:00
this.$emit('change', {
target,
stopPropagation () {
event.stopPropagation()
},
preventDefault () {
event.preventDefault()
},
})
},
onMouseEnter (e) {
this.$emit('mouseenter', e)
},
onMouseLeave (e) {
this.$emit('mouseleave', e)
},
focus () {
this.$refs.input.focus()
},
blur () {
this.$refs.input.blur()
2017-10-26 15:18:08 +08:00
},
},
2017-11-02 11:48:38 +08:00
watch: {
checked (val) {
2018-01-16 15:41:00 +08:00
this.sChecked = val
2017-11-02 11:48:38 +08:00
},
2018-01-16 15:41:00 +08:00
'checkboxGroupContext.sValue': function (val) {
this.sChecked = val.indexOf(this.value) !== -1
2017-11-06 17:46:08 +08:00
},
2017-11-02 11:48:38 +08:00
},
2018-01-16 15:41:00 +08:00
render () {
const { $props: props, checkboxGroupContext, checkboxClass, name, $slots, sChecked } = this
const {
prefixCls,
} = props
let disabled = props.disabled
let onChange = this.handleChange
if (checkboxGroupContext) {
onChange = () => checkboxGroupContext.toggleOption({ value: props.value })
disabled = props.disabled || checkboxGroupContext.disabled
}
return (
<label
class={`${prefixCls}-wrapper`}
onMouseenter={this.onMouseEnter}
onMouseleave={this.onMouseLeave}
>
<span class={checkboxClass}>
<input name={name} type='checkbox' disabled={disabled}
class={`${prefixCls}-input`} checked={sChecked}
onChange={onChange} ref='input'
/>
<span class={`${prefixCls}-inner`} />
</span>
{$slots.default ? <span>{$slots.default}</span> : null}
</label>
)
},
2017-10-26 15:18:08 +08:00
}
2018-03-19 10:16:27 +08:00