From ee0f8db0c07b1f82353a7ec4c587478a89a7f818 Mon Sep 17 00:00:00 2001 From: afc163 Date: Sat, 2 Dec 2017 20:56:17 +0800 Subject: [PATCH] Fix breadcrumb 404 link, close #343 --- src/components/PageHeader/index.js | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/components/PageHeader/index.js b/src/components/PageHeader/index.js index e9002a0c..ebaf7498 100644 --- a/src/components/PageHeader/index.js +++ b/src/components/PageHeader/index.js @@ -6,23 +6,23 @@ import styles from './index.less'; const { TabPane } = Tabs; -function getBreadcrumbName(breadcrumbNameMap, url) { - if (breadcrumbNameMap[url] && breadcrumbNameMap[url].name) { - return breadcrumbNameMap[url].name; +function getBreadcrumb(breadcrumbNameMap, url) { + if (breadcrumbNameMap[url]) { + return breadcrumbNameMap[url]; } const urlWithoutSplash = url.replace(/\/$/, ''); - if (breadcrumbNameMap[urlWithoutSplash] && breadcrumbNameMap[urlWithoutSplash].name) { - return breadcrumbNameMap[urlWithoutSplash].name; + if (breadcrumbNameMap[urlWithoutSplash]) { + return breadcrumbNameMap[urlWithoutSplash]; } - let breadcrumbName = ''; + let breadcrumb = ''; Object.keys(breadcrumbNameMap).forEach((item) => { const itemRegExpStr = `^${item.replace(/:[\w-]+/g, '[\\w-]+')}$`; const itemRegExp = new RegExp(itemRegExpStr); if (itemRegExp.test(url)) { - breadcrumbName = breadcrumbNameMap[item].name; + breadcrumb = breadcrumbNameMap[item]; } }); - return breadcrumbName || url; + return breadcrumb; } export default class PageHeader extends PureComponent { @@ -76,12 +76,14 @@ export default class PageHeader extends PureComponent { const pathSnippets = location.pathname.split('/').filter(i => i); const extraBreadcrumbItems = pathSnippets.map((_, index) => { const url = `/${pathSnippets.slice(0, index + 1).join('/')}`; + const currentBreadcrumb = getBreadcrumb(breadcrumbNameMap, url); + const isLinkable = (index !== pathSnippets.length - 1) && currentBreadcrumb.component; return ( {createElement( - (index === pathSnippets.length - 1) ? 'span' : linkElement, + isLinkable ? linkElement : 'span', { [linkElement === 'a' ? 'href' : 'to']: url }, - getBreadcrumbName(breadcrumbNameMap, url), + currentBreadcrumb.name || url, )} ); -- GitLab