import { isArray } from "lodash"; import React, { useCallback, useMemo } from "react"; import Select from "react-select"; import { SelectComponents } from "react-select/dist/declarations/src/components"; import "./selector.scss"; export interface SelectorProps { className?: string; placeholder?: string; options: readonly SelectorOption[]; disabled?: boolean; clearable?: boolean; loading?: boolean; multiple?: M; onChange?: (k: SelectorValueType) => void; onFocus?: (e: React.FocusEvent) => void; label?: (item: T) => string; defaultValue?: SelectorValueType; value?: SelectorValueType; components?: Partial>; } export function Selector( props: SelectorProps ) { const { className, placeholder, label, disabled, clearable, loading, options, multiple, onChange, onFocus, defaultValue, components, value, } = props; const nameFromItems = useCallback( (item: T) => { return options.find((v) => v.value === item)?.label; }, [options] ); // TODO: Force as any const wrapper = useCallback( (value: SelectorValueType | undefined | null): any => { if (value !== null && value !== undefined) { if (multiple) { return (value as SelectorValueType).map((v) => ({ label: label ? label(v) : nameFromItems(v) ?? "Unknown", value: v, })); } else { const v = value as T; return { label: label ? label(v) : nameFromItems(v) ?? "Unknown", value: v, }; } } return value; }, [label, multiple, nameFromItems] ); const defaultWrapper = useMemo( () => wrapper(defaultValue), [defaultValue, wrapper] ); const valueWrapper = useMemo(() => wrapper(value), [wrapper, value]); return ( ); }