iview/src/components/checkbox/checkbox.vue

125 lines
3.7 KiB
Vue
Raw Normal View History

2016-09-09 14:29:19 +08:00
<template>
<label :class="wrapClasses">
<span :class="checkboxClasses">
<span :class="innerClasses"></span>
<input
v-if="group"
type="checkbox"
: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"
@change="change">
<input
v-if="!group"
type="checkbox"
:class="inputClasses"
:disabled="disabled"
2017-03-01 17:58:40 +08:00
:checked="currentValue"
2016-09-09 14:29:19 +08:00
@change="change">
</span>
<slot><span v-if="showSlot">{{ label }}</span></slot>
2016-09-09 14:29:19 +08:00
</label>
</template>
<script>
2017-03-15 17:56:50 +08:00
import { findComponentUpward } from '../../utils/assist';
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',
mixins: [ Emitter ],
2016-09-09 14:29:19 +08:00
props: {
disabled: {
type: Boolean,
default: false
},
value: {
type: Boolean,
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
2016-09-09 14:29:19 +08:00
}
},
data () {
return {
model: [],
2017-03-01 17:58:40 +08:00
currentValue: this.value,
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,
2016-09-09 14:29:19 +08:00
[`${prefixCls}-wrapper-disabled`]: this.disabled
}
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();
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;
this.$emit('input', checked);
2016-09-09 14:29:19 +08:00
if (this.group) {
this.$parent.change(this.model);
2016-09-09 14:29:19 +08:00
} else {
2017-03-01 17:58:40 +08:00
this.$emit('on-change', checked);
this.dispatch('FormItem', 'on-form-change', checked);
2016-09-09 14:29:19 +08:00
}
},
updateModel () {
2017-03-01 17:58:40 +08:00
this.currentValue = this.value;
2016-09-09 14:29:19 +08:00
}
},
watch: {
2017-03-01 17:58:40 +08:00
value () {
2016-09-09 14:29:19 +08:00
this.updateModel();
}
}
2016-12-25 22:49:42 +08:00
};
</script>