import { Dialog, DialogContent, DialogHeader, DialogTitle } from "@/components/ui/dialog"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Checkbox } from "@/components/ui/checkbox"; import { useEffect, useMemo, useState } from "react"; export interface SelectItem { label: string; value: string; } interface SelectDialogProps { open: boolean; onClose: () => void; title: string; description?: string; icon?: React.ReactNode; items: SelectItem[]; selectedValues?: string[]; onConfirm: (values: string[]) => Promise | void; } export function SelectDialog({ open, onClose, title, description, icon, items, selectedValues, onConfirm, }: SelectDialogProps) { const [selected, setSelected] = useState([]); const [search, setSearch] = useState(""); useEffect(() => { if (!open) return; if (!selectedValues) return; setSelected(selectedValues); }, [open, selectedValues]); const filteredItems = useMemo(() => { return items.filter((item) => item.label.toLowerCase().includes(search.toLowerCase()) ); }, [items, search]); const toggleItem = (value: string) => { setSelected((prev) => prev.includes(value) ? prev.filter((v) => v !== value) : [...prev, value] ); }; const handleConfirm = async () => { await onConfirm(selected); setSelected([]); setSearch(""); onClose(); }; return ( {icon} {title} {description &&

{description}

}
setSearch(e.target.value)} className="my-2" />
{filteredItems.map((item) => (
toggleItem(item.value)} /> {item.label}
))} {filteredItems.length === 0 && (

Không có kết quả

)}
); }