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
|
|
|
|
|
|
|
import './Breadcrumbs.css';
|
|
|
|
|
|
|
|
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 (
|
|
|
|
<div className="breadcrumb-links">
|
2022-07-26 19:44:08 +00:00
|
|
|
{breadcrumbsArray.map((crumb, index) => (
|
2021-12-30 15:46:12 +00:00
|
|
|
<Fragment key={index}>
|
|
|
|
{renderCrumb(crumb)}
|
2022-07-26 19:44:08 +00:00
|
|
|
{index !== breadcrumbsArray.length - 1 ? ' > ' : ''}
|
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}
|
|
|
|
className="text-blue-9 hover:underline"
|
|
|
|
>
|
2021-12-30 15:46:12 +00:00
|
|
|
{crumb.label}
|
|
|
|
</Link>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return crumb.label;
|
|
|
|
}
|