项目作者: final-form

项目描述 :
🏁 Final Form "decorator" that will attempt to apply focus to the first field with an error upon an attempted form submission
高级语言: JavaScript
项目地址: git://github.com/final-form/final-form-focus.git
创建时间: 2018-03-19T08:53:59Z
项目社区:https://github.com/final-form/final-form-focus

开源协议:MIT License

下载


🏁 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

  1. npm install --save final-form final-form-focus

or

  1. yarn add final-form final-form-focus

Usage

🏁 Final Form Usage

  1. import { createForm } from 'final-form'
  2. import createDecorator from 'final-form-focus'
  3. // Create Form
  4. const form = createForm({ onSubmit })
  5. // Create Decorator
  6. const decorator = createDecorator()
  7. // Decorate form
  8. const undecorate = decorator(form)
  9. // Use form as normal

🏁 React Final Form Usage

  1. import React from 'react'
  2. import { Form, Field } from 'react-final-form'
  3. import createDecorator from 'final-form-focus'
  4. const focusOnErrors = createDecorator()
  5. ...
  6. <Form
  7. onSubmit={submit}
  8. decorators={[ focusOnErrors ]} // <--------- 😎
  9. validate={validate}
  10. render={({ handleSubmit }) =>
  11. <form onSubmit={handleSubmit}>
  12. ... inputs here ...
  13. </form>
  14. }
  15. />

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