layout.js
4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import React from 'react';
import { findIndex } from 'lodash';
import { Layout, Menu, Breadcrumb, Icon } from 'antd';
import { BrowserRouter, StaticRouter, Route, NavLink } from 'react-router-dom';
import LoginPage from './login';
import router from '../../router';
import 'antd/dist/antd.css'
import './layout.css'
const { SubMenu } = Menu;
const { Header, Content, Sider } = Layout;
class Page extends React.Component {
constructor() {
super();
this.menuList = [
{
name: '数据统计',
icon: 'deployment-unit',
sub: [
{
path: '/sales',
name: '销售额统计'
},
{
path: '/top300/stock',
name: 'top300库存'
}
]
},
{
name: '系统设置',
icon: 'setting',
sub: [
{
path: '/setting/account',
name: '账户管理'
}
]
}
];
}
renderSubMenu(list = [], preKey) {
return list.map((val, index) => {
return <Menu.Item key={preKey + '_' + index}><NavLink to={val.path}>{val.name}</NavLink></Menu.Item>;
});
}
renderMenu(menus, path) {
let openKey = [];
let selectKey = [];
let breadcrumb = [{path: '/', name: '首页'}];
const list = menus.map((val, index) => {
let key = 'sub' + index;
if (!selectKey.length) {
let active = findIndex(val.sub, (o) => {
return o.path === path;
});
if (active > -1) {
openKey.push(key);
selectKey.push(key + '_' + active);
breadcrumb.push(val.sub[active]);
}
}
return (
<SubMenu
key={key}
title={
<span>
<Icon type={val.icon} />
{val.name}
</span>
}
>
{this.renderSubMenu(val.sub, key)}
</SubMenu>
)
})
return {
openKey,
selectKey,
breadcrumb,
list
}
}
renderBreadcrumb(breadcrumb) {
return (
<Breadcrumb style={{ margin: '16px 0' }}>
{breadcrumb.map((val, index) => {
return <Breadcrumb.Item key={index}>{val.name}</Breadcrumb.Item>;
})}
</Breadcrumb>
)
}
render() {
let { path, client } = this.props.context || {};
const Router = client ? BrowserRouter : StaticRouter;
if (path === '/login.html') {
return <LoginPage></LoginPage>;
}
const subMenu = this.renderMenu(this.menuList, path);
return (
<Layout className="yoho-layout">
<Header className="yoho-header">
YOHO!UFO
</Header>
<Layout className="ant-layout-has-sider">
<Router>
<Sider width={200} style={{ background: '#fff' }}>
<Menu
mode="inline"
defaultSelectedKeys={subMenu.selectKey}
defaultOpenKeys={subMenu.openKey}
style={{ height: '100%', borderRight: 0 }}
>
{subMenu.list}
</Menu>
</Sider>
<Layout style={{ padding: '0 24px 24px' }}>
{this.renderBreadcrumb(subMenu.breadcrumb)}
<Content style={{
background: '#fff',
padding: 24,
margin: 0,
minHeight: 'auto',
}} >
{router.map((route, i) => (
<Route key={i} path={route.path} component={route.component}
exact={route.exact} />
))}
</Content>
</Layout>
</Router>
</Layout>
</Layout>
)
}
}
export default Page;