♻️(react) fix rhf reset with Input

when using reset(), Input field value wasn't updated
This commit is contained in:
Romain Le Cellier
2023-09-21 16:18:01 +02:00
parent e2738495dc
commit 034e299407
4 changed files with 64 additions and 8 deletions

View File

@@ -0,0 +1,26 @@
import { Controller, useFormContext } from "react-hook-form";
import React from "react";
import { Input, InputProps } from ".";
export const RhfInput = (props: InputProps & { name: string }) => {
const { control, setValue } = useFormContext();
return (
<Controller
control={control}
name={props.name}
render={({ field, fieldState }) => {
return (
<Input
{...props}
aria-invalid={!!fieldState.error}
state={fieldState.error ? "error" : "default"}
text={fieldState.error?.message}
onBlur={field.onBlur}
onChange={(e) => setValue(field.name, e.target.value)}
value={field.value}
/>
);
}}
/>
);
};