Skip to content
This repository was archived by the owner on Jan 26, 2022. It is now read-only.

Commit 6f85092

Browse files
committed
Optimize BaseMenu component
1 parent ed12505 commit 6f85092

File tree

1 file changed

+43
-50
lines changed

1 file changed

+43
-50
lines changed

src/components/SideBar/BaseMenu.tsx

Lines changed: 43 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Icon, Menu } from 'antd';
2-
import React, { FC, PureComponent } from 'react';
2+
import React, { PureComponent } from 'react';
33
import { getMenuMatches, urlToList } from '../../utils/utils';
44

55
import { Link } from 'dva/router';
@@ -14,55 +14,9 @@ export interface MenuItemProps {
1414
hideInMenu?: boolean;
1515
}
1616

17-
const MenuItem: FC<MenuItemProps> = (props) => {
18-
const { path, icon, name, hideInMenu, ...restProps } = props;
19-
20-
if (hideInMenu) {
21-
return null;
22-
}
23-
24-
return (
25-
<Item key={path} {...restProps}>
26-
<Link to={path}>
27-
{icon && <Icon type={icon} />}
28-
<span>{name}</span>
29-
</Link>
30-
</Item>
31-
);
32-
};
33-
34-
const SubMenuGroup: FC<MenuItemProps> = (props) => {
35-
const { name, icon, path, hideInMenu, children, ...restProps } = props;
36-
37-
if (hideInMenu) {
38-
return null;
39-
}
40-
41-
return (
42-
<SubMenu
43-
key={path}
44-
{...restProps}
45-
title={
46-
icon ? (
47-
<span>
48-
<Icon type={icon} />
49-
<span>{name}</span>
50-
</span>
51-
) : name
52-
}
53-
>
54-
{children && children.length ? children.map((item: MenuItemProps) => {
55-
return item.children ? <SubMenuGroup key={item.path} {...item} /> :
56-
<MenuItem key={item.path} {...item} />;
57-
}) : null}
58-
</SubMenu>
59-
);
60-
};
61-
6217
interface InternalProps {
6318
location: Location;
6419
menu: MenuItemProps[];
65-
style?: object;
6620
collapsed: boolean;
6721
className?: string;
6822
flatMenuKeys: string[];
@@ -84,9 +38,48 @@ class BaseMenu extends PureComponent<InternalProps> {
8438
return urlToList(pathname).map((path: string) => getMenuMatches(flatMenuKeys, path).pop()) as string[];
8539
}
8640

41+
public renderMenuItem(item: MenuItemProps) {
42+
const { path, icon, name, hideInMenu } = item;
43+
44+
if (hideInMenu) {
45+
return;
46+
}
47+
48+
return (
49+
<Item key={path}>
50+
<Link to={path}>
51+
{icon && <Icon type={icon} />}
52+
<span>{name}</span>
53+
</Link>
54+
</Item>
55+
);
56+
}
57+
58+
public renderSubMenu(props: MenuItemProps) {
59+
const { name, icon, path, children } = props;
60+
61+
return (
62+
<SubMenu
63+
key={path}
64+
title={
65+
icon ? (
66+
<span>
67+
<Icon type={icon} />
68+
<span>{name}</span>
69+
</span>
70+
) : name
71+
}
72+
>
73+
{children && children.length ? children.map((item: MenuItemProps) =>
74+
item.children ? this.renderSubMenu(item) : this.renderMenuItem(item))
75+
: null}
76+
</SubMenu>
77+
);
78+
}
79+
8780
public render() {
8881
const { className, collapsed, theme, openKeys, mode,
89-
style, menu, handleOpenChange, location } = this.props;
82+
menu, handleOpenChange, location } = this.props;
9083

9184
let selectedKeys = this.getSelectedMenuKeys(location.pathname);
9285
if (!selectedKeys.length && openKeys) {
@@ -103,16 +96,16 @@ class BaseMenu extends PureComponent<InternalProps> {
10396
return (
10497
<Menu
10598
key="Menu"
106-
style={style}
10799
className={className}
108100
theme={theme || 'dark'}
109101
mode={mode || 'inline'}
110102
selectedKeys={selectedKeys}
103+
forceSubMenuRender={true}
111104
onOpenChange={handleOpenChange}
112105
{...props}
113106
>
114107
{menu && menu.length && menu.map((item: MenuItemProps, index: number) => (
115-
item.children ? <SubMenuGroup key={index} {...item} /> : <MenuItem key={index} {...item} />
108+
item.children ? this.renderSubMenu(item) : this.renderMenuItem(item)
116109
))}
117110
</Menu>
118111
);

0 commit comments

Comments
 (0)