|
| 1 | +import type { JSX, ReactElement } from 'react' |
| 2 | + |
| 3 | +import { |
| 4 | + Controller as HookForm$Controller, |
| 5 | + type FieldValues, |
| 6 | + type ControllerFieldState as HookForm$ControllerFieldState, |
| 7 | + type ControllerProps as HookForm$ControllerProps, |
| 8 | + type ControllerRenderProps as HookForm$ControllerRenderProps, |
| 9 | + type FieldPath as HookForm$FieldPath, |
| 10 | + type UseFormStateReturn as HookForm$UseFormStateReturn, |
| 11 | +} from 'react-hook-form' |
| 12 | + |
| 13 | +export interface FieldRenderFnProps< |
| 14 | + TFieldValues extends FieldValues = FieldValues, |
| 15 | + TName extends HookForm$FieldPath<TFieldValues> = HookForm$FieldPath<TFieldValues>, |
| 16 | +> { |
| 17 | + /** |
| 18 | + * The object which contains the field props. This object contains the |
| 19 | + * properties, *e.g.*, which are injected into the input element to control |
| 20 | + * the field, *e.g.*, the field `value`, `onChange`, `onBlur`, and `ref`. |
| 21 | + * |
| 22 | + * @see https://react-hook-form.com/docs/usecontroller |
| 23 | + */ |
| 24 | + field: HookForm$ControllerRenderProps<TFieldValues, TName> |
| 25 | + /** |
| 26 | + * The object which contains the field state. This object contains the |
| 27 | + * properties such, *e.g.*, the field is touched, dirty, or invalid. |
| 28 | + * |
| 29 | + * @see {@link HookForm$ControllerFieldState} |
| 30 | + */ |
| 31 | + fieldState: HookForm$ControllerFieldState |
| 32 | + /** |
| 33 | + * The object which contains the form state. This object contains the |
| 34 | + * properties, *e.g.*, the form is submitting, submitting, or dirty. |
| 35 | + * |
| 36 | + * @see {@link HookForm$UseFormStateReturn} |
| 37 | + */ |
| 38 | + formState: HookForm$UseFormStateReturn<TFieldValues> |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * The type which defines the parameters and return type of the render function |
| 43 | + * passed down to the `FieldController` component. |
| 44 | + */ |
| 45 | +export type FieldRenderFn< |
| 46 | + TFieldValues extends FieldValues = FieldValues, |
| 47 | + TName extends HookForm$FieldPath<TFieldValues> = HookForm$FieldPath<TFieldValues>, |
| 48 | +> = (props: FieldRenderFnProps<TFieldValues, TName>) => ReactElement |
| 49 | + |
| 50 | +/** |
| 51 | + * @internal The type which defines the props for the `FieldController` |
| 52 | + * component. This type is used to pick the props from the |
| 53 | + * `FieldController` component and pass them down to the `Controller`. |
| 54 | + */ |
| 55 | +type PickedFieldControllerProps = Omit<HookForm$ControllerProps, 'render'> |
| 56 | + |
| 57 | +/** |
| 58 | + * The interface which defines the props for the `FieldController` component. |
| 59 | + */ |
| 60 | +export interface FieldControllerProps extends PickedFieldControllerProps { |
| 61 | + /** |
| 62 | + * The `children` prop is defined as a render function which will be passed |
| 63 | + * down to the `FieldController` component. This function will be called with |
| 64 | + * the `FieldController` props. |
| 65 | + * |
| 66 | + * @see {@link FieldRenderFn} |
| 67 | + */ |
| 68 | + children: FieldRenderFn |
| 69 | +} |
| 70 | + |
| 71 | +/** |
| 72 | + * The `FieldController` is a component which wraps the `Controller` component |
| 73 | + * from the `react-hook-form` library, connecting the field to the form state. |
| 74 | + * |
| 75 | + * @example |
| 76 | + * ```tsx |
| 77 | + * <FieldController name="email" control={control} rules={{ required: true }}> |
| 78 | + * {({ field, fieldState, formState }) => ( |
| 79 | + * <input type="email" {...field} /> |
| 80 | + * })} |
| 81 | + * </FieldController> |
| 82 | + * ``` |
| 83 | + * |
| 84 | + * @props {@link FieldControllerProps} |
| 85 | + */ |
| 86 | +function FieldController({ children, ...props }: FieldControllerProps): JSX.Element { |
| 87 | + return <HookForm$Controller {...props} render={children} /> |
| 88 | +} |
| 89 | + |
| 90 | +export default FieldController |
0 commit comments