From 09b8573173d20a12b16e7b2075ed81743ab9095e Mon Sep 17 00:00:00 2001 From: Vi Pro Date: Mon, 14 May 2018 00:47:11 +0800 Subject: [PATCH] feat(MdList): expand only one option (#1645) * feat(MdList): expand only one option fix #1641 * fix(MdList): rename expandOnlyOne to expandSingle better props name --- docs/app/pages/Components/List/List.vue | 9 +- .../List/examples/ListExpansion.vue | 99 ++++++++++++------- src/components/MdList/MdList.vue | 44 ++++++++- .../MdList/MdListItem/MdListItemExpand.vue | 11 +++ 4 files changed, 121 insertions(+), 42 deletions(-) diff --git a/docs/app/pages/Components/List/List.vue b/docs/app/pages/Components/List/List.vue index 59e1640f9..cf2263972 100644 --- a/docs/app/pages/Components/List/List.vue +++ b/docs/app/pages/Components/List/List.vue @@ -56,6 +56,7 @@

The following options can be applied to any list:

+
@@ -82,10 +83,10 @@ headings: ['Name', 'Description', 'Default'], props: [ { - name: 'id', - type: 'String', - description: 'The item id. Used when changing the selected item dynamically', - defaults: 'a random string' + name: 'md-expand-single', + type: 'Boolean', + description: 'If set true, one expandable list item could be expanded at most at a time. The expanded list item will be collapsed when another is expanded', + defaults: 'false' } ] }, diff --git a/docs/app/pages/Components/List/examples/ListExpansion.vue b/docs/app/pages/Components/List/examples/ListExpansion.vue index 1d2058738..ca23fa662 100644 --- a/docs/app/pages/Components/List/examples/ListExpansion.vue +++ b/docs/app/pages/Components/List/examples/ListExpansion.vue @@ -1,46 +1,51 @@ @@ -50,15 +55,28 @@ data () { return { - expandNews: false + expandNews: false, + expandSingle: false } } } diff --git a/src/components/MdList/MdList.vue b/src/components/MdList/MdList.vue index 366d1bd9f..d3b2a44e5 100644 --- a/src/components/MdList/MdList.vue +++ b/src/components/MdList/MdList.vue @@ -8,7 +8,49 @@ import MdComponent from 'core/MdComponent' export default new MdComponent({ - name: 'MdList' + name: 'MdList', + data () { + return { + MdList: { + expandable: [], + expandATab: this.expandATab, + pushExpandable: this.pushExpandable, + removeExpandable: this.removeExpandable + } + } + }, + provide () { + return { + MdList: this.MdList + } + }, + props: { + mdExpandSingle: { + default: false + } + }, + methods: { + expandATab (expandedListItem) { + if (this.mdExpandSingle && expandedListItem) { + const otherExpandableListItems = this.MdList.expandable.filter(target => target !== expandedListItem) + otherExpandableListItems.forEach(expandableListItem => expandableListItem.close()) + } + }, + pushExpandable (expandableListItem) { + let expandableListItems = this.MdList.expandable + + if (!expandableListItems.find(target => target === expandableListItem)) { + this.MdList.expandable = expandableListItems.concat([expandableListItem]) + } + }, + removeExpandable (expandableListItem) { + let expandableListItems = this.MdList.expandable + + if (expandableListItems.find(target => target === expandableListItem)) { + this.MdList.expandable = expandableListItems.filter(target => target !== expandableListItem) + } + } + } }) diff --git a/src/components/MdList/MdListItem/MdListItemExpand.vue b/src/components/MdList/MdListItem/MdListItemExpand.vue index b87b03a35..d471c6769 100644 --- a/src/components/MdList/MdListItem/MdListItemExpand.vue +++ b/src/components/MdList/MdListItem/MdListItemExpand.vue @@ -23,6 +23,7 @@ MdArrowDownIcon }, mixins: [MdListItemMixin], + inject: ['MdList'], data: () => ({ expandStyles: {}, showContent: false @@ -98,12 +99,22 @@ let expanded = this.showContent this.$emit('update:mdExpanded', expanded) this.$nextTick(() => this.$emit(expanded ? 'md-expanded' : 'md-collapsed')) + + if (expanded) { + this.MdList.expandATab(this) + } } }, + created () { + this.MdList.pushExpandable(this) + }, mounted () { if (this.mdExpanded) { this.open() } + }, + beforeDestroy () { + this.MdList.removeExpandable(this) } }