question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Custom onBlur on Field overrides default behavior

See original GitHub issue

Bug, Feature, or Question?

Bug/Feature

Current Behavior

a Field with a custom onBlur removes the default validation/touched behavior

<Field name="fname" onBlur={() => console.log("custom")} />

https://codesandbox.io/s/yk9mmj1kwx

Desired Behavior

custom onBlur prop will be chained to the internal handleBlur

Suggested Solutions

in handleBlur

if (this.props.onBlur) this.props.onBlur()

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
Andreycocommented, Feb 26, 2019

How about this?

function formikOnBlur(evt) {
  // formiks default handler
}

function getFieldProps(props) {
  return {
    ...props,
    onBlur: (...args) => {
      formikOnBlur(...args);
      props.onBlur && props.onBlur(...args);
    },
    // ... other props
  };
}

<Field name="email">
  {field => (
    <input
      {...field.getFieldProps({
        onBlur: () => console.log('blur'),
      })}
    />
  )}
</Field>

props getter would chain “overrides” props while still keeps Formik work.

1reaction
seanderscommented, Apr 4, 2019

I’m a big fan of the approach outlined by @Andreyco. This would follow the ‘prop getters’ pattern as is outlined here: https://kentcdodds.com/blog/how-to-give-rendering-control-to-users-with-prop-getters

It’s used to great effect in https://github.com/downshift-js/downshift

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Formik: My custom onBlur override default touched ...
I just started learning to code 2 months ago, this week I tried to work with Formik and had this issue where I...
Read more >
Validate Field onBlur() using Props Bindings in mobx-react-form
If we want to override the built-in field props, we have a good chance here passing some props to the bind() method. Then...
Read more >
blur() - CSS: Cascading Style Sheets - MDN Web Docs - Mozilla
The blur() CSS function applies a Gaussian blur to the input image. Its result is a <filter-function> . Try it. CSS Demo: blur()....
Read more >
Custom Widgets and Fields - react-jsonschema-form documentation
You can override any default field and widget, including the internal widgets like the CheckboxWidget that ObjectField renders for boolean values.
Read more >
Deep dive - Summernote
You can override the default list and specify a custom one. ... A module named editor supports several methods for editor's basic behavior...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found