| 1 |
import React from "react"; |
| 2 |
// PatternFly |
| 3 |
import { Breadcrumb, BreadcrumbItem } from "@patternfly/react-core"; |
| 4 |
// Redux |
| 5 |
import { useAppSelector } from "src/store/hooks"; |
| 6 |
|
| 7 |
export interface BreadCrumbItem { |
| 8 |
name: string; |
| 9 |
url: string; |
| 10 |
isActive?: boolean; |
| 11 |
} |
| 12 |
|
| 13 |
interface PropsToBreadcrumb { |
| 14 |
className?: string; |
| 15 |
preText?: string; |
| 16 |
breadcrumbItems?: BreadCrumbItem[]; |
| 17 |
} |
| 18 |
|
| 19 |
const BreadCrumb = (props: PropsToBreadcrumb) => { |
| 20 |
// Possibility of retrieving the 'breadcrumbItems' from the props or via Redux |
| 21 |
const pagesVisited = !props.breadcrumbItems |
| 22 |
? useAppSelector((state) => state.routes.breadCrumbPath) |
| 23 |
: props.breadcrumbItems; |
| 24 |
|
| 25 |
const breadcrumbItems = pagesVisited || []; |
| 26 |
|
| 27 |
// When rendering the elements, the last item can contain some text before the name |
| 28 |
return ( |
| 29 |
<Breadcrumb className={props.className}> |
| 30 |
{breadcrumbItems.map((page, idx) => |
| 31 |
idx === 0 || !page.isActive ? ( |
| 32 |
<BreadcrumbItem |
| 33 |
key={page.url} |
| 34 |
to={page.url} |
| 35 |
isActive={page.isActive || false} |
| 36 |
> |
| 37 |
{page.name} |
| 38 |
</BreadcrumbItem> |
| 39 |
) : ( |
| 40 |
<BreadcrumbItem key={page.url} isActive={page.isActive || false}> |
| 41 |
{props.preText && props.preText + " "} |
| 42 |
{page.name} |
| 43 |
</BreadcrumbItem> |
| 44 |
) |
| 45 |
)} |
| 46 |
</Breadcrumb> |
| 47 |
); |
| 48 |
}; |
| 49 |
|
| 50 |
export default BreadCrumb; |