Files
Ichsan Kurniawan 6b16bd2dd4 add new UI Component
2026-04-20 17:13:20 +07:00

157 lines
6.4 KiB
TypeScript

"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;
}
interface MultiSelectProps {
options: MultiSelectOption[];
value?: (string | number)[];
onChange: (value: (string | number)[]) => void;
placeholder?: string;
searchPlaceholder?: string;
emptyText?: string;
disabled?: boolean;
className?: string;
maxCount?: number; // How many badges to show before saying "+X more"
}
export function MultiSelect({
options,
value = [], // Default to empty array
onChange,
placeholder = "Select option...",
searchPlaceholder = "Search...",
emptyText = "Not found.",
disabled = false,
className,
maxCount = 3,
}: MultiSelectProps) {
const [open, setOpen] = React.useState(false);
// Map the selected string/number values back to their full option objects
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) {
// Remove the item
onChange(value.filter((v) => String(v) !== String(selectedValue)));
} else {
// Add the item
onChange([...value, selectedValue]);
}
};
const handleRemove = (e: React.MouseEvent, valueToRemove: string | number) => {
e.stopPropagation(); // Prevent opening/closing the popover when clicking the "X"
e.preventDefault();
onChange(value.filter((v) => String(v) !== String(valueToRemove)));
};
return (
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger asChild disabled={disabled}>
<Button
variant="outline"
role="combobox"
aria-expanded={open}
// min-h-10 and h-auto allows the button to grow if badges wrap to a new line
className={cn("w-full justify-between min-h-10 h-auto font-normal", className)}
>
<div className="flex flex-wrap gap-1 items-center overflow-hidden">
{selectedOptions.length === 0 && (
<span className="text-muted-foreground">{placeholder}</span>
)}
{/* Render Badges */}
{selectedOptions.slice(0, maxCount).map((option) => (
<Badge
key={option.value}
variant="secondary"
className="mr-1 mb-1 font-normal"
>
{option.label}
<div
role="button"
className="ml-1 ring-offset-background rounded-full outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 hover:bg-muted-foreground/20 cursor-pointer"
onClick={(e) => handleRemove(e, option.value)}
>
<X className="h-3 w-3 text-muted-foreground hover:text-foreground" />
</div>
</Badge>
))}
{/* Render "+X more" if selections exceed maxCount */}
{selectedOptions.length > maxCount && (
<Badge variant="secondary" className="mr-1 mb-1 font-normal">
+ {selectedOptions.length - maxCount} selected
</Badge>
)}
</div>
<ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
</Button>
</PopoverTrigger>
<PopoverContent className="w-(--radix-popover-trigger-width) p-0" align="start">
<Command>
<CommandInput placeholder={searchPlaceholder} />
<CommandList>
<CommandEmpty>{emptyText}</CommandEmpty>
<CommandGroup>
{options.map((option) => {
const isSelected = value.some((v) => String(v) === String(option.value));
return (
<CommandItem
key={option.value}
value={option.label}
onSelect={() => handleSelect(option.value)}
// Notice we DO NOT call setOpen(false) here, allowing multiple clicks
>
<div
className={cn(
"mr-2 flex h-4 w-4 items-center justify-center rounded-sm border border-primary",
isSelected
? "bg-primary text-primary-foreground"
: "opacity-50 [&_svg]:invisible"
)}
>
<Check className={cn("h-4 w-4")} />
</div>
<span>{option.label}</span>
</CommandItem>
);
})}
</CommandGroup>
</CommandList>
</Command>
</PopoverContent>
</Popover>
);
}