|
|
@@ -1,24 +1,17 @@
|
|
|
import React, { PureComponent, createElement } from 'react';
|
|
|
import PropTypes from 'prop-types';
|
|
|
+import pathToRegexp from 'path-to-regexp';
|
|
|
import { Breadcrumb, Tabs } from 'antd';
|
|
|
import classNames from 'classnames';
|
|
|
import styles from './index.less';
|
|
|
|
|
|
+
|
|
|
const { TabPane } = Tabs;
|
|
|
|
|
|
function getBreadcrumb(breadcrumbNameMap, url) {
|
|
|
- if (breadcrumbNameMap[url]) {
|
|
|
- return breadcrumbNameMap[url];
|
|
|
- }
|
|
|
- const urlWithoutSplash = url.replace(/\/$/, '');
|
|
|
- if (breadcrumbNameMap[urlWithoutSplash]) {
|
|
|
- return breadcrumbNameMap[urlWithoutSplash];
|
|
|
- }
|
|
|
let breadcrumb = {};
|
|
|
Object.keys(breadcrumbNameMap).forEach((item) => {
|
|
|
- const itemRegExpStr = `^${item.replace(/:[\w-]+/g, '[\\w-]+')}$`;
|
|
|
- const itemRegExp = new RegExp(itemRegExpStr);
|
|
|
- if (itemRegExp.test(url)) {
|
|
|
+ if (pathToRegexp(item).test(url)) {
|
|
|
breadcrumb = breadcrumbNameMap[item];
|
|
|
}
|
|
|
});
|
|
|
@@ -41,10 +34,90 @@ export default class PageHeader extends PureComponent {
|
|
|
return {
|
|
|
routes: this.props.routes || this.context.routes,
|
|
|
params: this.props.params || this.context.params,
|
|
|
- location: this.props.location || this.context.location,
|
|
|
+ routerLocation: this.props.location || this.context.location,
|
|
|
breadcrumbNameMap: this.props.breadcrumbNameMap || this.context.breadcrumbNameMap,
|
|
|
};
|
|
|
};
|
|
|
+ // Generated according to props
|
|
|
+ conversionFromProps= () => {
|
|
|
+ const {
|
|
|
+ breadcrumbList, linkElement = 'a',
|
|
|
+ } = this.props;
|
|
|
+ return (
|
|
|
+ <Breadcrumb className={styles.breadcrumb}>
|
|
|
+ {breadcrumbList.map(item => (
|
|
|
+ <Breadcrumb.Item key={item.title}>
|
|
|
+ {item.href ? (createElement(linkElement, {
|
|
|
+ [linkElement === 'a' ? 'href' : 'to']: item.href,
|
|
|
+ }, item.title)) : item.title}
|
|
|
+ </Breadcrumb.Item>
|
|
|
+ ))}
|
|
|
+ </Breadcrumb>
|
|
|
+ );
|
|
|
+ }
|
|
|
+ conversionFromLocation = (routerLocation, breadcrumbNameMap) => {
|
|
|
+ const { linkElement = 'a' } = this.props;
|
|
|
+ // Convert the path to an array
|
|
|
+ const pathSnippets = routerLocation.pathname.split('/').filter(i => i);
|
|
|
+ // Loop data mosaic routing
|
|
|
+ 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 currentBreadcrumb.name && !currentBreadcrumb.hideInBreadcrumb ? (
|
|
|
+ <Breadcrumb.Item key={url}>
|
|
|
+ {createElement(
|
|
|
+ isLinkable ? linkElement : 'span',
|
|
|
+ { [linkElement === 'a' ? 'href' : 'to']: url },
|
|
|
+ currentBreadcrumb.name,
|
|
|
+ )}
|
|
|
+ </Breadcrumb.Item>
|
|
|
+ ) : null;
|
|
|
+ });
|
|
|
+ // Add home breadcrumbs to your head
|
|
|
+ extraBreadcrumbItems.unshift(
|
|
|
+ <Breadcrumb.Item key="home">
|
|
|
+ {createElement(linkElement, {
|
|
|
+ [linkElement === 'a' ? 'href' : 'to']: '/' }, '首页')}
|
|
|
+ </Breadcrumb.Item>
|
|
|
+ );
|
|
|
+ return (
|
|
|
+ <Breadcrumb className={styles.breadcrumb}>
|
|
|
+ {extraBreadcrumbItems}
|
|
|
+ </Breadcrumb>
|
|
|
+ );
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 将参数转化为面包屑
|
|
|
+ * Convert parameters into breadcrumbs
|
|
|
+ */
|
|
|
+ conversionBreadcrumbList = () => {
|
|
|
+ const { breadcrumbList } = this.props;
|
|
|
+ const { routes, params, routerLocation, breadcrumbNameMap } = this.getBreadcrumbProps();
|
|
|
+ if (breadcrumbList && breadcrumbList.length) {
|
|
|
+ return this.conversionFromProps();
|
|
|
+ }
|
|
|
+ // 如果传入 routes 和 params 属性
|
|
|
+ // If pass routes and params attributes
|
|
|
+ if (routes && params) {
|
|
|
+ return (
|
|
|
+ <Breadcrumb
|
|
|
+ className={styles.breadcrumb}
|
|
|
+ routes={routes.filter(route => route.breadcrumbName)}
|
|
|
+ params={params}
|
|
|
+ itemRender={this.itemRender}
|
|
|
+ />
|
|
|
+ );
|
|
|
+ }
|
|
|
+ // 根据 location 生成 面包屑
|
|
|
+ // Generate breadcrumbs based on location
|
|
|
+ if (location && location.pathname) {
|
|
|
+ return this.conversionFromLocation(routerLocation, breadcrumbNameMap);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ // 渲染Breadcrumb 子节点
|
|
|
+ // Render the Breadcrumb child node
|
|
|
itemRender = (route, params, routes, paths) => {
|
|
|
const { linkElement = 'a' } = this.props;
|
|
|
const last = routes.indexOf(route) === routes.length - 1;
|
|
|
@@ -55,77 +128,19 @@ export default class PageHeader extends PureComponent {
|
|
|
to: paths.join('/') || '/',
|
|
|
}, route.breadcrumbName);
|
|
|
}
|
|
|
+
|
|
|
render() {
|
|
|
- const { routes, params, location, breadcrumbNameMap } = this.getBreadcrumbProps();
|
|
|
const {
|
|
|
title, logo, action, content, extraContent,
|
|
|
- breadcrumbList, tabList, className, linkElement = 'a',
|
|
|
- tabActiveKey,
|
|
|
+ tabList, className, tabActiveKey,
|
|
|
} = this.props;
|
|
|
const clsString = classNames(styles.pageHeader, className);
|
|
|
- let breadcrumb;
|
|
|
- if (breadcrumbList && breadcrumbList.length) {
|
|
|
- breadcrumb = (
|
|
|
- <Breadcrumb className={styles.breadcrumb}>
|
|
|
- {
|
|
|
- breadcrumbList.map(item => (
|
|
|
- <Breadcrumb.Item key={item.title}>
|
|
|
- {item.href ? (
|
|
|
- createElement(linkElement, {
|
|
|
- [linkElement === 'a' ? 'href' : 'to']: item.href,
|
|
|
- }, item.title)
|
|
|
- ) : item.title}
|
|
|
- </Breadcrumb.Item>)
|
|
|
- )
|
|
|
- }
|
|
|
- </Breadcrumb>
|
|
|
- );
|
|
|
- } else if (routes && params) {
|
|
|
- breadcrumb = (
|
|
|
- <Breadcrumb
|
|
|
- className={styles.breadcrumb}
|
|
|
- routes={routes.filter(route => route.breadcrumbName)}
|
|
|
- params={params}
|
|
|
- itemRender={this.itemRender}
|
|
|
- />
|
|
|
- );
|
|
|
- } else if (location && location.pathname) {
|
|
|
- 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 currentBreadcrumb.name && !currentBreadcrumb.hideInBreadcrumb ? (
|
|
|
- <Breadcrumb.Item key={url}>
|
|
|
- {createElement(
|
|
|
- isLinkable ? linkElement : 'span',
|
|
|
- { [linkElement === 'a' ? 'href' : 'to']: url },
|
|
|
- currentBreadcrumb.name,
|
|
|
- )}
|
|
|
- </Breadcrumb.Item>
|
|
|
- ) : null;
|
|
|
- });
|
|
|
- const breadcrumbItems = [(
|
|
|
- <Breadcrumb.Item key="home">
|
|
|
- {createElement(linkElement, {
|
|
|
- [linkElement === 'a' ? 'href' : 'to']: '/',
|
|
|
- }, '首页')}
|
|
|
- </Breadcrumb.Item>
|
|
|
- )].concat(extraBreadcrumbItems);
|
|
|
- breadcrumb = (
|
|
|
- <Breadcrumb className={styles.breadcrumb}>
|
|
|
- {breadcrumbItems}
|
|
|
- </Breadcrumb>
|
|
|
- );
|
|
|
- } else {
|
|
|
- breadcrumb = null;
|
|
|
- }
|
|
|
|
|
|
let tabDefaultValue;
|
|
|
if (tabActiveKey !== undefined && tabList) {
|
|
|
tabDefaultValue = tabList.filter(item => item.default)[0] || tabList[0];
|
|
|
}
|
|
|
-
|
|
|
+ const breadcrumb = this.conversionBreadcrumbList();
|
|
|
const activeKeyProps = {
|
|
|
defaultActiveKey: tabDefaultValue && tabDefaultValue.key,
|
|
|
};
|