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

onBlur handler on useField hook #2064

Closed
hatamsoyunov opened this issue Nov 25, 2019 · 2 comments
Closed

onBlur handler on useField hook #2064

hatamsoyunov opened this issue Nov 25, 2019 · 2 comments

Comments

@hatamsoyunov
Copy link

πŸ™Œ onBlur handler and real time validation on useField hook

Hello!

I started learning and practicing Formik, but ran into one problem. My project uses custom placeholders that move up with the focus field.

What do I need for this❓

  • Functional component with isFocused state
  • Change it with onFocus and onBlur
  • Depending on the state, change the position of the placeholder

πŸ’ͺ Started to do

To do this, I made a component CustomField with the useField()hook. Works, cool 😎

But later he noticed that real time validation and error messages stopped working πŸ€¦β€β™‚οΈ. Because I redefined onBlur handler. After searching, studying the problem did not find a solution using onBlur handler without interfering with Formik.

Prepared Sandbox πŸ‘‰ Sandbox

πŸ™ Please tell me what and where I missed, how can this be implemented? Thanks!

@Kinbaum
Copy link

Kinbaum commented Nov 26, 2019

In your handleBlur function, you can call field.onBlur and pass the synthetic event to it.
Something like this:

function handleBlur(e) {
    // Call the Formik onBlur event before your custom stuff
    field.onBlur(e);

    if (!field.value.length > 0) {
        onFocus(false);
    }
}

@hatamsoyunov
Copy link
Author

@Kinbaum Oh, it works, thanks a lot! πŸ˜€

Update πŸ‘‰ Sandbox Link

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

2 participants