Skip to content

Commit 8643afe

Browse files
authored
fix(ui5-combobox): prevent 'change' event from being fired if the value is set programmatically (#11048)
* fix(ui5-combobox): prevent 'change' event from being fired if the value is set programmatically fixes: #10959
1 parent 951e726 commit 8643afe

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe("Event firing", () => {
3535

3636
cy.get("@combo").then($combo => {
3737
$combo[0].addEventListener("focusin", () => {
38-
$combo[0].setAttribute("open", "true");
38+
$combo[0].setAttribute("open", "true");
3939
});
4040
});
4141

@@ -74,4 +74,31 @@ describe("Event firing", () => {
7474
cy.get("@comboOpened")
7575
.should("have.been.calledTwice");
7676
});
77+
78+
it("should not fire 'change' event on focusout if value is not changed by user interaction", () => {
79+
cy.mount(
80+
<>
81+
<ComboBox id="cb" value="ComboBox item text"></ComboBox>
82+
<ComboBox id="another-cb"></ComboBox>
83+
</>
84+
);
85+
86+
cy.get("#cb").then($cb => {
87+
$cb[0].addEventListener("ui5-change", cy.stub().as("changeStub"));
88+
});
89+
90+
cy.get("#cb").shadow().find("input").click();
91+
cy.get("#another-cb").shadow().find("input").click();
92+
cy.get("@changeStub").should("not.have.been.called");
93+
94+
cy.get("#cb").then(($cb) => {
95+
const comboBox = $cb[0] as ComboBox;
96+
comboBox.value = "Another ComboBox item text";
97+
});
98+
99+
cy.get("#cb").shadow().find("input").click();
100+
cy.get("#another-cb").shadow().find("input").click();
101+
cy.get("@changeStub").should("not.have.been.called");
102+
});
77103
});
104+

packages/main/src/ComboBox.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,10 @@ class ComboBox extends UI5Element implements IFormInputElement {
508508
this.focused = true;
509509
this._autocomplete = false;
510510

511+
if (!e.relatedTarget || (e.relatedTarget !== this.shadowRoot!.querySelector(".ui5-input-clear-icon"))) {
512+
this._lastValue = this.value;
513+
}
514+
511515
!isPhone() && (e.target as HTMLInputElement).setSelectionRange(0, this.value.length);
512516
}
513517

0 commit comments

Comments
 (0)