import { useRef } from "react"; import clsx from "clsx"; import { Combobox as HeadlessCombobox, ComboboxInput, ComboboxOption, ComboboxOptions } from "@headlessui/react"; import { cva } from "@/cva.config"; import Card from "./Card"; export interface ComboboxOption { value: string; label: string; } const sizes = { XS: "h-[24.5px] pl-3 pr-8 text-xs", SM: "h-[32px] pl-3 pr-8 text-[13px]", MD: "h-[40px] pl-4 pr-10 text-sm", LG: "h-[48px] pl-4 pr-10 px-5 text-base", } as const; const comboboxVariants = cva({ variants: { size: sizes }, }); type BaseProps = React.ComponentProps; interface ComboboxProps extends Omit { displayValue: (option: ComboboxOption) => string; onInputChange: (option: string) => void; options: () => ComboboxOption[]; placeholder?: string; emptyMessage?: string; size?: keyof typeof sizes; disabledMessage?: string; } export function Combobox({ onInputChange, displayValue, options, disabled = false, placeholder = "Search...", emptyMessage = "No results found", size = "MD", onChange, disabledMessage = "Input disabled", ...otherProps }: ComboboxProps) { const inputRef = useRef(null); const classes = comboboxVariants({ size }); return ( {() => ( <> onInputChange(event.target.value)} disabled={disabled} /> {options().length > 0 && ( {options().map((option) => ( {option.label} ))} )} {options().length === 0 && inputRef.current?.value && (
{emptyMessage}
)} )}
); }