Skip to content

A react.js hook for scrolling to the first failing error once you submit a form.

Notifications You must be signed in to change notification settings

dieguezz/react-scroll-to-first-input-error

Repository files navigation

react-scroll-to-first-error

A react hook for doing scroll to an invalid input on onSubmit. Works with any standard form and with other libraries like material-ui

NPM JavaScript Style Guide

Install

npm install --save react-scroll-to-first-error

Usage

      import React, { useRef } from 'react'
      import useScrollToError from 'react-scroll-to-error'

      function App() {
        const formEl = useRef(null);

        useScrollToError(formEl);

        return (
          <div>
            <form ref={formEl} noValidate>
              <input type="text" name="name" id="name" required />
              <input type="text" name="phone" id="name" />
              {/* we force height here for testing purposes */}
              <div style={{ height: "100vh" }} />
              <button type="submit">Submit</button>
            </form>
          </div>
        );
        }

License

GPL-3.0 © dieguezz

About

A react.js hook for scrolling to the first failing error once you submit a form.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published