JedWatson / JedWatson/react-select
Selected option isn't focused when opening menu
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 28k
- Forks
- 4.1k
- PR merge metrics
- No merged PRs in 30d
Description
Hey :)
Explanation
I like to report the a bug but for this, I need to explain the expected behaviour first:
When an option is selected, and one opens the menu I expect the selected option to appear selected, be in view and focused so up/down arrows work immediately.
This works in principle, however I found a caveat that I consider to be a bug, or at least, I haven't found a way to fix it in my code:
I use { label: string; value: string } objects as options for my select component. When the value (aka the selected option) of the select is one of the option objects in the options array (aka value={options[x]}) it works as expected. I'm guessing because value === options[x]. If I were to reconstruct the value object so the object references don't match any of the options but the contents are the same, it will break the expected behaviour and always focus the first option.
Reproduction
const options = new Array(20)
.fill(null)
.map((_, i) => ({ value: `${i + 1}`, label: `Option ${i + 1}` }));
// this will open the menu correctly
<Select options={options} value={options[14]} />
// this will reproduce the bug
<Select options={options} value={{ label: 'Option 15', value: '15' }} />
I also created a Codesandbox for reproduction.
Attempts to fix it
I tried to play around with getOptionValue and isOptionSelected properties but I didn't get it to work. Also menuShouldScrollIntoView didn't do anything.
I'm kinda out of ideas on what to try.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the Select component and reproduce the behavior from the provided CodeSandbox using an options array and a reconstructed value object. Review how getOptionValue, isOptionSelected, and menu opening determine the focused option; done means the matching selected option is visible and focused, with up/down arrows working immediately, and the regression is covered by a test.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- react, typescript
- Domain
- frontend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100