parent
dd6f87ed04
commit
163e803939
@ -1,72 +1,83 @@ |
|||||||
<script type="text/jsx"> |
<script type="text/jsx"> |
||||||
import SidebarItemContent from './SidebarItemContent' |
import SidebarItemContent from './SidebarItemContent' |
||||||
|
|
||||||
function getOnlyChild(item) { |
//获取不需要嵌套展示的菜单 |
||||||
const {children = [], meta: {alwaysShow} = {}} = item |
function getOnlyChild(menu) { |
||||||
|
const {children = [], meta: {alwaysShow} = {}} = menu |
||||||
|
|
||||||
if (children.length === 1) return alwaysShow ? null : children[0] |
if (!children.length) return {...menu, children: undefined} |
||||||
|
|
||||||
if (!children.length) return {...item, path: undefined, children: undefined} |
if (children.length === 1) return alwaysShow ? null : getOnlyChild(children[0]) |
||||||
|
|
||||||
return null |
return null |
||||||
} |
} |
||||||
|
|
||||||
function renderNode(h, {item, showParent, collapse}) { |
function renderSingleMenu(h, {index, icon, title}) { |
||||||
let onlyOneChild = getOnlyChild(item) |
return ( |
||||||
|
<el-menu-item index={index}> |
||||||
|
<SidebarItemContent icon={icon} title={title}/> |
||||||
|
</el-menu-item> |
||||||
|
) |
||||||
|
} |
||||||
|
|
||||||
|
function renderSubMenu(h, {index, icon, title, children}) { |
||||||
|
return ( |
||||||
|
<el-submenu index={index} popper-append-to-body> |
||||||
|
<SidebarItemContent slot="title" icon={icon} title={title}/> |
||||||
|
{children} |
||||||
|
</el-submenu> |
||||||
|
) |
||||||
|
} |
||||||
|
|
||||||
|
function renderChildrenWithParentMenu(h, {icon, title, children}) { |
||||||
|
return [ |
||||||
|
<div class="popover-menu__title el-menu-item"> |
||||||
|
<SidebarItemContent icon={icon} title={title}/> |
||||||
|
</div>, |
||||||
|
<div class="el-menu el-menu--inline">{children}</div> |
||||||
|
] |
||||||
|
} |
||||||
|
|
||||||
|
function renderMenu(h, {menu, showParent, collapse}) { |
||||||
|
const onlyOneChild = getOnlyChild(menu) |
||||||
|
|
||||||
const showSingle = onlyOneChild && !onlyOneChild.children |
const showSingle = onlyOneChild && !onlyOneChild.children |
||||||
|
|
||||||
if (showSingle) { |
if (showSingle) { |
||||||
const {icon, title} = onlyOneChild.meta |
const {icon, title} = onlyOneChild.meta |
||||||
|
return renderSingleMenu(h, {index: onlyOneChild.fullPath, icon, title}) |
||||||
return ( |
|
||||||
<el-menu-item index={onlyOneChild.fullPath}> |
|
||||||
<SidebarItemContent icon={icon} title={title}/> |
|
||||||
</el-menu-item> |
|
||||||
) |
|
||||||
} |
} |
||||||
else { |
|
||||||
const {icon, title} = item.meta |
|
||||||
|
|
||||||
let children = item.children.map(child => renderNode(h, { |
const {icon, title} = menu.meta |
||||||
item: child, |
|
||||||
|
let children = menu.children.map(child => renderMenu(h, { |
||||||
|
menu: child, |
||||||
showParent, |
showParent, |
||||||
collapse |
collapse |
||||||
})) |
})) |
||||||
|
|
||||||
//弹出菜单如果包裹滚动条,则在触发mouseleave时,不会触发父菜单的mouseleave事件 |
//这里弹出菜单如果包裹了<el-scrollbar>,则在触发mouseleave时,不会触发父菜单的mouseleave事件 |
||||||
if (collapse) { |
if (collapse) { |
||||||
//弹出菜单显示父级信息 |
//弹出菜单显示父级信息 |
||||||
if (showParent) { |
if (showParent) { |
||||||
children = [ |
children = renderChildrenWithParentMenu(h, {icon, title, children}) |
||||||
<div class="popover-menu__title el-menu-item"> |
|
||||||
<SidebarItemContent icon={icon} title={title}/> |
|
||||||
</div>, |
|
||||||
<div class="el-menu el-menu--inline">{children}</div> |
|
||||||
] |
|
||||||
} |
} |
||||||
} |
} |
||||||
|
|
||||||
return ( |
return renderSubMenu(h, {index: menu.fullPath, icon, title, children}) |
||||||
<el-submenu index={item.fullPath} popper-append-to-body> |
|
||||||
<SidebarItemContent slot="title" icon={icon} title={title}/> |
|
||||||
{children} |
|
||||||
</el-submenu> |
|
||||||
) |
|
||||||
} |
|
||||||
} |
} |
||||||
|
|
||||||
export default { |
export default { |
||||||
functional: true, |
functional: true, |
||||||
|
|
||||||
props: { |
props: { |
||||||
item: Object, |
menu: Object, |
||||||
showParent: Boolean, |
showParent: Boolean, |
||||||
collapse: Boolean |
collapse: Boolean |
||||||
}, |
}, |
||||||
|
|
||||||
render(h, context) { |
render(h, context) { |
||||||
return renderNode(h, context.props) |
return renderMenu(h, context.props) |
||||||
} |
} |
||||||
} |
} |
||||||
</script> |
</script> |
||||||
|
|||||||
Loading…
Reference in new issue