diff --git a/packages/collapse-item/index.js b/packages/collapse-item/index.js index cc6c2bbf5..b8cbc5ea6 100644 --- a/packages/collapse-item/index.js +++ b/packages/collapse-item/index.js @@ -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() { diff --git a/packages/collapse/index.js b/packages/collapse/index.js index 38dadf554..6652d4687 100644 --- a/packages/collapse/index.js +++ b/packages/collapse/index.js @@ -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) { diff --git a/packages/mixins/relation.js b/packages/mixins/relation.js index 6e2f63474..87146d793 100644 --- a/packages/mixins/relation.js +++ b/packages/mixins/relation.js @@ -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); + } + } + } } }; }