Simplifies Vertical and Horizontal tabs

This commit is contained in:
zomars committed 2022-09-19 00:58:28 +01:00
1 parent 25711ef18e
commit 588748d78e
9 files changed
+134 -239

No files matched your search

@@ -45,10 +45,10 @@ export default function BookingLayout({
<Shell {...rest}>
<div className="flex flex-col sm:space-x-2 xl:flex-row">
<div className="hidden xl:block">
<VerticalTabs tabNameKey="tabName" tabs={tabs} sticky />
<VerticalTabs tabs={tabs} sticky />
</div>
<div className="block xl:hidden">
<HorizontalTabs tabNameKey="tabName" tabs={tabs} />
<HorizontalTabs tabs={tabs} />
</div>
<main className="w-full max-w-6xl">{children}</main>
</div>
@@ -49,10 +49,10 @@ export default function InstalledAppsLayout({
<Shell {...rest}>
<div className="flex flex-col p-2 md:p-0 xl:flex-row">
<div className="hidden xl:block">
<VerticalTabs tabs={actualTabs} sticky />
<VerticalTabs tabs={actualTabs} sticky linkProps={{ shallow: true }} />
</div>
<div className="block xl:hidden">
<HorizontalTabs tabNameKey="tabName" tabs={actualTabs} />
<HorizontalTabs tabs={actualTabs} linkProps={{ shallow: true }} />
</div>
<main className="w-full xl:mx-5 xl:w-4/5 xl:pr-5">{children}</main>
</div>
@@ -1,65 +1,28 @@
import noop from "lodash/noop";
import Link from "next/link";
import { useRouter } from "next/router";
import { MouseEventHandler } from "react";
import { ComponentProps } from "react";
import classNames from "@calcom/lib/classNames";
import { useLocale } from "@calcom/lib/hooks/useLocale";
import { SkeletonText } from "../../skeleton";
export type HorizontalTabItemProps<T extends string = "tabName"> = {
export type HorizontalTabItemProps = {
name: string;
disabled?: boolean;
className?: string;
} & (
| {
href: string;
}
| ({
href?: never;
} & Partial<Record<T, string>>)
);
href: string;
linkProps?: Omit<ComponentProps<typeof Link>, "href">;
};
const HorizontalTabItem = function <T extends string>({
name,
href,
tabNameKey,
...props
}: HorizontalTabItemProps<T> & {
tabNameKey?: T;
}) {
const router = useRouter();
const HorizontalTabItem = function ({ name, href, linkProps, ...props }: HorizontalTabItemProps) {
const { t, isLocaleReady } = useLocale();
let newHref = "";
let isCurrent;
const _tabNameKey = tabNameKey || "tabName";
const tabName = props[tabNameKey as keyof typeof props];
if (href) {
newHref = href;
isCurrent = router.asPath.startsWith(href);
} else if (tabName) {
newHref = "";
isCurrent = router.query[_tabNameKey] === tabName;
}
const onClick: MouseEventHandler = tabName
? (e) => {
e.preventDefault();
router.push({
query: {
...router.query,
[_tabNameKey]: tabName,
},
});
}
: noop;
const { asPath } = useRouter();
const isCurrent = asPath.startsWith(href);
return (
<Link key={name} href={props.disabled ? "#" : newHref}>
<Link key={name} href={href} {...linkProps}>
<a
onClick={onClick}
className={classNames(
isCurrent ? "bg-gray-200 text-gray-900" : " text-gray-600 hover:bg-gray-100 hover:text-gray-900 ",
"mb-2 inline-flex items-center justify-center whitespace-nowrap rounded-md py-[10px] px-4 text-sm font-medium leading-4 md:mb-0",
@@ -2,13 +2,12 @@ import HorizontalTabItem, { HorizontalTabItemProps } from "./HorizontalTabItem";
export { HorizontalTabItem };
export interface NavTabProps<T extends string> {
tabs: HorizontalTabItemProps<T>[];
tabNameKey?: T;
export interface NavTabProps {
tabs: HorizontalTabItemProps[];
linkProps?: HorizontalTabItemProps["linkProps"];
}
const HorizontalTabs = function <T extends string>({ tabs, tabNameKey, ...props }: NavTabProps<T>) {
const _tabNameKey = tabNameKey || "tabName";
const HorizontalTabs = function ({ tabs, linkProps, ...props }: NavTabProps) {
return (
<div className="-mx-6 mb-2 w-[calc(100%+40px)]">
<nav
@@ -16,7 +15,7 @@ const HorizontalTabs = function <T extends string>({ tabs, tabNameKey, ...props
aria-label="Tabs"
{...props}>
{tabs.map((tab, idx) => (
<HorizontalTabItem tabNameKey={_tabNameKey} {...tab} key={idx} />
<HorizontalTabItem {...tab} key={idx} {...linkProps} />
))}
</nav>
</div>
@@ -1,80 +1,46 @@
import noop from "lodash/noop";
import Link from "next/link";
import { useRouter } from "next/router";
import { Fragment, MouseEventHandler } from "react";
import { ComponentProps, Fragment } from "react";
// import { ChevronRight } from "react-feather";
import classNames from "@calcom/lib/classNames";
import { useLocale } from "@calcom/lib/hooks/useLocale";
import { SVGComponent } from "@calcom/types/SVGComponent";
import { Icon } from "@calcom/ui/Icon";
export type VerticalTabItemProps<T extends string = "tabName"> = {
export type VerticalTabItemProps = {
name: string;
info?: string;
icon?: SVGComponent;
disabled?: boolean;
children?: VerticalTabItemProps<T>[];
children?: VerticalTabItemProps[];
textClassNames?: string;
className?: string;
isChild?: boolean;
hidden?: boolean;
disableChevron?: boolean;
} & (
| {
/** If you want to change query param tabName as per current tab */
href: string;
tabName?: never;
}
| ({
href?: never;
} & Partial<Record<T, string>>)
);
href: string;
linkProps?: Omit<ComponentProps<typeof Link>, "href">;
};
const VerticalTabItem = function <T extends string>({
const VerticalTabItem = function ({
name,
href,
tabNameKey,
info,
isChild,
disableChevron,
linkProps,
...props
}: VerticalTabItemProps<T> & {
tabNameKey?: T;
}) {
const router = useRouter();
}: VerticalTabItemProps) {
const { t } = useLocale();
let newHref = "";
let isCurrent;
const tabName = props[tabNameKey as keyof typeof props] as string;
const _tabNameKey = tabNameKey || "tabName";
if (href) {
newHref = href;
isCurrent = router.asPath.startsWith(href);
} else if (tabName) {
newHref = "";
isCurrent = router.query[_tabNameKey] === tabName;
}
const onClick: MouseEventHandler = tabName
? (e) => {
e.preventDefault();
router.push({
query: {
...router.query,
[_tabNameKey]: tabName,
},
});
}
: noop;
const { asPath } = useRouter();
const isCurrent = asPath.startsWith(href);
return (
<Fragment key={name}>
{!props.hidden && (
<>
<Link key={name} href={props.disabled ? "#" : newHref}>
<Link key={name} href={href} {...linkProps}>
<a
onClick={onClick}
className={classNames(
props.textClassNames || "text-sm font-medium leading-none text-gray-600",
"group flex w-64 flex-row items-center rounded-md px-3 py-[10px] hover:bg-gray-100 group-hover:text-gray-700 [&[aria-current='page']]:bg-gray-200 [&[aria-current='page']]:text-gray-900",
@@ -86,8 +52,6 @@ const VerticalTabItem = function <T extends string>({
data-testid={`vertical-tab-${name}`}
aria-current={isCurrent ? "page" : undefined}>
{props.icon && (
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
<props.icon className="mr-[10px] h-[16px] w-[16px] self-start stroke-[2px] md:mt-0" />
)}
<div>
@@ -106,7 +70,7 @@ const VerticalTabItem = function <T extends string>({
</a>
</Link>
{props.children?.map((child) => (
<VerticalTabItem tabNameKey={tabNameKey} key={child.name} {...child} isChild />
<VerticalTabItem key={child.name} {...child} isChild />
))}
</>
)}
@@ -4,23 +4,15 @@ import VerticalTabItem, { VerticalTabItemProps } from "./VerticalTabItem";
export { VerticalTabItem };
export interface NavTabProps<T extends string> {
tabs: VerticalTabItemProps<T>[];
export interface NavTabProps {
tabs: VerticalTabItemProps[];
children?: React.ReactNode;
className?: string;
sticky?: boolean;
tabNameKey?: T;
linkProps?: VerticalTabItemProps["linkProps"];
}
const NavTabs = function <T extends string>({
tabs,
tabNameKey,
className = "",
sticky,
...props
}: NavTabProps<T>) {
const _tabNameKey = tabNameKey || "tabName";
const NavTabs = function ({ tabs, className = "", sticky, linkProps, ...props }: NavTabProps) {
return (
<nav
className={classNames(
@@ -33,7 +25,7 @@ const NavTabs = function <T extends string>({
{sticky && <div className="pt-6" />}
{props.children}
{tabs.map((tab, idx) => (
<VerticalTabItem tabNameKey={_tabNameKey} {...tab} key={idx} />
<VerticalTabItem {...tab} key={idx} linkProps={linkProps} />
))}
</nav>
);