From 0902fb69d61742e48557765ba680a16115eaaf5a Mon Sep 17 00:00:00 2001 From: none Date: Tue, 21 Mar 2023 10:40:45 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=B7=A6=E4=BE=A7=E8=8F=9C?= =?UTF-8?q?=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/compenents/left-menu/index.tsx | 35 +++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/src/compenents/left-menu/index.tsx b/src/compenents/left-menu/index.tsx index 013adfb..14dcb76 100644 --- a/src/compenents/left-menu/index.tsx +++ b/src/compenents/left-menu/index.tsx @@ -89,14 +89,30 @@ export const LeftMenu: React.FC = () => { } return []; }; + const openKeyMerge = (pathname: string): string[] => { + let newOpenKeys = hit(pathname); + for (let i = 0; i < openKeys.length; i++) { + let isIn = false; + for (let j = 0; j < newOpenKeys.length; j++) { + if (newOpenKeys[j] === openKeys[i]) { + isIn = true; + break; + } + } + if (isIn) { + continue; + } + newOpenKeys.push(openKeys[i]); + } + return newOpenKeys; + }; - // 默认选中的菜单 - let defaultSelectedKeys: string[] = [location.pathname]; - // 默认打开的子菜单 - let defaultOpenKeys: string[] = hit(location.pathname); // 选中的菜单 - const [selectedKeys, setSelectedKeys] = - useState(defaultSelectedKeys); + const [selectedKeys, setSelectedKeys] = useState([ + location.pathname, + ]); + // 展开菜单 + const [openKeys, setOpenKeys] = useState(hit(location.pathname)); const onClick = (e: any) => { navigate(e.key); @@ -104,6 +120,7 @@ export const LeftMenu: React.FC = () => { useEffect(() => { setSelectedKeys([location.pathname]); + setOpenKeys(openKeyMerge(location.pathname)); }, [location.pathname]); return ( @@ -129,14 +146,16 @@ export const LeftMenu: React.FC = () => { width: 200, background: "#ffffff", }} - defaultSelectedKeys={defaultSelectedKeys} - defaultOpenKeys={defaultOpenKeys} selectedKeys={selectedKeys} + openKeys={openKeys} mode="inline" items={items} onSelect={(data: any) => { setSelectedKeys(data.selectedKeys); }} + onOpenChange={(keys: any) => { + setOpenKeys(keys); + }} /> );