Skip to content

Commit

Permalink
Update Select arrow down behavior to select first option in dropdown …
Browse files Browse the repository at this point in the history
…when selected option is visually hidden.
  • Loading branch information
atmgrifter00 committed Jul 31, 2024
1 parent cc002de commit 3906bbd
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
20 changes: 19 additions & 1 deletion packages/nimble-components/src/select/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -567,15 +567,33 @@ export class Select
*/
public override keydownHandler(e: KeyboardEvent): BooleanOrVoid {
const initialSelectedIndex = this.selectedIndex;
super.keydownHandler(e);
const key = e.key;
if (e.ctrlKey || e.shiftKey) {
return true;
}

if (key !== keyArrowDown) {
super.keydownHandler(e);
}

let currentActiveIndex = this.openActiveIndex ?? this.selectedIndex;
let commitValueThenClose = false;
switch (key) {
case keyArrowDown: {
const selectedOption = this.options[this.selectedIndex];
if (this.open && isListOption(selectedOption) && !isOptionOrGroupVisible(selectedOption)) {
if (this.openActiveIndex === this.selectedIndex) {
this.selectFirstOption();
} else {
this.selectNextOption();
}
} else {
super.keydownHandler(e);
}

currentActiveIndex = this.openActiveIndex ?? this.selectedIndex;
break;
}
case keySpace: {
// when dropdown is open allow user to enter a space for filter text
if (this.open && this.filterMode !== FilterMode.none) {
Expand Down
38 changes: 38 additions & 0 deletions packages/nimble-components/src/select/tests/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type OptionInitialState =
| 'disabled hidden'
| 'disabled selected hidden'
| 'hidden'
| 'hidden selected'
| 'visually-hidden';

async function setup(
Expand Down Expand Up @@ -585,6 +586,43 @@ describe('Select', () => {
await disconnect();
});

it('when second option is selected and hidden, pressing arrow down while dropdown is open selects first option', async () => {
const { element, connect, disconnect } = await setup(
undefined,
false,
undefined,
'hidden selected'
);
const pageObject = new SelectPageObject(element);
await connect();
await waitForUpdatesAsync();
pageObject.clickSelect();
pageObject.pressArrowDownKey();

expect(pageObject.getActiveOption()?.value).toBe('one');

await disconnect();
});

it('when second option is selected and hidden, pressing arrow down twice while dropdown is open selects third option', async () => {
const { element, connect, disconnect } = await setup(
undefined,
false,
undefined,
'hidden selected'
);
const pageObject = new SelectPageObject(element);
await connect();
await waitForUpdatesAsync();
pageObject.clickSelect();
pageObject.pressArrowDownKey();
pageObject.pressArrowDownKey();

expect(pageObject.getActiveOption()?.value).toBe('three');

await disconnect();
});

describe('with all options disabled', () => {
async function setupAllDisabled(): Promise<Fixture<Select>> {
const viewTemplate = html`
Expand Down

0 comments on commit 3906bbd

Please sign in to comment.