|
@@ -18,42 +18,51 @@ export const LayoutMenu: FC<LayoutMenuProps> = memo(
|
|
({ menuData, maxShowLevel = 2, ...rest }) => {
|
|
({ menuData, maxShowLevel = 2, ...rest }) => {
|
|
const location = useLocation();
|
|
const location = useLocation();
|
|
const navigate = useNavigate();
|
|
const navigate = useNavigate();
|
|
|
|
+
|
|
const defaultOpenKeys = useMemo(() => {
|
|
const defaultOpenKeys = useMemo(() => {
|
|
let currentPath = "";
|
|
let currentPath = "";
|
|
const stack: string[] = [];
|
|
const stack: string[] = [];
|
|
const paths = location.pathname.split("/").filter((path) => path !== "");
|
|
const paths = location.pathname.split("/").filter((path) => path !== "");
|
|
- for (let i = 0; i < paths.length - 1; i++) {
|
|
|
|
|
|
+ for (let i = 0; i < paths.length; i++) {
|
|
currentPath += `/${paths[i]}`;
|
|
currentPath += `/${paths[i]}`;
|
|
const item = findRouteByPath(menuData, currentPath);
|
|
const item = findRouteByPath(menuData, currentPath);
|
|
if (item) {
|
|
if (item) {
|
|
stack.push(item.path);
|
|
stack.push(item.path);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
return stack;
|
|
return stack;
|
|
}, [menuData, location]);
|
|
}, [menuData, location]);
|
|
|
|
|
|
const renderMenuItems = useCallback(
|
|
const renderMenuItems = useCallback(
|
|
(list: DageRouteItem[], level = 1): any[] => {
|
|
(list: DageRouteItem[], level = 1): any[] => {
|
|
- return list.map((item) => {
|
|
|
|
|
|
+ const stack: DageRouteItem[] = [];
|
|
|
|
+
|
|
|
|
+ list.forEach((item) => {
|
|
|
|
+ let child: DageRouteItem[] = [];
|
|
const { title, path, icon, children, hide } = item;
|
|
const { title, path, icon, children, hide } = item;
|
|
|
|
+ const params = {
|
|
|
|
+ key: path,
|
|
|
|
+ icon: icon,
|
|
|
|
+ label: title,
|
|
|
|
+ };
|
|
|
|
|
|
if (hide) return null;
|
|
if (hide) return null;
|
|
|
|
|
|
if (level <= maxShowLevel - 1 && children) {
|
|
if (level <= maxShowLevel - 1 && children) {
|
|
- return {
|
|
|
|
- key: path,
|
|
|
|
- icon: icon,
|
|
|
|
- label: title,
|
|
|
|
- children: renderMenuItems(children, level + 1),
|
|
|
|
- };
|
|
|
|
|
|
+ child = renderMenuItems(children, level + 1);
|
|
}
|
|
}
|
|
|
|
|
|
- return {
|
|
|
|
- key: path,
|
|
|
|
- icon: icon,
|
|
|
|
- label: title,
|
|
|
|
- };
|
|
|
|
|
|
+ if (child.length) {
|
|
|
|
+ // @ts-ignore
|
|
|
|
+ params.children = child;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // @ts-ignore
|
|
|
|
+ stack.push(params);
|
|
});
|
|
});
|
|
|
|
+
|
|
|
|
+ return stack;
|
|
},
|
|
},
|
|
[maxShowLevel]
|
|
[maxShowLevel]
|
|
);
|
|
);
|
|
@@ -65,15 +74,15 @@ export const LayoutMenu: FC<LayoutMenuProps> = memo(
|
|
[navigate]
|
|
[navigate]
|
|
);
|
|
);
|
|
|
|
|
|
- return (
|
|
|
|
|
|
+ return menuData.length ? (
|
|
<Menu
|
|
<Menu
|
|
mode="inline"
|
|
mode="inline"
|
|
items={renderMenuItems(menuData)}
|
|
items={renderMenuItems(menuData)}
|
|
- selectedKeys={[location.pathname]}
|
|
|
|
|
|
+ selectedKeys={location.pathname.split("/").map((i) => `/${i}`)}
|
|
defaultOpenKeys={defaultOpenKeys}
|
|
defaultOpenKeys={defaultOpenKeys}
|
|
onClick={handleMenu}
|
|
onClick={handleMenu}
|
|
{...rest}
|
|
{...rest}
|
|
/>
|
|
/>
|
|
- );
|
|
|
|
|
|
+ ) : null;
|
|
}
|
|
}
|
|
);
|
|
);
|