From c771ea290e22c80355bbfe361ca111b5a78c43c7 Mon Sep 17 00:00:00 2001 From: ddcat1115 Date: Fri, 3 Nov 2017 14:27:04 +0800 Subject: [PATCH] PageHeader - breadcrumb support url with params fix #64 --- src/components/PageHeader/index.js | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/components/PageHeader/index.js b/src/components/PageHeader/index.js index b6250a2b..c2251a55 100644 --- a/src/components/PageHeader/index.js +++ b/src/components/PageHeader/index.js @@ -6,6 +6,18 @@ import styles from './index.less'; const { TabPane } = Tabs; +function getBreadcrumbNameWithParams(breadcrumbNameMap, url) { + let name = ''; + Object.keys(breadcrumbNameMap).forEach((item) => { + const itemRegExpStr = `^${item.replace(/:[\w-]+/g, '[\\w-]+')}$`; + const itemRegExp = new RegExp(itemRegExpStr); + if (itemRegExp.test(url)) { + name = breadcrumbNameMap[item]; + } + }); + return name; +} + export default class PageHeader extends PureComponent { static contextTypes = { routes: PropTypes.array, @@ -59,9 +71,14 @@ export default class PageHeader extends PureComponent { const url = `/${pathSnippets.slice(0, index + 1).join('/')}`; return ( - {createElement(index === pathSnippets.length - 1 ? 'span' : linkElement, { - [linkElement === 'a' ? 'href' : 'to']: url, - }, breadcrumbNameMap[url] || breadcrumbNameMap[url.replace('/', '')] || url)} + {createElement( + index === pathSnippets.length - 1 ? 'span' : linkElement, + { [linkElement === 'a' ? 'href' : 'to']: url }, + breadcrumbNameMap[url] || + breadcrumbNameMap[url.replace('/', '')] || + getBreadcrumbNameWithParams(breadcrumbNameMap, url) || + url + )} ); }); -- GitLab