import * as React from "react"; import { useState, useMemo } 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 { Checkbox } from "@/components/ui/checkbox"; import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, } from "@/components/ui/command"; import { Popover, PopoverContent, PopoverTrigger, } from "@/components/ui/popover"; export interface MultiSelectOption { label: string; value: string; } interface MultiSelectComboboxProps { options: MultiSelectOption[]; value?: string[]; onValueChange: (values: string[]) => void; placeholder?: string; searchPlaceholder?: string; emptyText?: string; maxDisplay?: number; className?: string; } export function MultiSelectCombobox({ options, value = [], onValueChange, placeholder = "Select options...", searchPlaceholder = "Search...", emptyText = "No options found.", maxDisplay = 2, className, }: MultiSelectComboboxProps) { const [open, setOpen] = useState(false); const handleSelect = (selectedValue: string) => { const newValues = value.includes(selectedValue) ? value.filter((v) => v !== selectedValue) : [...value, selectedValue]; onValueChange(newValues); }; const handleRemove = (valueToRemove: string) => { onValueChange(value.filter((v) => v !== valueToRemove)); }; const displayText = useMemo(() => { if (value.length === 0) return placeholder; if (value.length <= maxDisplay) { return value .map((v) => options.find((o) => o.value === v)?.label) .filter(Boolean) .join(", "); } return `${value.length} selected`; }, [value, options, maxDisplay, placeholder]); return (