Skip to content

Commit

Permalink
Handle case where user clicks select after navigating to a different …
Browse files Browse the repository at this point in the history
…option in dropdown.
  • Loading branch information
atmgrifter00 committed May 7, 2024
1 parent dd5391a commit af4af19
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
5 changes: 4 additions & 1 deletion packages/nimble-components/src/select/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,20 +327,23 @@ export class Select
return;
}

let optionClicked = false;
if (this.open) {
const captured = (e.target as HTMLElement).closest<ListOption>(
'option,[role=option]'
);
optionClicked = captured !== null;

if (captured?.disabled) {
return;
}
}

const currentIndex = this.openActiveIndex ?? this.selectedIndex;
super.clickHandler(e);

this.open = this.collapsible && !this.open;
if (!this.open && this.selectedIndex !== -1) {
if (!this.open && this.selectedIndex !== currentIndex && optionClicked) {
this.updateValue(true);
}
}
Expand Down
18 changes: 18 additions & 0 deletions packages/nimble-components/src/select/tests/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,24 @@ describe('Select', () => {
await disconnect();
});

it('navigating to different option in dropdown and then clicking select (not dropdown) does not change value or emit change event', async () => {
const { element, connect, disconnect } = await setup();
await connect();
await waitForUpdatesAsync();
const changeEvent = jasmine.createSpy();
element.addEventListener('change', changeEvent);
const pageObject = new SelectPageObject(element);
await clickAndWaitForOpen(element);

pageObject.pressArrowDownKey();
pageObject.clickSelect();

expect(element.value).toBe('one');
expect(changeEvent.calls.count()).toBe(0);

await disconnect();
});

describe('with 500 options', () => {
async function setup500Options(): Promise<Fixture<Select>> {
// prettier-ignore
Expand Down

0 comments on commit af4af19

Please sign in to comment.