Skip to content

Commit f509612

Browse files
fix(ui5-combobox): fire 'selection-change' event only after user inte… (#12316)
fix(ui5-combobox): fire 'selection-change' event only after user interaction Co-authored-by: ilhan orhan <[email protected]>
1 parent 97065ec commit f509612

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

packages/main/cypress/specs/ComboBox.cy.tsx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2513,6 +2513,40 @@ describe("Event firing", () => {
25132513
}));
25142514
});
25152515

2516+
it("should NOT fire selection-change event when ComboBox items are set asynchronously after initial render", () => {
2517+
cy.mount(
2518+
<ComboBox id="async-combo" value="Bulgaria" loading>
2519+
{/* Items will be added asynchronously */}
2520+
</ComboBox>
2521+
);
2522+
2523+
cy.get("#async-combo")
2524+
.invoke('on', 'ui5-selection-change', cy.spy().as('selectionChangeSpy'));
2525+
2526+
cy.window().then(win => {
2527+
const combo = win.document.getElementById("async-combo");
2528+
const item1 = win.document.createElement("ui5-cb-item");
2529+
item1.setAttribute("text", "Argentina");
2530+
const item2 = win.document.createElement("ui5-cb-item");
2531+
item2.setAttribute("text", "Bulgaria");
2532+
combo?.appendChild(item1);
2533+
combo?.appendChild(item2);
2534+
(combo as any).loading = false;
2535+
});
2536+
2537+
cy.get("#async-combo").should("not.have.prop", "loading", true);
2538+
2539+
cy.get("@selectionChangeSpy").should("not.have.been.called");
2540+
2541+
cy.get("#async-combo").shadow().find("input").realClick();
2542+
cy.get("#async-combo").shadow().find("input").clear().realType("Argentina");
2543+
2544+
cy.get("@selectionChangeSpy").should("have.been.calledOnce");
2545+
cy.get("@selectionChangeSpy").should("have.been.calledWithMatch", Cypress.sinon.match(event => {
2546+
return event.detail.item.text === "Argentina";
2547+
}));
2548+
});
2549+
25162550
it("should check clear icon events", () => {
25172551
cy.mount(
25182552
<>

packages/main/src/ComboBox.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,6 +1202,12 @@ class ComboBox extends UI5Element implements IFormInputElement {
12021202
return item;
12031203
});
12041204

1205+
const noUserInteraction = !this.focused && !this._isKeyNavigation && !this._selectionPerformed && !this._iconPressed;
1206+
// Skip firing "selection-change" event if this is initial rendering or if there has been no user interaction yet
1207+
if (this._initialRendering || noUserInteraction) {
1208+
return;
1209+
}
1210+
12051211
// Fire selection-change event only when selection actually changes
12061212
if (previouslySelectedItem !== itemToBeSelected) {
12071213
if (itemToBeSelected) {

0 commit comments

Comments
 (0)