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

Commit ed12505

Browse files
committed
update BaseMenu component
1 parent 94284e8 commit ed12505

File tree

3 files changed

+76
-46
lines changed

3 files changed

+76
-46
lines changed

src/components/SideBar/BaseMenu.tsx

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

55
import { Link } from 'dva/router';
@@ -14,6 +14,51 @@ 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+
1762
interface InternalProps {
1863
location: Location;
1964
menu: MenuItemProps[];
@@ -25,57 +70,23 @@ interface InternalProps {
2570
mode?: 'inline' | 'vertical' | 'horizontal';
2671
openKeys: string[];
2772
onOpenChange: (openKeys: string[]) => void;
73+
handleOpenChange: (openKeys: string[]) => void;
2874
}
2975

3076
class BaseMenu extends PureComponent<InternalProps> {
3177

78+
private constructor(props: InternalProps) {
79+
super(props);
80+
}
81+
3282
public getSelectedMenuKeys(pathname: string) {
3383
const { flatMenuKeys } = this.props;
3484
return urlToList(pathname).map((path: string) => getMenuMatches(flatMenuKeys, path).pop()) as string[];
3585
}
3686

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

8091
let selectedKeys = this.getSelectedMenuKeys(location.pathname);
8192
if (!selectedKeys.length && openKeys) {
@@ -96,13 +107,12 @@ class BaseMenu extends PureComponent<InternalProps> {
96107
className={className}
97108
theme={theme || 'dark'}
98109
mode={mode || 'inline'}
99-
inlineCollapsed={true}
100110
selectedKeys={selectedKeys}
101-
onOpenChange={onOpenChange}
111+
onOpenChange={handleOpenChange}
102112
{...props}
103113
>
104-
{menu && menu.length && menu.map((item: MenuItemProps) => (
105-
item.children ? this.renderSubMenu(item) : this.renderMenuItem(item)
114+
{menu && menu.length && menu.map((item: MenuItemProps, index: number) => (
115+
item.children ? <SubMenuGroup key={index} {...item} /> : <MenuItem key={index} {...item} />
106116
))}
107117
</Menu>
108118
);

src/components/SideBar/SideBar.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ class SideBar extends PureComponent<InternalProps, InternalState> {
9595
flatMenuKeys={flatMenuKeys}
9696
menu={menu} location={location}
9797
onOpenChange={this.handleOpenChange}
98+
handleOpenChange={this.handleOpenChange}
9899
/>
99100
</Sider>
100101
);

src/routes/config.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,26 @@ export default {
3333
name: '设置中心',
3434
icon: 'setting',
3535
path: '/app/setting',
36-
component: 'SettingCenter',
36+
children: [
37+
{
38+
name: '消息设置',
39+
icon: 'setting',
40+
path: '/app/setting/message',
41+
component: 'MessageSetting',
42+
},
43+
{
44+
name: '系统设置',
45+
icon: 'setting',
46+
path: '/app/setting/system',
47+
component: 'MessageSetting',
48+
},
49+
{
50+
name: '用户设置',
51+
icon: 'setting',
52+
path: '/app/setting/user',
53+
component: 'SettingCenter',
54+
},
55+
],
3756
},
3857
{
3958
name: '用户中心',

0 commit comments

Comments
 (0)