UNPKG

1.96 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}`>
35 {item.page}
36 </Link>
37 </Menu.Item>
38 )
39 });
40
41 res.push((
42 <SubMenu key={cg + '-key'} title={cg}>
43 {items}
44 </SubMenu>
45 ));
46 });
47 return res;
48 }
49
50 componentDidMount() {
51 const{md, config} =this.props.db;
52 const cg = config.category[0];
53 if(cg) PubSub.publish('SET_PAGE', md[cg][0].page);
54 }
55
56 handleMenuClick({item, key, keyPath}) {
57 // console.log(item, key);
58 PubSub.publish('SET_PAGE', key);
59 }
60
61 render() {
62 const menuItems = this.$renderItem(this.props.db);
63 const{md, config} =this.props.db;
64
65 const defaultKeys = config.category.map(item => {
66 return item + '-key';
67 })
68
69 return (
70 <Menu
71 mode="inline"
72 defaultSelectedKeys={['0']}
73 defaultOpenKeys={defaultKeys}
74 style={{ height: '100%' }}
75 onClick={this.handleMenuClick.bind(this)}
76 >
77 {menuItems}
78
79 </Menu>
80 )
81 }
82}
\No newline at end of file