"use client"; import * as React from "react"; import { Check, ChevronsUpDown, LucideIcon } from "lucide-react"; import { cn } from "@/lib/utils"; import { Button, ButtonProps } from "@/components/ui/button"; import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, } from "@/components/ui/command"; import { Popover, PopoverContent, PopoverTrigger, } from "@/components/ui/popover"; export type ComboboxProps = { data: { label: string; value: string; Icon?: LucideIcon; }[]; onSelect: (value?: string) => void; messageUi?: { select?: string; selectIcon?: LucideIcon; empty?: string; placeholder?: string; }; initialValue?: string; className?: string; hideSearch?: boolean; buttonProps?: ButtonProps; }; export function Combobox({ data, initialValue, messageUi, className, hideSearch, buttonProps, onSelect, }: ComboboxProps) { const [open, setOpen] = React.useState(false); const [value, setValue] = React.useState(initialValue ?? ""); const selectedItem = data.find((item) => item.value === initialValue)!; return ( { if (!search.trim()) return 1; // Show all when no search input const entry = data.find((item) => item.value === value); // Assuming dataset is an array of objects with id and name if (!entry) return 0; // If no matching entry is found, exclude it return entry.label.toLowerCase().includes(search.toLowerCase()) ? 1 : 0; }} > {!hideSearch && ( )} {messageUi?.empty ?? "Nothing found."} {data.map((item) => ( { const newValue = currentValue === value ? "" : currentValue; setValue(newValue); onSelect(newValue); setOpen(false); }} >
{item?.Icon && } {item.label}
))}
); }