final-form-focus
DefinitelyTyped icon, indicating that this package has TypeScript declarations provided by the separate @types/final-form-focus package

1.1.2Ā ā€¢Ā PublicĀ ā€¢Ā Published

šŸ Final Form Focus šŸ§

NPM Version NPM Downloads Build Status codecov.io styled with prettier

Decorator for šŸ Final Form that will attempt to apply focus to the first field with an error upon an attempted form submission.


Demo


Installation

npmĀ installĀ --saveĀ final-formĀ final-form-focus

or

yarnĀ addĀ final-formĀ final-form-focus

Usage

šŸ Final Form Usage

importĀ {Ā createFormĀ }Ā fromĀ 'final-form'
importĀ createDecoratorĀ fromĀ 'final-form-focus'
Ā 
//Ā CreateĀ Form
constĀ formĀ =Ā createForm({Ā onSubmitĀ })
Ā 
//Ā CreateĀ Decorator
constĀ decoratorĀ =Ā createDecorator()
Ā 
//Ā DecorateĀ form
constĀ undecorateĀ =Ā decorator(form)
Ā 
//Ā UseĀ formĀ asĀ normal

šŸ React Final Form Usage

importĀ ReactĀ fromĀ 'react'
importĀ {Ā Form,Ā FieldĀ }Ā fromĀ 'react-final-form'
importĀ createDecoratorĀ fromĀ 'final-form-focus'
Ā 
constĀ focusOnErrorsĀ =Ā createDecorator()
...
<Form
Ā Ā onSubmit={submit}
Ā Ā decorators={[Ā focusOnErrorsĀ ]}Ā //Ā <---------Ā šŸ˜Ž
Ā Ā validate={validate}
Ā Ā render={({Ā handleSubmitĀ })Ā =>
Ā Ā Ā Ā <formĀ onSubmit={handleSubmit}>
Ā 
Ā Ā Ā Ā Ā Ā ...Ā inputsĀ hereĀ ...
Ā 
Ā Ā Ā Ā </form>
Ā Ā }
/>

Example

Focus On Error Example

Demonstrates how šŸ Final Form Focus šŸ§ works with šŸ React Final Form.

API

createDecorator: (getInputs?: GetInputs, findInput?: FindInput) => Decorator

A function that takes an optional function to collect a list of focusable inputs on the page and provides a šŸ Final Form Decorator that will focus on the top-most input on the page with an error when a form submission fails. If no getInputs parameter is provided, it will use a generic one that will return all inputs that appear in document.forms. If no findInput parameter is provided, it will use a generic one that matches the name attribute of the focusable input with the path in the error object.

getFormInputs: (formName: string) => GetInputs

A GetInputs generator that will narrow the list of inputs down to those contained in the named form, i.e. document.forms[formName].

Types

FocusableInput: { name: string, focus: () => void }

A light abstraction of any input that has a name property and upon which focus() may be called.

GetInputs: () => FocusableInput[]

A function that collects a list of focusable inputs that exist on the page.

FindInput: (FocusableInput[], {}) => ?FocusableInput

A function that returns the first element in a list of focusable inputs that has an error

Dependentsā€‚(49)

Package Sidebar

Install

npm i final-form-focus

Weekly Downloads

58,858

Version

1.1.2

License

MIT

Unpacked Size

21.3 kB

Total Files

11

Last publish

Collaborators

  • erikras