"use client"; import { cn } from "@lib/utils"; import { Button } from "@ui/components/button"; import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, } from "@ui/components/command"; import { Popover, PopoverContent, PopoverTrigger, } from "@ui/components/popover"; import { Check, ChevronsUpDown, X } from "lucide-react"; import * as React from "react"; interface Option { value: string; label: string; } interface ComboboxProps { options: Option[]; onSelect: (value: string) => void; onSubmit: (newName: string) => void; selectedValues: string[]; setSelectedValues: React.Dispatch>; className?: string; placeholder?: string; triggerClassName?: string; } export function Combobox({ options, onSelect, onSubmit, selectedValues, setSelectedValues, className, placeholder = "Select...", triggerClassName, }: ComboboxProps) { const [open, setOpen] = React.useState(false); const [inputValue, setInputValue] = React.useState(""); const handleSelect = (value: string) => { onSelect(value); setOpen(false); setInputValue(""); }; const handleCreate = () => { if (inputValue.trim()) { onSubmit(inputValue); setOpen(false); setInputValue(""); } }; const handleRemove = (valueToRemove: string) => { setSelectedValues((prev) => prev.filter((value) => value !== valueToRemove), ); }; const filteredOptions = options.filter( (option) => !selectedValues.includes(option.value), ); const isNewValue = inputValue.trim() && !options.some( (option) => option.label.toLowerCase() === inputValue.toLowerCase(), ); return ( {/** biome-ignore lint/a11y/useSemanticElements: shadcn*/} ); }) ) : ( {placeholder} )} {filteredOptions.length === 0 && !isNewValue && ( No options found. )} {filteredOptions.map((option) => ( handleSelect(option.value)} value={option.value} > {option.label} ))} {isNewValue && ( Create "{inputValue}" )} ); }