"use client"; import { memo, useState } from "react"; import { IconCheck, IconChevronDown } from "@tabler/icons-react"; import { cn } from "@/lib/utils"; import { Button } from "@kandev/ui/button"; import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, } from "@kandev/ui/command"; import { Popover, PopoverContent, PopoverTrigger } from "@kandev/ui/popover"; export type ComboboxOption = { value: string; label: string; description?: string; renderLabel?: () => React.ReactNode; }; interface ComboboxProps { options: ComboboxOption[]; value: string; onValueChange: (value: string) => void; dropdownLabel?: string; placeholder?: string; searchPlaceholder?: string; emptyMessage?: string; disabled?: boolean; className?: string; triggerClassName?: string; showSearch?: boolean; testId?: string; popoverSide?: "top" | "right" | "bottom" | "left"; popoverAlign?: "start" | "center" | "end"; /** When true, the trigger always renders the plain label text instead of renderLabel. */ plainTrigger?: boolean; } function TriggerLabel({ selectedOption, plainTrigger, placeholder, }: { selectedOption: ComboboxOption | undefined; plainTrigger: boolean; placeholder: string; }) { if (!plainTrigger && selectedOption?.renderLabel) { return selectedOption.renderLabel(); } return {selectedOption?.label || placeholder}; } function OptionsList({ options, value, onSelect, }: { options: ComboboxOption[]; value: string; onSelect: (value: string) => void; }) { return ( {options.map((option) => ( onSelect(option.value)} className="relative pr-7" > {option.renderLabel ? option.renderLabel() : option.label} ))} ); } export const Combobox = memo(function Combobox({ options, value, onValueChange, dropdownLabel, placeholder = "Select option...", searchPlaceholder = "Search...", emptyMessage = "No option found.", disabled = false, className, triggerClassName, showSearch = true, testId, popoverSide, popoverAlign = "start", plainTrigger = false, }: ComboboxProps) { const [open, setOpen] = useState(false); // Track the highlighted item. Defaults to the selected value so the current // selection is highlighted when the popover opens (not the first item). const [highlighted, setHighlighted] = useState(""); const selectedOption = options.find((option) => option.value === value); return ( { setOpen(next); if (next) setHighlighted(value); }} > {dropdownLabel ? ( {dropdownLabel} ) : null} {showSearch && } {emptyMessage} { onValueChange(v === value ? "" : v); setOpen(false); }} /> ); });