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

110 lines
2.8 KiB

7 years ago
<template>
<label :class="classes">
<span :class="checkboxClass">
<input :name="name" type="checkbox" :disabled="disabled"
7 years ago
:class="`${prefixCls}-input`" :checked="stateChecked"
7 years ago
@change="handleChange"
/>
<span :class="`${prefixCls}-inner`" />
</span>
<span v-if="hasDefaultSlot">
<slot></slot>
</span>
</label>
</template>
<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: undefined },
7 years ago
},
7 years ago
data () {
7 years ago
const { checkboxGroupContext, checked, defaultChecked, value } = this
7 years ago
let stateChecked
7 years ago
if (checkboxGroupContext && checkboxGroupContext.checkedStatus) {
stateChecked = checkboxGroupContext.checkedStatus.has(value)
7 years ago
}
7 years ago
return {
7 years ago
stateChecked: stateChecked === undefined
7 years ago
? !hasProp(this, 'checked') ? defaultChecked : checked
7 years ago
: stateChecked,
7 years ago
}
},
7 years ago
computed: {
hasDefaultSlot () {
return !!this.$slots.default
},
classes () {
const { prefixCls } = this
return {
[`${prefixCls}-wrapper`]: true,
}
},
checkboxClass () {
7 years ago
const { prefixCls, indeterminate, stateChecked, disabled } = this
7 years ago
return {
[`${prefixCls}`]: true,
7 years ago
[`${prefixCls}-checked`]: stateChecked,
7 years ago
[`${prefixCls}-disabled`]: disabled,
[`${prefixCls}-indeterminate`]: indeterminate,
}
},
},
mounted () {
},
methods: {
handleChange (event) {
7 years ago
const targetChecked = event.target.checked
this.$emit('input', targetChecked)
7 years ago
const { name, value, checked, checkboxGroupContext, stateChecked } = this
if ((checked === undefined && !checkboxGroupContext) || (checkboxGroupContext && checkboxGroupContext.value === undefined)) {
7 years ago
this.stateChecked = targetChecked
}
7 years ago
const target = {
name,
value,
7 years ago
checked: !stateChecked,
7 years ago
}
7 years ago
if (this.checkboxGroupContext) {
this.checkboxGroupContext.handleChange({ target })
7 years ago
} else {
this.$emit('change', {
target,
stopPropagation () {
event.stopPropagation()
},
preventDefault () {
event.preventDefault()
},
})
7 years ago
}
},
},
7 years ago
watch: {
checked (val) {
this.stateChecked = val
},
7 years ago
'checkboxGroupContext.checkedStatus': function (checkedStatus) {
7 years ago
this.stateChecked = checkedStatus.has(this.value)
},
7 years ago
},
7 years ago
}
</script>