import React, { useCallback, useMemo } from "react"; import Option from "./option"; import Autocomplete from "./AutoComplete"; import Pill from "./Pill"; interface MultiSelectProps { includeLabel?: boolean; name: string; label: string; error?: string; options?: Option[]; selectedOptions: Option[]; onAdd: (item: Option) => void; onDelete: (item: Option) => void; } const MultiSelect: React.FC = ({ includeLabel = true, name, label, error, options, selectedOptions, onAdd, onDelete, }) => { const handleDelete = useCallback( (id: string) => { const found = options?.find((x) => x._id === id); if (found) { onDelete(found); } }, [options, onDelete], ); const selectedBlock = useMemo(() => { if (selectedOptions.length === 0) return <>; return ( <> {selectedOptions.map((x) => ( ))} ); }, [selectedOptions, handleDelete]); return (
{includeLabel && }
{selectedBlock}
{error &&
{error}
}
); }; export default MultiSelect;