📝(react) add RHF examples
Rework a bit the recent work made on RHF example to make some component more generic, such as RhfSelect and RhfDatepicker, which is based on a design using RHF context hooks to provide a seamless integration. Fixes #144
This commit is contained in:
@@ -1,26 +0,0 @@
|
||||
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