checkbox component

This commit is contained in:
cookfront
2017-03-01 11:15:15 +08:00
parent 38d6771e72
commit e0be5b962e
14 changed files with 283 additions and 26 deletions

View File

@@ -0,0 +1,85 @@
<template>
<div
class="zan-checkbox"
:class="{
'is-disabled': isDisabled
}">
<span class="zan-checkbox__input">
<input
v-model="currentValue"
type="checkbox"
class="zan-checkbox__control"
:disabled="isDisabled">
<span class="zan-icon" :class="{
'zan-icon-checked': isChecked,
'zan-icon-check': !isChecked
}">
</span>
</span>
<span class="zan-checkbox__label">
<slot></slot>
</span>
</div>
</template>
<script>
import findParent from 'src/mixins/findParent';
export default {
name: 'zan-checkbox',
mixins: [findParent],
props: {
disabled: Boolean,
value: {},
name: [String, Number]
},
computed: {
isGroup() {
return !!this.findParentByComponentName('zan-checkbox-group');
},
currentValue: {
get() {
return this.isGroup && this.parentGroup ? this.parentGroup.value.indexOf(this.name) !== -1 : this.value;
},
set(val) {
if (this.isGroup && this.parentGroup) {
let parentValue = this.parentGroup.value.slice();
if (val) {
if (parentValue.indexOf(this.name) === -1) {
parentValue.push(this.name);
this.parentGroup.$emit('input', parentValue);
}
} else {
let index = parentValue.indexOf(this.name);
if (index !== -1) {
parentValue.splice(index, 1);
this.parentGroup.$emit('input', parentValue);
}
}
} else {
this.$emit('input', val);
}
}
},
isChecked() {
if ({}.toString.call(this.currentValue) === '[object Boolean]') {
return this.currentValue;
} else if (Array.isArray(this.currentValue)) {
return this.currentValue.indexOf(this.name) > -1;
}
},
isDisabled() {
return this.isGroup && this.parentGroup
? this.parentGroup.disabled || this.disabled
: this.disabled;
}
}
};
</script>