13 lines
350 B
TypeScript
13 lines
350 B
TypeScript
|
|
import type { FieldValues, Path, UseFormReturn } from "react-hook-form";
|
||
|
|
|
||
|
|
export const focusFirstInputFormError = <T extends FieldValues>(form: UseFormReturn<T>) => {
|
||
|
|
const errors = form.formState.errors;
|
||
|
|
const firstKey = Object.keys(errors)[0] as keyof T | undefined;
|
||
|
|
|
||
|
|
if (firstKey) {
|
||
|
|
form.setFocus(firstKey as Path<T>);
|
||
|
|
}
|
||
|
|
|
||
|
|
return;
|
||
|
|
};
|