11import { Icon , Menu } from 'antd' ;
2- import React , { PureComponent } from 'react' ;
2+ import React , { FC , PureComponent } from 'react' ;
33import { getMenuMatches , urlToList } from '../../utils/utils' ;
44
55import { 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+
1762interface 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
3076class 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 ) ;
0 commit comments