UNPKG

1.9 kBJavaScriptView Raw
1import React from 'react';
2import { Layout, Menu, Icon } from 'antd';
3import {
4 BrowserRouter as Router,
5 Route,
6 HashRouter,
7 Link
8} from 'react-router-dom';
9
10const { SubMenu } = Menu;
11const { Sider } = Layout;
12
13import PubSub from 'pubsub-js';
14
15export default class SiderView extends React.Component {
16
17 constructor(props) {
18 super(props);
19 }
20
21 $renderItem(db) {
22
23 const{md, config} = db;
24
25 const res = [];
26
27 const category = config.category.slice();
28
29 category.forEach((cg, i) => {
30 const list = md[cg];
31 const items = list.map((item, i) => {
32 return(
33 <Menu.Item key={item.page}>
34 <Link to=`/{item.page}`>{item.page}</Link>
35 </Menu.Item>
36 )
37 });
38
39 res.push((
40 <SubMenu key={cg + '-key'} title={cg}>
41 {items}
42 </SubMenu>
43 ));
44 });
45 return res;
46 }
47
48 componentDidMount() {
49 const{md, config} =this.props.db;
50 const cg = config.category[0];
51 if(cg) PubSub.publish('SET_PAGE', md[cg][0].page);
52 }
53
54 handleMenuClick({item, key, keyPath}) {
55 // console.log(item, key);
56 PubSub.publish('SET_PAGE', key);
57 }
58
59 render() {
60 const menuItems = this.$renderItem(this.props.db);
61 const{md, config} =this.props.db;
62
63 const defaultKeys = config.category.map(item => {
64 return item + '-key';
65 })
66
67 return (
68 <Menu
69 mode="inline"
70 defaultSelectedKeys={['0']}
71 defaultOpenKeys={defaultKeys}
72 style={{ height: '100%' }}
73 onClick={this.handleMenuClick.bind(this)}
74 >
75 {menuItems}
76
77 </Menu>
78 )
79 }
80}
\No newline at end of file