Commit ee0f8db0 authored by afc163's avatar afc163

Fix breadcrumb 404 link, close #343

parent 4ff721fa
...@@ -6,23 +6,23 @@ import styles from './index.less'; ...@@ -6,23 +6,23 @@ import styles from './index.less';
const { TabPane } = Tabs; const { TabPane } = Tabs;
function getBreadcrumbName(breadcrumbNameMap, url) { function getBreadcrumb(breadcrumbNameMap, url) {
if (breadcrumbNameMap[url] && breadcrumbNameMap[url].name) { if (breadcrumbNameMap[url]) {
return breadcrumbNameMap[url].name; return breadcrumbNameMap[url];
} }
const urlWithoutSplash = url.replace(/\/$/, ''); const urlWithoutSplash = url.replace(/\/$/, '');
if (breadcrumbNameMap[urlWithoutSplash] && breadcrumbNameMap[urlWithoutSplash].name) { if (breadcrumbNameMap[urlWithoutSplash]) {
return breadcrumbNameMap[urlWithoutSplash].name; return breadcrumbNameMap[urlWithoutSplash];
} }
let breadcrumbName = ''; let breadcrumb = '';
Object.keys(breadcrumbNameMap).forEach((item) => { Object.keys(breadcrumbNameMap).forEach((item) => {
const itemRegExpStr = `^${item.replace(/:[\w-]+/g, '[\\w-]+')}$`; const itemRegExpStr = `^${item.replace(/:[\w-]+/g, '[\\w-]+')}$`;
const itemRegExp = new RegExp(itemRegExpStr); const itemRegExp = new RegExp(itemRegExpStr);
if (itemRegExp.test(url)) { if (itemRegExp.test(url)) {
breadcrumbName = breadcrumbNameMap[item].name; breadcrumb = breadcrumbNameMap[item];
} }
}); });
return breadcrumbName || url; return breadcrumb;
} }
export default class PageHeader extends PureComponent { export default class PageHeader extends PureComponent {
...@@ -76,12 +76,14 @@ 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 pathSnippets = location.pathname.split('/').filter(i => i);
const extraBreadcrumbItems = pathSnippets.map((_, index) => { const extraBreadcrumbItems = pathSnippets.map((_, index) => {
const url = `/${pathSnippets.slice(0, index + 1).join('/')}`; const url = `/${pathSnippets.slice(0, index + 1).join('/')}`;
const currentBreadcrumb = getBreadcrumb(breadcrumbNameMap, url);
const isLinkable = (index !== pathSnippets.length - 1) && currentBreadcrumb.component;
return ( return (
<Breadcrumb.Item key={url}> <Breadcrumb.Item key={url}>
{createElement( {createElement(
(index === pathSnippets.length - 1) ? 'span' : linkElement, isLinkable ? linkElement : 'span',
{ [linkElement === 'a' ? 'href' : 'to']: url }, { [linkElement === 'a' ? 'href' : 'to']: url },
getBreadcrumbName(breadcrumbNameMap, url), currentBreadcrumb.name || url,
)} )}
</Breadcrumb.Item> </Breadcrumb.Item>
); );
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment