Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/scripts/choices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1845,9 +1845,11 @@ class Choices {
nextEl = this.dropdown.element.querySelector(selectableChoiceIdentifier);
}
} else {
const currentEl = this.dropdown.element.querySelector<HTMLElement>(
getClassNamesSelector(this.config.classNames.highlightedState),
);
const currentEl =
this.dropdown.element.querySelector<HTMLElement>(
getClassNamesSelector(this.config.classNames.highlightedState),
) ??
this.dropdown.element.querySelector<HTMLElement>(getClassNamesSelector(this.config.classNames.selectedState));
if (currentEl) {
nextEl = getAdjacentEl(currentEl, selectableChoiceIdentifier, directionInt);
} else {
Expand Down
26 changes: 26 additions & 0 deletions test-e2e/tests/select-one.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,32 @@ describe(`Choices - select one`, () => {
await expect(suite.choices.first()).toHaveClass(/is-highlighted/);
await expect(suite.choices.last()).not.toHaveClass(/is-highlighted/);
});

test('arrow key navigation resumes from selected item after closing without confirming', async ({
page,
bundle,
}) => {
const suite = new SelectTestSuit(page, bundle, testUrl, testId);
await suite.startWithClick();

// Navigate down twice so choices.nth(2) is highlighted, then escape
await suite.input.press('ArrowDown');
await suite.input.press('ArrowDown');
await expect(suite.choices.nth(2)).toHaveClass(/is-highlighted/);
await suite.escapeKey();
await suite.expectHiddenDropdown();

// The first choice should still be the selected (confirmed) item
await expect(suite.choices.first()).toHaveClass(/is-selected/);

// Reopen and press ArrowDown; should move relative to the selected item
await suite.startWithClick();
await suite.input.press('ArrowDown');

// Navigation should continue from the selected item, not restart from item 0
await expect(suite.choices.first()).not.toHaveClass(/is-highlighted/);
await expect(suite.choices.nth(1)).toHaveClass(/is-highlighted/);
});
});

describe('searching choices', () => {
Expand Down
Loading