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.

inputProps.onBlur not working on mobile

See original GitHub issue
  • Try out the homepage or codepen examples on mobile (iOS or Android). No issue on desktop browsers.
  • Steps to reproduce:
    1. Select the component, type a character to open the dropdown suggestion list.
    2. Select any item in the dropdown; e.g. scroll the list if possible.
    3. Tap elsewhere on the page (e.g. another dropdown).
    4. Observe that the original component doesn’t “close” (dropdown still open).
  • Expected behavior: The dropdown should have closed (and inputProps.onBlur event fired) when another component is selected.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
scotta-koorongcommented, Jul 31, 2017

Hey all,

The issue is that justSelectedSuggestion is set to true while you scroll and then blocks the .blur() in the onBlur function when you try and blur it afterwards. The displaying of the suggestions is not intrinsically bound to whether the suggestions are focused at the code level. So if justSelectedSuggestion is true then it can’t blur.

On mousedown sets justSelectedSuggestion to true even if you’re just scrolling. But it doesn’t properly unset.

if (!_this2.justSelectedSuggestion) {
    _this2.onBlur();
    _this2.onSuggestionsClearRequested();
}

This blocking with mousedown and not releasing is also linked to https://github.com/moroshko/react-autosuggest/issues/412

For this specific scroll event, a fix is to do this:

Look for these handlers

onMouseEnter: _this3.onSuggestionMouseEnter,
onMouseLeave: _this3.resetHighlightedSuggestionOnMouseLeave,
onMouseDown: _this3.onSuggestionMouseDown,
onTouchStart: _this3.onSuggestionMouseDown, // Because on iOS `onMouseDown` is not triggered

And add this underneath onTouchMove: _this3.onTouchMove,

Yours might look a little different as I’m just working off the dist code.

Now add this function below the this.onSuggestionMouseDown function

this.onTouchMove = function (e) {
    _this3.justSelectedSuggestion = false;      
  };

This means that moving your finger while touching will allow focus again.

However to fully fix this blur() blocking it might need some heavy changes or to catch other cases like this.

I don’t want to assume, since I haven’t been able to understand the focusing and blurring side of the code. But could aria-activedescendant be used to keep the input focused instead of justSelectedSuggestion? You can see that if you select a suggestion it unfocuses the input and then refocuses it just a moment after. This can be seen in the codepen examples by applying a colored border when focused. I know aria-activedescendant is already on it, but it doesn’t seem to be working as intended.

0reactions
aberezkincommented, Sep 7, 2018

@moroshko Hi, can you please explain why these lines are required?

onSuggestionMouseDown = () => {
  this.justSelectedSuggestion = true;
};

Looks like they are causing the issue and it’s not iOS related.

You can reproduce it on desktop:

  1. Focus input with autocomplete
  2. Make mouse down on any suggestion
  3. Move your mouse off the suggestion list
  4. Click anywhere except the suggestion list

Suggestion list will remain on the screen and can be removed only by clicking at any suggestion because this.justSelectedSuggestion is set false only in onSuggestionClick event handler by these lines:

setTimeout(() => {
    this.justSelectedSuggestion = false;
  });

UPD: Also looks like it’s identical to #412

Read more comments on GitHub >

github_iconTop Results From Across the Web

React material UI Select box onBlur event not working
Material UI's Select component doesn't have onBlur prop. Try onClose instead. https://material-ui.com/api/select/#props.
Read more >
react hook form onblur not working - You.com | The AI Search ...
You're missing to spread the InputProps , so basically you're overwriting all the default properties of your InputProps . The function signature of...
Read more >
onBlur not working on Mobile Devices - SAP Community
I've created an ITS application and generated the html code for the screen. I've added javascript code that triggers the onBlur event upon...
Read more >
Initializing focus state in React: you might be doing it wrong
function Input(props) { const [hasFocus, setFocus] = useState(false); ... In terms of conditional rendering, you can hide or show elements, but not mount...
Read more >
react-phone-number-input - npm
Start using react-phone-number-input in your project by running `npm i react-phone-number-input`. There are 284 other projects in the npm ...
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