"use client";
import * as React from "react";
import { Check, ChevronsUpDown } 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 {
value: string | number;
label: string;
// Allows you to pass raw data for custom UI rendering without using 'any'
originalData?: unknown;
}
interface ComboboxProps {
options: ComboboxOption[];
value?: string | number;
onChange: (value: string | number | undefined) => void;
onSelect?: (option: ComboboxOption) => void;
placeholder?: string;
searchPlaceholder?: string;
emptyText?: string;
disabled?: boolean;
className?: string;
// Optional props for Custom UI
renderTrigger?: (selectedOption?: ComboboxOption) => React.ReactNode;
renderOption?: (option: ComboboxOption, isSelected: boolean) => React.ReactNode;
}
export function ComboboxCustomUI({
options,
value,
onChange,
onSelect,
placeholder = "Select option...",
searchPlaceholder = "Search...",
emptyText = "No results found.",
disabled = false,
className,
renderTrigger,
renderOption,
}: ComboboxProps) {
const [open, setOpen] = React.useState(false);
// Safely find the selected option by comparing strings to avoid number/string mismatch bugs
const selectedOption = React.useMemo(
() => options.find((opt) => String(opt.value) === String(value)),
[value, options]
);
return (
{emptyText}
{options.map((option) => {
const isSelected = String(option.value) === String(value);
return (
{
// Toggle selection: if already selected, clear it (undefined), else set new value
onChange(isSelected ? undefined : option.value);
setOpen(false);
onSelect?.(option);
}}
>
{renderOption ? (
renderOption(option, isSelected)
) : (
<>
{option.label}
>
)}
);
})}
);
}
/** Standalone (No Form)
export function SalesFilter() {
const [selectedId, setSelectedId] = useState();
return (
);
}
*/
/** TanStack Form (Standard UI)
import { Combobox } from "@/components/ui/combobox";
import { useMemo } from "react";
export function SalesForm() {
// 1. Map your raw data to the standard option format
const mappedSalesPrograms = useMemo(() => {
return dataSalesProgram?.map((item) => ({
label: item.nama,
value: item.id,
})) || [];
}, [dataSalesProgram]);
return (
{(field) => (
Sales Program field.handleChange(val as number)} // Cast back to your strict type
placeholder="Pilih Sales Program"
searchPlaceholder="Cari Sales Program..."
/>
{field.state.meta.errors && {field.state.meta.errors}}
)}
);
}
*/
/** Custom UI Selection
export function CustomUICombobox() {
// Passing the raw item in `originalData` so we can use it in the custom render
const users = [
{ id: 1, name: "Luthfi Aditama", role: "Team Leader" },
{ id: 2, name: "Ihsan", role: "Developer" },
];
const options = users.map(user => ({
value: user.id,
label: user.name,
originalData: user // <-- Store raw data here
}));
return (
console.log(val)}
// 1. Custom Selected Value
renderTrigger={(selected) => (
{selected?.label || "Pilih User"}
)}
// 2. Custom Dropdown Items
renderOption={(option, isSelected) => {
// Safely cast originalData back to your expected type
const user = option.originalData as typeof users[0];
return (