Skip to content

Commit

Permalink
fix(ui5-input): call change before submit event
Browse files Browse the repository at this point in the history
FIXES: #10534
  • Loading branch information
MapTo0 committed Jan 21, 2025
1 parent 491538b commit 3ba97bd
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
35 changes: 35 additions & 0 deletions packages/main/cypress/specs/Input.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,39 @@ describe("Input Tests", () => {
.find("ul")
.should("not.have.attr", "tabindex", "0");
});

it("tests submit and change event order", () => {
cy.mount(html`
<form>
<ui5-input />
</form>
`);

cy.get("form")
.as("form");

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

// spy change event
cy.get<Input>("@input")
.then($input => {
$input.get(0).addEventListener("change", cy.spy().as("change"));
});

// 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<Input>("@input")
.shadow()
.find("input")
.type("test{enter}");

cy.get("@change").should("have.been.calledBefore", cy.get("@submit"));
});
});
14 changes: 7 additions & 7 deletions packages/main/src/Input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ class Input extends UI5Element implements SuggestionComponent, IFormInputElement
_handleResizeBound: ResizeObserverCallback;
_keepInnerValue: boolean;
_shouldAutocomplete?: boolean;
_keyDown?: boolean;
_enterKeyDown?: boolean;
_isKeyNavigation?: boolean;
Suggestions?: InputSuggestions;
_selectedText?: string;
Expand Down Expand Up @@ -780,6 +780,7 @@ class Input extends UI5Element implements SuggestionComponent, IFormInputElement
}

if (isEnter(e)) {
this._enterKeyDown = true;
return this._handleEnter(e);
}

Expand Down Expand Up @@ -807,7 +808,6 @@ class Input extends UI5Element implements SuggestionComponent, IFormInputElement
this._clearPopoverFocusAndSelection();
}

this._keyDown = true;
this._isKeyNavigation = false;
}

Expand All @@ -818,7 +818,7 @@ class Input extends UI5Element implements SuggestionComponent, IFormInputElement
this.value = (e.target as HTMLInputElement).value;
}

this._keyDown = false;
this._enterKeyDown = false;
}

_handleUp(e: KeyboardEvent) {
Expand Down Expand Up @@ -871,10 +871,6 @@ class Input extends UI5Element implements SuggestionComponent, IFormInputElement
if (!suggestionItemPressed) {
this.lastConfirmedValue = this.value;

if (this._internals.form) {
submitForm(this);
}

return;
}

Expand Down Expand Up @@ -1025,6 +1021,10 @@ class Input extends UI5Element implements SuggestionComponent, IFormInputElement
this._changeToBeFired = true;
} else {
fireChange();

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

0 comments on commit 3ba97bd

Please sign in to comment.