168 lines
5.2 KiB
TypeScript
168 lines
5.2 KiB
TypeScript
import { Link, useLocation, useNavigate } from "react-router";
|
|
|
|
import clsx from "clsx";
|
|
import { useEffect, useState } from "react";
|
|
import { useTranslation } from "react-i18next";
|
|
import { getBestActiveRouteIndex } from "~/utils/navigation/routeMatching";
|
|
import InputSelect from "../input/InputSelect";
|
|
|
|
export interface TabItem {
|
|
name: any;
|
|
routePath?: string;
|
|
exact?: boolean;
|
|
}
|
|
|
|
interface Props {
|
|
className?: string;
|
|
tabs: TabItem[];
|
|
asLinks?: boolean;
|
|
onSelected?: (idx: number) => void;
|
|
breakpoint?: "sm" | "md" | "lg" | "xl" | "2xl";
|
|
exact?: boolean;
|
|
selectedTab?: number;
|
|
}
|
|
|
|
export default function Tabs({ className = "", breakpoint = "md", tabs = [], asLinks = true, onSelected, exact: _exact, selectedTab = 0 }: Props) {
|
|
const { t } = useTranslation();
|
|
|
|
const navigate = useNavigate();
|
|
const location = useLocation();
|
|
|
|
const [selected, setSelected] = useState(selectedTab);
|
|
|
|
useEffect(() => {
|
|
const activeIndex = getBestActiveRouteIndex(tabs, location.pathname);
|
|
if (activeIndex >= 0) setSelected(activeIndex);
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, [tabs, location.pathname]);
|
|
|
|
function selectTab(idx: number) {
|
|
const tab = tabs[idx];
|
|
setSelected(idx);
|
|
if (asLinks) {
|
|
if (tab?.routePath) {
|
|
navigate(tab.routePath);
|
|
}
|
|
} else {
|
|
if (onSelected) {
|
|
onSelected(idx);
|
|
}
|
|
}
|
|
}
|
|
function isCurrent(idx: number) {
|
|
return currentTab() === tabs[idx];
|
|
}
|
|
const currentTab = () => {
|
|
if (asLinks) {
|
|
const activeIndex = getBestActiveRouteIndex(tabs, location.pathname);
|
|
return activeIndex >= 0 ? tabs[activeIndex] : undefined;
|
|
} else {
|
|
return tabs[selected];
|
|
}
|
|
};
|
|
return (
|
|
<div className={className}>
|
|
<div
|
|
className={clsx(
|
|
breakpoint === "sm" && "sm:hidden",
|
|
breakpoint === "md" && "md:hidden",
|
|
breakpoint === "lg" && "lg:hidden",
|
|
breakpoint === "xl" && "xl:hidden",
|
|
breakpoint === "2xl" && "2xl:hidden"
|
|
)}
|
|
>
|
|
<InputSelect
|
|
name="tabs"
|
|
value={selected}
|
|
setValue={(e) => selectTab(Number(e))}
|
|
options={tabs.map((tab, idx) => ({
|
|
value: Number(idx),
|
|
name: tab.name,
|
|
}))}
|
|
canClear={false}
|
|
triggerAriaLabel={t("components.tabs.ariaLabel")}
|
|
highContrastText
|
|
/>
|
|
{/* <label htmlFor="tabs" className="sr-only">
|
|
{t("app.shared.tabs.select")}
|
|
</label>
|
|
<select
|
|
id="tabs"
|
|
name="tabs"
|
|
className="bg-background text-foreground focus:border-border focus:ring-ring border-border block w-full rounded-md"
|
|
onChange={(e) => selectTab(Number(e.target.value))}
|
|
value={selected}
|
|
>
|
|
{tabs.map((tab, idx) => {
|
|
return (
|
|
<option key={tab.name} value={Number(idx)}>
|
|
{tab.name}
|
|
</option>
|
|
);
|
|
})}
|
|
</select> */}
|
|
</div>
|
|
<div
|
|
className={clsx(
|
|
breakpoint === "sm" && "hidden sm:block",
|
|
breakpoint === "md" && "hidden md:block",
|
|
breakpoint === "lg" && "hidden lg:block",
|
|
breakpoint === "xl" && "hidden xl:block",
|
|
breakpoint === "2xl" && "hidden 2xl:block"
|
|
)}
|
|
>
|
|
{(() => {
|
|
if (asLinks) {
|
|
return (
|
|
<nav className="flex space-x-4" aria-label={t("components.tabs.ariaLabel")}>
|
|
{tabs
|
|
.filter((f) => f.routePath)
|
|
.map((tab, idx) => {
|
|
return (
|
|
<Link
|
|
key={tab.name}
|
|
to={tab.routePath ?? ""}
|
|
className={clsx(
|
|
"truncate",
|
|
isCurrent(idx)
|
|
? "text-text-neutral-primary bg-surface-neutral-secondary"
|
|
: "text-text-neutral-tertiary hover:text-text-neutral-primary hover:bg-surface-neutral-secondary",
|
|
"rounded-sm px-3 py-2 text-sm font-medium"
|
|
)}
|
|
>
|
|
{tab.name}
|
|
</Link>
|
|
);
|
|
})}
|
|
</nav>
|
|
);
|
|
} else {
|
|
return (
|
|
<nav className="flex space-x-4" aria-label={t("components.tabs.ariaLabel")}>
|
|
{tabs.map((tab, idx) => {
|
|
return (
|
|
<button
|
|
type="button"
|
|
key={idx}
|
|
onClick={() => selectTab(idx)}
|
|
className={clsx(
|
|
"truncate",
|
|
isCurrent(idx)
|
|
? "text-text-neutral-primary bg-surface-neutral-secondary"
|
|
: "text-text-neutral-tertiary hover:text-text-neutral-primary hover:bg-surface-neutral-secondary",
|
|
"rounded-sm px-3 py-2 text-sm font-medium"
|
|
)}
|
|
>
|
|
{tab.name}
|
|
</button>
|
|
);
|
|
})}
|
|
</nav>
|
|
);
|
|
}
|
|
})()}
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|