element/packages/checkbox/src/checkbox.vue

135 lines
3.2 KiB
Vue
Raw Normal View History

2016-07-27 06:15:02 +00:00
<template>
<label class="el-checkbox">
<span class="el-checkbox__input"
:class="{
'is-disabled': disabled,
'is-checked': isChecked,
'is-indeterminate': indeterminate,
'is-focus': focus
}"
>
<span class="el-checkbox__inner"></span>
2016-07-27 06:15:02 +00:00
<input
v-if="trueLabel || falseLabel"
class="el-checkbox__original"
type="checkbox"
2016-11-13 15:51:44 +00:00
:name="name"
:disabled="disabled"
2016-07-27 06:15:02 +00:00
:true-value="trueLabel"
:false-value="falseLabel"
v-model="model"
2016-11-25 09:17:05 +00:00
@change="$emit('change', $event)"
2016-07-27 06:15:02 +00:00
@focus="focus = true"
@blur="focus = false">
2016-07-27 06:15:02 +00:00
<input
v-else
class="el-checkbox__original"
type="checkbox"
:disabled="disabled"
2016-07-27 06:15:02 +00:00
:value="label"
2016-11-13 15:51:44 +00:00
:name="name"
v-model="model"
2016-11-25 09:17:05 +00:00
@change="$emit('change', $event)"
2016-07-27 06:15:02 +00:00
@focus="focus = true"
@blur="focus = false">
2016-07-27 06:15:02 +00:00
</span>
2016-09-04 02:27:09 +00:00
<span class="el-checkbox__label" v-if="$slots.default || label">
2016-07-27 06:15:02 +00:00
<slot></slot>
2016-09-04 02:27:09 +00:00
<template v-if="!$slots.default">{{label}}</template>
2016-07-27 06:15:02 +00:00
</span>
</label>
</template>
<script>
2016-10-13 03:12:24 +00:00
import Emitter from 'element-ui/src/mixins/emitter';
2016-08-15 03:59:11 +00:00
2016-07-27 06:15:02 +00:00
export default {
name: 'ElCheckbox',
2016-08-12 06:45:06 +00:00
mixins: [Emitter],
componentName: 'ElCheckbox',
2016-07-27 06:15:02 +00:00
data() {
return {
selfModel: false,
focus: false
};
},
2016-07-27 06:15:02 +00:00
computed: {
model: {
2016-07-27 06:15:02 +00:00
get() {
return this.isGroup
? this.store : this.value !== undefined
? this.value : this.selfModel;
2016-07-27 06:15:02 +00:00
},
set(val) {
if (this.isGroup) {
this.dispatch('ElCheckboxGroup', 'input', [val]);
} else if (this.value !== undefined) {
this.$emit('input', val);
} else {
this.selfModel = val;
2016-07-27 06:15:02 +00:00
}
}
},
isChecked() {
if ({}.toString.call(this.model) === '[object Boolean]') {
return this.model;
} else if (Array.isArray(this.model)) {
return this.model.indexOf(this.label) > -1;
} else if (this.model !== null && this.model !== undefined) {
return this.model === this.trueLabel;
2016-07-27 06:15:02 +00:00
}
2016-12-02 00:08:12 +00:00
},
isGroup() {
let parent = this.$parent;
while (parent) {
if (parent.$options.componentName !== 'ElCheckboxGroup') {
parent = parent.$parent;
} else {
this._checkboxGroup = parent;
return true;
}
}
return false;
},
store() {
return this._checkboxGroup ? this._checkboxGroup.value : this.value;
2016-07-27 06:15:02 +00:00
}
},
props: {
value: {},
2016-11-27 11:06:46 +00:00
label: {},
indeterminate: Boolean,
disabled: Boolean,
checked: Boolean,
name: String,
trueLabel: [String, Number],
falseLabel: [String, Number]
},
methods: {
addToStore() {
if (
Array.isArray(this.model) &&
this.model.indexOf(this.label) === -1
) {
this.model.push(this.label);
} else {
this.model = this.trueLabel || true;
}
}
},
created() {
this.checked && this.addToStore();
2016-07-27 06:15:02 +00:00
}
};
</script>