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.

Dropdown loses focus and jumps to first option instead of active option with controlled values

See original GitHub issue

When the value is set from state (controlled) the dropdown jumps to the first option and loses focus whenever it is opened, as opposed to auto scrolling to the active option.

I couldn’t find an option to enable this. Is this a bug? Everything is fine when the value is hardcoded like in your original plunkr

Please try this plunkr to reproduce: https://plnkr.co/edit/LyMQDWVVu4yfL6pZeux4?p=preview

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
anuprai11commented, Feb 19, 2017

this might be a bug but there are 2 work arounds to this, first the reason of the problem getFocusableOptionIndex is the function that calculate of the value which option need to be focused it does nothing it takes the first value from the value-array find its index in the options sent and focus then it get focussed,

now as you are saving the value in the second plunkr as an object and passing an object back to react-select, the value is not expanded from the options. as in expandValue function there is a check if (valueType !== 'string' && valueType !== 'number' && valueType !== 'boolean') return value; Now this is in place thinking you have send a value which is from the options already so no need to expand.

After that the getFocusableOptionIndex function calculate which option to focus using options.indexOf(value) so basically comparing reference to find the index of the selected option.

The problem with your code is this. you have created the option array inside the render function so options mutate on each render. so when a value is selected an your reference to the selected differ as a new array with new objects is created.

so expand values leaves thinking what you have sent is in the options but as you have sent new objects in the options the indexOf comparision fails and focussed options is nothing.

there are two solutions one is send the value as the value of the option selected then react select will expand the value from options and focus on it or dont mutate your the objects in options after selecting a value.

I dont think here something is needed to be fixed in react-select as it is following its contract. one thing could be done is in expandValue function not returning for obects and expanding them like

if (options[i][valueKey] === value[valueKey]) return options[i]; but that is not very fruitful i think as the problem can be easily solved from outside.

0reactions
bladeycommented, May 28, 2020

Hello -

In an effort to sustain the react-select project going forward, we’re closing old issues.

We understand this might be inconvenient but in the best interest of supporting the broader community we have to direct our limited efforts to maintain the latest version.

If you aren’t using the latest version of react-select please consider upgrading to see if it resolves any issues you’re having.

However, if you feel this issue is still relevant and you’d like us to review it - please leave a comment and we’ll do our best to get back to you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Disable Focus on First option in React-Select - Stack Overflow
I have a dropdown in React Select, i am facing an issue with the focus. The first item in the option list is...
Read more >
Select-Only Combobox Example | APG | WAI - W3C
First opens the listbox if it is not already displayed and then moves visual focus to the first option. DOM focus remains on...
Read more >
Line-by-line: advanced CSS tricks for click-to-open drop-down ...
Then, as a workaround for the lack of :focus-within , I decided to attempt to detect Edge and automatically keep any .dropdown list...
Read more >
How to build custom form controls - Learn web development
Note: We'll focus on building the control, not on how to make the code ... the user hits the up arrow key when...
Read more >
Windows 7 Drop-down Lists Combo Boxes - Win32 apps
Is the control used to choose one option from a list of mutually exclusive values? If not, use another control. To choose multiple...
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