-
-
Save iamnimnul/3b8a333da678e2e2d5c1635a09e1eef0 to your computer and use it in GitHub Desktop.
Formik Scroll To First Invalid Element W/O Refs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import { connect } from 'formik'; | |
class ErrorFocus extends React.Component { | |
componentDidUpdate(prevProps) { | |
const { isSubmitting, isValidating, errors } = prevProps.formik; | |
const keys = Object.keys(errors); | |
if (keys.length > 0 && isSubmitting && !isValidating) { | |
const selector = `[name="${keys[0]}"]`; | |
const errorElement = document.querySelector(selector); | |
errorElement.focus(); | |
} | |
} | |
render() { | |
return null; | |
} | |
} | |
export default connect(ErrorFocus); |
Author
iamnimnul
commented
Jan 8, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment