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.

Re-focus input after `clear()`

See original GitHub issue

I am using the clear() method to implement input box clearing on mobile devices, i.e. when tapping on a small “x” in a circle. It kinda works, but the fact that clear() will automatically also blur the input field feels a bit weird. I have to focus() it immediately, and this causes flickering. Thus, it would be better if clear() had an option to prevent blurring.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
ericgiocommented, Oct 31, 2017

This works for me with no flickering:

<div>
  <Typeahead
    options={[...]}
    ref={(ref) => this._typeahead = ref}
  />
  <button
    onClick={() => {
      const instance = this._typeahead.getInstance();
      instance.clear();
      instance.focus();
    }}>
    Clear
  </button>
</div>
1reaction
ericgiocommented, Feb 21, 2018

As of v2.4.0, the input retains focus when clicking the clear button. Note that if the button is focused via keystroke (eg: tabbing), the input will blur and won’t automatically regain focus on clear.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Set focus back to input after resetting form - Stack Overflow
I have a form that has an input field that has autofocus with a reset button. When resetting the form, I would like...
Read more >
How To Clear Input Field on Focus - W3Schools
Learn how to clear an input field on focus. Click on the input field to clear it: Clear Input Field on Focus. Example....
Read more >
ReactBits: Clearable Input with Autofocus | by Luis Guerrero
An input with a clear button inside; The clear button should only be displayed if the input is not empty and it is...
Read more >
How to clear an input field on focus with JavaScript
JavaScript has a onfocus() event that occurs when the element gets focused. This onfocus() event can be used to clear the input field....
Read more >
Clear Field on Focus - CSS-Tricks
I am trying to tweak the CSS code above to clear my form fields. ... The only script for the mini cart is...
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