import { XIcon, CheckIcon } from "@heroicons/react/outline"; import React, { ForwardedRef, useEffect, useState } from "react"; import { OptionsType } from "react-select/lib/types"; import Avatar from "@components/ui/Avatar"; import Select from "@components/ui/form/Select"; export type CheckedSelectProps = { defaultValue?: []; placeholder?: string; name?: string; options: []; onChange: (options: OptionsType) => void; disabled: []; }; export const CheckedSelect = React.forwardRef((props: CheckedSelectProps, ref: ForwardedRef) => { const [selectedOptions, setSelectedOptions] = useState<[]>(props.defaultValue || []); useEffect(() => { props.onChange(selectedOptions); }, [selectedOptions]); const formatOptionLabel = ({ label, avatar, disabled }) => (
{label} {disabled && (
)}
); const options = props.options.map((option) => ({ ...option, disabled: !!selectedOptions.find((selectedOption) => selectedOption.value === option.value), })); const removeOption = (value) => setSelectedOptions(selectedOptions.filter((option) => option.value !== value)); const changeHandler = (selections) => selections.forEach((selected) => { if (selectedOptions.find((option) => option.value === selected.value)) { removeOption(selected.value); return; } setSelectedOptions(selectedOptions.concat(selected)); }); return ( <>