Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure selected option is focused when listbox is opened #1768

Merged
merged 1 commit into from
Nov 5, 2024

Conversation

acelaya
Copy link
Contributor

@acelaya acelaya commented Nov 5, 2024

This PR fixes an unintended regression introduced by making options render only when the listbox is open.

We now make sure the tabIndex is 0 for the selected option, which will make useArrowKeyNavigation focus it the first time.

This worked before because the options were kept in the DOM, preserving their previously set tabIndex, but now we render them "from scratch" every time it is opened.

Before:

focus-option-broken-2024-11-05_14.01.59.mp4

After:

focus-option-fixed-2024-11-05_14.01.21.mp4

Testing steps

  1. Check out this branch.
  2. Go to http://localhost:4001/input-select
  3. Using the keyboard, focus the first Select, then click ArrowDown to open the listbox. The first option should be focused.
  4. Using arrows, move the focus to a different option, and select it by pressing Enter. The focus will return to the button.
  5. Press ArrowDown again to open the listbox. The focused option should be the selected one.

If you follow these steps in main branch, you'll notice the first option is always focused when the listbox is opened, regardless of which one is selected.

@acelaya acelaya requested a review from robertknight November 5, 2024 13:04
@acelaya acelaya force-pushed the focus-selected-option branch from 7b09cf5 to a84605d Compare November 5, 2024 13:04
@acelaya acelaya marked this pull request as draft November 5, 2024 13:06
@acelaya
Copy link
Contributor Author

acelaya commented Nov 5, 2024

I forgot to add a test. Moving back to draft Done

Copy link

codecov bot commented Nov 5, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (986c9d5) to head (207342d).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #1768   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           67        67           
  Lines         1203      1204    +1     
  Branches       454       455    +1     
=========================================
+ Hits          1203      1204    +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@acelaya acelaya force-pushed the focus-selected-option branch from a84605d to 313c8c9 Compare November 5, 2024 13:35
@acelaya acelaya marked this pull request as ready for review November 5, 2024 13:35
src/components/input/test/Select-test.js Outdated Show resolved Hide resolved
@acelaya acelaya force-pushed the focus-selected-option branch from 313c8c9 to 207342d Compare November 5, 2024 14:11
@acelaya acelaya merged commit 5e45050 into main Nov 5, 2024
4 checks passed
@acelaya acelaya deleted the focus-selected-option branch November 5, 2024 14:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants