JedWatson / react-select

The Select Component for React.js
https://react-select.com/
MIT License
27.63k stars 4.13k forks source link

Async: defaultOptions={true} gives strange behaviour when lodash debounce function is in use #5359

Open jphawk opened 2 years ago

jphawk commented 2 years ago

Hello,

I noticed that AsyncSelect behaviour is weird when I use a lodash debounce function together with defaultOptions={true}. When I use keyboard to focus the Select component, I can start typing and both loading state in the menu and options appear as intended. However, when I click the Select component, I can see a Loading state in the Menu for a moment which I think isn't supposed to be visible and then it disappears. Looks like there is some difference in handling this between onClick and onFocus events.

Here is a Sandbox where you can check this bug.

How to reproduce?

  1. On first render click on AsyncSelect with an empty input
  2. Menu with Loading state appears which is not an intended behaviour. If the Menu with Loading doesn't appear, try reloading the page and it will be there.
  3. After that component works as intended. Loading state doesn't appear on next clicks with an empty input.

Can be compared to the keyboard behaviour:

  1. On first render Tab to AsyncSelect
  2. Menu with Loading state doesn't appear
  3. Component works as intended

Are you reporting a bug or runtime error?

Bug

Rall3n commented 2 years ago

@jphawk If I understand this correctly the main problem you encounter is that the "Loading..." text inside the menu is visible on click, but not on keyboard focus immediately after first render?

First, click and keyboard focus have different behaviors, because <select> element opens on click and not on keyboard focus. But this behavior is controllable using the openMenuOnClick and openMenuOnFocus props.

Second, the "Loading..." text is visible because the Select is still loading your defaultOptions after first render due to the 500ms debounce and the 1s loading timeout of your options.

jphawk commented 2 years ago

@Rall3n

If I understand this correctly the main problem you encounter is that the "Loading..." text inside the menu is visible on click, but not on keyboard focus immediately after first render?

Correct, "Loading..." is visible on first click, but not on first keyboard focus. I would like to prevent "Loading..." appearing on first click. I understand why it is happening and that the select is still loading options, but users don't need to see this process on the first interaction when they haven't even typed anything yet. To me it looks like a UI bug, which clearly doesn't happen on focus and on click as well when it's not the first one.

I'm aware of openMenuOnClick and openMenuOnFocus, but these props do not solve my problem of preventing "Loading..." to appear on first click with debounce. I'm quite happy with default functionality, but in this case defaults for focus and click behaviour should match from the UI point of view, but they don't.