From c6590f9995e1a9e35e2f04fac0dc9998aec89cce Mon Sep 17 00:00:00 2001 From: dream2023 <1098626505@qq.com> Date: Wed, 6 Dec 2023 15:10:55 +0800 Subject: [PATCH] fix: menu title duplicate --- package.json | 2 +- src/hooks/useMenu.tsx | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index a648e33..274558d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dumi-theme-nocobase", - "version": "0.2.18", + "version": "0.2.19", "description": "", "keywords": [ "dumi", diff --git a/src/hooks/useMenu.tsx b/src/hooks/useMenu.tsx index 6cda641..d8c35dc 100644 --- a/src/hooks/useMenu.tsx +++ b/src/hooks/useMenu.tsx @@ -52,7 +52,7 @@ const useMenu = (options: UseMenuOptions = {}): [MenuProps['items'], string] => const isSubMenu = (v: any): v is SidebarEnhanceSubType => { return v && typeof v === 'object' && 'children' in v; }; - function processMenu(menu: SidebarEnhanceType): ItemType { + function processMenu(menu: SidebarEnhanceType, parentKey?: string): ItemType { if (typeof menu === 'string') { // menu: '/introduction' return { @@ -71,13 +71,13 @@ const useMenu = (options: UseMenuOptions = {}): [MenuProps['items'], string] => return { type: 'group', label: menu.title, - key: menu.title, - children: menu.children.map(processMenu) + key: parentKey ? `${parentKey}-${menu.title}` : menu.title, + children: menu.children.map((item) => processMenu(item, menu.title)) }; } if (isSubMenu(menu)) { return { - key: menu.title, + key: parentKey ? `${parentKey}-${menu.title}` : menu.title, label: menu.subTitle ? ( {menu.title} @@ -93,7 +93,7 @@ const useMenu = (options: UseMenuOptions = {}): [MenuProps['items'], string] => ) : ( {menu.title} ), - children: menu.children.map(processMenu) + children: menu.children.map((item) => processMenu(item, menu.title)) }; } if (isItemMenu(menu)) { @@ -107,7 +107,7 @@ const useMenu = (options: UseMenuOptions = {}): [MenuProps['items'], string] => } if (!currentSidebarEnhanceData) return undefined; - return currentSidebarEnhanceData.map(processMenu); + return currentSidebarEnhanceData.map((item) => processMenu(item)); }, [after, before, currentSidebarEnhanceData, linkTitleMap, search]); const menuItems = useMemo(() => {