"use client"; import * as React from "react"; import { Check, ChevronsUpDown, X } from "lucide-react"; import { cn } from "@/lib/utils"; import { Button } from "@/components/ui/button"; import { Badge } from "@/components/ui/badge"; import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, } from "@/components/ui/command"; import { Popover, PopoverContent, PopoverTrigger, } from "@/components/ui/popover"; export interface MultiSelectOption { value: string | number; label: string; originalData?: unknown; // Holds your raw API data } interface MultiSelectProps { options: MultiSelectOption[]; value?: (string | number)[]; onChange: (value: (string | number)[]) => void; placeholder?: string; searchPlaceholder?: string; emptyText?: string; disabled?: boolean; className?: string; maxCount?: number; // --- NEW CUSTOM UI PROPS --- renderBadge?: (option: MultiSelectOption, onRemove: () => void) => React.ReactNode; renderOption?: (option: MultiSelectOption, isSelected: boolean) => React.ReactNode; } export function MultiSelectCustomSelection({ options, value = [], onChange, placeholder = "Select option...", searchPlaceholder = "Search...", emptyText = "Not found.", disabled = false, className, maxCount = 3, renderBadge, renderOption, }: MultiSelectProps) { const [open, setOpen] = React.useState(false); const selectedOptions = React.useMemo(() => { return value.map((val) => options.find((opt) => String(opt.value) === String(val)) ).filter(Boolean) as MultiSelectOption[]; }, [value, options]); const handleSelect = (selectedValue: string | number) => { const isSelected = value.some((v) => String(v) === String(selectedValue)); if (isSelected) { onChange(value.filter((v) => String(v) !== String(selectedValue))); } else { onChange([...value, selectedValue]); } }; // Helper to safely remove items without closing the popover const handleRemove = (valueToRemove: string | number) => { onChange(value.filter((v) => String(v) !== String(valueToRemove))); }; return ( {emptyText} {/* --- RENDER OPTIONS --- */} {options.map((option) => { const isSelected = value.some((v) => String(v) === String(option.value)); return ( handleSelect(option.value)} > {renderOption ? ( renderOption(option, isSelected) ) : ( // Default Option UI <>
{option.label} )}
); })}
); }