Skip to content

Commit

Permalink
Merge pull request #1121 from easyops-cn/sailorshe/master/navMenuFilter
Browse files Browse the repository at this point in the history
fix(): nav-menu filter menuItems
  • Loading branch information
weareoutman committed Jun 21, 2024
2 parents 6018d0d + 4404cfe commit ed7e3a0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
5 changes: 5 additions & 0 deletions bricks/nav/src/nav-menu/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ describe("eo-nav-menu", () => {
},
],
},
{
title: "filterItem",
type: "subMenu",
items: [],
},
],
};
document.body.appendChild(element);
Expand Down
27 changes: 20 additions & 7 deletions bricks/nav/src/nav-menu/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -372,15 +372,28 @@ function NavMenuComponent(props: NavMenuProps) {
);
const { pathname, search } = location;

const menuItems = useMemo(() => {
return (menu?.menuItems ?? []).filter(
(item) =>
// 默认
item.type === "default" ||
// 没有类型
!item.type ||
// 分组类型并且没有子项,过滤
(["group", "subMenu"].includes(item.type) &&
(item as SidebarMenuGroup).items?.length)
);
}, [menu?.menuItems]);

const selectedKey = useMemo(() => {
const { selectedKeys } = initMenuItemAndMatchCurrentPathKeys(
menu?.menuItems ?? [],
menuItems ?? [],
pathname,
search,
""
);
return selectedKeys;
}, [menu?.menuItems, pathname, search]);
}, [menuItems, pathname, search]);

useEffect(() => {
const unListen: UnregisterCallback = history.listen((location) => {
Expand Down Expand Up @@ -434,17 +447,17 @@ function NavMenuComponent(props: NavMenuProps) {
(): SidebarMenuItem => ({
type: "subMenu",
title: "···",
items: menu?.menuItems.slice(
items: menuItems.slice(
overflowIndex,
menu.menuItems.length
menuItems.length
) as SidebarMenuItem[],
}),
[menu?.menuItems, overflowIndex]
[menuItems, overflowIndex]
);

return (
<div ref={navMenuWrapperRef} className="nav-menu-wrapper">
{menu?.menuItems.map((item, index) => {
{menuItems.map((item, index) => {
const isHidden = overflowIndex <= index;
return (
<React.Fragment key={item.key}>
Expand Down Expand Up @@ -482,7 +495,7 @@ function NavMenuComponent(props: NavMenuProps) {
);
})}
<RenderMenuItemCom
hidden={overflowIndex > (menu?.menuItems?.length ?? 0)}
hidden={overflowIndex > menuItems.length}
item={overflowMenu}
showTooltip={showTooltip}
selectedKey={selectedKey}
Expand Down

0 comments on commit ed7e3a0

Please sign in to comment.