Skip to content

Commit e262c5c

Browse files
benelanjcfranco
andauthored
fix(combobox): fix error that occurs when a click is emitted when the component is appended to the DOM (#9380)
**Related Issue:** #9321 ## Summary This fixes an issue where references to internal elements were undefined in the window-level click handler. This would happen because the window click handler was added when the component was connected, but before its internals were rendered. Co-authored-by: JC Franco <[email protected]>
1 parent 3b5e8eb commit e262c5c

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

packages/calcite-components/src/components/combobox/combobox.e2e.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
import { html } from "../../../support/formatting";
1616
import { CSS as ComboboxItemCSS } from "../combobox-item/resources";
1717
import { CSS as XButtonCSS } from "../functional/XButton";
18-
import { getElementXY, skipAnimations } from "../../tests/utils";
18+
import { getElementXY, newProgrammaticE2EPage, skipAnimations } from "../../tests/utils";
1919
import { CSS } from "./resources";
2020

2121
const selectionModes = ["single", "single-persist", "ancestors", "multiple"];
@@ -2020,4 +2020,14 @@ describe("calcite-combobox", () => {
20202020

20212021
expect(await combobox.getProperty("open")).toBe(false);
20222022
});
2023+
2024+
it("does not throw an error when a click emits on connect (#9321)", async () => {
2025+
const page = await newProgrammaticE2EPage();
2026+
await page.evaluate(async () => {
2027+
const combobox = document.createElement("calcite-combobox");
2028+
document.body.click();
2029+
document.body.append(combobox);
2030+
});
2031+
await page.waitForChanges();
2032+
});
20232033
});

packages/calcite-components/src/components/combobox/combobox.tsx

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ import {
6464
updateMessages,
6565
} from "../../utils/t9n";
6666
import { Scale, SelectionMode, Status } from "../interfaces";
67-
import { XButton, CSS as XButtonCSS } from "../functional/XButton";
68-
import { getIconScale } from "../../utils/component";
67+
import { CSS as XButtonCSS, XButton } from "../functional/XButton";
68+
import { componentOnReady, getIconScale } from "../../utils/component";
6969
import { Validation } from "../functional/Validation";
7070
import { ComboboxMessages } from "./assets/combobox/t9n";
7171
import { ComboboxChildElement, SelectionDisplay } from "./interfaces";
@@ -343,16 +343,12 @@ export class Combobox
343343
//--------------------------------------------------------------------------
344344

345345
@Listen("click", { target: "document" })
346-
documentClickHandler(event: PointerEvent): void {
347-
if (this.disabled) {
346+
async documentClickHandler(event: PointerEvent): Promise<void> {
347+
if (this.disabled || event.composedPath().includes(this.el)) {
348348
return;
349349
}
350350

351-
const composedPath = event.composedPath();
352-
353-
if (composedPath.includes(this.el) || composedPath.includes(this.referenceEl)) {
354-
return;
355-
}
351+
await componentOnReady(this.el);
356352

357353
if (!this.allowCustomValues && this.textInput.value) {
358354
this.clearInputValue();

0 commit comments

Comments
 (0)