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>
|
)
|
}
|
}
|