Scroll to the first error in your Formik form and set focus
Wrapper around scroll-to-element that scrolls to the first error element in Formik.
Install the package locally within you project folder with your package manager:
With npm
:
npm install formik-error-focus
With yarn
:
yarn add formik-error-focus
With pnpm
:
pnpm add formik-error-focus
import React from "react";
import { Formik, Field, Form } from "formik";
import FormikErrorFocus from "formik-error-focus";
export const Signup = () => (
<div>
<h1>My Uncool Persisted Form</h1>
<Formik
onSubmit={(values) => console.log(values)}
initialValues={{ firstName: "", lastName: "", email: "" }}
render={(props) => (
<Form className="whatever">
<Field name="firstName" placeholder="First Name" />
<Field name="lastName" placeholder="Last Name" />
<Field name="email" type="email" placeholder="Email Address" />
<button type="submit">Submit</button>
<FormikErrorFocus
// See scroll-to-element for configuration options: https://www.npmjs.com/package/scroll-to-element
offset={0}
align={"top"}
focusDelay={200}
ease={"linear"}
duration={1000}
/>
</Form>
)}
/>
</div>
);
For all configuration options, please see the API docs.
Got an idea for a new feature? Found a bug? Contributions are welcome! Please open up an issue or make a pull request.