[improvement] Checkbox: use relation mixin
This commit is contained in:
@@ -2,10 +2,10 @@
|
||||
* Common part of Checkbox & Radio
|
||||
*/
|
||||
import Icon from '../icon';
|
||||
import { FindParentMixin } from './find-parent';
|
||||
import { ChildrenMixin } from './relation';
|
||||
|
||||
export const CheckboxMixin = (parent, bem) => ({
|
||||
mixins: [FindParentMixin],
|
||||
mixins: [ChildrenMixin(parent)],
|
||||
|
||||
props: {
|
||||
name: null,
|
||||
@@ -20,10 +20,6 @@ export const CheckboxMixin = (parent, bem) => ({
|
||||
}
|
||||
},
|
||||
|
||||
created() {
|
||||
this.findParent(parent);
|
||||
},
|
||||
|
||||
computed: {
|
||||
isDisabled() {
|
||||
return (this.parent && this.parent.disabled) || this.disabled;
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
/**
|
||||
* find parent component by name
|
||||
*/
|
||||
|
||||
export const FindParentMixin = {
|
||||
data() {
|
||||
return {
|
||||
parent: null
|
||||
};
|
||||
},
|
||||
|
||||
methods: {
|
||||
findParent(name) {
|
||||
let parent = this.$parent;
|
||||
while (parent) {
|
||||
if (parent.$options.name === name) {
|
||||
this.parent = parent;
|
||||
break;
|
||||
}
|
||||
parent = parent.$parent;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -1,6 +1,10 @@
|
||||
export function ChildrenMixin(parent) {
|
||||
return {
|
||||
inject: [parent],
|
||||
inject: {
|
||||
[parent]: {
|
||||
default: null
|
||||
}
|
||||
},
|
||||
|
||||
computed: {
|
||||
parent() {
|
||||
@@ -18,11 +22,17 @@ export function ChildrenMixin(parent) {
|
||||
},
|
||||
|
||||
beforeDestroy() {
|
||||
this.parent.children = this.parent.children.filter(item => item !== this);
|
||||
if (this.parent) {
|
||||
this.parent.children = this.parent.children.filter(item => item !== this);
|
||||
}
|
||||
},
|
||||
|
||||
methods: {
|
||||
bindRelation() {
|
||||
if (!this.parent) {
|
||||
return;
|
||||
}
|
||||
|
||||
const { children } = this.parent;
|
||||
|
||||
if (children.indexOf(this) === -1) {
|
||||
|
||||
Reference in New Issue
Block a user