Skip to content
30 changes: 30 additions & 0 deletions packages/main/cypress/specs/Input.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,36 @@ describe("Input Tests", () => {
cy.get("@change").should("have.been.calledOnce");
});

it("should not fire a submit event if there is more than one input field in a form", () => {
cy.mount(
<form>
<Input id="first-input" onChange={cy.stub().as("change")}></Input>
<Input></Input>
</form>
);

// spy submit event and prevent it
cy.get("form")
.then($form => {
$form.get(0).addEventListener("submit", e => e.preventDefault());
$form.get(0).addEventListener("submit", cy.spy().as("submit"));
});

// check if submit is triggered after change
cy.get("#first-input")
.as("input")
.realClick();

cy.get("@input")
.should("be.focused");

cy.realType("test");

cy.realPress("Enter");

cy.get("@submit").should("have.not.been.called");
});

it("tests if pressing enter twice fires submit 2 times and change once", () => {
cy.mount(
<form>
Expand Down
8 changes: 6 additions & 2 deletions packages/main/src/Input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -837,9 +837,11 @@ class Input extends UI5Element implements SuggestionComponent, IFormInputElement

if (isEnter(e)) {
const isValueUnchanged = this.previousValue === this.getInputDOMRefSync()!.value;
const shouldSubmit = this._internals.form && this._internals.form.querySelectorAll("[ui5-input]").length === 1;

this._enterKeyDown = true;

if (isValueUnchanged && this._internals.form) {
if (isValueUnchanged && shouldSubmit) {
submitForm(this);
}

Expand Down Expand Up @@ -1138,6 +1140,8 @@ class Input extends UI5Element implements SuggestionComponent, IFormInputElement
}

_handleChange() {
const shouldSubmit = this._internals.form && this._internals.form.querySelectorAll("[ui5-input]").length === 1;

if (this._clearIconClicked) {
this._clearIconClicked = false;
return;
Expand All @@ -1159,7 +1163,7 @@ class Input extends UI5Element implements SuggestionComponent, IFormInputElement
} else {
fireChange();

if (this._enterKeyDown && this._internals.form) {
if (this._enterKeyDown && shouldSubmit) {
submitForm(this);
}
}
Expand Down
Loading