UNPKG

2.6 kBJavaScriptView Raw
1import React from 'react';
2import { Layout, Menu, Icon } from 'antd';
3import {
4 BrowserRouter as Router,
5 Route,
6 HashRouter,
7 Redirect,
8 Link
9} from 'react-router-dom';
10import createHistory from 'history/createBrowserHistory'
11
12
13const { SubMenu } = Menu;
14const { Sider } = Layout;
15
16export default class SiderView extends React.Component {
17
18 constructor(props) {
19 super(props);
20 }
21
22 $renderItem(db) {
23
24 const{md, config} = db;
25
26 const res = [];
27
28 const category = config.category.slice();
29
30
31 const renderMenuItem = (list, cg) => {
32 const items = list.map((item, i) => {
33 const to = `/page/${item.page}`;
34 return(
35 <Menu.Item key={item.page}>
36 <Link to={to}>
37 {item.page}
38 </Link>
39 </Menu.Item>
40 )
41 });
42
43 return items;
44 };
45
46 // 如果md 文件没有设置 category 直接渲染 menuItem;
47 if (md.default && md.default.length) {
48 res.push(renderMenuItem(md.default));
49 }
50
51 category.forEach((cg, i) => {
52 const list = md[cg];
53 const items = renderMenuItem(list, cg);
54
55 res.push((
56 <SubMenu key={cg + '-key'} title={cg}>
57 {items}
58 </SubMenu>
59 ));
60 });
61
62 return res;
63 }
64
65 componentDidMount() {
66 // const{md, config} =this.props.db;
67 // const cg = config.category[0];
68 // if(cg) PubSub.publish('SET_PAGE', md[cg][0].page);
69 const history = createHistory()
70 const location = history.location;
71 console.log(1111, location)
72 history.push('#/page/B/', { some: 'state' })
73 }
74
75 render() {
76 const menuItems = this.$renderItem(this.props.db);
77 const{md, config} =this.props.db;
78 const cg = config.category[0];
79
80 const defaultKeys = config.category.map(item => {
81 return item + '-key';
82 })
83
84 // <Redirect to={`/page/${md[cg][0].page}`}/>
85
86 // window.location('/page/B')
87 console.log(window.history);
88
89 window.history.pushState({
90 foo: "bar"
91 }, "page 2")
92
93 return (
94 <Menu
95 mode="inline"
96 defaultSelectedKeys={['0']}
97 defaultOpenKeys={defaultKeys}
98 style={{ height: '100%' }}
99 >
100 {menuItems}
101 </Menu>
102 )
103 }
104}
\No newline at end of file