import React from 'react'; import Select from 'react-select'; import { Controller, Path } from 'react-hook-form'; import { FieldValues } from 'react-hook-form/dist/types/fields'; import { FieldPath } from 'react-hook-form/dist/types/path'; import { FieldPathValue, UnpackNestedValue } from 'react-hook-form/dist/types'; import { FormControlledComponent } from '../../models/form-component'; import { AbstractFormItem, AbstractFormItemProps } from './abstract-form-item'; interface FormSelectProps extends FormControlledComponent, AbstractFormItemProps { options: Array>, valueDefault?: TOptionValue, onChange?: (value: TOptionValue) => void, placeholder?: string, clearable?: boolean, } /** * Option format, expected by react-select * @see https://github.com/JedWatson/react-select */ type selectOption = { value: TOptionValue, label: string }; /** * This component is a wrapper for react-select to use with react-hook-form */ export const FormSelect = ({ id, label, tooltip, className, control, placeholder, options, valueDefault, error, warning, rules, disabled, onChange, readOnly, clearable, formState }: FormSelectProps) => { /** * The following callback will trigger the onChange callback, if it was passed to this component, * when the selected option changes. */ const onChangeCb = (newValue: TOptionValue): void => { if (typeof onChange === 'function') { onChange(newValue); } }; return ( } control={control} defaultValue={valueDefault as UnpackNestedValue>>} render={({ field: { onChange, value, ref } }) =>