2023-02-24 18:20:28 +08:00
|
|
|
import DownOutlined from '@ant-design/icons/DownOutlined';
|
2023-05-06 15:49:37 +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-04-27 22:43:42 +08:00
|
|
|
import type { ItemType } from './Breadcrumb';
|
2023-02-24 18:20:28 +08:00
|
|
|
import BreadcrumbSeparator from './BreadcrumbSeparator';
|
2023-04-27 22:43:42 +08:00
|
|
|
import { renderItem } from './useItemRender';
|
2023-09-13 22:07:33 +08:00
|
|
|
import { devUseWarning } from '../_util/warning';
|
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 {
|
2023-03-26 12:01:14 +08:00
|
|
|
key?: React.Key;
|
2023-03-05 20:57:49 +08:00
|
|
|
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-10-23 00:33:45 +08:00
|
|
|
|
2023-06-29 13:03:52 +08:00
|
|
|
export const InternalBreadcrumbItem: React.FC<BreadcrumbItemProps> = (props) => {
|
2023-04-27 22:43:42 +08:00
|
|
|
const { prefixCls, separator = '/', children, menu, overlay, dropdownProps, href } = props;
|
2022-10-23 00:33:45 +08:00
|
|
|
|
|
|
|
// Warning for deprecated usage
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
2023-09-13 22:07:33 +08:00
|
|
|
const warning = devUseWarning('Breadcrumb.Item');
|
2023-09-11 17:28:04 +08:00
|
|
|
|
2023-09-13 22:07:33 +08:00
|
|
|
warning.deprecated(!('overlay' in props), 'overlay', 'menu');
|
2022-10-23 00:33:45 +08:00
|
|
|
}
|
|
|
|
|
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) {
|
2023-03-26 12:01:14 +08:00
|
|
|
const { items, ...menuProps } = menu || {};
|
2023-03-05 20:57:49 +08:00
|
|
|
mergeDropDownProps.menu = {
|
|
|
|
...menuProps,
|
2023-03-25 14:00:20 +08:00
|
|
|
items: items?.map(({ key, title, label, path, ...itemProps }, index) => {
|
2023-03-05 20:57:49 +08:00
|
|
|
let mergedLabel: React.ReactNode = label ?? title;
|
|
|
|
|
|
|
|
if (path) {
|
|
|
|
mergedLabel = <a href={`${href}${path}`}>{mergedLabel}</a>;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
...itemProps,
|
2023-03-25 14:00:20 +08:00
|
|
|
key: key ?? index,
|
2023-03-05 20:57:49 +08:00
|
|
|
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
|
|
|
|
2020-05-28 15:22:00 +08:00
|
|
|
// wrap to dropDown
|
2023-04-27 22:43:42 +08:00
|
|
|
const link = renderBreadcrumbNode(children);
|
|
|
|
if (link !== undefined && link !== 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;
|
|
|
|
};
|
|
|
|
|
2023-06-29 13:03:52 +08:00
|
|
|
type CompoundedComponent = React.FC<BreadcrumbItemProps> & {
|
|
|
|
/** @internal */
|
|
|
|
__ANT_BREADCRUMB_ITEM: boolean;
|
|
|
|
};
|
|
|
|
|
|
|
|
const BreadcrumbItem: CompoundedComponent = (props) => {
|
2023-04-27 22:43:42 +08:00
|
|
|
const { prefixCls: customizePrefixCls, children, href, ...restProps } = props;
|
|
|
|
const { getPrefixCls } = React.useContext(ConfigContext);
|
|
|
|
const prefixCls = getPrefixCls('breadcrumb', customizePrefixCls);
|
|
|
|
return (
|
|
|
|
<InternalBreadcrumbItem {...restProps} prefixCls={prefixCls}>
|
|
|
|
{renderItem(prefixCls, restProps as ItemType, children, href)}
|
|
|
|
</InternalBreadcrumbItem>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-05-28 15:22:00 +08:00
|
|
|
BreadcrumbItem.__ANT_BREADCRUMB_ITEM = true;
|
|
|
|
|
|
|
|
export default BreadcrumbItem;
|