You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ant-design-vue/components/checkbox/Checkbox.vue

126 lines
3.3 KiB

7 years ago
<script>
7 years ago
import hasProp from '../_util/props-util'
7 years ago
export default {
name: 'Checkbox',
props: {
prefixCls: {
default: 'ant-checkbox',
type: String,
},
7 years ago
defaultChecked: Boolean,
checked: { type: Boolean, default: undefined },
7 years ago
disabled: Boolean,
isGroup: Boolean,
value: [String, Number, Boolean],
name: String,
indeterminate: Boolean,
},
model: {
prop: 'checked',
},
7 years ago
inject: {
7 years ago
checkboxGroupContext: { default: null },
7 years ago
test: { default: null },
7 years ago
},
7 years ago
data () {
7 years ago
const { checkboxGroupContext, checked, defaultChecked, value } = this
7 years ago
let sChecked
if (checkboxGroupContext) {
sChecked = checkboxGroupContext.sValue.indexOf(value) !== -1
} else {
sChecked = !hasProp(this, 'checked') ? defaultChecked : checked
7 years ago
}
7 years ago
return {
7 years ago
sChecked,
7 years ago
}
},
7 years ago
computed: {
checkboxClass () {
7 years ago
const { prefixCls, indeterminate, sChecked, $props, checkboxGroupContext } = this
let disabled = $props.disabled
if (checkboxGroupContext) {
disabled = disabled || checkboxGroupContext.disabled
}
7 years ago
return {
[`${prefixCls}`]: true,
7 years ago
[`${prefixCls}-checked`]: sChecked,
7 years ago
[`${prefixCls}-disabled`]: disabled,
[`${prefixCls}-indeterminate`]: indeterminate,
}
},
},
methods: {
handleChange (event) {
7 years ago
const targetChecked = event.target.checked
this.$emit('input', targetChecked)
7 years ago
const { name, value, checked, checkboxGroupContext, sChecked } = this
if ((checked === undefined && !checkboxGroupContext) || (checkboxGroupContext && checkboxGroupContext.sValue === undefined)) {
this.sChecked = targetChecked
7 years ago
}
7 years ago
const target = {
name,
value,
7 years ago
checked: !sChecked,
7 years ago
}
7 years ago
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()
7 years ago
},
},
7 years ago
watch: {
checked (val) {
7 years ago
this.sChecked = val
7 years ago
},
7 years ago
'checkboxGroupContext.sValue': function (val) {
this.sChecked = val.indexOf(this.value) !== -1
7 years ago
},
7 years ago
},
7 years ago
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>
)
},
7 years ago
}
</script>