Giter Club home page Giter Club logo

final-form-focus's Introduction

🏁 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

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

final-form-focus's People

Contributors

andarist avatar erikras avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

final-form-focus's Issues

After reseting form decorator focuses first invalid field.

Hello! I'm

What is the current behavior?

I use the FormSpy component to watch the submitSucceeded event. If this event occurs, I do this.props.form.reset(). The form's values are updated, and then the first invalid field gets into focus.

What is the expected behavior?

No focus on reset.

Sandbox Link

Example

What's your environment?

  • final-form": "^4.18.4",
  • final-form-focus": "^1.1.2",
  • node: v8.15.1
  • MacOS Mojave
  • Any browser

Providing a custom findInput method

Are you submitting a bug report or a feature request?

Feature Request

What is the current behavior?

I am using custom components in my form (react-select components in this case). They don'r render any input components, so they aren't found with the findInput method that is currently implemented.

What is the expected behavior?

I suggest adding an optional parameter to the createDecorator function, which is used to find the input if provided. Just like the getInputs parameter.

What's your environment?

final-form: "^4.8.3",
react-final-form: "^3.3.1",
Chrome Version 67.0.3396.99 on MacOS 10.13.6
node v10.7.0

Other information

I have forked this and implemented it for my own use. I can create a PR out of it, if needed. But it's only a very simple change.

https://github.com/nik-lampe/final-form-focus/tree/feature/find-input

Add development information about this package

As I understood this package assumes that nps util is installed globally and to build this project one should run nps build. Is it correct? I can add info about it in Readme, but I need your approve.

Is it possible to use final-form-focus without using <form> tag?

Check this demo: https://codesandbox.io/s/react-final-form-focus-on-first-error-ib23b

If you avoid <form> tag and you use just
<button onClick={form.submit} disabled={submitting}>Submit</button>, validation is provided but focus is not there.

Problem with <form onSubmit={handleSubmit} /> is that, that even Enter on textbox will trigger it or even worse every other button without specifying type="".
It would be also helpful to have it without <form> tag

Focusing on React Select

Are you submitting a bug report or a feature request?

Request

What is the current behavior?

Focus does not appear to work out of the box with React Select.

What is the expected behavior?

Seeing as React Select is so popular and people tend to use it with React Final Form so much, could we get this to work out of the box?

Alternatively if there is a simple or well known solution, would anyone mind sharing it here?

Sandbox Link

No link. Using the Third Party link in the examples can show the issue.

What's your environment?

Mac OS 10.15. FireFox or Chrome. Everything latest at the time of this post, but as this is a feature request, I don’t think this pertains?

Other information

Field is incorrectly focussed when using Wizard

Are you submitting a bug report or a feature request?

bug report

What is the current behavior?

When using a Wizard form with final-form-focus, similar to the react final form docs example, linked in the sandbox below, when submitting one page of the form, the first required input on the next page is focussed, even though no error is displayed, this also means the error is then displayed on any click causing blur of the input

What is the expected behavior?

Inputs are only focussed on each page after clicking 'Next' or 'Submit'

Sandbox Link

https://codesandbox.io/s/github/final-form/react-final-form/tree/master/examples/wizard?file=/Wizard.js:1831-1835

What's your environment?

final-form-focus: 1.1.2
final-form: 4.19.1
react-final-form: 6.4.0
browser: Firefox Developer Edition
node version: 12.13.0

Other information

Focus not working with FieldArray and server side validation

Are you submitting a bug report or a feature request?

bug

What is the current behavior?

  1. Create a form with a field array (subForm in the demo) and one field (firstName in the demo)
  2. Add validation to subForm's fields
  3. Add server side validation to firstName
  4. Trigger only the server side validation
  5. The error is displayed, but firstName isn't focused

What is the expected behavior?

firstName should be focused

Sandbox Link

https://codesandbox.io/s/react-final-form-focus-on-first-error-forked-vz1kh?file=/src/validate.js

What's your environment?

final-form 4.20.2
final-form-arrays 3.0.2
final-form-focus 1.1.2
react 16.13.1
react-dom 16.13.1
react-final-form 6.5.0
react-final-form-arrays 3.1.2

Provide custom focus implementation

feature request

It would be nice to specify focus implementation. For example to make smooth scroll to element + Npx above it before focusing:

import createDecorator from 'final-form-focus'

const onFocus = input => {
  const { top } = getOffset(input);
  const scrollTop = top - 50;

  window.scrollTo({
    top: scrollTop,
	behavior: 'smooth',
  });

  input.focus();
}

const focusOnErrors = createDecorator(null, null, onFocus)

Module not found: Can't resolve 'react-final-form'

Hi, I am trying to use this library, but when import import { Form, Field } from 'react-final-form' show this message:
Failed to compile.

./src/components/InputPage1/ContentInputPage1.tsx
Module not found: Can't resolve 'react-final-form' in '/Users/afonseca/Desktop/hcw/hcw/src/components/InputPag

I try removing the package-lock.json and enter npm install and npm start but is not working.

This is my package.json
{
"name": "hcw",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.3.2",
"@testing-library/user-event": "^7.1.2",
"@types/jest": "^25.1.4",
"@types/node": "^13.9.8",
"@types/react": "^16.9.29",
"@types/react-dom": "^16.9.5",
"axios": "^0.19.0",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-redux": "^7.1.3",
"react-scripts": "3.4.1",
"react-semantic-ui-datepickers": "^2.4.0",
"semantic-ui-css": "^2.4.1",
"semantic-ui-react": "^0.88.2",
"typescript": "^3.8.3"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}

Thank such much .

Hello, I don't know if I use the select component, how to make the interface slide to the corresponding position when the validation fails. Can you tell me how to use findInput? Can it fulfill my needs?

@nik-lampe @erikras Hello, I don't know if I use the select component, how to make the interface slide to the corresponding position when the validation fails. Can you tell me how to use findInput? Can it fulfill my needs?
PS: It is best to have a demo demo, thank you very much.

Originally posted by @Gzbox in #2 (comment)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.