π Final Form "decorator" that will attempt to apply focus to the first field on the form.
npm install --save final-form-focus-on-first-field
or
yarn add final-form-focus-on-first-field
import React from 'react';
import { Form, Field } from 'react-final-form';
import createFocusOnFirstFieldDecorator from 'final-form-focus-on-first-field';
const focusOnFirstFieldDecorator = createFocusOnFirstFieldDecorator();
const MyForm = () => (
<Form
onSubmit={submit}
decorators={[focusOnFirstFieldDecorator]} // <---------
validate={validate}
render={({ handleSubmit }) =>
<form onSubmit={handleSubmit}>
... inputs here ...
</form>
}
/>
);
import React, { useMemo } from 'react';
import { Form, Field } from 'react-final-form';
import createFocusOnFirstFieldDecorator from 'final-form-focus-on-first-field';
const MyForm = ({ focusOnFields }) => {
const focusOnFirstFieldDecorator = useMemo(() => createFocusOnFirstFieldDecorator(focusOnFields), []);
return (
<Form
onSubmit={submit}
decorators={[focusOnFirstFieldDecorator]} // <---------
validate={validate}
render={({ handleSubmit }) =>
<form onSubmit={handleSubmit}>
... inputs here ...
</form>
}
/>
);
};
...
// Turn off focus on fields
<MyForm focusOnFields={false} />
// Try to focus on field with name `name` or `description`
<MyForm focusOnFields={['name', 'description']} />
Demonstrates how π Final Form Focus On First Field works with π React Final Form.
A function that takes a set of field names to focus on and returns a π Final Form Decorator
.
If focusOnFields == []
or not set then decorator will try to focus on form registered fields.
If focusOnFields == false
then decorator will not focus on any fields.