Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
focus-formik-error
Advanced tools
This component allows you to automatically focus errors in your Formik form, the focus runs after the validation.
npm i focus-formik-error
You can use it by including it inside the Formik provider or also use it with useFormik hook as follow.
Include the <ConnectedFocusError />
component inside the Formik context provider.
import React from 'react'
import { Formik, Form, Field, ErrorMessage } from 'formik'
import { ConnectedFocusError } from 'focus-formik-error'
const Basic = () => (
<Formik
initialValues={{
name: 'Poseidon',
}}
onSubmit={(values, { setSubmitting, setErrors }) => {
// Simulates server side validation
setTimeout(() => {
const errors = {} as any
if (!values.name) {
errors.country = 'Name is required'
}
setErrors(errors)
setSubmitting(false)
}, 500)
}}>
{({ errors, isSubmitting }) => (
<Form className={'form'}>
<ConnectedFocusError />
<div className={'input-container'}>
<label>Name </label>
<Field
type='text'
name='name'
className={`input ${errors.name ? 'input-error' : ''}`}
/>
<ErrorMessage name='name' component='div' className={'error-text'} />
</div>
<button
type='submit'
disabled={isSubmitting}
style={{ marginBottom: '5em' }}>
{isSubmitting ? 'Submitting' : 'Submit'}
</button>
</Form>
)}
</Formik>
)
export default Basic
Include the <FocusError />
component inside you form and pass formik
as props.
NOTE: Follow the same pattern in the name attribute of the input component used in the
initialValues
or the focus is not going to work. I.g:name="values.name"
import React from 'react'
import { useFormik } from 'formik'
import { FocusError } from 'focus-formik-error'
const UseFormikExample = () => {
const formik = useFormik({
initialValues: {
name: '',
},
onSubmit: (values, { setSubmitting, setErrors }) => {
setTimeout(() => {
// Simulates server side validation
const errors = {} as any
if (!values.name) {
errors.country = 'Name is required'
}
setErrors(errors)
setSubmitting(false)
}, 400)
},
})
return (
<form onSubmit={formik.handleSubmit}>
<FocusError formik={formik} />
<input
id='values.name'
name='values.name'
type='text'
onChange={formik.handleChange}
value={formik.values.name}
/>
{formik.errors.name}
<button type='submit' disabled={formik.isSubmitting}>
Submit
</button>
</form>
)
}
export default UseFormikExample
Prop | Type | Default | Description |
---|---|---|---|
focusDelay | number (ms) | 100 | Time in ms to execute the focus in the error component |
onFocus | Function | undefined | Function, which executes after an element was focussed |
I actively welcome pull requests for improvements or fixes.
FAQs
Simple auto focus to first Formik error
The npm package focus-formik-error receives a total of 4,239 weekly downloads. As such, focus-formik-error popularity was classified as popular.
We found that focus-formik-error demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.