element/packages/checkbox/src/checkbox.vue

126 lines
3.0 KiB
Vue
Raw Normal View History

2016-07-27 06:15:02 +00:00
<template>
<label class="el-checkbox">
<span class="el-checkbox__input">
<span class="el-checkbox__inner"
:class="{
'is-disabled': isLimit || disabled,
'is-checked': checked,
'is-indeterminate': indeterminate,
'is-focus': focus
}"
></span>
<input
v-if="trueLabel || falseLabel"
class="el-checkbox__original"
:true-value="trueLabel"
:false-value="falseLabel"
v-model="_value"
type="checkbox"
@focus="focus = true"
@blur="focus = false"
:disabled="isLimit || disabled"
2016-08-12 06:45:06 +00:00
ref="checkbox">
2016-07-27 06:15:02 +00:00
<input
v-else
class="el-checkbox__original"
:value="label"
v-model="_value"
@focus="focus = true"
@blur="focus = false"
type="checkbox"
:disabled="isLimit || disabled">
</span>
<span class="el-checkbox__label">
<slot></slot>
2016-08-12 06:45:06 +00:00
<template v-if="!$slots || !$slots.default">{{label}}</template>
2016-07-27 06:15:02 +00:00
</span>
</label>
</template>
<script>
2016-08-12 06:45:06 +00:00
import Emitter from 'main/mixins/emitter';
2016-07-27 06:15:02 +00:00
/**
* checkbox
* @module components/basic/checkbox
* @desc 多选按钮
* @param {string[]} value - 绑定值
* @param {string} value - 真实值
* @param {string} [label] - 显示值
* @param {boolean} [disabled=false] - 是否禁用
*
* @example
* <el-checkbox :value.sync="data" value="Jack"></el-checkbox>
* <el-checkbox :value.sync="data" value="John"></el-checkbox>
* <el-checkbox :value.sync="data" value="Mike" disabled></el-checkbox>
*/
export default {
name: 'ElCheckbox',
2016-08-12 06:45:06 +00:00
mixins: [Emitter],
2016-07-27 06:15:02 +00:00
props: {
2016-08-12 06:45:06 +00:00
value: {},
2016-07-27 06:15:02 +00:00
label: {
type: String
},
indeterminate: Boolean,
disabled: Boolean,
trueLabel: {
default: ''
},
falseLabel: {
default: ''
}
},
computed: {
_value: {
get() {
return this.value !== undefined ? this.value : this.$parent.value;
},
set(newValue) {
if (this.value !== undefined) {
2016-08-12 06:45:06 +00:00
this.$emit('input', newValue);
2016-07-27 06:15:02 +00:00
} else {
2016-08-12 06:45:06 +00:00
this.$parent.$emit('input', newValue);
2016-07-27 06:15:02 +00:00
}
}
},
checked() {
var type = Object.prototype.toString.call(this._value);
if (type === '[object Boolean]') {
return this._value;
} else if (type === '[object Array]') {
return this._value.indexOf(this.label) > -1;
} else if (type === '[object String]') {
return this._value === this.trueLabel;
}
}
},
data() {
return {
isLimit: false,
focus: false
};
},
watch: {
checked(sure) {
2016-08-12 06:45:06 +00:00
this.$emit('on-change', sure);
this.dispatch('element.checkbox', sure);
2016-07-27 06:15:02 +00:00
}
2016-08-12 06:45:06 +00:00
},
created() {
this.$on('element.checkbox.disabled', () => {
if (this.checked) return;
this.isLimit = true;
});
this.$on('element.checkbox.enabled', () => {
this.isLimit = false;
});
2016-07-27 06:15:02 +00:00
}
};
</script>