Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/web/app/components/ui/tabs/Tabs.tsx
T

167 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 UrlUtils from "~/utils/app/UrlUtils";
import InputSelect from "../input/InputSelect";
export interface TabItem {
name: any;
routePath?: string;
}
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, selectedTab = 0 }: Props) {
const { t } = useTranslation();
const navigate = useNavigate();
const location = useLocation();
const [selected, setSelected] = useState(selectedTab);
useEffect(() => {
tabs.forEach((tab, index) => {
if (tab.routePath && (location.pathname + location.search).includes(tab.routePath)) {
setSelected(index);
}
});
// 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) {
if (true) {
return tabs.find((element) => element.routePath && UrlUtils.stripTrailingSlash(location.pathname) === UrlUtils.stripTrailingSlash(element.routePath));
} else {
return tabs.find((element) => element.routePath && (location.pathname + location.search).includes(element.routePath));
}
} 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}
/>
{/* <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>
);
}