import { Menu, Transition } from "@headlessui/react"; import { Fragment, useState } from "react"; import { ChevronDownIcon, ChevronRightIcon, ChevronLeftIcon } from "@heroicons/react/20/solid"; type MultiLevelDropdownProps = { label: string; options: { id: string; label: string; value: any; selected?: boolean; children?: { id: string; label: string; value: any; selected?: boolean; }[]; }[]; onSelect: (value: any) => void; direction?: "left" | "right"; }; export const MultiLevelDropdown: React.FC = (props) => { const { label, options, onSelect, direction = "right" } = props; const [openChildFor, setOpenChildFor] = useState(null); return ( {({ open }) => ( <>
{ setOpenChildFor(null); }} className={`group flex items-center gap-2 rounded-md border bg-transparent p-2 text-xs font-medium hover:bg-gray-100 hover:text-gray-900 focus:outline-none ${ open ? "bg-gray-100 text-gray-900" : "text-gray-500" }`} > {label}
{options.map((option) => (
{ if (option.children) { if (openChildFor === option.id) { e.stopPropagation(); e.preventDefault(); setOpenChildFor(null); } else { e.stopPropagation(); e.preventDefault(); setOpenChildFor(option.id); } } else { onSelect(option.value); } }} className="w-full" > {({ active }) => ( <>
{direction === "left" && option.children && (
)}
{option.children && option.id === openChildFor && ( {option.children.map((child) => (
{({ active }) => ( <> )}
))}
)}
))}
)}
); };