Commit a163daa07a51b694db6fe52bc9f5e3c50b8f866f

Authored by zhigang.li
1 parent 3e3fba0e

Add logical treatment to 'accordion'

examples/routers/menu.vue
1 <template> 1 <template>
2 <div> 2 <div>
3 - <Menu :theme="theme1" active-name="1" @on-select="handleSelect" @on-open-change="handleOpen" :open-names="openArr"> 3 + <Menu :theme="theme1" active-name="1" accordion @on-select="handleSelect" @on-open-change="handleOpen" :open-names="openArr">
4 <Menu-item name="1"> 4 <Menu-item name="1">
5 <Icon type="ios-paper"></Icon> 5 <Icon type="ios-paper"></Icon>
6 一级1 6 一级1
@@ -41,6 +41,22 @@ @@ -41,6 +41,22 @@
41 </MenuItem> 41 </MenuItem>
42 </MenuGroup> 42 </MenuGroup>
43 </Submenu> 43 </Submenu>
  44 + <Submenu name="3-3-4">
  45 + <template slot="title">
  46 + <Icon type="stats-bars"></Icon>
  47 + 三级4
  48 + </template>
  49 + <Menu-item name="3-3-4-1">四级1</Menu-item>
  50 + <Menu-item name="3-3-4-2">四级2</Menu-item>
  51 + </Submenu>
  52 + </Submenu>
  53 + <Submenu name="3-4">
  54 + <template slot="title">
  55 + <Icon type="stats-bars"></Icon>
  56 + 二级4
  57 + </template>
  58 + <Menu-item name="3-4-1">三级1</Menu-item>
  59 + <Menu-item name="3-4-2">三级2</Menu-item>
44 </Submenu> 60 </Submenu>
45 </Submenu> 61 </Submenu>
46 <Menu-item name="4"> 62 <Menu-item name="4">
src/components/menu/menu.vue
@@ -2,7 +2,7 @@ @@ -2,7 +2,7 @@
2 <ul :class="classes" :style="styles"><slot></slot></ul> 2 <ul :class="classes" :style="styles"><slot></slot></ul>
3 </template> 3 </template>
4 <script> 4 <script>
5 - import { oneOf, findComponentsDownward } from '../../utils/assist'; 5 + import { oneOf, findComponentsDownward, findBrothersComponents } from '../../utils/assist';
6 import Emitter from '../../mixins/emitter'; 6 import Emitter from '../../mixins/emitter';
7 7
8 const prefixCls = 'ivu-menu'; 8 const prefixCls = 'ivu-menu';
@@ -82,7 +82,14 @@ @@ -82,7 +82,14 @@
82 } else { 82 } else {
83 this.openNames.push(name); 83 this.openNames.push(name);
84 if (this.accordion) { 84 if (this.accordion) {
85 - this.openNames.splice(0, this.openNames.length); 85 + let currentSubmenu = {};
  86 + findComponentsDownward(this, 'Submenu').forEach(item => {
  87 + if (item.name === name) currentSubmenu = item;
  88 + });
  89 + findBrothersComponents(currentSubmenu, 'Submenu').forEach(item => {
  90 + let index = this.openNames.indexOf(item.name);
  91 + this.openNames.splice(index, index >= 0 ? 1 : 0);
  92 + });
86 this.openNames.push(name); 93 this.openNames.push(name);
87 } 94 }
88 } 95 }
src/components/menu/submenu.vue
1 <template> 1 <template>
2 <li :class="classes" @mouseenter="handleMouseenter" @mouseleave="handleMouseleave"> 2 <li :class="classes" @mouseenter="handleMouseenter" @mouseleave="handleMouseleave">
3 - <div :class="[prefixCls + '-submenu-title']" ref="reference" @click="handleClick" :style="titleStyle"> 3 + <div :class="[prefixCls + '-submenu-title']" ref="reference" @click.stop="handleClick" :style="titleStyle">
4 <slot name="title"></slot> 4 <slot name="title"></slot>
5 <Icon type="ios-arrow-down" :class="[prefixCls + '-submenu-title-icon']"></Icon> 5 <Icon type="ios-arrow-down" :class="[prefixCls + '-submenu-title-icon']"></Icon>
6 </div> 6 </div>
@@ -112,7 +112,7 @@ @@ -112,7 +112,7 @@
112 if (this.mode === 'horizontal') return; 112 if (this.mode === 'horizontal') return;
113 const opened = this.opened; 113 const opened = this.opened;
114 if (this.accordion) { 114 if (this.accordion) {
115 - this.parent.$children.forEach(item => { 115 + this.$parent.$children.forEach(item => {
116 if (item.$options.name === 'Submenu') item.opened = false; 116 if (item.$options.name === 'Submenu') item.opened = false;
117 }); 117 });
118 } 118 }
src/utils/assist.js
@@ -228,6 +228,16 @@ export function findComponentsUpward (context, componentName) { @@ -228,6 +228,16 @@ export function findComponentsUpward (context, componentName) {
228 } 228 }
229 } 229 }
230 230
  231 +// Find brothers components
  232 +export function findBrothersComponents (context, componentName) {
  233 + let res = context.$parent.$children.filter(item => {
  234 + return item.$options.name === componentName;
  235 + });
  236 + let index = res.indexOf(context);
  237 + res.splice(index, 1);
  238 + return res;
  239 +}
  240 +
231 /* istanbul ignore next */ 241 /* istanbul ignore next */
232 const trim = function(string) { 242 const trim = function(string) {
233 return (string || '').replace(/^[\s\uFEFF]+|[\s\uFEFF]+$/g, ''); 243 return (string || '').replace(/^[\s\uFEFF]+|[\s\uFEFF]+$/g, '');