(react) react-hook-form Input example

Our form elements needs to be usable with react-hook-form
This commit is contained in:
Romain Le Cellier
2023-07-26 16:52:22 +02:00
parent bdc08cf043
commit b72d0d5c90
5 changed files with 99 additions and 19 deletions

View File

@@ -0,0 +1,26 @@
import { FieldValues, FormState } from "react-hook-form";
export function getFieldState<FormValues extends FieldValues>(
field: keyof FormValues,
formState: FormState<FormValues>,
) {
if (field in formState.errors) {
return "error";
}
return "default";
}
export function getFieldErrorMessage<FormValues extends FieldValues>(
field: keyof FormValues,
formState: FormState<FormValues>,
): string {
const errorMessage = formState.errors[field]?.message;
if (!errorMessage) {
return "";
}
return errorMessage as string;
}
export function onSubmit<FormValues>(data: FormValues) {
alert(`Submited form values: ${JSON.stringify(data)}`);
}