mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
70 lines
1.2 KiB
JavaScript
70 lines
1.2 KiB
JavaScript
import { sortChildren } from '../utils/vnodes';
|
|
|
|
export function ChildrenMixin(parent, options = {}) {
|
|
const indexKey = options.indexKey || 'index';
|
|
|
|
return {
|
|
inject: {
|
|
// TODO: disableBindRelation
|
|
parent: {
|
|
from: parent,
|
|
default: null,
|
|
},
|
|
},
|
|
|
|
computed: {
|
|
[indexKey]() {
|
|
this.bindRelation();
|
|
|
|
if (this.parent) {
|
|
return this.parent.children.indexOf(this);
|
|
}
|
|
|
|
return null;
|
|
},
|
|
},
|
|
|
|
mounted() {
|
|
this.bindRelation();
|
|
},
|
|
|
|
beforeDestroy() {
|
|
if (this.parent) {
|
|
this.parent.children = this.parent.children.filter(
|
|
(item) => item !== this
|
|
);
|
|
}
|
|
},
|
|
|
|
methods: {
|
|
bindRelation() {
|
|
if (!this.parent || this.parent.children.indexOf(this) !== -1) {
|
|
return;
|
|
}
|
|
|
|
const children = [...this.parent.children, this];
|
|
|
|
sortChildren(children, this.parent);
|
|
|
|
this.parent.children = children;
|
|
},
|
|
},
|
|
};
|
|
}
|
|
|
|
export function ParentMixin(parent) {
|
|
return {
|
|
provide() {
|
|
return {
|
|
[parent]: this,
|
|
};
|
|
},
|
|
|
|
data() {
|
|
return {
|
|
children: [],
|
|
};
|
|
},
|
|
};
|
|
}
|