"use client"; import { ReactNode } from "react"; import { Controller, FieldPathByValue, FieldValues, UseControllerProps, useFormContext, } from "react-hook-form"; import { PatchedControllerRenderProps } from "./types.js"; import { WithRHFError } from "./WithRHFError.js"; type Props< TValues extends FieldValues, TValueType, TName extends FieldPathByValue = FieldPathByValue< TValues, TValueType >, > = { name: TName; rules?: UseControllerProps["rules"]; children: ( props: PatchedControllerRenderProps ) => ReactNode; }; // Helper component for custom controlled react-hook-form connected components. export function ControlledFormInput< TValues extends FieldValues, TValueType, TName extends FieldPathByValue = FieldPathByValue< TValues, TValueType >, >({ name, rules, children }: Props) { const { control } = useFormContext(); return ( (
{children( // our controller components don't allow ChangeEvent events; // also, Typescript is not smart enough to infer that FieldPathByValue is the inverse of FieldPathValue field as Omit & { onChange: (event: TValueType) => void; } )}
)} />
); } /* Usage example: * * * {({ onChange, value }) => } * */