import React, { PureComponent } from 'react'; import { Layout } from 'antd'; import { Link } from 'dva/router'; import styles from './index.less'; import BaseMeun, { getMeunMatcheys } from './BaseMeun'; import { urlToList } from '../utils/pathTools'; const { Sider } = Layout; export default class SiderMenu extends PureComponent { constructor(props) { super(props); this.menus = props.menuData; this.flatMenuKeys = this.getFlatMenuKeys(props.menuData); this.state = { openKeys: this.getDefaultCollapsedSubMenus(props), }; } componentWillReceiveProps(nextProps) { if (nextProps.location.pathname !== this.props.location.pathname) { this.setState({ openKeys: this.getDefaultCollapsedSubMenus(nextProps), }); } } /** * Recursively flatten the data * [{path:string},{path:string}] => {path,path2} * @param menus */ getFlatMenuKeys(menus) { let keys = []; menus.forEach((item) => { if (item.children) { keys = keys.concat(this.getFlatMenuKeys(item.children)); } keys.push(item.path); }); return keys; } /** * Convert pathname to openKeys * /list/search/articles = > ['list','/list/search'] * @param props */ getDefaultCollapsedSubMenus(props) { const { location: { pathname } } = props || this.props; return urlToList(pathname) .map((item) => { return getMeunMatcheys(this.flatMenuKeys, item)[0]; }) .filter(item => item); } handleOpenChange = (openKeys) => { const lastOpenKey = openKeys[openKeys.length - 1]; const moreThanOne = openKeys.filter(openKey => this.isMainMenu(openKey)).length > 1; this.setState({ openKeys: moreThanOne ? [lastOpenKey] : [...openKeys], }); }; render() { const { logo, collapsed, onCollapse } = this.props; const { openKeys } = this.state; return (
logo

Ant Design Pro

); } }