import clsx from "clsx"; import { useEffect, useState } from "react"; import { GroupBase, StylesConfig } from "react-select"; import ReactSelect from "react-select"; import Option from "@/interfaces/option"; interface Props { defaultValue?: Option | Option[]; options: Option[]; value?: Option | Option[] | null; isLoading?: boolean; loadOptions: (inputValue: string) => void; onMenuScrollToBottom: (event: WheelEvent | TouchEvent) => void; disabled?: boolean; placeholder?: string; isClearable?: boolean; styles?: StylesConfig>; className?: string; label?: string; flat?: boolean; } interface MultiProps { isMulti: true; onChange: (value: Option[] | null) => void; } interface SingleProps { isMulti?: false; onChange: (value: Option | null) => void; } export default function AsyncSelect({ value, isMulti, defaultValue, options, loadOptions, onMenuScrollToBottom, placeholder, disabled, onChange, styles, isClearable, isLoading, label, className, flat, }: Props & (MultiProps | SingleProps)) { const [target, setTarget] = useState(); useEffect(() => { if (document) setTarget(document.body); }, []); return (
{label && ( )} "Loading..."} onInputChange={(inputValue) => { loadOptions(inputValue); }} options={options} value={value} onChange={onChange as any} placeholder={placeholder} menuPortalTarget={target} defaultValue={defaultValue} onMenuScrollToBottom={onMenuScrollToBottom} styles={ styles || { menuPortal: (base) => ({ ...base, zIndex: 9999 }), control: (styles) => ({ ...styles, paddingLeft: "4px", border: "none", outline: "none", ":focus": { outline: "none", }, }), option: (styles, state) => ({ ...styles, backgroundColor: state.isFocused ? "#D5D9F0" : state.isSelected ? "#7872BF" : "white", color: state.isFocused ? "black" : styles.color, }), } } isDisabled={disabled} isClearable={isClearable} />
); }