"use client"; /** * Multi Select Field * Renders a multi-select combobox with search and badge chips with X to remove */ import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, } from "@mdxui/primitives/command"; import { Popover, PopoverContent, PopoverTrigger, } from "@mdxui/primitives/popover"; import { ChevronsUpDown, X } from "lucide-react"; import { useState } from "react"; import { ANIMATION_CLASSES, getStaggerStyle, STAGGER_PRESETS, } from "../animations"; import { cx, focusInput } from "../lib/utils"; import { OnboardingCheckbox } from "../primitives/onboarding-checkbox"; import { OnboardingLabel } from "../primitives/onboarding-label"; import type { MultiSelectFieldProps } from "../types/fields"; export function MultiSelectField({ id, label, placeholder = "Select items...", searchPlaceholder = "Search...", emptyMessage = "No items found.", description, required = false, options, value, onChange, minSelections, maxSelections, animationIndex = 0, disabled = false, error, touched = false, }: MultiSelectFieldProps) { const [open, setOpen] = useState(false); const handleSelect = (optionValue: string) => { if (value.includes(optionValue)) { onChange(value.filter((v) => v !== optionValue)); } else { if (maxSelections && value.length >= maxSelections) { return; } onChange([...value, optionValue]); } }; const handleRemove = (optionValue: string, e: React.MouseEvent) => { e.stopPropagation(); onChange(value.filter((v) => v !== optionValue)); }; return (
{label} {required && *} {description && (

{description}

)} {emptyMessage} {options.map((option) => { const isSelected = value.includes(option.value); const isDisabled = option.disabled || (!isSelected && !!maxSelections && value.length >= maxSelections); return ( handleSelect(option.value)} className="cursor-pointer" > {option.label} ); })} {minSelections && value.length < minSelections && (

Select at least {minSelections} option{minSelections > 1 ? "s" : ""}

)} {maxSelections && (

{value.length}/{maxSelections} selected

)} {touched && error && (

{error}

)}
); }