forked from huge/frontEnd/hugeOA

Mr Ke
2020-06-24 cb0a06de99bd629c2021e3d9b32fe8eae8c54879
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
import React from 'react';
import { Layout } from 'antd';
 
import TreeMenu from '../component/TreeMenu';
import data from '../data/menu';
const { Sider } = Layout;
function find(data, pathname) {
  for (const { path, children } of data) {
    if (path === pathname) return path;
    if (children) {
      const result = find(children, pathname);
      if (result) return result;
    }
  }
  return null;
}
 
function selected(data, pathname) {
  if (!pathname || pathname === '/') return pathname;
  return find(data, pathname) || selected(data, pathname.split('/').slice(0, -1).join('/'));
}
 
export default class Menu extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      collapsed: false
    }
  }
  onCollapse = collapsed => {
    this.setState({ collapsed });
  }
  render() {
    const pathname = selected(data, this.props.location.pathname);
    let defaultOpenKeys = (data.find(node => node.children && node.children.find(child => child.path == pathname)) || {}).name
    return (
      <Sider onCollapse={this.onCollapse}
        collapsed={this.state.collapsed}
        className="sider-light"
        breakpoint="lg"
        collapsible={false}
      >
        <TreeMenu
          data={data}
          defaultOpenKeys={[(data.find(node => node.children && node.children.find(child => child.path == pathname)) || {}).name]}
          defaultSelectedKeys={[pathname]}
        />
      </Sider>
    )
  }
}