From f81c795261cbb07d14942dec8a0734e25ccb4ee4 Mon Sep 17 00:00:00 2001 From: drizzlesconsin <69514654+drizzlesconsin@users.noreply.github.com> Date: Wed, 2 Apr 2025 13:49:03 +0800 Subject: [PATCH] =?UTF-8?q?feat(routes):=20=E4=BC=98=E5=8C=96=E7=88=B6?= =?UTF-8?q?=E7=BA=A7=E8=B7=AF=E7=94=B1=E7=9A=84=E6=98=BE=E7=A4=BA=E9=9A=90?= =?UTF-8?q?=E8=97=8F=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 当所有可见子节点都被隐藏时(hideInMenu: true),自动隐藏父节点 - 避免出现空的父级菜单项 - 添加相关测试用例验证新逻辑 --- .../plugin-access/src/utils/getRuntimeUtil.ts | 12 +++++++++ .../tests/utils/runtimeUtil.test.ts | 26 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/packages/plugin-access/src/utils/getRuntimeUtil.ts b/packages/plugin-access/src/utils/getRuntimeUtil.ts index 9b7a49ab..50926434 100644 --- a/packages/plugin-access/src/utils/getRuntimeUtil.ts +++ b/packages/plugin-access/src/utils/getRuntimeUtil.ts @@ -69,6 +69,18 @@ export default (strictMode: boolean = false) => if (!currentRoute.unaccessible && isAllChildChildrenUnaccessible) { currentRoute.unaccessible = true; } + + // 父节点显示/隐藏逻辑: + // 1. 遍历所有子节点,找出未设置 hideInMenu 的节点(即可见节点) + // 2. 如果没有任何可见子节点,则隐藏父节点 + const hasVisibleChildren = + Array.isArray(finallyChildList) && + finallyChildList.some((route) => !route.hideInMenu); + + if (!hasVisibleChildren) { + currentRoute.hideInMenu = true; + } + if (finallyChildList && finallyChildList?.length > 0) { return { ...currentRoute, diff --git a/packages/plugin-access/tests/utils/runtimeUtil.test.ts b/packages/plugin-access/tests/utils/runtimeUtil.test.ts index b7aa1349..c72f80a0 100644 --- a/packages/plugin-access/tests/utils/runtimeUtil.test.ts +++ b/packages/plugin-access/tests/utils/runtimeUtil.test.ts @@ -131,4 +131,30 @@ describe('TraverseModifyRoutes', () => { traverseModifyRoutes(routes, access); }).toThrowError(); }); + + it('should hide parent route when all visible children are hidden', () => { + const testRoutes = [ + { + path: '/parent', + routes: [ + { path: '/child1', hideInMenu: true }, + { path: '/child2', hideInMenu: true }, + { path: '/child3', hideInMenu: true } + ] + }, + { + path: '/parent2', + routes: [ + { path: '/child1', hideInMenu: true }, + { path: '/child2' } + ] + } + ]; + + const result = traverseModifyRoutes(testRoutes, access); + + expect(result[0].hideInMenu).toBe(true); + + expect(result[1].hideInMenu).toBeUndefined(); + }); });