Track HiveGrid Cloud planning reference
This commit is contained in:
1 parent
b3d5ea6764
commit
e341c5938a
5784 files changed
+1104929
-3
No files matched your search
@@ -0,0 +1,166 @@
|
||||
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>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user