2019-08-15 09:28:15 +08:00
|
|
|
import * as React from 'react';
|
2016-12-30 21:41:28 +08:00
|
|
|
import classNames from 'classnames';
|
2019-08-20 15:58:37 +08:00
|
|
|
import toArray from 'rc-util/lib/Children/toArray';
|
2018-12-05 19:12:18 +08:00
|
|
|
import BreadcrumbItem from './BreadcrumbItem';
|
2019-07-31 10:47:34 +08:00
|
|
|
import BreadcrumbSeparator from './BreadcrumbSeparator';
|
2019-05-06 12:04:39 +08:00
|
|
|
import Menu from '../menu';
|
2020-05-28 15:22:00 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2020-05-14 15:57:04 +08:00
|
|
|
import devWarning from '../_util/devWarning';
|
2020-05-14 20:54:49 +08:00
|
|
|
import { cloneElement } from '../_util/reactNode';
|
2016-03-31 17:46:35 +08:00
|
|
|
|
2017-11-22 11:10:33 +08:00
|
|
|
export interface Route {
|
|
|
|
path: string;
|
|
|
|
breadcrumbName: string;
|
2019-05-13 17:54:18 +08:00
|
|
|
children?: Omit<Route, 'children'>[];
|
2017-11-22 11:10:33 +08:00
|
|
|
}
|
|
|
|
|
2016-08-29 16:52:35 +08:00
|
|
|
export interface BreadcrumbProps {
|
|
|
|
prefixCls?: string;
|
2017-11-22 11:10:33 +08:00
|
|
|
routes?: Route[];
|
2019-06-24 11:29:58 +08:00
|
|
|
params?: any;
|
2017-01-20 20:10:50 +08:00
|
|
|
separator?: React.ReactNode;
|
2018-12-07 20:02:01 +08:00
|
|
|
itemRender?: (
|
2019-06-16 20:51:47 +08:00
|
|
|
route: Route,
|
2019-06-24 11:29:58 +08:00
|
|
|
params: any,
|
2019-06-16 20:51:47 +08:00
|
|
|
routes: Array<Route>,
|
2018-12-07 20:02:01 +08:00
|
|
|
paths: Array<string>,
|
|
|
|
) => React.ReactNode;
|
2016-08-29 16:52:35 +08:00
|
|
|
style?: React.CSSProperties;
|
2016-12-30 21:41:28 +08:00
|
|
|
className?: string;
|
2017-05-25 16:54:15 +08:00
|
|
|
}
|
2016-08-29 16:52:35 +08:00
|
|
|
|
2017-11-22 11:10:33 +08:00
|
|
|
function getBreadcrumbName(route: Route, params: any) {
|
2016-08-29 16:52:35 +08:00
|
|
|
if (!route.breadcrumbName) {
|
2016-06-10 21:12:52 +08:00
|
|
|
return null;
|
|
|
|
}
|
2016-06-11 17:05:19 +08:00
|
|
|
const paramsKeys = Object.keys(params).join('|');
|
2016-08-29 16:52:35 +08:00
|
|
|
const name = route.breadcrumbName.replace(
|
2016-06-11 17:05:19 +08:00
|
|
|
new RegExp(`:(${paramsKeys})`, 'g'),
|
2017-03-23 21:15:49 +08:00
|
|
|
(replacement, key) => params[key] || replacement,
|
2016-06-11 16:11:08 +08:00
|
|
|
);
|
2016-08-29 16:52:35 +08:00
|
|
|
return name;
|
2016-07-14 13:29:50 +08:00
|
|
|
}
|
|
|
|
|
2017-11-22 11:10:33 +08:00
|
|
|
function defaultItemRender(route: Route, params: any, routes: Route[], paths: string[]) {
|
2016-10-24 12:04:26 +08:00
|
|
|
const isLastItem = routes.indexOf(route) === routes.length - 1;
|
|
|
|
const name = getBreadcrumbName(route, params);
|
2018-12-07 20:02:01 +08:00
|
|
|
return isLastItem ? <span>{name}</span> : <a href={`#/${paths.join('/')}`}>{name}</a>;
|
2016-10-24 12:04:26 +08:00
|
|
|
}
|
|
|
|
|
2020-05-28 15:22:00 +08:00
|
|
|
const getPath = (path: string, params: any) => {
|
|
|
|
path = (path || '').replace(/^\//, '');
|
|
|
|
Object.keys(params).forEach(key => {
|
|
|
|
path = path.replace(`:${key}`, params[key]);
|
|
|
|
});
|
|
|
|
return path;
|
|
|
|
};
|
|
|
|
|
2021-11-26 12:18:21 +08:00
|
|
|
const addChildPath = (paths: string[], childPath: string, params: any) => {
|
2020-05-28 15:22:00 +08:00
|
|
|
const originalPaths = [...paths];
|
2021-11-26 12:18:21 +08:00
|
|
|
const path = getPath(childPath || '', params);
|
2020-05-28 15:22:00 +08:00
|
|
|
if (path) {
|
|
|
|
originalPaths.push(path);
|
|
|
|
}
|
|
|
|
return originalPaths;
|
|
|
|
};
|
2016-07-14 13:29:50 +08:00
|
|
|
|
2020-05-28 15:22:00 +08:00
|
|
|
interface BreadcrumbInterface extends React.FC<BreadcrumbProps> {
|
|
|
|
Item: typeof BreadcrumbItem;
|
|
|
|
Separator: typeof BreadcrumbSeparator;
|
|
|
|
}
|
2016-03-31 17:46:35 +08:00
|
|
|
|
2020-05-28 15:22:00 +08:00
|
|
|
const Breadcrumb: BreadcrumbInterface = ({
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
separator = '/',
|
|
|
|
style,
|
|
|
|
className,
|
|
|
|
routes,
|
|
|
|
children,
|
|
|
|
itemRender = defaultItemRender,
|
|
|
|
params = {},
|
|
|
|
...restProps
|
|
|
|
}) => {
|
|
|
|
const { getPrefixCls, direction } = React.useContext(ConfigContext);
|
|
|
|
|
|
|
|
let crumbs;
|
|
|
|
const prefixCls = getPrefixCls('breadcrumb', customizePrefixCls);
|
|
|
|
if (routes && routes.length > 0) {
|
|
|
|
// generated by route
|
2019-05-06 12:04:39 +08:00
|
|
|
const paths: string[] = [];
|
2020-05-28 15:22:00 +08:00
|
|
|
crumbs = routes.map(route => {
|
|
|
|
const path = getPath(route.path, params);
|
2019-05-06 12:04:39 +08:00
|
|
|
|
|
|
|
if (path) {
|
|
|
|
paths.push(path);
|
|
|
|
}
|
|
|
|
// generated overlay by route.children
|
2019-11-06 14:49:20 +08:00
|
|
|
let overlay;
|
2019-05-06 12:04:39 +08:00
|
|
|
if (route.children && route.children.length) {
|
|
|
|
overlay = (
|
2022-03-18 15:20:35 +08:00
|
|
|
<Menu
|
|
|
|
items={route.children.map(child => ({
|
|
|
|
key: child.path || child.breadcrumbName,
|
|
|
|
label: itemRender(child, params, routes, addChildPath(paths, child.path, params)),
|
|
|
|
}))}
|
|
|
|
/>
|
2019-05-06 12:04:39 +08:00
|
|
|
);
|
|
|
|
}
|
2016-09-09 13:55:46 +08:00
|
|
|
|
2019-05-06 12:04:39 +08:00
|
|
|
return (
|
2020-02-27 11:00:57 +08:00
|
|
|
<BreadcrumbItem overlay={overlay} separator={separator} key={path || route.breadcrumbName}>
|
2019-05-06 12:04:39 +08:00
|
|
|
{itemRender(route, params, routes, paths)}
|
|
|
|
</BreadcrumbItem>
|
|
|
|
);
|
|
|
|
});
|
2020-05-28 15:22:00 +08:00
|
|
|
} else if (children) {
|
|
|
|
crumbs = toArray(children).map((element: any, index) => {
|
|
|
|
if (!element) {
|
|
|
|
return element;
|
|
|
|
}
|
|
|
|
|
|
|
|
devWarning(
|
|
|
|
element.type &&
|
|
|
|
(element.type.__ANT_BREADCRUMB_ITEM === true ||
|
|
|
|
element.type.__ANT_BREADCRUMB_SEPARATOR === true),
|
|
|
|
'Breadcrumb',
|
|
|
|
"Only accepts Breadcrumb.Item and Breadcrumb.Separator as it's children",
|
|
|
|
);
|
2019-07-31 10:47:34 +08:00
|
|
|
|
2020-05-28 15:22:00 +08:00
|
|
|
return cloneElement(element, {
|
|
|
|
separator,
|
|
|
|
key: index,
|
2016-03-31 17:46:35 +08:00
|
|
|
});
|
2020-01-21 22:01:53 +08:00
|
|
|
});
|
2018-12-05 19:12:18 +08:00
|
|
|
}
|
2020-05-28 15:22:00 +08:00
|
|
|
|
2020-09-06 13:07:39 +08:00
|
|
|
const breadcrumbClassName = classNames(
|
|
|
|
prefixCls,
|
|
|
|
{
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
},
|
|
|
|
className,
|
|
|
|
);
|
2020-05-28 15:22:00 +08:00
|
|
|
|
|
|
|
return (
|
2022-03-10 11:46:42 +08:00
|
|
|
<nav className={breadcrumbClassName} style={style} {...restProps}>
|
2022-03-18 15:20:35 +08:00
|
|
|
<ol>{crumbs}</ol>
|
2022-03-10 11:46:42 +08:00
|
|
|
</nav>
|
2020-05-28 15:22:00 +08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
Breadcrumb.Item = BreadcrumbItem;
|
|
|
|
|
|
|
|
Breadcrumb.Separator = BreadcrumbSeparator;
|
|
|
|
|
|
|
|
export default Breadcrumb;
|