Skip to content

fix(ui5-textarea): escape interaction can now be prevented #12122

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 6 commits into from
Aug 15, 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
38 changes: 38 additions & 0 deletions packages/main/cypress/specs/TextArea.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,44 @@ describe("TextArea general interaction", () => {
.should("have.value", "");
});

it("Should allow preventing escape behavior by preventing the input event", () => {
cy.mount(<TextArea value="initial value"></TextArea>);

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

cy.get("@textarea")
.then(textarea => {
textarea.get(0).addEventListener("ui5-input", (event: CustomEvent) => {
if (event.detail && event.detail.escapePressed) {
event.preventDefault();
}
});
});

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

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

cy.get("@textarea")
.realType(" modified");

cy.get("@textarea")
.shadow()
.find("textarea")
.should("have.value", "initial value modified");

cy.get("@textarea")
.realPress("Escape");

cy.get("@textarea")
.shadow()
.find("textarea")
.should("have.value", "initial value modified");
});

it("Value state type should be added to the screen readers default value states announcement", () => {
// Negative
cy.mount(<TextArea valueState="Negative"></TextArea>);
Expand Down
20 changes: 16 additions & 4 deletions packages/main/src/TextArea.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ type ExceededText = {
calcedMaxLength?: number;
};

type TextAreaInputEventDetail = {
escapePressed?: boolean;
};

/**
* @class
*
Expand Down Expand Up @@ -97,10 +101,12 @@ type ExceededText = {
* Fired when the value of the component changes at each keystroke or when
* something is pasted.
* @since 1.0.0-rc.5
* @param {boolean} escapePressed Indicates whether the Escape key was pressed, which triggers a revert to the previous value
* @public
*/
@event("input", {
bubbles: true,
cancelable: true,
})

/**
Expand All @@ -126,7 +132,7 @@ type ExceededText = {
class TextArea extends UI5Element implements IFormInputElement {
eventDetails!: {
"change": void;
"input": void;
"input": TextAreaInputEventDetail;
"select": void;
"scroll": void;
"value-changed": void;
Expand Down Expand Up @@ -393,9 +399,14 @@ class TextArea extends UI5Element implements IFormInputElement {
if (isEscape(e)) {
const nativeTextArea = this.getInputDomRef();

this.value = this.previousValue;
nativeTextArea.value = this.value;
this.fireDecoratorEvent("input");
const prevented = !this.fireDecoratorEvent("input", {
escapePressed: true,
});

if (!prevented) {
this.value = this.previousValue;
nativeTextArea.value = this.value;
}
}
}

Expand Down Expand Up @@ -641,3 +652,4 @@ class TextArea extends UI5Element implements IFormInputElement {
TextArea.define();

export default TextArea;
export type { TextAreaInputEventDetail };
11 changes: 11 additions & 0 deletions packages/main/test/pages/TextArea.html
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,11 @@
<ui5-textarea id="taWithLabelID2" class="textarea-width" placeholder="Enter some text here" accessible-name="Here aria label should be this one"></ui5-textarea>
</section>

<section class="group">
<ui5-label>Text Area: Escape Prevention</ui5-title>
<ui5-textarea id="textarea-prevent-escape" class="textarea-width" placeholder="Escape prevention test"></ui5-textarea>
</section>

<script>
var changeCounter = 0;
var inputCounter = 0;
Expand All @@ -223,6 +228,12 @@
inputCounter += 1;
document.getElementById('inputResult').value = `${inputCounter}`;
});

document.getElementById('textarea-prevent-escape').addEventListener("ui5-input", function (event) {
if (event.detail.escapePressed) {
event.preventDefault();
}
});
</script>

</body>
Expand Down
Loading