Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide custom focus implementation #13

Open
likerRr opened this issue Aug 15, 2019 · 3 comments
Open

Provide custom focus implementation #13

likerRr opened this issue Aug 15, 2019 · 3 comments

Comments

@likerRr
Copy link

likerRr commented Aug 15, 2019

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)
@AndyOGo
Copy link

AndyOGo commented Feb 26, 2020

@erikras Any plans to support custom focus implementations?

@mattrabe
Copy link

My team has rolled its own solution for this, which focuses on the input and smooth-scrolls to it using scrollIntoView. Therefore, we stopped using this library. Our solutions looks like this:

import React from 'react'
import { Form as FinalForm } from 'react-final-form'

const Form = props => (
  <FinalForm {...props}>
    {({
      form: finalFormApi,
      handleSubmit,
    }) => (
      <form
        finalFormApi={finalFormApi}
        {...props}
        onSubmit={event => {
          const invalidField = finalFormApi.getRegisteredFields().find(field => finalFormApi.getFieldState(field).invalid)

          if (invalidField) {
            const targetInput = document.querySelector(`[name="${invalidField}"]`)
            const targetLabel = document.querySelector(`label[for="${invalidField}"]`) || targetInput
            if (targetInput) {
              targetInput.focus({ preventScroll: true })
              targetLabel.scrollIntoView({
                behavior: 'smooth',
                block: 'center',
              })
            }
          }

          handleSubmit(event)
        }}
      >
        {props.children}
      </form>
    )}
  </FinalForm>
)

export default Form

@TheRusskiy
Copy link

FYI, here's a PR for this: #25

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants