import { CaretDown, Check } from "@phosphor-icons/react"; import { cn } from "@reactive-resume/utils"; import { forwardRef, useState } from "react"; import { Button } from "./button"; import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, } from "./command"; import { Popover, PopoverContent, PopoverTrigger } from "./popover"; export type ComboboxOption = { value: string; label: React.ReactNode; }; type ComboboxPropsSingle = { options: ComboboxOption[]; emptyText?: string; clearable?: boolean; selectPlaceholder?: string; searchPlaceholder?: string; multiple?: false; value?: string; onValueChange?: (value: string) => void; }; type ComboboxPropsMultiple = { options: ComboboxOption[]; emptyText?: string; clearable?: boolean; selectPlaceholder?: string; searchPlaceholder?: string; multiple: true; value?: string[]; onValueChange?: (value: string[]) => void; }; export type ComboboxProps = ComboboxPropsSingle | ComboboxPropsMultiple; const handleSingleSelect = (props: ComboboxPropsSingle, option: ComboboxOption) => { if (props.clearable) { props.onValueChange?.(option.value === props.value ? "" : option.value); } else { props.onValueChange?.(option.value); } }; const handleMultipleSelect = (props: ComboboxPropsMultiple, option: ComboboxOption) => { if (props.value?.includes(option.value)) { if (!props.clearable && props.value.length === 1) return false; props.onValueChange?.(props.value.filter((value) => value !== option.value)); } else { props.onValueChange?.([...(props.value ?? []), option.value]); } }; export const Combobox = forwardRef( (props: ComboboxProps, ref: React.ForwardedRef) => { const [open, setOpen] = useState(false); return ( {props.emptyText ?? "No results found"} {props.options.map((option) => ( { const option = props.options.find( (option) => option.value.toLowerCase().trim() === selectedValue, ); if (!option) return null; if (props.multiple) { handleMultipleSelect(props, option); } else { handleSingleSelect(props, option); setOpen(false); } }} > {option.label} ))} ); }, );