2023-03-16 10:57:18 +00:00
|
|
|
import { Fragment, useState } from "react";
|
2023-03-18 06:04:29 +00:00
|
|
|
|
|
|
|
// headless ui
|
|
|
|
import { Menu, Transition } from "@headlessui/react";
|
|
|
|
// icons
|
2023-03-16 12:44:07 +00:00
|
|
|
import { ChevronDownIcon } from "@heroicons/react/24/outline";
|
2023-03-18 06:04:29 +00:00
|
|
|
import { ChevronRightIcon, ChevronLeftIcon } from "@heroicons/react/20/solid";
|
2023-03-16 10:57:18 +00:00
|
|
|
|
|
|
|
type MultiLevelDropdownProps = {
|
|
|
|
label: string;
|
|
|
|
options: {
|
|
|
|
id: string;
|
|
|
|
label: string;
|
|
|
|
value: any;
|
|
|
|
selected?: boolean;
|
|
|
|
children?: {
|
|
|
|
id: string;
|
2023-03-16 12:44:07 +00:00
|
|
|
label: string | JSX.Element;
|
2023-03-16 10:57:18 +00:00
|
|
|
value: any;
|
|
|
|
selected?: boolean;
|
|
|
|
}[];
|
|
|
|
}[];
|
|
|
|
onSelect: (value: any) => void;
|
|
|
|
direction?: "left" | "right";
|
2023-03-23 17:55:08 +00:00
|
|
|
height?: "sm" | "md" | "rg" | "lg";
|
2023-03-16 10:57:18 +00:00
|
|
|
};
|
|
|
|
|
2023-03-16 12:44:07 +00:00
|
|
|
export const MultiLevelDropdown: React.FC<MultiLevelDropdownProps> = ({
|
|
|
|
label,
|
|
|
|
options,
|
|
|
|
onSelect,
|
|
|
|
direction = "right",
|
2023-03-23 17:55:08 +00:00
|
|
|
height = "md",
|
2023-03-16 12:44:07 +00:00
|
|
|
}) => {
|
2023-03-16 10:57:18 +00:00
|
|
|
const [openChildFor, setOpenChildFor] = useState<string | null>(null);
|
|
|
|
|
|
|
|
return (
|
2023-06-23 14:00:53 +00:00
|
|
|
<Menu as="div" className="relative z-10 inline-block text-left">
|
2023-03-16 10:57:18 +00:00
|
|
|
{({ open }) => (
|
|
|
|
<>
|
|
|
|
<div>
|
|
|
|
<Menu.Button
|
2023-03-16 12:44:07 +00:00
|
|
|
onClick={() => setOpenChildFor(null)}
|
2023-04-20 12:43:21 +00:00
|
|
|
className={`group flex items-center justify-between gap-2 rounded-md border border-brand-base px-3 py-1.5 text-xs shadow-sm duration-300 focus:outline-none ${
|
2023-04-20 08:11:24 +00:00
|
|
|
open ? "bg-brand-surface-1 text-brand-base" : "text-brand-secondary"
|
2023-03-16 10:57:18 +00:00
|
|
|
}`}
|
|
|
|
>
|
|
|
|
{label}
|
2023-03-16 12:44:07 +00:00
|
|
|
<ChevronDownIcon className="h-3 w-3" aria-hidden="true" />
|
2023-03-16 10:57:18 +00:00
|
|
|
</Menu.Button>
|
|
|
|
</div>
|
|
|
|
<Transition
|
|
|
|
as={Fragment}
|
|
|
|
enter="transition ease-out duration-100"
|
|
|
|
enterFrom="transform opacity-0 scale-95"
|
|
|
|
enterTo="transform opacity-100 scale-100"
|
|
|
|
leave="transition ease-in duration-75"
|
|
|
|
leaveFrom="transform opacity-100 scale-100"
|
|
|
|
leaveTo="transform opacity-0 scale-95"
|
|
|
|
>
|
|
|
|
<Menu.Items
|
|
|
|
static
|
2023-04-21 05:16:04 +00:00
|
|
|
className="absolute right-0 z-10 mt-1 w-36 origin-top-right select-none rounded-md bg-brand-surface-1 text-xs shadow-lg focus:outline-none"
|
2023-03-16 10:57:18 +00:00
|
|
|
>
|
|
|
|
{options.map((option) => (
|
2023-03-16 12:44:07 +00:00
|
|
|
<div className="relative p-1" key={option.id}>
|
2023-03-16 10:57:18 +00:00
|
|
|
<Menu.Item
|
|
|
|
as="button"
|
|
|
|
onClick={(e: any) => {
|
|
|
|
if (option.children) {
|
2023-03-22 11:28:32 +00:00
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
if (openChildFor === option.id) setOpenChildFor(null);
|
|
|
|
else setOpenChildFor(option.id);
|
2023-03-16 10:57:18 +00:00
|
|
|
} else {
|
|
|
|
onSelect(option.value);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
className="w-full"
|
|
|
|
>
|
|
|
|
{({ active }) => (
|
|
|
|
<>
|
|
|
|
<div
|
|
|
|
className={`${
|
2023-04-21 05:16:04 +00:00
|
|
|
active || option.selected ? "bg-brand-surface-2" : ""
|
2023-04-20 12:43:21 +00:00
|
|
|
} flex items-center gap-1 rounded px-1 py-1.5 text-brand-secondary ${
|
2023-03-16 12:44:07 +00:00
|
|
|
direction === "right" ? "justify-between" : ""
|
|
|
|
}`}
|
2023-03-16 10:57:18 +00:00
|
|
|
>
|
|
|
|
{direction === "left" && option.children && (
|
|
|
|
<ChevronLeftIcon className="h-4 w-4" aria-hidden="true" />
|
|
|
|
)}
|
|
|
|
<span>{option.label}</span>
|
|
|
|
{direction === "right" && option.children && (
|
|
|
|
<ChevronRightIcon className="h-4 w-4" aria-hidden="true" />
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</Menu.Item>
|
|
|
|
{option.children && option.id === openChildFor && (
|
2023-03-29 12:48:45 +00:00
|
|
|
<div
|
2023-05-02 14:30:33 +00:00
|
|
|
className={`absolute top-0 w-auto min-w-[144px] max-w-[192px] origin-top-right select-none overflow-y-scroll rounded-md bg-brand-surface-1 shadow-lg focus:outline-none ${
|
2023-03-16 10:57:18 +00:00
|
|
|
direction === "left"
|
2023-03-16 12:44:07 +00:00
|
|
|
? "right-full -translate-x-1"
|
|
|
|
: "left-full translate-x-1"
|
2023-03-23 17:55:08 +00:00
|
|
|
} ${
|
|
|
|
height === "sm"
|
|
|
|
? "max-h-28"
|
|
|
|
: height === "md"
|
|
|
|
? "max-h-44"
|
|
|
|
: height === "rg"
|
|
|
|
? "max-h-56"
|
|
|
|
: height === "lg"
|
|
|
|
? "max-h-80"
|
|
|
|
: ""
|
2023-03-16 10:57:18 +00:00
|
|
|
}`}
|
|
|
|
>
|
2023-03-18 06:04:29 +00:00
|
|
|
<div className="space-y-1 p-1">
|
2023-03-16 12:44:07 +00:00
|
|
|
{option.children.map((child) => (
|
2023-03-29 12:48:45 +00:00
|
|
|
<button
|
2023-03-16 12:44:07 +00:00
|
|
|
key={child.id}
|
|
|
|
type="button"
|
|
|
|
onClick={() => {
|
|
|
|
onSelect(child.value);
|
|
|
|
}}
|
2023-03-29 12:48:45 +00:00
|
|
|
className={`${
|
2023-04-21 05:16:04 +00:00
|
|
|
child.selected ? "bg-brand-surface-2" : ""
|
2023-06-23 14:00:11 +00:00
|
|
|
} flex w-full items-center whitespace-nowrap break-words rounded px-1 py-1.5 text-left capitalize text-brand-secondary hover:bg-brand-surface-2`}
|
2023-03-16 12:44:07 +00:00
|
|
|
>
|
|
|
|
{child.label}
|
2023-03-29 12:48:45 +00:00
|
|
|
</button>
|
2023-03-16 12:44:07 +00:00
|
|
|
))}
|
|
|
|
</div>
|
2023-03-29 12:48:45 +00:00
|
|
|
</div>
|
2023-03-16 10:57:18 +00:00
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
))}
|
|
|
|
</Menu.Items>
|
|
|
|
</Transition>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</Menu>
|
|
|
|
);
|
|
|
|
};
|