[Improvement] optimize find parent mixin (#781)

This commit is contained in:
neverland
2018-03-26 21:20:00 +08:00
committed by GitHub
parent dadf733d71
commit 5497eef5ef
13 changed files with 82 additions and 105 deletions

View File

@@ -40,44 +40,33 @@ export default create({
}
},
watch: {
value(val) {
this.$emit('change', val);
}
},
data() {
this.findParentByName('van-checkbox-group');
return {};
},
computed: {
currentValue: {
get() {
return this.parentGroup
? this.parentGroup.value.indexOf(this.name) !== -1
return this.parent
? this.parent.value.indexOf(this.name) !== -1
: this.value;
},
set(val) {
const { parentGroup } = this;
if (parentGroup) {
const parentValue = this.parentGroup.value.slice();
const { parent } = this;
if (parent) {
const parentValue = this.parent.value.slice();
if (val) {
if (parentGroup.max && parentValue.length >= parentGroup.max) {
if (parent.max && parentValue.length >= parent.max) {
return;
}
/* istanbul ignore else */
if (parentValue.indexOf(this.name) === -1) {
parentValue.push(this.name);
parentGroup.$emit('input', parentValue);
parent.$emit('input', parentValue);
}
} else {
const index = parentValue.indexOf(this.name);
/* istanbul ignore else */
if (index !== -1) {
parentValue.splice(index, 1);
parentGroup.$emit('input', parentValue);
parent.$emit('input', parentValue);
}
}
} else {
@@ -96,10 +85,20 @@ export default create({
},
isDisabled() {
return (this.parentGroup && this.parentGroup.disabled) || this.disabled;
return (this.parent && this.parent.disabled) || this.disabled;
}
},
watch: {
value(val) {
this.$emit('change', val);
}
},
created() {
this.findParent('van-checkbox-group');
},
methods: {
onClick(target) {
if (!this.isDisabled && !(target === 'label' && this.labelDisabled)) {