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

View File

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

View File

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