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';
|
2022-06-22 14:57:09 +08:00
|
|
|
import * as React from 'react';
|
2020-05-28 15:22:00 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2022-11-15 15:10:47 +08:00
|
|
|
import type { DropdownProps } from '../dropdown';
|
2022-06-22 14:57:09 +08:00
|
|
|
import Menu from '../menu';
|
2020-05-14 20:54:49 +08:00
|
|
|
import { cloneElement } from '../_util/reactNode';
|
2022-06-22 14:57:09 +08:00
|
|
|
import warning from '../_util/warning';
|
2022-11-15 15:10:47 +08:00
|
|
|
import type { BreadcrumbItemProps } from './BreadcrumbItem';
|
2022-06-22 14:57:09 +08:00
|
|
|
import BreadcrumbItem from './BreadcrumbItem';
|
|
|
|
import BreadcrumbSeparator from './BreadcrumbSeparator';
|
2016-03-31 17:46:35 +08:00
|
|
|
|
2022-05-24 01:13:36 +08:00
|
|
|
import useStyle from './style';
|
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;
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName?: string;
|
2022-04-08 22:55:42 +08:00
|
|
|
children?: React.ReactNode;
|
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(/^\//, '');
|
2022-11-15 15:10:47 +08:00
|
|
|
Object.keys(params).forEach((key) => {
|
2020-05-28 15:22:00 +08:00
|
|
|
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
|
|
|
|
2022-11-19 16:56:23 +08:00
|
|
|
type CompoundedComponent = React.FC<BreadcrumbProps> & {
|
2020-05-28 15:22:00 +08:00
|
|
|
Item: typeof BreadcrumbItem;
|
|
|
|
Separator: typeof BreadcrumbSeparator;
|
2022-11-19 16:56:23 +08:00
|
|
|
};
|
2016-03-31 17:46:35 +08:00
|
|
|
|
2022-11-19 16:56:23 +08:00
|
|
|
const Breadcrumb: CompoundedComponent = ({
|
2020-05-28 15:22:00 +08:00
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
separator = '/',
|
|
|
|
style,
|
|
|
|
className,
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName,
|
2020-05-28 15:22:00 +08:00
|
|
|
routes,
|
|
|
|
children,
|
|
|
|
itemRender = defaultItemRender,
|
|
|
|
params = {},
|
|
|
|
...restProps
|
|
|
|
}) => {
|
|
|
|
const { getPrefixCls, direction } = React.useContext(ConfigContext);
|
|
|
|
|
2022-10-14 11:37:48 +08:00
|
|
|
let crumbs: React.ReactNode;
|
2020-05-28 15:22:00 +08:00
|
|
|
const prefixCls = getPrefixCls('breadcrumb', customizePrefixCls);
|
2022-05-24 01:13:36 +08:00
|
|
|
const [wrapSSR, hashId] = useStyle(prefixCls);
|
|
|
|
|
2020-05-28 15:22:00 +08:00
|
|
|
if (routes && routes.length > 0) {
|
|
|
|
// generated by route
|
2019-05-06 12:04:39 +08:00
|
|
|
const paths: string[] = [];
|
2022-11-15 15:10:47 +08:00
|
|
|
crumbs = routes.map((route) => {
|
2020-05-28 15:22:00 +08:00
|
|
|
const path = getPath(route.path, params);
|
2019-05-06 12:04:39 +08:00
|
|
|
|
|
|
|
if (path) {
|
|
|
|
paths.push(path);
|
|
|
|
}
|
|
|
|
// generated overlay by route.children
|
2022-11-15 15:10:47 +08:00
|
|
|
let overlay: DropdownProps['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
|
2022-11-15 15:10:47 +08:00
|
|
|
items={route.children.map((child) => ({
|
2022-03-18 15:20:35 +08:00
|
|
|
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
|
|
|
|
2022-11-15 15:10:47 +08:00
|
|
|
const itemProps: BreadcrumbItemProps = { separator };
|
|
|
|
|
|
|
|
if (overlay) {
|
|
|
|
itemProps.overlay = overlay;
|
|
|
|
}
|
|
|
|
|
2019-05-06 12:04:39 +08:00
|
|
|
return (
|
2022-11-15 15:10:47 +08:00
|
|
|
<BreadcrumbItem {...itemProps} 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) {
|
2023-02-24 18:20:28 +08:00
|
|
|
const childrenLength = toArray(children).length;
|
2020-05-28 15:22:00 +08:00
|
|
|
crumbs = toArray(children).map((element: any, index) => {
|
|
|
|
if (!element) {
|
|
|
|
return element;
|
|
|
|
}
|
|
|
|
|
2022-05-10 15:43:29 +08:00
|
|
|
warning(
|
2020-05-28 15:22:00 +08:00
|
|
|
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",
|
|
|
|
);
|
2023-02-24 18:20:28 +08:00
|
|
|
const isLastItem = index === childrenLength - 1;
|
2020-05-28 15:22:00 +08:00
|
|
|
return cloneElement(element, {
|
2023-02-24 18:20:28 +08:00
|
|
|
separator: isLastItem ? '' : separator,
|
2020-05-28 15:22:00 +08:00
|
|
|
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,
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName,
|
2022-05-24 01:13:36 +08:00
|
|
|
hashId,
|
2020-09-06 13:07:39 +08:00
|
|
|
);
|
2020-05-28 15:22:00 +08:00
|
|
|
|
2022-05-24 01:13:36 +08:00
|
|
|
return wrapSSR(
|
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-05-24 01:13:36 +08:00
|
|
|
</nav>,
|
2020-05-28 15:22:00 +08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
Breadcrumb.Item = BreadcrumbItem;
|
|
|
|
Breadcrumb.Separator = BreadcrumbSeparator;
|
|
|
|
|
2023-01-08 21:30:41 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
Breadcrumb.displayName = 'Breadcrumb';
|
|
|
|
}
|
|
|
|
|
2020-05-28 15:22:00 +08:00
|
|
|
export default Breadcrumb;
|