2021-12-30 15:46:12 +00:00
|
|
|
import { Fragment } from 'react';
|
|
|
|
|
2022-06-17 16:18:42 +00:00
|
|
|
import { Link } from '@@/Link';
|
2021-12-30 15:46:12 +00:00
|
|
|
|
|
|
|
export interface Crumb {
|
|
|
|
label: string;
|
|
|
|
link?: string;
|
|
|
|
linkParams?: Record<string, unknown>;
|
|
|
|
}
|
|
|
|
interface Props {
|
2022-07-26 19:44:08 +00:00
|
|
|
breadcrumbs: (Crumb | string)[] | string;
|
2021-12-30 15:46:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function Breadcrumbs({ breadcrumbs }: Props) {
|
2022-07-26 19:44:08 +00:00
|
|
|
const breadcrumbsArray = Array.isArray(breadcrumbs)
|
|
|
|
? breadcrumbs
|
|
|
|
: [breadcrumbs];
|
|
|
|
|
2021-12-30 15:46:12 +00:00
|
|
|
return (
|
2022-08-16 15:08:08 +00:00
|
|
|
<div className="text-xs font-medium text-gray-7 th-dark:text-gray-5 th-highcontrast:text-white space-x-2">
|
2022-07-26 19:44:08 +00:00
|
|
|
{breadcrumbsArray.map((crumb, index) => (
|
2021-12-30 15:46:12 +00:00
|
|
|
<Fragment key={index}>
|
2022-08-12 03:47:56 +00:00
|
|
|
<span>{renderCrumb(crumb)}</span>
|
|
|
|
{index !== breadcrumbsArray.length - 1 && <span>></span>}
|
2021-12-30 15:46:12 +00:00
|
|
|
</Fragment>
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-06-23 06:32:18 +00:00
|
|
|
function renderCrumb(crumb: Crumb | string) {
|
2022-07-26 15:50:49 +00:00
|
|
|
if (!crumb) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2022-06-23 06:32:18 +00:00
|
|
|
if (typeof crumb === 'string') {
|
|
|
|
return crumb;
|
|
|
|
}
|
|
|
|
|
2021-12-30 15:46:12 +00:00
|
|
|
if (crumb.link) {
|
|
|
|
return (
|
2022-07-27 21:53:19 +00:00
|
|
|
<Link
|
|
|
|
to={crumb.link}
|
|
|
|
params={crumb.linkParams}
|
2022-08-12 03:47:56 +00:00
|
|
|
className="text-blue-9 hover:underline hover:text-blue-11 th-dark:text-blue-7 th-dark:hover:text-blue-9 th-highcontrast:text-blue-5"
|
2022-07-27 21:53:19 +00:00
|
|
|
>
|
2021-12-30 15:46:12 +00:00
|
|
|
{crumb.label}
|
|
|
|
</Link>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return crumb.label;
|
|
|
|
}
|