From 85d556d30140233738d7ac16984a1105b07588cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=B8=85?= Date: Thu, 4 Oct 2018 22:24:42 +0800 Subject: [PATCH] move menuData to state --- src/components/SiderMenu/BaseMenu.js | 4 +- src/layouts/BasicLayout.js | 59 +++++++++++++++++----------- 2 files changed, 37 insertions(+), 26 deletions(-) diff --git a/src/components/SiderMenu/BaseMenu.js b/src/components/SiderMenu/BaseMenu.js index 3fd4abf3..76c21bc0 100644 --- a/src/components/SiderMenu/BaseMenu.js +++ b/src/components/SiderMenu/BaseMenu.js @@ -80,7 +80,7 @@ export default class BaseMenu extends PureComponent { getSubMenuOrItem = item => { // doc: add hideChildrenInMenu if (item.children && !item.hideChildrenInMenu && item.children.some(child => child.name)) { - const name = formatMessage({ id: item.locale }); + const name = item.locale ? formatMessage({ id: item.locale }) : item.name; return ( { - const name = formatMessage({ id: item.locale }); + const name = item.locale ? formatMessage({ id: item.locale }) : item.name; const itemPath = this.conversionPath(item.path); const icon = getIcon(item.icon); const { target } = item; diff --git a/src/layouts/BasicLayout.js b/src/layouts/BasicLayout.js index 9d09d19c..df33f326 100644 --- a/src/layouts/BasicLayout.js +++ b/src/layouts/BasicLayout.js @@ -22,28 +22,39 @@ const { Content } = Layout; // Conversion router to menu. function formatter(data, parentPath = '', parentAuthority, parentName) { - return data.map(item => { - let locale = 'menu'; - if (parentName && item.name) { - locale = `${parentName}.${item.name}`; - } else if (item.name) { - locale = `menu.${item.name}`; - } else if (parentName) { - locale = parentName; - } - const result = { - ...item, - locale, - authority: item.authority || parentAuthority, - }; - if (item.routes) { - const children = formatter(item.routes, `${parentPath}${item.path}/`, item.authority, locale); - // Reduce memory usage - result.children = children; - } - delete result.routes; - return result; - }); + return data + .map(item => { + let locale = 'menu'; + if (parentName && item.name) { + locale = `${parentName}.${item.name}`; + } else if (item.name) { + locale = `menu.${item.name}`; + } else if (parentName) { + locale = parentName; + } + if (item.path) { + const result = { + ...item, + locale, + authority: item.authority || parentAuthority, + }; + if (item.routes) { + const children = formatter( + item.routes, + `${parentPath}${item.path}/`, + item.authority, + locale + ); + // Reduce memory usage + result.children = children; + } + delete result.routes; + return result; + } + + return null; + }) + .filter(item => item); } const memoizeOneFormatter = memoizeOne(formatter, isEqual); @@ -85,6 +96,7 @@ class BasicLayout extends React.PureComponent { state = { rendering: true, isMobile: false, + menuData: this.getMenuData(), }; componentDidMount() { @@ -224,9 +236,8 @@ class BasicLayout extends React.PureComponent { children, location: { pathname }, } = this.props; - const { isMobile } = this.state; + const { isMobile, menuData } = this.state; const isTop = PropsLayout === 'topmenu'; - const menuData = this.getMenuData(); const routerConfig = this.matchParamsPath(pathname); const layout = ( -- GitLab