2017-04-21 08:12:38 +00:00
|
|
|
<template>
|
|
|
|
<label
|
|
|
|
class="el-checkbox-button"
|
|
|
|
:class="[
|
|
|
|
size ? 'el-checkbox-button--' + size : '',
|
2017-08-25 05:37:56 +00:00
|
|
|
{ 'is-disabled': isDisabled },
|
2017-04-21 08:12:38 +00:00
|
|
|
{ 'is-checked': isChecked },
|
|
|
|
{ 'is-focus': focus },
|
|
|
|
]"
|
2017-08-05 10:33:50 +00:00
|
|
|
role="checkbox"
|
|
|
|
:aria-checked="isChecked"
|
2017-08-25 05:37:56 +00:00
|
|
|
:aria-disabled="isDisabled"
|
2017-04-21 08:12:38 +00:00
|
|
|
>
|
|
|
|
<input
|
|
|
|
v-if="trueLabel || falseLabel"
|
|
|
|
class="el-checkbox-button__original"
|
|
|
|
type="checkbox"
|
|
|
|
:name="name"
|
2017-08-25 05:37:56 +00:00
|
|
|
:disabled="isDisabled"
|
2017-04-21 08:12:38 +00:00
|
|
|
:true-value="trueLabel"
|
|
|
|
:false-value="falseLabel"
|
|
|
|
v-model="model"
|
|
|
|
@change="handleChange"
|
|
|
|
@focus="focus = true"
|
|
|
|
@blur="focus = false">
|
|
|
|
<input
|
|
|
|
v-else
|
|
|
|
class="el-checkbox-button__original"
|
|
|
|
type="checkbox"
|
|
|
|
:name="name"
|
2017-08-25 05:37:56 +00:00
|
|
|
:disabled="isDisabled"
|
2017-04-21 08:12:38 +00:00
|
|
|
:value="label"
|
|
|
|
v-model="model"
|
|
|
|
@change="handleChange"
|
|
|
|
@focus="focus = true"
|
|
|
|
@blur="focus = false">
|
|
|
|
|
|
|
|
<span class="el-checkbox-button__inner"
|
|
|
|
v-if="$slots.default || label"
|
|
|
|
:style="isChecked ? activeStyle : null">
|
|
|
|
<slot>{{label}}</slot>
|
|
|
|
</span>
|
|
|
|
|
|
|
|
</label>
|
|
|
|
</template>
|
|
|
|
<script>
|
|
|
|
import Emitter from 'element-ui/src/mixins/emitter';
|
|
|
|
|
|
|
|
export default {
|
|
|
|
name: 'ElCheckboxButton',
|
|
|
|
|
|
|
|
mixins: [Emitter],
|
|
|
|
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
selfModel: false,
|
2017-08-12 15:11:11 +00:00
|
|
|
focus: false,
|
|
|
|
isLimitExceeded: false
|
2017-04-21 08:12:38 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
props: {
|
|
|
|
value: {},
|
|
|
|
label: {},
|
|
|
|
disabled: Boolean,
|
|
|
|
checked: Boolean,
|
|
|
|
name: String,
|
|
|
|
trueLabel: [String, Number],
|
|
|
|
falseLabel: [String, Number]
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
model: {
|
|
|
|
get() {
|
|
|
|
return this._checkboxGroup
|
|
|
|
? this.store : this.value !== undefined
|
|
|
|
? this.value : this.selfModel;
|
|
|
|
},
|
|
|
|
|
|
|
|
set(val) {
|
|
|
|
if (this._checkboxGroup) {
|
2017-08-12 15:11:11 +00:00
|
|
|
this.isLimitExceeded = false;
|
2017-04-21 08:12:38 +00:00
|
|
|
(this._checkboxGroup.min !== undefined &&
|
|
|
|
val.length < this._checkboxGroup.min &&
|
2017-08-12 15:11:11 +00:00
|
|
|
(this.isLimitExceeded = true));
|
2017-04-21 08:12:38 +00:00
|
|
|
|
|
|
|
(this._checkboxGroup.max !== undefined &&
|
|
|
|
val.length > this._checkboxGroup.max &&
|
2017-08-12 15:11:11 +00:00
|
|
|
(this.isLimitExceeded = true));
|
2017-04-21 08:12:38 +00:00
|
|
|
|
2017-08-12 15:11:11 +00:00
|
|
|
this.isLimitExceeded === false &&
|
2017-04-21 08:12:38 +00:00
|
|
|
this.dispatch('ElCheckboxGroup', 'input', [val]);
|
|
|
|
} else if (this.value !== undefined) {
|
|
|
|
this.$emit('input', val);
|
|
|
|
} else {
|
|
|
|
this.selfModel = val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_checkboxGroup() {
|
|
|
|
let parent = this.$parent;
|
|
|
|
while (parent) {
|
|
|
|
if (parent.$options.componentName !== 'ElCheckboxGroup') {
|
|
|
|
parent = parent.$parent;
|
|
|
|
} else {
|
|
|
|
return parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
|
|
|
store() {
|
|
|
|
return this._checkboxGroup ? this._checkboxGroup.value : this.value;
|
|
|
|
},
|
|
|
|
|
|
|
|
activeStyle() {
|
|
|
|
return {
|
|
|
|
backgroundColor: this._checkboxGroup.fill || '',
|
|
|
|
borderColor: this._checkboxGroup.fill || '',
|
|
|
|
color: this._checkboxGroup.textColor || '',
|
|
|
|
'box-shadow': '-1px 0 0 0 ' + this._checkboxGroup.fill
|
|
|
|
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
size() {
|
|
|
|
return this._checkboxGroup.size;
|
2017-08-25 05:37:56 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
isDisabled() {
|
|
|
|
return this._checkboxGroup
|
|
|
|
? this._checkboxGroup.disabled || this.disabled
|
|
|
|
: this.disabled;
|
2017-04-21 08:12:38 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
addToStore() {
|
|
|
|
if (
|
|
|
|
Array.isArray(this.model) &&
|
|
|
|
this.model.indexOf(this.label) === -1
|
|
|
|
) {
|
|
|
|
this.model.push(this.label);
|
|
|
|
} else {
|
|
|
|
this.model = this.trueLabel || true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
handleChange(ev) {
|
2017-08-12 15:11:11 +00:00
|
|
|
this.$nextTick(() => {
|
|
|
|
if (this.isLimitExceeded) return;
|
|
|
|
this.$emit('change', this.model, ev);
|
|
|
|
if (this._checkboxGroup) {
|
2017-04-21 08:12:38 +00:00
|
|
|
this.dispatch('ElCheckboxGroup', 'change', [this._checkboxGroup.value]);
|
2017-08-12 15:11:11 +00:00
|
|
|
}
|
|
|
|
});
|
2017-04-21 08:12:38 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
created() {
|
|
|
|
this.checked && this.addToStore();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
</script>
|