Merge pull request #2822 from lison16/menu
make menu support more than 2 levels
This commit is contained in:
commit
b4c3794697
8 changed files with 168 additions and 41 deletions
|
@ -1,10 +1,11 @@
|
|||
<template>
|
||||
<li :class="[prefixCls + '-item-group']">
|
||||
<div :class="[prefixCls + '-item-group-title']">{{ title }}</div>
|
||||
<div :class="[prefixCls + '-item-group-title']" :style="groupStyle">{{ title }}</div>
|
||||
<ul><slot></slot></ul>
|
||||
</li>
|
||||
</template>
|
||||
<script>
|
||||
import { findComponentUpward, findComponentsUpward } from '../../utils/assist';
|
||||
const prefixCls = 'ivu-menu';
|
||||
|
||||
export default {
|
||||
|
@ -19,6 +20,19 @@
|
|||
return {
|
||||
prefixCls: prefixCls
|
||||
};
|
||||
},
|
||||
computed: {
|
||||
parentSubmenuNum () {
|
||||
return findComponentsUpward(this, 'Submenu').length;
|
||||
},
|
||||
hasParentSubmenu () {
|
||||
return findComponentUpward(this, 'Submenu');
|
||||
},
|
||||
groupStyle () {
|
||||
return this.hasParentSubmenu ? {
|
||||
paddingLeft: 43 + (this.parentSubmenuNum - 1) * 28 + 'px'
|
||||
} : {};
|
||||
}
|
||||
}
|
||||
};
|
||||
</script>
|
||||
|
|
|
@ -1,13 +1,15 @@
|
|||
<template>
|
||||
<li :class="classes" @click.stop="handleClick"><slot></slot></li>
|
||||
<li :class="classes" @click.stop="handleClick" :style="itemStyle"><slot></slot></li>
|
||||
</template>
|
||||
<script>
|
||||
import Emitter from '../../mixins/emitter';
|
||||
import { findComponentUpward } from '../../utils/assist';
|
||||
const prefixCls = 'ivu-menu';
|
||||
import mixin from './mixin';
|
||||
|
||||
export default {
|
||||
name: 'MenuItem',
|
||||
mixins: [ Emitter ],
|
||||
mixins: [ Emitter, mixin ],
|
||||
props: {
|
||||
name: {
|
||||
type: [String, Number],
|
||||
|
@ -33,18 +35,18 @@
|
|||
[`${prefixCls}-item-disabled`]: this.disabled
|
||||
}
|
||||
];
|
||||
},
|
||||
itemStyle () {
|
||||
return this.hasParentSubmenu ? {
|
||||
paddingLeft: 43 + (this.parentSubmenuNum - 1) * 24 + 'px'
|
||||
} : {};
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
handleClick () {
|
||||
if (this.disabled) return;
|
||||
|
||||
let parent = this.$parent;
|
||||
let name = parent.$options.name;
|
||||
while (parent && (!name || name !== 'Submenu')) {
|
||||
parent = parent.$parent;
|
||||
if (parent) name = parent.$options.name;
|
||||
}
|
||||
let parent = findComponentUpward(this, 'Submenu');
|
||||
|
||||
if (parent) {
|
||||
this.dispatch('Submenu', 'on-menu-item-select', this.name);
|
||||
|
@ -57,7 +59,7 @@
|
|||
this.$on('on-update-active-name', (name) => {
|
||||
if (this.name === name) {
|
||||
this.active = true;
|
||||
this.dispatch('Submenu', 'on-update-active-name', true);
|
||||
this.dispatch('Submenu', 'on-update-active-name', name);
|
||||
} else {
|
||||
this.active = false;
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
<ul :class="classes" :style="styles"><slot></slot></ul>
|
||||
</template>
|
||||
<script>
|
||||
import { oneOf, findComponentsDownward } from '../../utils/assist';
|
||||
import { oneOf, findComponentsDownward, findBrothersComponents } from '../../utils/assist';
|
||||
import Emitter from '../../mixins/emitter';
|
||||
|
||||
const prefixCls = 'ivu-menu';
|
||||
|
@ -82,7 +82,14 @@
|
|||
} else {
|
||||
this.openNames.push(name);
|
||||
if (this.accordion) {
|
||||
this.openNames.splice(0, this.openNames.length);
|
||||
let currentSubmenu = {};
|
||||
findComponentsDownward(this, 'Submenu').forEach(item => {
|
||||
if (item.name === name) currentSubmenu = item;
|
||||
});
|
||||
findBrothersComponents(currentSubmenu, 'Submenu').forEach(item => {
|
||||
let index = this.openNames.indexOf(item.name);
|
||||
this.openNames.splice(index, index >= 0 ? 1 : 0);
|
||||
});
|
||||
this.openNames.push(name);
|
||||
}
|
||||
}
|
||||
|
|
11
src/components/menu/mixin.js
Normal file
11
src/components/menu/mixin.js
Normal file
|
@ -0,0 +1,11 @@
|
|||
import { findComponentUpward, findComponentsUpward } from '../../utils/assist';
|
||||
export default {
|
||||
computed: {
|
||||
hasParentSubmenu () {
|
||||
return findComponentUpward(this, 'Submenu');
|
||||
},
|
||||
parentSubmenuNum () {
|
||||
return findComponentsUpward(this, 'Submenu').length;
|
||||
}
|
||||
}
|
||||
};
|
|
@ -1,6 +1,6 @@
|
|||
<template>
|
||||
<li :class="classes" @mouseenter="handleMouseenter" @mouseleave="handleMouseleave">
|
||||
<div :class="[prefixCls + '-submenu-title']" ref="reference" @click="handleClick">
|
||||
<div :class="[prefixCls + '-submenu-title']" ref="reference" @click.stop="handleClick" :style="titleStyle">
|
||||
<slot name="title"></slot>
|
||||
<Icon type="ios-arrow-down" :class="[prefixCls + '-submenu-title-icon']"></Icon>
|
||||
</div>
|
||||
|
@ -21,14 +21,15 @@
|
|||
import Drop from '../select/dropdown.vue';
|
||||
import Icon from '../icon/icon.vue';
|
||||
import CollapseTransition from '../base/collapse-transition';
|
||||
import { getStyle, findComponentUpward } from '../../utils/assist';
|
||||
import { getStyle, findComponentUpward, findComponentsDownward } from '../../utils/assist';
|
||||
import Emitter from '../../mixins/emitter';
|
||||
import mixin from './mixin';
|
||||
|
||||
const prefixCls = 'ivu-menu';
|
||||
|
||||
export default {
|
||||
name: 'Submenu',
|
||||
mixins: [ Emitter ],
|
||||
mixins: [ Emitter, mixin ],
|
||||
components: { Icon, Drop, CollapseTransition },
|
||||
props: {
|
||||
name: {
|
||||
|
@ -54,9 +55,11 @@
|
|||
return [
|
||||
`${prefixCls}-submenu`,
|
||||
{
|
||||
[`${prefixCls}-item-active`]: this.active,
|
||||
[`${prefixCls}-item-active`]: this.active && !this.hasParentSubmenu,
|
||||
[`${prefixCls}-opened`]: this.opened,
|
||||
[`${prefixCls}-submenu-disabled`]: this.disabled
|
||||
[`${prefixCls}-submenu-disabled`]: this.disabled,
|
||||
[`${prefixCls}-submenu-has-parent-submenu`]: this.hasParentSubmenu,
|
||||
[`${prefixCls}-child-item-active`]: this.active
|
||||
}
|
||||
];
|
||||
},
|
||||
|
@ -71,6 +74,11 @@
|
|||
|
||||
if (this.dropWidth) style.minWidth = `${this.dropWidth}px`;
|
||||
return style;
|
||||
},
|
||||
titleStyle () {
|
||||
return this.hasParentSubmenu ? {
|
||||
paddingLeft: 43 + (this.parentSubmenuNum - 1) * 24 + 'px'
|
||||
} : {};
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
|
@ -99,7 +107,7 @@
|
|||
if (this.mode === 'horizontal') return;
|
||||
const opened = this.opened;
|
||||
if (this.accordion) {
|
||||
this.parent.$children.forEach(item => {
|
||||
this.$parent.$children.forEach(item => {
|
||||
if (item.$options.name === 'Submenu') item.opened = false;
|
||||
});
|
||||
}
|
||||
|
@ -131,6 +139,10 @@
|
|||
return true;
|
||||
});
|
||||
this.$on('on-update-active-name', (status) => {
|
||||
if (findComponentUpward(this, 'Submenu')) this.dispatch('Submenu', 'on-update-active-name', status);
|
||||
if (findComponentsDownward(this, 'Submenu')) findComponentsDownward(this, 'Submenu').forEach(item => {
|
||||
item.active = false;
|
||||
});
|
||||
this.active = status;
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue