Skip to content

feat: support ArrowUp and ArrowDown on single-select dropdowns - #1357

Open
sviat-barbutsa wants to merge 1 commit into
testing-library:mainfrom
sviat-barbutsa:feat/select-keyboard-navigation
Open

sviat-barbutsa wants to merge 1 commit into
testing-library:mainfrom
sviat-barbutsa:feat/select-keyboard-navigation

Conversation

@sviat-barbutsa

Copy link
Copy Markdown

What

Refs #786. With a native single-select focused, await user.keyboard('{ArrowDown}') now selects the next available option. ArrowUp moves back. Both keys keep focus on the dropdown. A change fires input and then change before keyup. At either end of the list, or when an app cancels keydown, the selection stays put.

Why

The New York Public Library linked #786 when adding interaction tests for its design system. The team could test choosing a color with the mouse, but the keyboard events it tried never changed the selection. Its current story uses selectOptions to choose Green and Black. NYPL’s PR · Current Select story

I tried the same color list locally with NYPL’s published React component (4.6.0). Starting on Red, I pressed Down three times and Up once. Native Chrome selected Green, Blue, Black, then Blue. On current main, user.keyboard stayed on Red through all four presses. With this patch, it matched Chrome on every press, and the form submitted Blue.

How

The keydown handler reads the current options after application listeners run. It skips disabled options, disabled groups and options hidden with display: none, then updates the selected option using the same event dispatch as selectOptions. This covers plain Up and Down on a collapsed dropdown. Opening a native popup with Space and committing a highlighted choice with Enter is a separate part of #786.

I added 15 cases. Six fail on unmodified main. All 594 Jest tests pass on Linux and Windows. The 15 new cases also pass in each of the three toolbox environments. Across the full toolbox run, I got the same 19 failures on clean main and the patch. Type-checking, lint and the Linux build passed. The Windows build stopped on the same ESM path error on both revisions.

I also compared 51 key presses per browser on Windows. Chrome, Edge and Opera matched on every press. Firefox differed on Up from an empty selection, and Playwright WebKit also differed on which hidden options it visits. The patch follows Chromium for those cases. I could test WebKit on Windows only.

Checklist

  • Documentation
  • Tests
  • Ready to be merged

Comment on lines -65 to -67
bubbles: true,
cancelable: false,
composed: true,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was lost, why?

Comment thread tests/keyboard/select.ts
await user.keyboard('{ArrowDown}{ArrowUp}')

expect(element).toHaveFocus()
expect(getEventSnapshot()).toMatchInlineSnapshot(`

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it doesn't look like this is the order of events in the browser
https://jsfiddle.net/0ukrw85p/

Screenshot 2026-11-27 at 10 31 55 am

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make sure to read all the comments in the original issue you linked to, I believe it pointed this out.

Comment thread tests/keyboard/select.ts
},
)

test('read available options after keydown listeners run', async () => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this mean?

Comment thread tests/keyboard/select.ts
},
)

test.each([['Alt'], ['Control'], ['Shift'], ['Meta']])(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't appear to match the browser. Control+ArrowUp/Down does something special on Mac, but all the other combinations appear to open the select. At least in Chrome.

This branch has not been deployed

No deployments
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