Skip to content

Commit 50bb5e6

Browse files
fix(ui5-input): emit change when suggestion is clicked (#12206)
1 parent 546f0fb commit 50bb5e6

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

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

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,78 @@ describe("Input general interaction", () => {
443443
cy.get("@input")
444444
.should("have.prop", "typedInValue", "");
445445
});
446+
447+
it("Should fire 'change' event once when clicking a suggestion equal to the typed value", () => {
448+
const onChange = cy.spy().as("onChange");
449+
const onSelectionChange = cy.spy().as("onSelectionChange");
450+
451+
cy.mount(
452+
<Input
453+
id="input-equal-click"
454+
showSuggestions
455+
noTypeahead
456+
onChange={onChange}
457+
onSelectionChange={onSelectionChange}
458+
>
459+
<SuggestionItem text="Cozy" />
460+
<SuggestionItem text="Compact" />
461+
</Input>
462+
);
463+
464+
cy.get("#input-equal-click")
465+
.shadow()
466+
.find("input")
467+
.click()
468+
.realType("Cozy");
469+
470+
cy.get("#input-equal-click")
471+
.shadow()
472+
.find<ResponsivePopover>("[ui5-responsive-popover]")
473+
.ui5ResponsivePopoverOpened();
474+
475+
cy.get('#input-equal-click')
476+
.find('ui5-suggestion-item[text="Cozy"]')
477+
.click();
478+
479+
cy.get("#input-equal-click").should("have.value", "Cozy");
480+
cy.get("@onChange").should("have.been.calledOnce");
481+
});
482+
483+
it("Should fire 'change' event once when selecting a suggestion equal to the typed value with keyboard", () => {
484+
const onChange = cy.spy().as("onChange");
485+
const onSelectionChange = cy.spy().as("onSelectionChange");
486+
487+
cy.mount(
488+
<Input
489+
id="input-equal-keyboard"
490+
showSuggestions
491+
noTypeahead
492+
onChange={onChange}
493+
onSelectionChange={onSelectionChange}
494+
>
495+
<SuggestionItem text="Cozy" />
496+
<SuggestionItem text="Compact" />
497+
</Input>
498+
);
499+
500+
cy.get("#input-equal-keyboard")
501+
.shadow()
502+
.find("input")
503+
.click()
504+
.realType("Cozy");
505+
506+
cy.get("#input-equal-keyboard")
507+
.shadow()
508+
.find<ResponsivePopover>("[ui5-responsive-popover]")
509+
.ui5ResponsivePopoverOpened();
510+
511+
cy.realPress("ArrowDown");
512+
cy.realPress("Enter");
513+
514+
cy.get("#input-equal-keyboard").should("have.value", "Cozy");
515+
cy.get("@onChange").should("have.been.calledOnce");
516+
cy.get("@onSelectionChange").should("have.been.calledOnce");
517+
});
446518
});
447519

448520
describe("Input arrow navigation", () => {

packages/main/src/Input.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1424,10 +1424,9 @@ class Input extends UI5Element implements SuggestionComponent, IFormInputElement
14241424
return;
14251425
}
14261426

1427-
const value = this.typedInValue || this.value;
14281427
const itemText = item.text || "";
14291428
const fireChange = keyboardUsed
1430-
? this.valueBeforeItemSelection !== itemText : value !== itemText;
1429+
? this.valueBeforeItemSelection !== itemText : this.previousValue !== itemText;
14311430

14321431
this.hasSuggestionItemSelected = true;
14331432
this.value = itemText;

0 commit comments

Comments
 (0)