2016-09-09 14:29:19 +08:00
|
|
|
<template>
|
2018-01-18 20:32:20 +01:00
|
|
|
<label
|
|
|
|
:class="wrapClasses"
|
|
|
|
@keydown.space.prevent="$el.click()"
|
|
|
|
tabindex="0">
|
2016-09-09 14:29:19 +08:00
|
|
|
<span :class="checkboxClasses">
|
|
|
|
<span :class="innerClasses"></span>
|
|
|
|
<input
|
|
|
|
v-if="group"
|
|
|
|
type="checkbox"
|
2018-01-18 22:03:53 +01:00
|
|
|
tabindex="-1"
|
2016-09-09 14:29:19 +08:00
|
|
|
:class="inputClasses"
|
|
|
|
:disabled="disabled"
|
2017-03-01 17:58:40 +08:00
|
|
|
:value="label"
|
2016-09-09 14:29:19 +08:00
|
|
|
v-model="model"
|
2017-09-19 16:45:02 +08:00
|
|
|
:name="name"
|
2018-01-18 22:03:53 +01:00
|
|
|
@change="change"
|
|
|
|
@focus="$el.focus()"
|
|
|
|
>
|
2016-09-09 14:29:19 +08:00
|
|
|
<input
|
|
|
|
v-if="!group"
|
|
|
|
type="checkbox"
|
2018-01-18 22:03:53 +01:00
|
|
|
tabindex="-1"
|
2016-09-09 14:29:19 +08:00
|
|
|
:class="inputClasses"
|
|
|
|
:disabled="disabled"
|
2017-03-01 17:58:40 +08:00
|
|
|
:checked="currentValue"
|
2017-09-19 16:45:02 +08:00
|
|
|
:name="name"
|
2018-01-18 22:03:53 +01:00
|
|
|
@change="change"
|
|
|
|
@focus="$el.focus()"
|
|
|
|
>
|
2016-09-09 14:29:19 +08:00
|
|
|
</span>
|
2017-03-15 18:52:33 +08:00
|
|
|
<slot><span v-if="showSlot">{{ label }}</span></slot>
|
2016-09-09 14:29:19 +08:00
|
|
|
</label>
|
|
|
|
</template>
|
|
|
|
<script>
|
2017-08-24 17:25:21 +08:00
|
|
|
import { findComponentUpward, oneOf } from '../../utils/assist';
|
2017-03-09 11:14:40 +08:00
|
|
|
import Emitter from '../../mixins/emitter';
|
|
|
|
|
2016-09-09 14:29:19 +08:00
|
|
|
const prefixCls = 'ivu-checkbox';
|
|
|
|
|
|
|
|
export default {
|
2017-03-01 17:58:40 +08:00
|
|
|
name: 'Checkbox',
|
2017-03-09 11:14:40 +08:00
|
|
|
mixins: [ Emitter ],
|
2016-09-09 14:29:19 +08:00
|
|
|
props: {
|
|
|
|
disabled: {
|
|
|
|
type: Boolean,
|
|
|
|
default: false
|
|
|
|
},
|
|
|
|
value: {
|
2017-08-09 09:54:31 -05:00
|
|
|
type: [String, Number, Boolean],
|
|
|
|
default: false
|
|
|
|
},
|
|
|
|
trueValue: {
|
|
|
|
type: [String, Number, Boolean],
|
|
|
|
default: true
|
|
|
|
},
|
|
|
|
falseValue: {
|
|
|
|
type: [String, Number, Boolean],
|
2016-09-09 14:29:19 +08:00
|
|
|
default: false
|
2017-02-07 13:34:16 +08:00
|
|
|
},
|
2017-03-01 17:58:40 +08:00
|
|
|
label: {
|
|
|
|
type: [String, Number, Boolean]
|
|
|
|
},
|
2017-02-07 13:34:16 +08:00
|
|
|
indeterminate: {
|
|
|
|
type: Boolean,
|
|
|
|
default: false
|
2017-08-24 17:25:21 +08:00
|
|
|
},
|
|
|
|
size: {
|
|
|
|
validator (value) {
|
|
|
|
return oneOf(value, ['small', 'large', 'default']);
|
|
|
|
}
|
2017-09-19 16:45:02 +08:00
|
|
|
},
|
|
|
|
name: {
|
|
|
|
type: String
|
2016-09-09 14:29:19 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
data () {
|
|
|
|
return {
|
|
|
|
model: [],
|
2017-03-01 17:58:40 +08:00
|
|
|
currentValue: this.value,
|
2016-11-07 17:15:06 +08:00
|
|
|
group: false,
|
2017-03-15 17:56:50 +08:00
|
|
|
showSlot: true,
|
|
|
|
parent: findComponentUpward(this, 'CheckboxGroup')
|
2016-12-25 22:49:42 +08:00
|
|
|
};
|
2016-09-09 14:29:19 +08:00
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
wrapClasses () {
|
|
|
|
return [
|
|
|
|
`${prefixCls}-wrapper`,
|
|
|
|
{
|
|
|
|
[`${prefixCls}-group-item`]: this.group,
|
2017-03-01 17:58:40 +08:00
|
|
|
[`${prefixCls}-wrapper-checked`]: this.currentValue,
|
2017-08-24 17:25:21 +08:00
|
|
|
[`${prefixCls}-wrapper-disabled`]: this.disabled,
|
|
|
|
[`${prefixCls}-${this.size}`]: !!this.size
|
2016-09-09 14:29:19 +08:00
|
|
|
}
|
2016-12-25 22:49:42 +08:00
|
|
|
];
|
2016-09-09 14:29:19 +08:00
|
|
|
},
|
|
|
|
checkboxClasses () {
|
|
|
|
return [
|
|
|
|
`${prefixCls}`,
|
|
|
|
{
|
2017-03-01 17:58:40 +08:00
|
|
|
[`${prefixCls}-checked`]: this.currentValue,
|
2017-02-07 13:34:16 +08:00
|
|
|
[`${prefixCls}-disabled`]: this.disabled,
|
|
|
|
[`${prefixCls}-indeterminate`]: this.indeterminate
|
2016-09-09 14:29:19 +08:00
|
|
|
}
|
2016-12-25 22:49:42 +08:00
|
|
|
];
|
2016-09-09 14:29:19 +08:00
|
|
|
},
|
|
|
|
innerClasses () {
|
|
|
|
return `${prefixCls}-inner`;
|
|
|
|
},
|
|
|
|
inputClasses () {
|
|
|
|
return `${prefixCls}-input`;
|
|
|
|
}
|
|
|
|
},
|
2017-03-01 17:58:40 +08:00
|
|
|
mounted () {
|
2017-03-15 17:56:50 +08:00
|
|
|
this.parent = findComponentUpward(this, 'CheckboxGroup');
|
|
|
|
if (this.parent) this.group = true;
|
2016-09-09 14:29:19 +08:00
|
|
|
if (!this.group) {
|
|
|
|
this.updateModel();
|
2017-03-15 18:52:33 +08:00
|
|
|
this.showSlot = this.$slots.default !== undefined;
|
2017-04-27 14:33:25 +08:00
|
|
|
} else {
|
|
|
|
this.parent.updateModel(true);
|
2016-09-09 14:29:19 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
change (event) {
|
|
|
|
if (this.disabled) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-01 17:58:40 +08:00
|
|
|
const checked = event.target.checked;
|
|
|
|
this.currentValue = checked;
|
2017-08-09 09:54:31 -05:00
|
|
|
|
|
|
|
let value = checked ? this.trueValue : this.falseValue;
|
|
|
|
this.$emit('input', value);
|
2016-09-09 14:29:19 +08:00
|
|
|
|
|
|
|
if (this.group) {
|
2017-06-05 16:04:22 +08:00
|
|
|
this.parent.change(this.model);
|
2016-09-09 14:29:19 +08:00
|
|
|
} else {
|
2017-08-09 09:54:31 -05:00
|
|
|
this.$emit('on-change', value);
|
|
|
|
this.dispatch('FormItem', 'on-form-change', value);
|
2016-09-09 14:29:19 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
updateModel () {
|
2017-08-09 09:54:31 -05:00
|
|
|
this.currentValue = this.value === this.trueValue;
|
2016-09-09 14:29:19 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
watch: {
|
2017-08-09 09:54:31 -05:00
|
|
|
value (val) {
|
|
|
|
if (val !== this.trueValue && val !== this.falseValue) {
|
|
|
|
throw 'Value should be trueValue or falseValue.';
|
|
|
|
}
|
2016-09-09 14:29:19 +08:00
|
|
|
this.updateModel();
|
|
|
|
}
|
|
|
|
}
|
2016-12-25 22:49:42 +08:00
|
|
|
};
|
|
|
|
</script>
|