2023-02-24 18:20:28 +08:00
|
|
|
import DownOutlined from '@ant-design/icons/DownOutlined';
|
2023-03-05 20:57:49 +08:00
|
|
|
import * as React from 'react';
|
2022-06-22 14:57:09 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { DropdownProps } from '../dropdown/dropdown';
|
|
|
|
import Dropdown from '../dropdown/dropdown';
|
2023-03-05 20:57:49 +08:00
|
|
|
import warning from '../_util/warning';
|
2023-02-24 18:20:28 +08:00
|
|
|
import BreadcrumbSeparator from './BreadcrumbSeparator';
|
2016-03-31 17:46:35 +08:00
|
|
|
|
2023-03-05 20:57:49 +08:00
|
|
|
export interface SeparatorType {
|
2016-07-14 13:29:50 +08:00
|
|
|
separator?: React.ReactNode;
|
2023-03-05 20:57:49 +08:00
|
|
|
key?: React.Key;
|
|
|
|
}
|
|
|
|
|
2023-03-21 16:00:28 +08:00
|
|
|
type MenuType = NonNullable<DropdownProps['menu']>;
|
2023-03-05 20:57:49 +08:00
|
|
|
interface MenuItem {
|
|
|
|
title?: React.ReactNode;
|
|
|
|
label?: React.ReactNode;
|
|
|
|
path?: string;
|
2016-07-14 13:29:50 +08:00
|
|
|
href?: string;
|
2023-03-05 20:57:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface BreadcrumbItemProps extends SeparatorType {
|
|
|
|
prefixCls?: string;
|
|
|
|
href?: string;
|
|
|
|
menu?: Omit<MenuType, 'items'> & {
|
|
|
|
items?: MenuItem[];
|
|
|
|
};
|
2022-04-21 22:09:59 +08:00
|
|
|
dropdownProps?: DropdownProps;
|
2019-03-11 23:48:37 +08:00
|
|
|
onClick?: React.MouseEventHandler<HTMLAnchorElement | HTMLSpanElement>;
|
2020-12-04 09:37:04 +08:00
|
|
|
className?: string;
|
2022-04-08 22:55:42 +08:00
|
|
|
children?: React.ReactNode;
|
2022-10-23 00:33:45 +08:00
|
|
|
// Deprecated
|
|
|
|
/** @deprecated Please use `menu` instead */
|
|
|
|
overlay?: DropdownProps['overlay'];
|
2016-07-14 13:29:50 +08:00
|
|
|
}
|
2022-12-01 14:33:51 +08:00
|
|
|
type CompoundedComponent = React.FC<BreadcrumbItemProps> & {
|
2020-05-28 15:22:00 +08:00
|
|
|
__ANT_BREADCRUMB_ITEM: boolean;
|
2022-12-01 14:33:51 +08:00
|
|
|
};
|
2023-02-24 18:20:28 +08:00
|
|
|
const BreadcrumbItem: CompoundedComponent = (props: BreadcrumbItemProps) => {
|
2022-10-23 00:33:45 +08:00
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
separator = '/',
|
|
|
|
children,
|
|
|
|
menu,
|
|
|
|
overlay,
|
|
|
|
dropdownProps,
|
2023-03-05 20:57:49 +08:00
|
|
|
href,
|
2022-10-23 00:33:45 +08:00
|
|
|
...restProps
|
|
|
|
} = props;
|
|
|
|
|
2020-05-28 15:22:00 +08:00
|
|
|
const { getPrefixCls } = React.useContext(ConfigContext);
|
|
|
|
const prefixCls = getPrefixCls('breadcrumb', customizePrefixCls);
|
2022-10-23 00:33:45 +08:00
|
|
|
|
|
|
|
// Warning for deprecated usage
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
warning(
|
|
|
|
!('overlay' in props),
|
|
|
|
'Breadcrumb.Item',
|
|
|
|
'`overlay` is deprecated. Please use `menu` instead.',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-04-21 22:09:59 +08:00
|
|
|
/** If overlay is have Wrap a Dropdown */
|
2020-05-28 15:22:00 +08:00
|
|
|
const renderBreadcrumbNode = (breadcrumbItem: React.ReactNode) => {
|
2022-10-23 00:33:45 +08:00
|
|
|
if (menu || overlay) {
|
2023-01-18 15:39:00 +08:00
|
|
|
const mergeDropDownProps: DropdownProps = {
|
|
|
|
...dropdownProps,
|
|
|
|
};
|
2023-03-05 20:57:49 +08:00
|
|
|
|
|
|
|
if (menu) {
|
|
|
|
const { items, ...menuProps } = menu! || {};
|
|
|
|
mergeDropDownProps.menu = {
|
|
|
|
...menuProps,
|
|
|
|
items: items?.map(({ title, label, path, ...itemProps }, index) => {
|
|
|
|
let mergedLabel: React.ReactNode = label ?? title;
|
|
|
|
|
|
|
|
if (path) {
|
|
|
|
mergedLabel = <a href={`${href}${path}`}>{mergedLabel}</a>;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
...itemProps,
|
|
|
|
key: index,
|
|
|
|
label: mergedLabel as string,
|
|
|
|
};
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
} else if (overlay) {
|
2023-01-18 15:39:00 +08:00
|
|
|
mergeDropDownProps.overlay = overlay;
|
|
|
|
}
|
2023-03-05 20:57:49 +08:00
|
|
|
|
2019-05-06 12:04:39 +08:00
|
|
|
return (
|
2023-03-05 20:57:49 +08:00
|
|
|
<Dropdown placement="bottom" {...mergeDropDownProps}>
|
2019-06-03 16:13:15 +08:00
|
|
|
<span className={`${prefixCls}-overlay-link`}>
|
2019-05-06 12:04:39 +08:00
|
|
|
{breadcrumbItem}
|
2019-11-28 12:34:33 +08:00
|
|
|
<DownOutlined />
|
2019-06-03 16:13:15 +08:00
|
|
|
</span>
|
2022-04-21 22:09:59 +08:00
|
|
|
</Dropdown>
|
2019-05-06 12:04:39 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return breadcrumbItem;
|
|
|
|
};
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2022-10-14 11:37:48 +08:00
|
|
|
let link: React.ReactNode;
|
2023-03-05 20:57:49 +08:00
|
|
|
if (href !== undefined) {
|
2020-05-28 15:22:00 +08:00
|
|
|
link = (
|
2023-03-05 20:57:49 +08:00
|
|
|
<a className={`${prefixCls}-link`} href={href} {...restProps}>
|
2020-05-28 15:22:00 +08:00
|
|
|
{children}
|
|
|
|
</a>
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
link = (
|
|
|
|
<span className={`${prefixCls}-link`} {...restProps}>
|
|
|
|
{children}
|
|
|
|
</span>
|
|
|
|
);
|
2018-12-05 19:12:18 +08:00
|
|
|
}
|
2020-05-28 15:22:00 +08:00
|
|
|
|
|
|
|
// wrap to dropDown
|
|
|
|
link = renderBreadcrumbNode(link);
|
2022-10-14 11:37:48 +08:00
|
|
|
if (children !== undefined && children !== null) {
|
2020-05-28 15:22:00 +08:00
|
|
|
return (
|
2023-02-24 18:20:28 +08:00
|
|
|
<>
|
|
|
|
<li>{link}</li>
|
|
|
|
{separator && <BreadcrumbSeparator>{separator}</BreadcrumbSeparator>}
|
|
|
|
</>
|
2020-05-28 15:22:00 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
BreadcrumbItem.__ANT_BREADCRUMB_ITEM = true;
|
|
|
|
|
|
|
|
export default BreadcrumbItem;
|