|
1 | | -import { Component } from 'react'; |
| 1 | +import React, { useState, FunctionComponent } from 'react'; |
2 | 2 | import { Layout, Menu, Breadcrumb } from 'antd'; |
3 | 3 | import Link from 'next/link'; |
4 | | -import { withRouter } from 'next/router' |
| 4 | +import { withRouter, NextRouter } from 'next/router'; |
| 5 | +import { WithRouterProps } from 'next/dist/client/with-router'; |
5 | 6 |
|
6 | 7 | import { |
7 | 8 | DesktopOutlined, |
8 | 9 | DashboardOutlined, |
9 | 10 | SettingOutlined, |
10 | | -} from '@ant-design/icons' |
| 11 | +} from '@ant-design/icons'; |
11 | 12 |
|
12 | | -import './Layout.less'; |
| 13 | +import './Layout.css'; |
13 | 14 |
|
14 | 15 | const { SubMenu, Item } = Menu; |
15 | 16 | const { Sider, Content } = Layout; |
16 | 17 |
|
17 | | -interface Props { |
18 | | - router: any |
| 18 | +interface Router extends NextRouter { |
| 19 | + path: string; |
| 20 | + breadcrumbName: string; |
19 | 21 | } |
20 | 22 |
|
21 | | -function itemRender(route: any, params: any, routes: any, paths: string[]) { |
| 23 | +interface Props extends WithRouterProps { |
| 24 | + router: Router; |
| 25 | +} |
| 26 | + |
| 27 | +function itemRender(route: Router) { |
22 | 28 | return route.path === 'index' ? ( |
23 | | - <Link href={'/'}><a>{route.breadcrumbName}</a></Link> |
| 29 | + <Link href={'/'}> |
| 30 | + <a>{route.breadcrumbName}</a> |
| 31 | + </Link> |
24 | 32 | ) : ( |
25 | | - <span>{route.breadcrumbName}</span> |
26 | | - ); |
| 33 | + <span>{route.breadcrumbName}</span> |
| 34 | + ); |
27 | 35 | } |
28 | 36 |
|
29 | 37 | function routesMaker(pathsplit: string[]) { |
30 | 38 | let routes = [ |
31 | 39 | { |
32 | 40 | path: 'index', |
33 | 41 | breadcrumbName: 'home', |
34 | | - } |
| 42 | + }, |
35 | 43 | ]; |
36 | 44 | for (let v of pathsplit) { |
37 | 45 | const pathInfo = { |
38 | 46 | path: v, |
39 | 47 | breadcrumbName: v, |
40 | | - } |
41 | | - if (v !== "") routes.push(pathInfo) |
| 48 | + }; |
| 49 | + if (v !== '') routes.push(pathInfo); |
42 | 50 | } |
43 | | - return routes |
| 51 | + return routes; |
44 | 52 | } |
45 | 53 |
|
46 | | -class AppLayout extends Component<Props> { |
47 | | - state = { |
48 | | - collapsed: false, |
49 | | - }; |
| 54 | +const AppLayout = (props: React.PropsWithChildren<Props>) => { |
| 55 | + const [isCollapsed, setIsCollapsed] = useState(false); |
50 | 56 |
|
51 | | - onCollapse = (collapsed: boolean) => { |
52 | | - this.setState({ collapsed }); |
| 57 | + const onChangeIsCollapsed = (isCollapsed: boolean) => { |
| 58 | + setIsCollapsed(isCollapsed); |
53 | 59 | }; |
54 | 60 |
|
55 | | - render() { |
56 | | - const pathname = this.props.router.pathname; |
57 | | - const pathsplit: string[] = pathname.split('/'); |
58 | | - const routes = routesMaker(pathsplit); |
59 | | - return ( |
60 | | - <Layout style={{ minHeight: '100vh' }}> |
61 | | - <Sider |
62 | | - collapsible collapsed={this.state.collapsed} |
63 | | - onCollapse={this.onCollapse} |
| 61 | + const pathname = props.router.pathname; |
| 62 | + const pathsplit: string[] = pathname.split('/'); |
| 63 | + const routes = routesMaker(pathsplit); |
| 64 | + |
| 65 | + return ( |
| 66 | + <Layout style={{ minHeight: '100vh' }}> |
| 67 | + <Sider |
| 68 | + collapsible |
| 69 | + collapsed={isCollapsed} |
| 70 | + onCollapse={onChangeIsCollapsed} |
| 71 | + > |
| 72 | + <Link href="/menu1"> |
| 73 | + <a> |
| 74 | + <div className="App-logo" /> |
| 75 | + </a> |
| 76 | + </Link> |
| 77 | + <Menu |
| 78 | + theme="dark" |
| 79 | + defaultSelectedKeys={['/menu1']} |
| 80 | + selectedKeys={[pathsplit.pop()]} |
| 81 | + defaultOpenKeys={[pathsplit[1]]} |
| 82 | + mode="inline" |
64 | 83 | > |
65 | | - <Link href="/menu1"> |
66 | | - <a><div className="App-logo" /></a> |
67 | | - </Link> |
68 | | - <Menu |
69 | | - theme="dark" |
70 | | - defaultSelectedKeys={['/menu1']} |
71 | | - selectedKeys={[pathsplit.pop()]} |
72 | | - defaultOpenKeys={[pathsplit[1]]} |
73 | | - mode="inline"> |
74 | | - <Item key="menu1" icon={<DesktopOutlined />}> |
75 | | - <Link href="/menu1"><a>Menu 1</a></Link> |
| 84 | + <Item key="menu1" icon={<DesktopOutlined />}> |
| 85 | + <Link href="/menu1"> |
| 86 | + <a>Menu 1</a> |
| 87 | + </Link> |
| 88 | + </Item> |
| 89 | + <Item key="menu2" icon={<DashboardOutlined />}> |
| 90 | + <Link href="/menu2"> |
| 91 | + <a>Menu 2</a> |
| 92 | + </Link> |
| 93 | + </Item> |
| 94 | + <SubMenu key="menu3" icon={<SettingOutlined />} title="Menu 3"> |
| 95 | + <Item key="submenu1"> |
| 96 | + <Link href="/menu3/submenu1"> |
| 97 | + <a>Submenu 1</a> |
| 98 | + </Link> |
76 | 99 | </Item> |
77 | | - <Item key="menu2" icon={<DashboardOutlined />}> |
78 | | - <Link href="/menu2"><a>Menu 2</a></Link> |
| 100 | + <Item key="submenu2"> |
| 101 | + <Link href="/menu3/submenu2"> |
| 102 | + <a>Submenu 2</a> |
| 103 | + </Link> |
79 | 104 | </Item> |
80 | | - <SubMenu key="menu3" icon={<SettingOutlined />} title="Menu 3" > |
81 | | - <Item key="submenu1"> |
82 | | - <Link href="/menu3/submenu1"><a>Submenu 1</a></Link> |
83 | | - </Item> |
84 | | - <Item key="submenu2"> |
85 | | - <Link href="/menu3/submenu2"><a>Submenu 2</a></Link> |
86 | | - </Item> |
87 | | - </SubMenu> |
88 | | - </Menu> |
89 | | - </Sider> |
90 | | - <Layout style={{ padding: '0 16px 16px' }}> |
91 | | - <Breadcrumb style={{ margin: '16px 0' }} itemRender={itemRender} routes={routes} /> |
92 | | - <Content |
93 | | - className="site-layout-background" |
94 | | - style={{ |
95 | | - padding: 16, |
96 | | - minHeight: 280, |
97 | | - backgroundColor: '#ffffff', |
98 | | - }} |
99 | | - > |
100 | | - {this.props.children} |
101 | | - </Content> |
102 | | - </Layout> |
| 105 | + </SubMenu> |
| 106 | + </Menu> |
| 107 | + </Sider> |
| 108 | + <Layout style={{ padding: '0 16px 16px' }}> |
| 109 | + <Breadcrumb |
| 110 | + style={{ margin: '16px 0' }} |
| 111 | + itemRender={itemRender} |
| 112 | + routes={routes} |
| 113 | + /> |
| 114 | + <Content |
| 115 | + className="site-layout-background" |
| 116 | + style={{ |
| 117 | + padding: 16, |
| 118 | + minHeight: 280, |
| 119 | + backgroundColor: '#ffffff', |
| 120 | + }} |
| 121 | + > |
| 122 | + {props.children} |
| 123 | + </Content> |
103 | 124 | </Layout> |
104 | | - ) |
105 | | - } |
106 | | -} |
| 125 | + </Layout> |
| 126 | + ); |
| 127 | +}; |
107 | 128 |
|
108 | | -export default withRouter(AppLayout) |
| 129 | +export default withRouter(AppLayout); |
0 commit comments