import React, { useEffect, useRef, useState } from "react"; import { get, isArray, isNil } from "lodash"; import { useTranslations } from "next-intl"; import { Controller, FieldPath, FieldValues, useFormContext, useWatch } from "react-hook-form"; import { ActionMeta, GroupBase, OnChangeValue, Props } from "react-select"; import AsyncSelect from "react-select/async"; import { Prettify } from "@/types"; import { Field, GenericFieldProps } from "./Field"; import { BaseOption, createSelectClassNamesFn } from "./Select"; export type AsyncSelectFieldProps< TFieldValues extends FieldValues = FieldValues, TFieldName extends FieldPath = FieldPath, IsMulti extends boolean = false, T = string, D = unknown, > = Prettify< GenericFieldProps & Omit< Props, IsMulti, GroupBase>>, "options" | "onChange" | "value" | "classNames" > & { required?: boolean; loadOption: (id: T) => Promise | undefined>; loadOptions: (searchString?: string) => Promise[]>; onInformChange?: (data: BaseOption[] | null) => void; separators?: boolean; size?: "small" | "medium"; } >; export const AsyncSelectField = < TFieldValues extends FieldValues = FieldValues, TFieldName extends FieldPath = FieldPath, IsMulti extends boolean = false, T = string, D = unknown, >({ name, control, className, label, hideErrorMessage, required, size, loadOption, loadOptions, onInformChange, placeholder, separators, ...selectProps }: AsyncSelectFieldProps) => { const t = useTranslations("App"); const [loadingValues, setLoadingValues] = useState(false); const [_loadedInitialValues, setLoadedInitialValues] = useState(false); const [loadingOptions, setLoadingOptions] = useState(false); const valueOptions = useRef[]>([]); const searchOptions = useRef[]>([]); const form = useFormContext(); const value = useWatch({ control: form.control, name }); // Fetch the option(s) for the current value(s) when mounted useEffect(() => { const doLoad = async () => { try { const values = !value ? [] : isArray(value) ? value : [value]; const unFetchedValues = values.filter( () => valueOptions.current.find((o) => o.value === value) === undefined, ); if (unFetchedValues.length > 0) { setLoadingValues(true); const options = await Promise.all( unFetchedValues.map((v: T) => { return loadOption(v); }), ); valueOptions.current = [ ...valueOptions.current, ...options.filter((vo): vo is BaseOption => vo !== undefined), ]; setLoadingValues(false); onInformChange?.( values.map((v) => valueOptions.current.find((o) => o.value === v)!) as BaseOption< T, D >[], ); // This forced a state update, which enures that the select is updated once the query has finished setLoadedInitialValues(true); } } catch (e) { console.log(e); setLoadingValues(false); } }; doLoad(); }, [value, setLoadingValues, loadOption, onInformChange]); const { formState: { errors }, } = form; const hasError = !!get(errors, name)?.message; const valueToOption = (value: T) => [...valueOptions.current, ...searchOptions.current].find((o) => o.value === value) ?? { value, label: "", }; const fetchOptions = async (searchString?: string) => { setLoadingOptions(true); const options = await loadOptions(searchString); searchOptions.current = options; setLoadingOptions(false); return options; }; const classNames = createSelectClassNamesFn, IsMulti>({ size, }); return ( { const onSelectChange = ( newValue: OnChangeValue, IsMulti>, actionMeta: ActionMeta>, ) => { if (isNil(newValue) || actionMeta.action === "clear") { onChange(null); onInformChange?.(null); valueOptions.current = []; } else if (isArray(newValue)) { onChange(newValue.map((option) => option.value)); onInformChange?.(newValue); valueOptions.current = newValue; } else { onChange((newValue as BaseOption).value); onInformChange?.([newValue as BaseOption]); valueOptions.current = [newValue as BaseOption]; } }; const optionValue = isNil(value) || value === "" ? null : isArray(value) ? // @ts-ignore - this is too complex for TS to understand value.map(valueToOption) : valueToOption(value); return ( , IsMulti> isClearable value={optionValue} onChange={onSelectChange} loadOptions={fetchOptions} defaultOptions isLoading={loadingOptions || loadingValues} noOptionsMessage={() => t("noOptionsMessage")} placeholder={placeholder ?? t("selectPlaceholder")} unstyled styles={{ input: (base) => ({ ...base, "input:focus": { boxShadow: "none", }, }), }} classNames={classNames(hasError, separators ?? false)} {...selectProps} /> ); }} /> ); };