Skip to content

Commit e548778

Browse files
authored
fix(ui5-textarea): escape interaction can now be prevented (#12122)
1 parent c428f77 commit e548778

File tree

3 files changed

+65
-4
lines changed

3 files changed

+65
-4
lines changed

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,44 @@ describe("TextArea general interaction", () => {
533533
.should("have.value", "");
534534
});
535535

536+
it("Should allow preventing escape behavior by preventing the input event", () => {
537+
cy.mount(<TextArea value="initial value"></TextArea>);
538+
539+
cy.get("[ui5-textarea]")
540+
.as("textarea");
541+
542+
cy.get("@textarea")
543+
.then(textarea => {
544+
textarea.get(0).addEventListener("ui5-input", (event: CustomEvent) => {
545+
if (event.detail && event.detail.escapePressed) {
546+
event.preventDefault();
547+
}
548+
});
549+
});
550+
551+
cy.get("@textarea")
552+
.realClick();
553+
554+
cy.get("@textarea")
555+
.should("be.focused");
556+
557+
cy.get("@textarea")
558+
.realType(" modified");
559+
560+
cy.get("@textarea")
561+
.shadow()
562+
.find("textarea")
563+
.should("have.value", "initial value modified");
564+
565+
cy.get("@textarea")
566+
.realPress("Escape");
567+
568+
cy.get("@textarea")
569+
.shadow()
570+
.find("textarea")
571+
.should("have.value", "initial value modified");
572+
});
573+
536574
it("Value state type should be added to the screen readers default value states announcement", () => {
537575
// Negative
538576
cy.mount(<TextArea valueState="Negative"></TextArea>);

packages/main/src/TextArea.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ type ExceededText = {
4848
calcedMaxLength?: number;
4949
};
5050

51+
type TextAreaInputEventDetail = {
52+
escapePressed?: boolean;
53+
};
54+
5155
/**
5256
* @class
5357
*
@@ -97,10 +101,12 @@ type ExceededText = {
97101
* Fired when the value of the component changes at each keystroke or when
98102
* something is pasted.
99103
* @since 1.0.0-rc.5
104+
* @param {boolean} escapePressed Indicates whether the Escape key was pressed, which triggers a revert to the previous value
100105
* @public
101106
*/
102107
@event("input", {
103108
bubbles: true,
109+
cancelable: true,
104110
})
105111

106112
/**
@@ -126,7 +132,7 @@ type ExceededText = {
126132
class TextArea extends UI5Element implements IFormInputElement {
127133
eventDetails!: {
128134
"change": void;
129-
"input": void;
135+
"input": TextAreaInputEventDetail;
130136
"select": void;
131137
"scroll": void;
132138
"value-changed": void;
@@ -393,9 +399,14 @@ class TextArea extends UI5Element implements IFormInputElement {
393399
if (isEscape(e)) {
394400
const nativeTextArea = this.getInputDomRef();
395401

396-
this.value = this.previousValue;
397-
nativeTextArea.value = this.value;
398-
this.fireDecoratorEvent("input");
402+
const prevented = !this.fireDecoratorEvent("input", {
403+
escapePressed: true,
404+
});
405+
406+
if (!prevented) {
407+
this.value = this.previousValue;
408+
nativeTextArea.value = this.value;
409+
}
399410
}
400411
}
401412

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

643654
export default TextArea;
655+
export type { TextAreaInputEventDetail };

packages/main/test/pages/TextArea.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,11 @@
210210
<ui5-textarea id="taWithLabelID2" class="textarea-width" placeholder="Enter some text here" accessible-name="Here aria label should be this one"></ui5-textarea>
211211
</section>
212212

213+
<section class="group">
214+
<ui5-label>Text Area: Escape Prevention</ui5-title>
215+
<ui5-textarea id="textarea-prevent-escape" class="textarea-width" placeholder="Escape prevention test"></ui5-textarea>
216+
</section>
217+
213218
<script>
214219
var changeCounter = 0;
215220
var inputCounter = 0;
@@ -223,6 +228,12 @@
223228
inputCounter += 1;
224229
document.getElementById('inputResult').value = `${inputCounter}`;
225230
});
231+
232+
document.getElementById('textarea-prevent-escape').addEventListener("ui5-input", function (event) {
233+
if (event.detail.escapePressed) {
234+
event.preventDefault();
235+
}
236+
});
226237
</script>
227238

228239
</body>

0 commit comments

Comments
 (0)