From c1c36b6118a3ffa4429cd2a195c56bf14f338675 Mon Sep 17 00:00:00 2001 From: jim Date: Thu, 31 May 2018 11:42:51 +0800 Subject: [PATCH] Fix menu click exception --- .webpackrc.js | 5 ++--- package.json | 12 ++++++------ src/components/Sidebar/index.js | 2 +- src/components/SiderMenu/SiderMenu.js | 26 ++++++++++++++++++-------- 4 files changed, 27 insertions(+), 18 deletions(-) diff --git a/.webpackrc.js b/.webpackrc.js index 79ac4d29..fe823e7a 100755 --- a/.webpackrc.js +++ b/.webpackrc.js @@ -2,9 +2,7 @@ const path = require('path'); export default { entry: 'src/index.js', - extraBabelPlugins: [ - ['import', { libraryName: 'antd', libraryDirectory: 'es', style: true }], - ], + extraBabelPlugins: [['import', { libraryName: 'antd', libraryDirectory: 'es', style: true }]], env: { development: { extraBabelPlugins: ['dva-hmr'], @@ -19,5 +17,6 @@ export default { template: './src/index.ejs', }, publicPath: '/', + disableDynamicImport: true, hash: true, }; diff --git a/package.json b/package.json index 8cd618ff..cb285677 100755 --- a/package.json +++ b/package.json @@ -39,7 +39,7 @@ "path-to-regexp": "^2.1.0", "prop-types": "^15.5.10", "qs": "^6.5.0", - "rc-drawer-menu": "^0.5.0", + "rc-drawer-menu": "^1.1.0", "react": "^16.3.1", "react-container-query": "^0.11.0", "react-document-title": "^2.0.3", @@ -51,14 +51,14 @@ "devDependencies": { "@types/react": "^16.3.8", "@types/react-dom": "^16.0.5", + "antd-theme-webpack-plugin": "^1.0.8", "babel-eslint": "^8.1.2", "babel-plugin-dva-hmr": "^0.4.1", "babel-plugin-import": "^1.6.7", "babel-plugin-module-resolver": "^3.1.1", - "cross-env": "^5.1.1", - "antd-theme-webpack-plugin": "^1.0.8", "babel-plugin-transform-decorators-legacy": "^1.3.4", "babel-runtime": "^6.9.2", + "cross-env": "^5.1.1", "cross-port-killer": "^1.0.1", "enzyme": "^3.1.0", "eslint": "^4.14.0", @@ -74,13 +74,13 @@ "husky": "^0.14.3", "lint-staged": "^7.0.5", "mockjs": "^1.0.1-beta3", - "prettier": "1.12.1", + "prettier": "1.13.2", "pro-download": "^1.0.1", "redbox-react": "^1.5.0", "regenerator-runtime": "^0.11.1", - "roadhog": "^2.4.1", + "roadhog": "2.3.0", "roadhog-api-doc": "^1.0.3", - "stylelint": "^8.4.0", + "stylelint": "^9.2.1", "stylelint-config-prettier": "^3.0.4", "stylelint-config-standard": "^18.0.0" }, diff --git a/src/components/Sidebar/index.js b/src/components/Sidebar/index.js index 1ba2b4a3..b6436c8e 100644 --- a/src/components/Sidebar/index.js +++ b/src/components/Sidebar/index.js @@ -136,7 +136,7 @@ class Sidebar extends PureComponent { ); export default class SiderMenu extends PureComponent { - static getDerivedStateFromProps(nextProps) { - return { - openKeys: getDefaultCollapsedSubMenus(nextProps), - }; + static getDerivedStateFromProps(props, state) { + const { pathname } = state; + if (props.location.pathname !== pathname) { + return { + pathname: props.location.pathname, + openKeys: getDefaultCollapsedSubMenus(props), + }; + } + return null; } constructor(props) { super(props); this.menus = props.menuData; this.flatMenuKeys = getFlatMenuKeys(props.menuData); this.state = { + pathname: props.location.pathname, openKeys: getDefaultCollapsedSubMenus(props), }; } @@ -113,7 +119,7 @@ export default class SiderMenu extends PureComponent { { @@ -200,13 +206,17 @@ export default class SiderMenu extends PureComponent { return ItemDom; }; isMainMenu = key => { - return this.props.menuData.some(item => key && (item.key === key || item.path === key)); + return this.props.menuData.some(item => { + if (key) { + return item.key === key || item.path === key; + } + return false; + }); }; handleOpenChange = openKeys => { - const lastOpenKey = openKeys[openKeys.length - 1]; const moreThanOne = openKeys.filter(openKey => this.isMainMenu(openKey)).length > 1; this.setState({ - openKeys: moreThanOne ? [lastOpenKey] : [...openKeys], + openKeys: moreThanOne ? [openKeys.pop()] : [...openKeys], }); }; render() { -- GitLab