2022-04-26 08:48:17 +00:00
|
|
|
import { AdminRequired } from "components/ui/AdminRequired";
|
2022-05-17 20:43:27 +00:00
|
|
|
import noop from "lodash/noop";
|
2021-09-29 21:33:18 +00:00
|
|
|
import Link, { LinkProps } from "next/link";
|
|
|
|
import { useRouter } from "next/router";
|
2022-05-17 19:31:49 +00:00
|
|
|
import { FC, Fragment, MouseEventHandler } from "react";
|
2021-09-29 21:33:18 +00:00
|
|
|
|
2022-05-26 17:07:14 +00:00
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
|
|
|
|
2021-09-29 21:33:18 +00:00
|
|
|
import classNames from "@lib/classNames";
|
2022-05-17 19:31:49 +00:00
|
|
|
import { SVGComponent } from "@lib/types/SVGComponent";
|
2021-09-29 21:33:18 +00:00
|
|
|
|
2022-04-26 08:48:17 +00:00
|
|
|
export interface NavTabProps {
|
2021-09-29 21:33:18 +00:00
|
|
|
tabs: {
|
|
|
|
name: string;
|
2022-05-05 14:29:49 +00:00
|
|
|
/** If you want to change the path as per current tab */
|
|
|
|
href?: string;
|
|
|
|
/** If you want to change query param tabName as per current tab */
|
|
|
|
tabName?: string;
|
2022-05-17 19:31:49 +00:00
|
|
|
icon?: SVGComponent;
|
2022-04-26 08:48:17 +00:00
|
|
|
adminRequired?: boolean;
|
2022-07-14 12:40:53 +00:00
|
|
|
className?: string;
|
2021-09-29 21:33:18 +00:00
|
|
|
}[];
|
|
|
|
linkProps?: Omit<LinkProps, "href">;
|
|
|
|
}
|
|
|
|
|
2022-05-05 14:29:49 +00:00
|
|
|
const NavTabs: FC<NavTabProps> = ({ tabs, linkProps, ...props }) => {
|
2021-09-29 21:33:18 +00:00
|
|
|
const router = useRouter();
|
2022-05-26 17:07:14 +00:00
|
|
|
const { t } = useLocale();
|
2021-09-29 21:33:18 +00:00
|
|
|
return (
|
2021-09-30 19:59:34 +00:00
|
|
|
<>
|
2022-05-05 14:29:49 +00:00
|
|
|
<nav
|
2022-06-09 10:46:03 +00:00
|
|
|
className="no-scrollbar -mb-px flex space-x-5 overflow-x-scroll rtl:space-x-reverse sm:rtl:space-x-reverse"
|
2022-05-05 14:29:49 +00:00
|
|
|
aria-label="Tabs"
|
|
|
|
{...props}>
|
2021-09-30 19:59:34 +00:00
|
|
|
{tabs.map((tab) => {
|
2022-05-05 14:29:49 +00:00
|
|
|
if ((tab.tabName && tab.href) || (!tab.tabName && !tab.href)) {
|
|
|
|
throw new Error("Use either tabName or href");
|
|
|
|
}
|
2022-05-17 20:43:27 +00:00
|
|
|
let href = "";
|
|
|
|
let isCurrent;
|
2022-05-05 14:29:49 +00:00
|
|
|
if (tab.href) {
|
|
|
|
href = tab.href;
|
|
|
|
isCurrent = router.asPath === tab.href;
|
|
|
|
} else if (tab.tabName) {
|
|
|
|
href = "";
|
|
|
|
isCurrent = router.query.tabName === tab.tabName;
|
|
|
|
}
|
2022-05-17 20:43:27 +00:00
|
|
|
|
2022-05-05 14:29:49 +00:00
|
|
|
const onClick: MouseEventHandler = tab.tabName
|
|
|
|
? (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
router.push({
|
|
|
|
query: {
|
|
|
|
...router.query,
|
|
|
|
tabName: tab.tabName,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2022-05-17 20:43:27 +00:00
|
|
|
: noop;
|
2022-05-05 14:29:49 +00:00
|
|
|
|
2022-04-26 08:48:17 +00:00
|
|
|
const Component = tab.adminRequired ? AdminRequired : Fragment;
|
2022-07-14 12:40:53 +00:00
|
|
|
const className = tab.className || "";
|
2021-09-30 19:59:34 +00:00
|
|
|
return (
|
2022-04-26 08:48:17 +00:00
|
|
|
<Component key={tab.name}>
|
2023-01-06 12:13:56 +00:00
|
|
|
<Link key={tab.name} href={href} {...linkProps} legacyBehavior>
|
2022-04-26 08:48:17 +00:00
|
|
|
<a
|
2022-05-05 14:29:49 +00:00
|
|
|
onClick={onClick}
|
2022-04-26 08:48:17 +00:00
|
|
|
className={classNames(
|
|
|
|
isCurrent
|
2023-01-12 16:57:43 +00:00
|
|
|
? "border-neutral-900 text-gray-900"
|
2022-04-26 08:48:17 +00:00
|
|
|
: "border-transparent text-gray-500 hover:border-gray-300 hover:text-gray-700",
|
2022-07-14 12:40:53 +00:00
|
|
|
"group inline-flex items-center border-b-2 py-4 px-1 text-sm font-medium",
|
|
|
|
className
|
2022-04-26 08:48:17 +00:00
|
|
|
)}
|
|
|
|
aria-current={isCurrent ? "page" : undefined}>
|
|
|
|
{tab.icon && (
|
|
|
|
<tab.icon
|
|
|
|
className={classNames(
|
2023-01-12 16:57:43 +00:00
|
|
|
isCurrent ? "text-gray-900" : "text-gray-400 group-hover:text-gray-500",
|
2022-07-27 02:24:00 +00:00
|
|
|
"-ml-0.5 hidden h-4 w-4 ltr:mr-2 rtl:ml-2 sm:inline-block"
|
2022-04-26 08:48:17 +00:00
|
|
|
)}
|
|
|
|
aria-hidden="true"
|
|
|
|
/>
|
|
|
|
)}
|
2022-05-26 17:07:14 +00:00
|
|
|
<span>{t(tab.name)}</span>
|
2022-04-26 08:48:17 +00:00
|
|
|
</a>
|
|
|
|
</Link>
|
|
|
|
</Component>
|
2021-09-30 19:59:34 +00:00
|
|
|
);
|
|
|
|
})}
|
|
|
|
</nav>
|
|
|
|
<hr />
|
|
|
|
</>
|
2021-09-29 21:33:18 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default NavTabs;
|