Skip to content

Commit 0944107

Browse files
authored
fix(ui5-input): fire submit only if single input in a form (#12244)
* fix(ui5-input): fire submit only if single input in a form to match the native input in form behavior, the submit event should be fired on enter key press and change event only if there is a single input in the form. fixes: #12221
1 parent 6937756 commit 0944107

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,35 @@ describe("Input Tests", () => {
198198
cy.get("@change").should("have.been.calledOnce");
199199
});
200200

201+
it("should not fire 'submit' event when there is more than one input field in a form", () => {
202+
cy.mount(
203+
<form>
204+
<Input id="first-input" onChange={cy.stub().as("change")}></Input>
205+
<Input></Input>
206+
</form>
207+
);
208+
209+
// spy submit event and prevent it
210+
cy.get("form")
211+
.then($form => {
212+
$form.get(0).addEventListener("submit", cy.spy().as("submit"));
213+
});
214+
215+
// check if submit is triggered after change
216+
cy.get("#first-input")
217+
.as("input")
218+
.realClick();
219+
220+
cy.get("@input")
221+
.should("be.focused");
222+
223+
cy.realType("test");
224+
225+
cy.realPress("Enter");
226+
227+
cy.get("@submit").should("have.not.been.called");
228+
});
229+
201230
it("tests if pressing enter twice fires submit 2 times and change once", () => {
202231
cy.mount(
203232
<form>

packages/main/src/Input.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -837,9 +837,11 @@ class Input extends UI5Element implements SuggestionComponent, IFormInputElement
837837

838838
if (isEnter(e)) {
839839
const isValueUnchanged = this.previousValue === this.getInputDOMRefSync()!.value;
840+
const shouldSubmit = this._internals.form && this._internals.form.querySelectorAll("[ui5-input]").length === 1;
841+
840842
this._enterKeyDown = true;
841843

842-
if (isValueUnchanged && this._internals.form) {
844+
if (isValueUnchanged && shouldSubmit) {
843845
submitForm(this);
844846
}
845847

@@ -1138,6 +1140,8 @@ class Input extends UI5Element implements SuggestionComponent, IFormInputElement
11381140
}
11391141

11401142
_handleChange() {
1143+
const shouldSubmit = this._internals.form && this._internals.form.querySelectorAll("[ui5-input]").length === 1;
1144+
11411145
if (this._clearIconClicked) {
11421146
this._clearIconClicked = false;
11431147
return;
@@ -1159,7 +1163,7 @@ class Input extends UI5Element implements SuggestionComponent, IFormInputElement
11591163
} else {
11601164
fireChange();
11611165

1162-
if (this._enterKeyDown && this._internals.form) {
1166+
if (this._enterKeyDown && shouldSubmit) {
11631167
submitForm(this);
11641168
}
11651169
}

0 commit comments

Comments
 (0)