-
Notifications
You must be signed in to change notification settings - Fork 105
feat(web): add typeahead keyboard selection to Select (Alpha) #870
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
Merged
adrienzheng-cb
merged 3 commits into
master
from
adrienzheng/cds-2505-select-alpha-lacks-typeahead-keyboard-selection
Sep 16, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| cds: minor | ||
| --- | ||
|
|
||
| Feat: add native-style typeahead keyboard selection to the web `Select (Alpha)`, supporting a multi-character search buffer, repeated-key cycling, and matching both when the listbox is open and closed. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
packages/web/src/alpha/select/__tests__/useTypeahead.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| import { | ||
| getTypeaheadMatchIndex, | ||
| isPrintableTypeaheadKey, | ||
| isTypeaheadKeyEvent, | ||
| normalizeOptionText, | ||
| TYPEAHEAD_RESET_MS, | ||
| } from '../useTypeahead'; | ||
|
|
||
| describe('typeahead helpers', () => { | ||
| describe('isPrintableTypeaheadKey', () => { | ||
| it.each(['a', 'Z', '5'])('treats single alphanumeric "%s" as printable', (key) => { | ||
| expect(isPrintableTypeaheadKey(key)).toBe(true); | ||
| }); | ||
|
|
||
| it.each(['Enter', 'ArrowDown', 'Escape', ' ', '-', 'ab'])( | ||
| 'treats "%s" as non-printable', | ||
| (key) => { | ||
| expect(isPrintableTypeaheadKey(key)).toBe(false); | ||
| }, | ||
| ); | ||
| }); | ||
|
|
||
| describe('isTypeaheadKeyEvent', () => { | ||
| const keyEvent = ( | ||
| event: Pick<KeyboardEvent, 'key' | 'ctrlKey' | 'metaKey' | 'altKey'>, | ||
| ): KeyboardEvent => event as KeyboardEvent; | ||
|
|
||
| it('accepts a bare printable key', () => { | ||
| expect( | ||
| isTypeaheadKeyEvent(keyEvent({ key: 'a', ctrlKey: false, metaKey: false, altKey: false })), | ||
| ).toBe(true); | ||
| }); | ||
|
|
||
| it('rejects printable keys pressed with a modifier', () => { | ||
| expect( | ||
| isTypeaheadKeyEvent(keyEvent({ key: 'a', ctrlKey: true, metaKey: false, altKey: false })), | ||
| ).toBe(false); | ||
| expect( | ||
| isTypeaheadKeyEvent(keyEvent({ key: 'a', ctrlKey: false, metaKey: true, altKey: false })), | ||
| ).toBe(false); | ||
| expect( | ||
| isTypeaheadKeyEvent(keyEvent({ key: 'a', ctrlKey: false, metaKey: false, altKey: true })), | ||
| ).toBe(false); | ||
| }); | ||
|
|
||
| it('rejects non-printable keys', () => { | ||
| expect( | ||
| isTypeaheadKeyEvent( | ||
| keyEvent({ key: 'Enter', ctrlKey: false, metaKey: false, altKey: false }), | ||
| ), | ||
| ).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe('normalizeOptionText', () => { | ||
| it('lowercases the text', () => { | ||
| expect(normalizeOptionText('Banana')).toBe('banana'); | ||
| }); | ||
|
|
||
| it('strips leading non-alphanumeric characters', () => { | ||
| expect(normalizeOptionText(' ✓ Banana')).toBe('banana'); | ||
| }); | ||
|
|
||
| it('handles nullish input', () => { | ||
| expect(normalizeOptionText(null)).toBe(''); | ||
| expect(normalizeOptionText(undefined)).toBe(''); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getTypeaheadMatchIndex', () => { | ||
| const labels = ['apple', 'banana', 'blueberry', 'cherry']; | ||
|
|
||
| it('returns -1 when there is no search or no options', () => { | ||
| expect(getTypeaheadMatchIndex(labels, '', -1)).toBe(-1); | ||
| expect(getTypeaheadMatchIndex([], 'a', -1)).toBe(-1); | ||
| }); | ||
|
|
||
| it('finds the first prefix match when nothing is focused', () => { | ||
| expect(getTypeaheadMatchIndex(labels, 'b', -1)).toBe(1); | ||
| }); | ||
|
|
||
| it('cycles to the next match on a repeated single character', () => { | ||
| // Focused on "banana" (index 1), pressing "b" again should move to "blueberry". | ||
| expect(getTypeaheadMatchIndex(labels, 'b', 1)).toBe(2); | ||
| }); | ||
|
|
||
| it('wraps around when cycling past the last match', () => { | ||
| // Focused on "blueberry" (index 2), pressing "b" wraps back to "banana". | ||
| expect(getTypeaheadMatchIndex(labels, 'b', 2)).toBe(1); | ||
| }); | ||
|
|
||
| it('treats a repeated same character buffer as cycling', () => { | ||
| expect(getTypeaheadMatchIndex(labels, 'bb', 1)).toBe(2); | ||
| }); | ||
|
|
||
| it('performs a multi-character prefix match and keeps the current option when it still matches', () => { | ||
| // Focused on "banana" (index 1) after typing "b"; refining to "ba" keeps "banana". | ||
| expect(getTypeaheadMatchIndex(labels, 'ba', 1)).toBe(1); | ||
| }); | ||
|
|
||
| it('moves to a different option when the refined buffer no longer matches the current one', () => { | ||
| // Focused on "banana" (index 1); refining to "bl" should jump to "blueberry". | ||
| expect(getTypeaheadMatchIndex(labels, 'bl', 1)).toBe(2); | ||
| }); | ||
|
|
||
| it('returns -1 when nothing matches', () => { | ||
| expect(getTypeaheadMatchIndex(labels, 'z', -1)).toBe(-1); | ||
| }); | ||
| }); | ||
|
|
||
| it('exposes a reset timeout constant', () => { | ||
| expect(TYPEAHEAD_RESET_MS).toBe(500); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we label what these different effects are for? Maybe there is value in giving them names and pulling them into custom hooks just for oganization's sake (e.g.
useFocusInPortaledDropdownetc.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is largely addressed: all the typeahead effects were extracted out of
Select.tsxinto a dedicateduseTypeaheadhook (useTypeahead.ts), which is the "pull them into a custom hook for organization" direction you suggested. Inside the hook I also added terse labels — the type-to-open focus effect and the window-listener effect each have a one-line comment describing their purpose. I intentionally kept it as a single cohesive hook rather than splitting into several micro-hooks (e.g.useFocusInPortaledDropdown), since the effects share the buffer/timeout/focus refs and splitting would add indirection without real clarity. Happy to split further if you feel strongly.