import React, { useEffect, useState } from 'react' import AsyncSelect from 'react-select/async' import { FormStoreType, FormFields } from '../../../store' import { observer } from 'mobx-react' import { ActionMeta, SingleValue } from 'react-select' export interface SelectOption { label: string value: string } interface SingleSelectProps { name: keyof T loadOptions: (inputValue: string) => Promise formStore: FormStoreType } const CustomSelect = observer( ({ name, loadOptions, formStore }: SingleSelectProps) => { const [selectedOption, setSelectedOption] = useState(null) useEffect(() => { const loadInitialOption = async () => { const currentValue = formStore.fields[name] as string if (currentValue) { const options = await loadOptions('') const selected = options.find((option) => option.value === currentValue) setSelectedOption(selected || null) } } loadInitialOption() }, [formStore.fields, name, loadOptions]) const handleChange = ( newValue: SingleValue, actionMeta: ActionMeta, ) => { setSelectedOption(newValue) if (newValue) { formStore.setField(name, newValue.value as unknown as T[keyof T]) } else { formStore.setField(name, '' as unknown as T[keyof T]) } } return (
{formStore.errors[name] && ( {formStore.errors[name]} )}
) }, ) export default CustomSelect