Skip to content

fix(ui5-input): selection-change is now fired when item is reselected #11301

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 81 additions & 26 deletions packages/main/cypress/specs/Input.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -384,13 +384,13 @@ describe("Input general interaction", () => {
it("tests selection-change with custom items", () => {
cy.mount(
<Input id="myInput2" showSuggestions class="input3auto">
<SuggestionItem text="Cozy" />
<SuggestionItem text="Compact" />
<SuggestionItem text="Condensed" />
<SuggestionItem text="Compact" />
<SuggestionItem text="Condensed" />
<SuggestionItem text="Cozy" />
<SuggestionItem text="Compact" />
<SuggestionItem text="Condensed" />
<SuggestionItem text="Compact" />
<SuggestionItem text="Condensed" />
</Input>
);
);

cy.get("ui5-input")
.as("input");
Expand Down Expand Up @@ -435,11 +435,11 @@ describe("Input general interaction", () => {
it("Should fire 'input' event when the value is cleared with ESC", () => {
cy.mount(
<Input></Input>
);
);

cy.get("[ui5-input]").then($input => {
$input[0].addEventListener("ui5-input", cy.spy().as("inputEvent"));
});
cy.get("[ui5-input]").then($input => {
$input[0].addEventListener("ui5-input", cy.spy().as("inputEvent"));
});

cy.get("[ui5-input]").realClick();
cy.get("[ui5-input]").realPress("a");
Expand Down Expand Up @@ -499,13 +499,13 @@ describe("Input arrow navigation", () => {
it("Should navigate up and down through the suggestions popover with arrow keys", () => {
cy.mount(
<Input id="myInput2" showSuggestions class="input3auto">
<SuggestionItem text="Cozy" />
<SuggestionItem text="Compact" />
<SuggestionItem text="Condensed" />
<SuggestionItem text="Compact" />
<SuggestionItem text="Condensed" />
<SuggestionItem text="Cozy" />
<SuggestionItem text="Compact" />
<SuggestionItem text="Condensed" />
<SuggestionItem text="Compact" />
<SuggestionItem text="Condensed" />
</Input>
);
);

cy.get("#myInput2")
.as("input");
Expand Down Expand Up @@ -551,7 +551,7 @@ describe("Input PAGEUP/PAGEDOWN navigation", () => {
beforeEach(() => {
cy.mount(
<Input id="myInput" showSuggestions placeholder="Search for a country ...">
<SuggestionItemGroup headerText="A">
<SuggestionItemGroup headerText="A">
<SuggestionItem text="Afghanistan" />
<SuggestionItem text="Argentina" />
<SuggestionItem text="Albania" />
Expand All @@ -564,9 +564,9 @@ describe("Input PAGEUP/PAGEDOWN navigation", () => {
<SuggestionItem text="Azerbaijan" />
<SuggestionItem text="Aruba" />
<SuggestionItem text="Antigua and Barbuda" />
</SuggestionItemGroup>
</SuggestionItemGroup>
</Input>
);
);
});
it("Should focus the tenth item from the suggestions popover with PAGEDOWN", () => {
cy.get("ui5-input")
Expand Down Expand Up @@ -626,11 +626,11 @@ describe("Selection-change event", () => {
it("Selection-change event fires with null arguments when suggestion was selected but user alters input value to something else", () => {
cy.mount(
<Input id="input-selection-change" showSuggestions>
<SuggestionItem text="Cozy" />
<SuggestionItem text="Compact" />
<SuggestionItem text="Condensed" />
<SuggestionItem text="Cozy" />
<SuggestionItem text="Compact" />
<SuggestionItem text="Condensed" />
</Input>
);
);

cy.get("ui5-input")
.as("input");
Expand Down Expand Up @@ -676,6 +676,61 @@ describe("Selection-change event", () => {
expect(eventCount).to.equal(2);
});
});

Copy link
Contributor

@ndeshev ndeshev Apr 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can check event firing with stubs or spies like:

	cy.get("@input")
		.then($input => {
		        $input[0].addEventListener("ui5-change", cy.stub().as("inputChange"));
		});

and then

		cy.get("@inputChange").should("have.been.calledTwice");

it("Fires selection-change when same item is reselected after input is changed", () => {
cy.mount(
<Input id="input-selection-change" showSuggestions>
<SuggestionItem text="Cozy" />
<SuggestionItem text="Compact" />
<SuggestionItem text="Condensed" />
</Input>
);

cy.get("ui5-input")
.as("input");

cy.get("ui5-input")
.shadow()
.find("input")
.as("innerInput");

cy.get("@input").then($input => { $input[0].addEventListener("ui5-selection-change", cy.stub().as("inputSelectionChange")); });

cy.get("@innerInput")
.realClick();

cy.get("[ui5-suggestion-item")
.eq(0)
.as("suggestion-item");

cy.get("@innerInput")
.type("C");

cy.get("@suggestion-item")
.realClick();

cy.get("@inputSelectionChange").should("have.been.calledOnce");

cy.get("@innerInput")
.should("have.value", "Cozy");

cy.get("@innerInput")
.realClick();
cy.get("@innerInput")
.clear();

cy.get("@inputSelectionChange").should("have.been.calledTwice");

cy.get("@innerInput")
.type("C");
cy.get("@suggestion-item")
.realClick();

cy.get("@inputSelectionChange").should("have.been.calledThrice");

cy.get("@innerInput")
.should("have.value", "Cozy");
});
});

describe("Change event behavior when selecting the same suggestion item", () => {
Expand All @@ -684,15 +739,15 @@ describe("Change event behavior when selecting the same suggestion item", () =>
beforeEach(() => {
cy.mount(
<Input id="myInput" showSuggestions placeholder="Search for a country ...">
<SuggestionItemGroup headerText="A">
<SuggestionItemGroup headerText="A">
<SuggestionItem text="Afghanistan" />
<SuggestionItem text="Argentina" />
<SuggestionItem text="Albania" />
<SuggestionItem text="Armenia" />
<SuggestionItem text="Algeria" />
</SuggestionItemGroup>
</SuggestionItemGroup>
</Input>
);
);

cy.get("#myInput")
.as("input");
Expand Down
1 change: 1 addition & 0 deletions packages/main/src/Input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1534,6 +1534,7 @@ class Input extends UI5Element implements SuggestionComponent, IFormInputElement
fireResetSelectionChange() {
if (this._isLatestValueFromSuggestions) {
this.fireSelectionChange(null, false);
this.valueBeforeItemSelection = this.value;
}
}

Expand Down