✨(react) react-hook-form Input example
Our form elements needs to be usable with react-hook-form
This commit is contained in:
26
packages/react/src/tests/reactHookFormUtils.tsx
Normal file
26
packages/react/src/tests/reactHookFormUtils.tsx
Normal 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)}`);
|
||||
}
|
||||
Reference in New Issue
Block a user