2016-07-27 06:15:02 +00:00
|
|
|
|
<template>
|
2017-08-05 10:33:50 +00:00
|
|
|
|
<label
|
|
|
|
|
class="el-checkbox"
|
2017-08-25 05:37:56 +00:00
|
|
|
|
:class="[
|
|
|
|
|
border && checkboxSize ? 'el-checkbox--' + checkboxSize : '',
|
|
|
|
|
{ 'is-disabled': isDisabled },
|
|
|
|
|
{ 'is-bordered': border },
|
|
|
|
|
{ 'is-checked': isChecked }
|
|
|
|
|
]"
|
2017-08-05 10:33:50 +00:00
|
|
|
|
role="checkbox"
|
|
|
|
|
:aria-checked="indeterminate ? 'mixed': isChecked"
|
2017-08-25 05:37:56 +00:00
|
|
|
|
:aria-disabled="isDisabled"
|
2017-08-05 10:33:50 +00:00
|
|
|
|
:id="id"
|
|
|
|
|
>
|
2016-12-22 16:55:24 +00:00
|
|
|
|
<span class="el-checkbox__input"
|
|
|
|
|
:class="{
|
2017-08-25 05:37:56 +00:00
|
|
|
|
'is-disabled': isDisabled,
|
2016-12-22 16:55:24 +00:00
|
|
|
|
'is-checked': isChecked,
|
|
|
|
|
'is-indeterminate': indeterminate,
|
|
|
|
|
'is-focus': focus
|
|
|
|
|
}"
|
2017-08-05 10:33:50 +00:00
|
|
|
|
aria-checked="mixed"
|
2016-12-22 16:55:24 +00:00
|
|
|
|
>
|
|
|
|
|
<span class="el-checkbox__inner"></span>
|
2016-07-27 06:15:02 +00:00
|
|
|
|
<input
|
|
|
|
|
v-if="trueLabel || falseLabel"
|
|
|
|
|
class="el-checkbox__original"
|
2016-10-10 16:23:27 +00:00
|
|
|
|
type="checkbox"
|
2016-11-13 15:51:44 +00:00
|
|
|
|
:name="name"
|
2017-08-25 05:37:56 +00:00
|
|
|
|
:disabled="isDisabled"
|
2016-07-27 06:15:02 +00:00
|
|
|
|
:true-value="trueLabel"
|
|
|
|
|
:false-value="falseLabel"
|
2016-11-25 05:13:41 +00:00
|
|
|
|
v-model="model"
|
2017-01-16 02:39:24 +00:00
|
|
|
|
@change="handleChange"
|
2016-07-27 06:15:02 +00:00
|
|
|
|
@focus="focus = true"
|
2016-11-25 05:13:41 +00:00
|
|
|
|
@blur="focus = false">
|
2016-07-27 06:15:02 +00:00
|
|
|
|
<input
|
|
|
|
|
v-else
|
|
|
|
|
class="el-checkbox__original"
|
2016-10-10 16:23:27 +00:00
|
|
|
|
type="checkbox"
|
2017-08-25 05:37:56 +00:00
|
|
|
|
:disabled="isDisabled"
|
2016-07-27 06:15:02 +00:00
|
|
|
|
:value="label"
|
2016-11-13 15:51:44 +00:00
|
|
|
|
:name="name"
|
2016-11-25 05:13:41 +00:00
|
|
|
|
v-model="model"
|
2017-01-16 02:39:24 +00:00
|
|
|
|
@change="handleChange"
|
2016-07-27 06:15:02 +00:00
|
|
|
|
@focus="focus = true"
|
2016-11-25 05:13:41 +00:00
|
|
|
|
@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>
|
2017-05-03 03:50:17 +00:00
|
|
|
|
import Emitter from 'element-ui/src/mixins/emitter';
|
|
|
|
|
|
2016-07-27 06:15:02 +00:00
|
|
|
|
export default {
|
|
|
|
|
name: 'ElCheckbox',
|
|
|
|
|
|
2017-05-03 03:50:17 +00:00
|
|
|
|
mixins: [Emitter],
|
|
|
|
|
|
2016-11-25 05:13:41 +00:00
|
|
|
|
componentName: 'ElCheckbox',
|
2016-07-27 06:15:02 +00:00
|
|
|
|
|
2016-12-18 17:10:49 +00:00
|
|
|
|
data() {
|
|
|
|
|
return {
|
2016-12-20 16:57:11 +00:00
|
|
|
|
selfModel: false,
|
2017-08-12 15:11:11 +00:00
|
|
|
|
focus: false,
|
|
|
|
|
isLimitExceeded: false
|
2016-12-18 17:10:49 +00:00
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
|
2016-07-27 06:15:02 +00:00
|
|
|
|
computed: {
|
2016-11-25 05:13:41 +00:00
|
|
|
|
model: {
|
2016-07-27 06:15:02 +00:00
|
|
|
|
get() {
|
2016-12-18 17:10:49 +00:00
|
|
|
|
return this.isGroup
|
|
|
|
|
? this.store : this.value !== undefined
|
|
|
|
|
? this.value : this.selfModel;
|
2016-07-27 06:15:02 +00:00
|
|
|
|
},
|
2016-11-25 05:13:41 +00:00
|
|
|
|
|
|
|
|
|
set(val) {
|
|
|
|
|
if (this.isGroup) {
|
2017-08-12 15:11:11 +00:00
|
|
|
|
this.isLimitExceeded = false;
|
2017-05-03 03:50:17 +00:00
|
|
|
|
(this._checkboxGroup.min !== undefined &&
|
|
|
|
|
val.length < this._checkboxGroup.min &&
|
2017-08-12 15:11:11 +00:00
|
|
|
|
(this.isLimitExceeded = true));
|
2017-03-22 21:21:33 +00:00
|
|
|
|
|
2017-05-03 03:50:17 +00:00
|
|
|
|
(this._checkboxGroup.max !== undefined &&
|
|
|
|
|
val.length > this._checkboxGroup.max &&
|
2017-08-12 15:11:11 +00:00
|
|
|
|
(this.isLimitExceeded = true));
|
2017-03-22 21:21:33 +00:00
|
|
|
|
|
2017-08-12 15:11:11 +00:00
|
|
|
|
this.isLimitExceeded === false &&
|
2017-05-03 03:50:17 +00:00
|
|
|
|
this.dispatch('ElCheckboxGroup', 'input', [val]);
|
2016-12-18 17:10:49 +00:00
|
|
|
|
} else {
|
2017-06-15 11:16:02 +00:00
|
|
|
|
this.$emit('input', val);
|
2016-12-18 17:10:49 +00:00
|
|
|
|
this.selfModel = val;
|
2016-07-27 06:15:02 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2016-11-25 05:13:41 +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() {
|
2017-05-03 03:50:17 +00:00
|
|
|
|
let parent = this.$parent;
|
|
|
|
|
while (parent) {
|
|
|
|
|
if (parent.$options.componentName !== 'ElCheckboxGroup') {
|
|
|
|
|
parent = parent.$parent;
|
|
|
|
|
} else {
|
|
|
|
|
this._checkboxGroup = parent;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
2016-12-02 00:08:12 +00:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
store() {
|
2017-05-03 03:50:17 +00:00
|
|
|
|
return this._checkboxGroup ? this._checkboxGroup.value : this.value;
|
2017-08-25 05:37:56 +00:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
isDisabled() {
|
|
|
|
|
return this.isGroup
|
|
|
|
|
? this._checkboxGroup.disabled || this.disabled
|
|
|
|
|
: this.disabled;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
checkboxSize() {
|
|
|
|
|
return this.isGroup
|
|
|
|
|
? this._checkboxGroup.size || this.size
|
|
|
|
|
: this.size;
|
2016-07-27 06:15:02 +00:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2016-11-25 05:13:41 +00:00
|
|
|
|
props: {
|
|
|
|
|
value: {},
|
2016-11-27 11:06:46 +00:00
|
|
|
|
label: {},
|
2016-11-25 05:13:41 +00:00
|
|
|
|
indeterminate: Boolean,
|
|
|
|
|
disabled: Boolean,
|
|
|
|
|
checked: Boolean,
|
|
|
|
|
name: String,
|
|
|
|
|
trueLabel: [String, Number],
|
2017-08-05 10:33:50 +00:00
|
|
|
|
falseLabel: [String, Number],
|
|
|
|
|
id: String, /* 当indeterminate为真时,为controls提供相关连的checkbox的id,表明元素间的控制关系*/
|
2017-08-25 05:37:56 +00:00
|
|
|
|
controls: String, /* 当indeterminate为真时,为controls提供相关连的checkbox的id,表明元素间的控制关系*/
|
|
|
|
|
border: Boolean,
|
|
|
|
|
size: String
|
2016-11-25 05:13:41 +00:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
methods: {
|
|
|
|
|
addToStore() {
|
2016-12-18 17:10:49 +00:00
|
|
|
|
if (
|
|
|
|
|
Array.isArray(this.model) &&
|
|
|
|
|
this.model.indexOf(this.label) === -1
|
|
|
|
|
) {
|
|
|
|
|
this.model.push(this.label);
|
2016-11-25 05:13:41 +00:00
|
|
|
|
} else {
|
|
|
|
|
this.model = this.trueLabel || true;
|
2016-10-10 16:23:27 +00:00
|
|
|
|
}
|
2017-01-16 02:39:24 +00:00
|
|
|
|
},
|
|
|
|
|
handleChange(ev) {
|
2017-08-12 15:11:11 +00:00
|
|
|
|
this.$nextTick(() => {
|
|
|
|
|
if (this.isLimitExceeded) return;
|
|
|
|
|
this.$emit('change', this.model, ev);
|
|
|
|
|
if (this.isGroup) {
|
2017-05-03 03:50:17 +00:00
|
|
|
|
this.dispatch('ElCheckboxGroup', 'change', [this._checkboxGroup.value]);
|
2017-08-12 15:11:11 +00:00
|
|
|
|
}
|
|
|
|
|
});
|
2016-10-10 16:23:27 +00:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2016-11-25 05:13:41 +00:00
|
|
|
|
created() {
|
|
|
|
|
this.checked && this.addToStore();
|
2017-08-05 10:33:50 +00:00
|
|
|
|
},
|
|
|
|
|
mounted() { // 为indeterminate元素 添加aria-controls 属性
|
|
|
|
|
if (this.indeterminate) {
|
|
|
|
|
this.$el.setAttribute('aria-controls', this.controls);
|
|
|
|
|
}
|
2016-07-27 06:15:02 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
</script>
|