import React, { SelectHTMLAttributes } 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'; interface FabSelectProps extends SelectHTMLAttributes, FormControlledComponent { id: string, label?: string, options: Array>, valueDefault?: TOptionValue, } /** * 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 FabSelect = ({ id, label, className, control, placeholder, options, valueDefault, error, rules, disabled }: FabSelectProps) => { const classNames = ` fab-select ${className || ''} ${error && error[id] ? 'is-incorrect' : ''} ${rules && rules.required ? 'is-required' : ''} ${disabled ? 'is-disabled' : ''}`; return ( {label && {label} } } control={control} defaultValue={valueDefault as UnpackNestedValue>>} render={({ field: { onChange, value, ref } }) => c.value === value)} onChange={val => onChange(val.value)} placeholder={placeholder} options={options} /> } /> ); };
{label}