import React, { useState } from "react"; import { Listbox, Transition } from "@headlessui/react"; import { CheckIcon, ChevronUpDownIcon } from "@heroicons/react/20/solid"; type TSelectOption = { id: string; label: string; value: any; children?: | (TSelectOption & { children?: null; })[] | null; }; type TMultipleSelectProps = { options: TSelectOption[]; selected: TSelectOption | null; setSelected: (value: any) => void; label: string; }; export const MultiSelect: React.FC = (props) => { const { options, selected, setSelected, label } = props; const [openChildFor, setOpenChildFor] = useState(null); return (
{ if (value?.children === null) { setSelected(value); setOpenChildFor(null); } else setOpenChildFor(value); }} > {({ open }) => (
setOpenChildFor(null)} className="relative w-full cursor-default rounded-lg bg-white py-2 pl-3 pr-10 text-left shadow-md focus:outline-none focus-visible:border-indigo-500 focus-visible:ring-2 focus-visible:ring-white focus-visible:ring-opacity-75 focus-visible:ring-offset-2 focus-visible:ring-offset-orange-300 sm:text-sm" > {selected?.label ?? label} {options.map((option) => ( `relative cursor-default select-none py-2 pl-10 pr-4 ${ active ? "bg-amber-100 text-amber-900" : "text-gray-900" }` } onClick={(e: any) => { if (option.children !== null) { e.preventDefault(); setOpenChildFor(option); } }} value={option} > {({ selected }) => ( <> {openChildFor?.id === option.id && (
{option.children?.map((child) => ( `relative cursor-default select-none py-2 pl-10 pr-4 ${ active ? "bg-amber-100 text-amber-900" : "text-gray-900" }` } as="div" value={child} > {({ selected }) => ( <> {child.label} {selected ? ( ) : null} )} ))}
)} {option.label} {selected ? ( ) : null} )} ))}
)}
); };