-
Notifications
You must be signed in to change notification settings - Fork 540
/
Copy pathBreadcrumbs.tsx
174 lines (163 loc) · 5.07 KB
/
Breadcrumbs.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import { Link, usePath } from "raviger";
import { useState } from "react";
import CareIcon from "@/CAREUI/icons/CareIcon";
import {
Breadcrumb,
BreadcrumbItem,
BreadcrumbList,
} from "@/components/ui/breadcrumb";
import { Button } from "@/components/ui/button";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import useAppHistory from "@/hooks/useAppHistory";
import { classNames } from "@/Utils/utils";
const MENU_TAGS: { [key: string]: string } = {
facility: "Facilities",
patients: "Patients",
assets: "Assets",
shifting: "Shiftings",
resource: "Resources",
users: "Users",
notice_board: "Notice Board",
};
const capitalize = (string: string) =>
string
.replace(/[_-]/g, " ")
.split(" ")
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
.join(" ");
interface BreadcrumbsProps {
replacements?: {
[key: string]: {
name?: string;
uri?: string;
style?: string;
hide?: boolean;
};
};
className?: string;
hideBack?: boolean;
backUrl?: string;
onBackClick?: () => boolean | void;
}
export default function Breadcrumbs({
replacements = {},
className = "",
hideBack = false,
backUrl,
onBackClick,
}: BreadcrumbsProps) {
const { goBack } = useAppHistory();
const path = usePath();
const [showFullPath, setShowFullPath] = useState(false);
const crumbs = path
?.slice(1)
.split("/")
.filter((field) => replacements[field]?.hide !== true)
.map((field, i) => ({
name: replacements[field]?.name || MENU_TAGS[field] || capitalize(field),
uri:
replacements[field]?.uri ||
path
.split("/")
.slice(0, i + 2)
.join("/"),
style: replacements[field]?.style || "",
}));
const renderCrumb = (crumb: any, index: number) => {
const isLastItem = index === crumbs!.length - 1;
return (
<BreadcrumbItem
key={crumb.name}
className={classNames("text-sm font-normal", crumb.style)}
>
<div className="flex items-center">
{!isLastItem ? (
<Button
asChild
variant="link"
className="p-1 font-normal text-gray-800 hover:text-gray-700"
>
<Link href={crumb.uri}>{crumb.name}</Link>
</Button>
) : (
<span className="text-gray-600">{crumb.name}</span>
)}
</div>
</BreadcrumbItem>
);
};
return (
<Breadcrumb>
<nav className={classNames("w-full", className)} aria-label="Breadcrumb">
<BreadcrumbList>
{!hideBack && (
<BreadcrumbItem>
<Button
variant="link"
type="button"
className="rounded bg-gray-200/50 px-1 text-sm font-normal text-gray-800 transition hover:bg-gray-200/75 hover:no-underline"
size="xs"
onClick={() => {
if (onBackClick && onBackClick() === false) return;
goBack(backUrl);
}}
>
<CareIcon icon="l-arrow-left" className="h-5 text-gray-700" />
<span className="pr-2">Back</span>
</Button>
</BreadcrumbItem>
)}
<BreadcrumbItem>
<Button
asChild
variant="link"
className="p-1 font-normal text-gray-800 hover:text-gray-700"
>
<Link href="/">Home</Link>
</Button>
</BreadcrumbItem>
{crumbs && crumbs.length > 1 && (
<>
{!showFullPath && (
<BreadcrumbItem>
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button
variant="link"
className="h-auto p-0 font-light text-gray-500 hover:text-gray-700"
onClick={() => setShowFullPath(true)}
>
•••
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="start">
{crumbs.slice(0, -1).map((crumb, index) => (
<DropdownMenuItem key={index}>
<Button
asChild
variant="link"
className="p-1 font-normal text-gray-800 underline underline-offset-2 hover:text-gray-700"
>
<Link href={crumb.uri}>{crumb.name}</Link>
</Button>
</DropdownMenuItem>
))}
</DropdownMenuContent>
</DropdownMenu>
</BreadcrumbItem>
)}
{showFullPath && crumbs.slice(0, -1).map(renderCrumb)}
</>
)}
{crumbs?.length &&
renderCrumb(crumbs[crumbs.length - 1], crumbs.length - 1)}
</BreadcrumbList>
</nav>
</Breadcrumb>
);
}