import {
Listbox,
ListboxButton,
ListboxOption,
ListboxOptions,
} from "@headlessui/react"
import classNames from "classnames"
import DisclosureArrow from "../public/ui/disclosure-arrow.svg?inline"
export type SelectMenuProps = {
label: React.ReactNode
onChange: (value: string) => void
value: string
options: {
label: React.ReactNode
value: string
}[]
}
export const SelectMenu = ({
label,
onChange,
options,
value,
}: SelectMenuProps) => {
const selectedLabel = options.find((option) => option.value === value)?.label
return (
<Listbox value={value} onChange={onChange}>
<div className="b3 inline-flex w-full sm:w-auto">
<div className="w-full relative">
<ListboxButton className="flex w-full items-center gap-2 cursor-pointer rounded-md border border-gray-3 p-4 text-left focus:outline-none focus:ring-1 focus:ring-blurple-500 sm:w-auto">
<span className="block truncate text-gray-1">
<span className="font-medium">{label}: </span>
<span className="font-bold">{selectedLabel}</span>
</span>
<span className="pointer-events-none">
<DisclosureArrow
className="h-4 w-4 text-gray-2"
fill="currentColor"
/>
</span>
</ListboxButton>
<ListboxOptions className="absolute z-10 mt-1 max-h-56 w-full min-w-max overflow-auto rounded-md bg-white py-1 shadow-lg ring-1 ring-gray-3 focus:outline-none">
{options.map(({ label: optionLabel, value: optionValue }) => (
<ListboxOption
key={optionValue}
value={optionValue}
className={({ focus }) =>
classNames(
focus ? "bg-blurple-500 text-white" : "text-gray-1",
"relative cursor-pointer select-none py-3 px-4 font-medium text-gray-1"
)
}
>
{({ selected }) => (
<>
<span className={selected ? "font-bold" : ""}>
{optionLabel}
</span>
</>
)}
</ListboxOption>
))}
</ListboxOptions>
</div>
</div>
</Listbox>
)
}
export default SelectMenu