UNPKG

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