From 4ff721fa5b0ec748ede6e7b4d32d9af23db27195 Mon Sep 17 00:00:00 2001 From: afc163 Date: Sat, 2 Dec 2017 20:51:27 +0800 Subject: [PATCH] Refactor breadcrumbMap --- src/components/PageHeader/index.js | 22 +++++++++++++--------- src/layouts/BasicLayout.js | 5 ++++- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/components/PageHeader/index.js b/src/components/PageHeader/index.js index 80f5fc36..e9002a0c 100644 --- a/src/components/PageHeader/index.js +++ b/src/components/PageHeader/index.js @@ -6,16 +6,23 @@ import styles from './index.less'; const { TabPane } = Tabs; -function getBreadcrumbNameWithParams(breadcrumbNameMap, url) { - let name = ''; +function getBreadcrumbName(breadcrumbNameMap, url) { + if (breadcrumbNameMap[url] && breadcrumbNameMap[url].name) { + return breadcrumbNameMap[url].name; + } + const urlWithoutSplash = url.replace(/\/$/, ''); + if (breadcrumbNameMap[urlWithoutSplash] && breadcrumbNameMap[urlWithoutSplash].name) { + return breadcrumbNameMap[urlWithoutSplash].name; + } + let breadcrumbName = ''; Object.keys(breadcrumbNameMap).forEach((item) => { const itemRegExpStr = `^${item.replace(/:[\w-]+/g, '[\\w-]+')}$`; const itemRegExp = new RegExp(itemRegExpStr); if (itemRegExp.test(url)) { - name = breadcrumbNameMap[item]; + breadcrumbName = breadcrumbNameMap[item].name; } }); - return name; + return breadcrumbName || url; } export default class PageHeader extends PureComponent { @@ -72,12 +79,9 @@ export default class PageHeader extends PureComponent { return ( {createElement( - index === pathSnippets.length - 1 ? 'span' : linkElement, + (index === pathSnippets.length - 1) ? 'span' : linkElement, { [linkElement === 'a' ? 'href' : 'to']: url }, - breadcrumbNameMap[url] || - breadcrumbNameMap[url.replace('/', '')] || - getBreadcrumbNameWithParams(breadcrumbNameMap, url) || - url + getBreadcrumbName(breadcrumbNameMap, url), )} ); diff --git a/src/layouts/BasicLayout.js b/src/layouts/BasicLayout.js index 180cec75..a46cb572 100644 --- a/src/layouts/BasicLayout.js +++ b/src/layouts/BasicLayout.js @@ -61,7 +61,10 @@ class BasicLayout extends React.PureComponent { const breadcrumbNameMap = {}; routeData.concat(menuData).forEach((item) => { - breadcrumbNameMap[item.path] = item.name; + breadcrumbNameMap[item.path] = { + name: item.name, + component: item.component, + }; }); return { location, breadcrumbNameMap }; } -- GitLab