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.

Option to blur input box on change

See original GitHub issue

Great lib, thx. However I want our single-select box to blur when an option is selected rather than remain focused with cursor in the input. To accomplish this right now it seems I not only have to ...querySelector('.Select-input input').blur() in the onChange handler but I have to put it in a timeout. Is there an easier way to accomplish this and/or get it as an option?

Issue Analytics

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

github_iconTop GitHub Comments

4reactions
Jaraujo6commented, May 11, 2020

I think this issue should be re-opened as well. I am currently rendering a React-Select component in a Formik form, and was running into issues because there wasn’t an obvious way to run the React-Select onBlur after onChange.

To be more specific, the form has a validation that says the input field is a required field. To ensure this, my onBlur handler sets the Formik field to touched which triggers the validation handler to run and surface the error:

<Select
  ...
  onBlur={() => setFieldTouched()}
/>

When I do select an option I am setting the Formik field manually in the Select onChange:

<Select
  ...
  onChange={({ value }: { value: string; label: string }) => setFieldValue(value)}
/>

Lastly, I also have a design requirement that the dropdown loses focus on selection, which is easily handled with the blurInputOnSelect flag.

My issue is that enabling the blurInputOnSelect forces the onBlur handler to run before the onChange handler, so the validation error was surfacing even though the field was valid. After much trial and error, the only solution I could think of was to use setTimeout to force the Formik handler to get added to behind the onChange handler in the event queue.

<Select
  ...
  onChange={({ value }: { value: string; label: string }) => setFieldValue(value)}
  onBlur={() => {
       setTimeout(() => setFieldTouched(), 1);
   }}
  blurInputOnSelect
/>

This solution is not ideal as it is best practice to avoid async functions in react outside of hooks. Is it possible to introduce a blurOnChange flag or modify the blurOnInputSelect flag to mimic this behavior? Is there a reason the onBlur has to occur before onChange?

@JedWatson if you have a moment I would greatly appreciate your insight on this matter.

1reaction
Vandell63commented, Jun 15, 2018

Hey there! maybe it can help for material-ui 1.. user but can work for others too. I use an Input and add the React-Select component in inputComponent of Input props. Then add your ref to your select. You can now access it with : this.refs.select.input.input.blur() ( twice input here, cause the first one ref the AutosizeInput and the second one is the input Element) Hope it help someone.

Read more comments on GitHub >

github_iconTop Results From Across the Web

input change event triggered on blur - javascript - Stack Overflow
when I change the value of a text input and then blur that input it triggers the input change event again. How can...
Read more >
Element: blur event - Web APIs | MDN
The blur event fires when an element has lost focus. The event does not bubble, but the related focusout event that follows does...
Read more >
The difference between onBlur vs onChange for React text ...
onBlur is fired when you have moved away from an object without necessarily having changed its value. onChange is only called when you...
Read more >
onblur Event - W3Schools
The onblur event occurs when an element loses focus. The onblur event is often used on input fields. The onblur event is often...
Read more >
Focusing: focus/blur - The Modern JavaScript Tutorial
The focus event is called on focusing, and blur – when the element loses the focus. Let's use them for validation of an...
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