diff --git a/change/@microsoft-fast-foundation-c128481d-c811-401e-9be7-71f80b0350a1.json b/change/@microsoft-fast-foundation-c128481d-c811-401e-9be7-71f80b0350a1.json new file mode 100644 index 00000000000..26630d78aba --- /dev/null +++ b/change/@microsoft-fast-foundation-c128481d-c811-401e-9be7-71f80b0350a1.json @@ -0,0 +1,7 @@ +{ + "type": "minor", + "comment": "feat: close combobox if indicator is clicked and no selection is made", + "packageName": "@microsoft/fast-foundation", + "email": "=", + "dependentChangeType": "patch" +} diff --git a/packages/web-components/fast-foundation/src/combobox/combobox.spec.ts b/packages/web-components/fast-foundation/src/combobox/combobox.spec.ts index 1666c93ae9b..2bece35a385 100644 --- a/packages/web-components/fast-foundation/src/combobox/combobox.spec.ts +++ b/packages/web-components/fast-foundation/src/combobox/combobox.spec.ts @@ -207,6 +207,22 @@ describe("Combobox", () => { }); }); + it.only("should close the listbox if the indicator is clicked without making a selection", async () => { + const { element, connect, disconnect } = await setup(); + + await connect(); + + element.click(); // open dropdown + + expect(element.open).to.be.true; + + element.click(); + + expect(element.open).to.be.false; + + await disconnect(); + }); + it("should emit a 'change' event when the user clicks away after selecting option in dropdown", async () => { const { element, connect, disconnect } = await setup(); diff --git a/packages/web-components/fast-foundation/src/combobox/combobox.ts b/packages/web-components/fast-foundation/src/combobox/combobox.ts index d4f71f8ac81..66cef8fbe6f 100644 --- a/packages/web-components/fast-foundation/src/combobox/combobox.ts +++ b/packages/web-components/fast-foundation/src/combobox/combobox.ts @@ -274,23 +274,25 @@ export class Combobox extends FormAssociatedCombobox { * @internal */ public clickHandler(e: MouseEvent): boolean | void { - if (this.disabled) { + const captured = (e.target as HTMLElement).closest( + `option,[role=option]` + ) as ListboxOption | null; + + if (this.disabled || captured?.disabled) { return; } if (this.open) { - const captured = (e.target as HTMLElement).closest( - `option,[role=option]` - ) as ListboxOption | null; - - if (!captured || captured.disabled) { + if (e.composedPath()[0] === this.control) { return; } - this.selectedOptions = [captured]; - this.control.value = captured.text; - this.clearSelectionRange(); - this.updateValue(true); + if (captured) { + this.selectedOptions = [captured]; + this.control.value = captured.text; + this.clearSelectionRange(); + this.updateValue(true); + } } this.open = !this.open;