"use client"; import { Fragment, useState, useRef } from "react"; // next import Link from "next/link"; // headless import { Popover, Transition } from "@headlessui/react"; import { ChevronLeftIcon, CheckIcon } from "@heroicons/react/20/solid"; // hooks import useOutSideClick from "hooks/use-outside-click"; type ItemOptionType = { display: React.ReactNode; as?: "button" | "link" | "div"; href?: string; isSelected?: boolean; onClick?: () => void; children?: ItemOptionType[] | null; }; type DropdownItemProps = { item: ItemOptionType; }; type DropDownListProps = { open: boolean; handleClose?: () => void; items: ItemOptionType[]; }; type DropdownProps = { button: React.ReactNode | (() => React.ReactNode); items: ItemOptionType[]; }; const DropdownList: React.FC = (props) => { const { open, items, handleClose } = props; const ref = useRef(null); useOutSideClick(ref, () => { if (handleClose) handleClose(); }); return (
{items.map((item, index) => ( ))}
); }; const DropdownItem: React.FC = (props) => { const { item } = props; const { display, children, as: as_, href, onClick, isSelected } = item; const [open, setOpen] = useState(false); return (
{(!as_ || as_ === "button" || as_ === "div") && ( )} {as_ === "link" && {display}} {children && setOpen(false)} items={children} />}
); }; const Dropdown: React.FC = (props) => { const { button, items } = props; return ( {({ open }) => ( <> {typeof button === "function" ? button() : button}
{items.map((item, index) => ( ))}
)}
); }; export { Dropdown };