From 4e0bab825584ce45fafa2e891ea68462a1cc820a Mon Sep 17 00:00:00 2001 From: aleksandar-terziev Date: Tue, 16 Sep 2025 18:15:47 +0300 Subject: [PATCH] fix(ui5-combobox): fire 'selection-change' event only after user interaction --- packages/main/cypress/specs/ComboBox.cy.tsx | 34 +++++++++++++++++++++ packages/main/src/ComboBox.ts | 6 ++++ 2 files changed, 40 insertions(+) diff --git a/packages/main/cypress/specs/ComboBox.cy.tsx b/packages/main/cypress/specs/ComboBox.cy.tsx index 7c59ff1f61a1..12e2a879ad62 100644 --- a/packages/main/cypress/specs/ComboBox.cy.tsx +++ b/packages/main/cypress/specs/ComboBox.cy.tsx @@ -2513,6 +2513,40 @@ describe("Event firing", () => { })); }); + it("should NOT fire selection-change event when ComboBox items are set asynchronously after initial render", () => { + cy.mount( + + {/* Items will be added asynchronously */} + + ); + + cy.get("#async-combo") + .invoke('on', 'ui5-selection-change', cy.spy().as('selectionChangeSpy')); + + cy.window().then(win => { + const combo = win.document.getElementById("async-combo"); + const item1 = win.document.createElement("ui5-cb-item"); + item1.setAttribute("text", "Argentina"); + const item2 = win.document.createElement("ui5-cb-item"); + item2.setAttribute("text", "Bulgaria"); + combo?.appendChild(item1); + combo?.appendChild(item2); + (combo as any).loading = false; + }); + + cy.get("#async-combo").should("not.have.prop", "loading", true); + + cy.get("@selectionChangeSpy").should("not.have.been.called"); + + cy.get("#async-combo").shadow().find("input").realClick(); + cy.get("#async-combo").shadow().find("input").clear().realType("Argentina"); + + cy.get("@selectionChangeSpy").should("have.been.calledOnce"); + cy.get("@selectionChangeSpy").should("have.been.calledWithMatch", Cypress.sinon.match(event => { + return event.detail.item.text === "Argentina"; + })); + }); + it("should check clear icon events", () => { cy.mount( <> diff --git a/packages/main/src/ComboBox.ts b/packages/main/src/ComboBox.ts index 9b7a691a254c..53bf1b3f86b5 100644 --- a/packages/main/src/ComboBox.ts +++ b/packages/main/src/ComboBox.ts @@ -1202,6 +1202,12 @@ class ComboBox extends UI5Element implements IFormInputElement { return item; }); + const noUserInteraction = !this.focused && !this._isKeyNavigation && !this._selectionPerformed && !this._iconPressed; + // Skip firing "selection-change" event if this is initial rendering or if there has been no user interaction yet + if (this._initialRendering || noUserInteraction) { + return; + } + // Fire selection-change event only when selection actually changes if (previouslySelectedItem !== itemToBeSelected) { if (itemToBeSelected) {