[improvement] Collapse: use relation mixin

This commit is contained in:
陈嘉涵 2019-05-07 09:57:43 +08:00
parent 7eb5846d62
commit 912d691f9e
3 changed files with 24 additions and 32 deletions

View File

@ -2,13 +2,13 @@ import { use, isDef } from '../utils';
import { raf } from '../utils/raf';
import Cell from '../cell';
import { cellProps } from '../cell/shared';
import { FindParentMixin } from '../mixins/find-parent';
import { ChildrenMixin } from '../mixins/relation';
const [sfc, bem] = use('collapse-item');
const CELL_SLOTS = ['title', 'icon', 'right-icon'];
export default sfc({
mixins: [FindParentMixin],
mixins: [ChildrenMixin('vanCollapse')],
props: {
...cellProps,
@ -28,14 +28,6 @@ export default sfc({
},
computed: {
items() {
return this.parent.items;
},
index() {
return this.items.indexOf(this);
},
currentName() {
return isDef(this.name) ? this.name : this.index;
},
@ -53,16 +45,10 @@ export default sfc({
},
created() {
this.findParent('van-collapse');
this.items.push(this);
this.show = this.expanded;
this.inited = this.expanded;
},
destroyed() {
this.items.splice(this.index, 1);
},
watch: {
expanded(expanded, prev) {
if (prev === null) {
@ -105,8 +91,7 @@ export default sfc({
parent.accordion && this.currentName === parent.value
? ''
: this.currentName;
const expanded = !this.expanded;
this.parent.switch(name, expanded);
this.parent.switch(name, !this.expanded);
},
onTransitionEnd() {

View File

@ -1,8 +1,11 @@
import { use } from '../utils';
import { ParentMixin } from '../mixins/relation';
const [sfc, bem] = use('collapse');
export default sfc({
mixins: [ParentMixin('vanCollapse')],
props: {
accordion: Boolean,
value: [String, Number, Array],
@ -12,12 +15,6 @@ export default sfc({
}
},
data() {
return {
items: []
};
},
methods: {
switch(name, expanded) {
if (!this.accordion) {

View File

@ -8,23 +8,33 @@ export function ChildrenMixin(parent) {
},
index() {
this.bindRelation();
return this.parent.children.indexOf(this);
}
},
created() {
const { children } = this.parent;
const index = this.parent.slots().indexOf(this.$vnode);
if (index === -1) {
children.push(this);
} else {
children.splice(index, 0, this);
}
this.bindRelation();
},
beforeDestroy() {
this.parent.children = this.parent.children.filter(item => item !== this);
},
methods: {
bindRelation() {
const { children } = this.parent;
if (children.indexOf(this) === -1) {
const vnodeIndex = this.parent.slots().indexOf(this.$vnode);
if (vnodeIndex === -1) {
children.push(this);
} else {
children.splice(vnodeIndex, 0, this);
}
}
}
}
};
}