"use client";
import { useState } from "react";
import { Button } from "@/components/ui/button";
import { Checkbox } from "@/components/ui/checkbox";
import {
Popover,
PopoverContent,
PopoverTrigger,
} from "@/components/ui/popover";
import { Filter, X, ChevronDown } from "lucide-react";
interface FilterOption {
id: string;
label: string;
count?: number;
}
interface MultiSelectFilterProps {
title: string;
options: FilterOption[];
selectedValues: string[];
onChange: (selectedValues: string[]) => void;
placeholder?: string;
icon?: React.ReactNode;
}
export function MultiSelectFilter({
title,
options,
selectedValues,
onChange,
placeholder = "Sélectionner...",
icon = ,
}: MultiSelectFilterProps) {
const [isOpen, setIsOpen] = useState(false);
const handleToggle = (value: string) => {
const newSelected = selectedValues.includes(value)
? selectedValues.filter((v) => v !== value)
: [...selectedValues, value];
onChange(newSelected);
};
const handleSelectAll = () => {
if (selectedValues.length === options.length) {
onChange([]);
} else {
onChange(options.map((opt) => opt.id));
}
};
const clearSelection = () => {
onChange([]);
};
const getDisplayText = () => {
if (selectedValues.length === 0) return placeholder;
if (selectedValues.length === 1) {
const option = options.find((opt) => opt.id === selectedValues[0]);
return option?.label || selectedValues[0];
}
return `${selectedValues.length} sélectionné(s)`;
};
return (
{/* Header avec actions */}
{title}
{selectedValues.length > 0 && (
)}
{/* Options */}
{options.map((option) => (
handleToggle(option.id)}
>
{}} // Géré par le onClick du div parent
/>
))}
{/* Résumé de la sélection */}
{selectedValues.length > 0 && (
{selectedValues.slice(0, 3).map((value) => {
const option = options.find((opt) => opt.id === value);
return (
{option?.label || value}
);
})}
{selectedValues.length > 3 && (
+{selectedValues.length - 3}
)}
)}
);
}