"use client" import * as React from "react" import { Check, ChevronsUpDown, PlusCircle } from "lucide-react" import { cn } from "@/lib/utils" import { Button } from "@/components/ui/button" import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, } from "@/components/ui/command" import { Popover, PopoverContent, PopoverTrigger, } from "@/components/ui/popover" export interface ComboboxOption { label: string; value: string; } interface ComboboxProps { options: ComboboxOption[]; value?: string; onChange: (value: string) => void; onCreate?: (inputValue: string) => void; placeholder?: string; emptyMessage?: string; } export function Combobox({ options, value, onChange, onCreate, placeholder, emptyMessage }: ComboboxProps) { const [open, setOpen] = React.useState(false); const [inputValue, setInputValue] = React.useState(""); const handleSelect = (currentValue: string) => { onChange(currentValue === value ? "" : currentValue); setOpen(false); }; const handleCreate = () => { if (onCreate && inputValue) { onCreate(inputValue); setInputValue(""); setOpen(false); } }; const filteredOptions = options.filter(option => option.label.toLowerCase().includes(inputValue.toLowerCase()) ); const showCreateOption = onCreate && inputValue && !filteredOptions.some(option => option.label.toLowerCase() === inputValue.toLowerCase()); const selectedOption = options.find((option) => option.value === value); return ( {filteredOptions.map((option) => ( {option.label} ))} {showCreateOption && ( Crear "{inputValue}" )} {filteredOptions.length === 0 && !showCreateOption && ( {emptyMessage || "No se encontraron opciones."} )} ) }