2017-10-26 07:18:08 +00:00
|
|
|
<template>
|
|
|
|
<label :class="classes">
|
|
|
|
<span :class="checkboxClass">
|
|
|
|
<input :name="name" type="checkbox" :disabled="disabled"
|
2017-11-02 03:48:38 +00:00
|
|
|
:class="`${prefixCls}-input`" :checked="stateChecked"
|
2017-10-26 07:18:08 +00:00
|
|
|
@change="handleChange"
|
|
|
|
/>
|
|
|
|
<span :class="`${prefixCls}-inner`" />
|
|
|
|
</span>
|
|
|
|
<span v-if="hasDefaultSlot">
|
|
|
|
<slot></slot>
|
|
|
|
</span>
|
|
|
|
</label>
|
|
|
|
</template>
|
|
|
|
<script>
|
|
|
|
export default {
|
|
|
|
name: 'Checkbox',
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
model: {
|
|
|
|
prop: 'checked',
|
|
|
|
},
|
2017-11-06 09:46:08 +00:00
|
|
|
inject: {
|
2017-11-07 03:58:47 +00:00
|
|
|
checkboxGroupContext: { default: undefined },
|
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
|
2017-11-06 09:46:08 +00:00
|
|
|
let stateChecked
|
2017-11-07 03:58:47 +00:00
|
|
|
if (checkboxGroupContext && checkboxGroupContext.checkedStatus) {
|
|
|
|
stateChecked = checkboxGroupContext.checkedStatus.has(value)
|
2017-11-06 09:46:08 +00:00
|
|
|
}
|
2017-11-02 03:48:38 +00:00
|
|
|
return {
|
2017-11-06 09:46:08 +00:00
|
|
|
stateChecked: stateChecked === undefined
|
|
|
|
? checked === undefined ? defaultChecked : checked
|
|
|
|
: stateChecked,
|
2017-11-02 03:48:38 +00:00
|
|
|
}
|
|
|
|
},
|
2017-10-26 07:18:08 +00:00
|
|
|
computed: {
|
|
|
|
hasDefaultSlot () {
|
|
|
|
return !!this.$slots.default
|
|
|
|
},
|
|
|
|
classes () {
|
|
|
|
const { prefixCls } = this
|
|
|
|
return {
|
|
|
|
[`${prefixCls}-wrapper`]: true,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
checkboxClass () {
|
2017-11-02 03:48:38 +00:00
|
|
|
const { prefixCls, indeterminate, stateChecked, disabled } = this
|
2017-10-26 07:18:08 +00:00
|
|
|
return {
|
|
|
|
[`${prefixCls}`]: true,
|
2017-11-02 03:48:38 +00:00
|
|
|
[`${prefixCls}-checked`]: stateChecked,
|
2017-10-26 07:18:08 +00:00
|
|
|
[`${prefixCls}-disabled`]: disabled,
|
|
|
|
[`${prefixCls}-indeterminate`]: indeterminate,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
mounted () {
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
handleChange (event) {
|
2017-11-02 03:48:38 +00:00
|
|
|
const targetChecked = event.target.checked
|
|
|
|
this.$emit('input', targetChecked)
|
2017-11-07 03:58:47 +00:00
|
|
|
const { name, value, checked, checkboxGroupContext, stateChecked } = this
|
|
|
|
if ((checked === undefined && !checkboxGroupContext) || (checkboxGroupContext && checkboxGroupContext.value === undefined)) {
|
2017-11-02 03:48:38 +00:00
|
|
|
this.stateChecked = targetChecked
|
|
|
|
}
|
2017-10-26 07:18:08 +00:00
|
|
|
const target = {
|
|
|
|
name,
|
|
|
|
value,
|
2017-11-06 09:46:08 +00:00
|
|
|
checked: !stateChecked,
|
2017-10-26 07:18:08 +00:00
|
|
|
}
|
2017-11-07 03:58:47 +00:00
|
|
|
if (this.checkboxGroupContext) {
|
|
|
|
this.checkboxGroupContext.handleChange({ target })
|
2017-11-06 09:46:08 +00:00
|
|
|
} else {
|
|
|
|
this.$emit('change', {
|
|
|
|
target,
|
|
|
|
stopPropagation () {
|
|
|
|
event.stopPropagation()
|
|
|
|
},
|
|
|
|
preventDefault () {
|
|
|
|
event.preventDefault()
|
|
|
|
},
|
|
|
|
})
|
2017-10-26 07:18:08 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
2017-11-02 03:48:38 +00:00
|
|
|
watch: {
|
|
|
|
checked (val) {
|
|
|
|
this.stateChecked = val
|
|
|
|
},
|
2017-11-07 03:58:47 +00:00
|
|
|
'checkboxGroupContext.checkedStatus': function (checkedStatus) {
|
2017-11-06 09:46:08 +00:00
|
|
|
this.stateChecked = checkedStatus.has(this.value)
|
|
|
|
},
|
2017-11-02 03:48:38 +00:00
|
|
|
},
|
2017-10-26 07:18:08 +00:00
|
|
|
}
|
|
|
|
</script>
|