forked from huge/frontEnd/hugeOA

Mr Ke
2020-04-06 1a784f157c2b1ef8b464b229f993ba44c356e9fe
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/* eslint-disable */
/**柯礼钦
 * 4/2/2020, 11:22:51 AM
 * doc comment for the file goes here
 */
 
/** 菜单组件 */
 
import React from 'react';
import { Spin, Layout, Menu, Icon } from 'antd';
import MenView from '../../components/common/MenuView';
import { menus, getMenuListByPermission } from '../../menu';
 
// import Fetch from '../fetch';
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 MenuView extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      collapsed: true,
      data: []
    };
  }
 
  componentDidMount() {
    this.setState({ data: menus });
  }
 
  onCollapse = collapsed => {
    this.setState({ collapsed });
  }
 
  render() {
    const { data, collapsed } = this.state;
    const pathname = selected(data, this.props.location.pathname);
    return (
      <Sider
        onCollapse={this.onCollapse}
        collapsed={collapsed}
        breakpoint="lg"
        collapsible={true}
      >
        <MenView
          history={this.props.history}
          pathname={pathname}
          key={data.length}
          menudata={data}
        />
      </Sider>
    );
  }
 
}