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

126 lines
3.3 KiB
React
Raw Normal View History

2018-03-19 02:16:27 +00:00
2018-01-12 08:10:41 +00:00
import hasProp from '../_util/props-util'
2018-05-05 09:39:56 +00:00
import PropTypes from '../_util/vue-types'
2017-10-26 07:18:08 +00:00
export default {
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,
},
2017-11-02 03:48:38 +00:00
defaultChecked: Boolean,
checked: { type: Boolean, default: undefined },
2017-10-26 07:18:08 +00:00
disabled: Boolean,
isGroup: Boolean,
value: [String, Number, Boolean],
name: String,
indeterminate: Boolean,
2018-05-05 09:39:56 +00:00
type: PropTypes.string.def('checkbox'),
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-11-02 03:48:38 +00:00
data () {
2017-11-07 03:58:47 +00:00
const { checkboxGroupContext, checked, defaultChecked, value } = this
2018-01-16 07:41:00 +00:00
let sChecked
if (checkboxGroupContext) {
sChecked = checkboxGroupContext.sValue.indexOf(value) !== -1
} else {
sChecked = !hasProp(this, 'checked') ? defaultChecked : checked
2017-11-06 09:46:08 +00:00
}
2017-11-02 03:48:38 +00:00
return {
2018-01-16 07:41:00 +00:00
sChecked,
2017-11-02 03:48:38 +00:00
}
},
2017-10-26 07:18:08 +00:00
computed: {
checkboxClass () {
2018-01-16 07:41:00 +00:00
const { prefixCls, indeterminate, sChecked, $props, checkboxGroupContext } = this
let disabled = $props.disabled
if (checkboxGroupContext) {
disabled = disabled || checkboxGroupContext.disabled
}
2017-10-26 07:18:08 +00:00
return {
[`${prefixCls}`]: true,
2018-01-16 07:41:00 +00:00
[`${prefixCls}-checked`]: sChecked,
2017-10-26 07:18:08 +00:00
[`${prefixCls}-disabled`]: disabled,
[`${prefixCls}-indeterminate`]: indeterminate,
}
},
},
methods: {
handleChange (event) {
2017-11-02 03:48:38 +00:00
const targetChecked = event.target.checked
this.$emit('input', targetChecked)
2018-05-05 09:42:33 +00:00
const { checked, checkboxGroupContext } = this
2018-01-16 07:41:00 +00:00
if ((checked === undefined && !checkboxGroupContext) || (checkboxGroupContext && checkboxGroupContext.sValue === undefined)) {
this.sChecked = targetChecked
2017-11-02 03:48:38 +00:00
}
2017-10-26 07:18:08 +00:00
const target = {
2018-05-05 09:42:33 +00:00
...this.$props,
2018-05-05 09:39:56 +00:00
checked: targetChecked,
2017-10-26 07:18:08 +00:00
}
2018-01-16 07:41:00 +00: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 07:18:08 +00:00
},
},
2017-11-02 03:48:38 +00:00
watch: {
checked (val) {
2018-01-16 07:41:00 +00:00
this.sChecked = val
2017-11-02 03:48:38 +00:00
},
2018-01-16 07:41:00 +00:00
'checkboxGroupContext.sValue': function (val) {
this.sChecked = val.indexOf(this.value) !== -1
2017-11-06 09:46:08 +00:00
},
2017-11-02 03:48:38 +00:00
},
2018-01-16 07:41:00 +00: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 07:18:08 +00:00
}
2018-03-19 02:16:27 +00:00