Skip to content

Commit 2d19119

Browse files
authored
feat(ui5-input): enable preventing input event (#9930)
FIXES: #9884
1 parent de91b66 commit 2d19119

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

packages/main/src/Input.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ type InputSuggestionScrollEventDetail = {
221221
/**
222222
* Fired when the value of the component changes at each keystroke,
223223
* and when a suggestion item has been selected.
224+
* @allowPreventDefault
224225
* @public
225226
*/
226227
@event("input")
@@ -1014,8 +1015,15 @@ class Input extends UI5Element implements SuggestionComponent, IFormInputElement
10141015
}
10151016

10161017
_clear() {
1018+
const valueBeforeClear = this.value;
10171019
this.value = "";
1018-
this.fireEvent<InputEventDetail>(INPUT_EVENTS.INPUT, { inputType: "" });
1020+
const prevented = !this.fireEvent<InputEventDetail>(INPUT_EVENTS.INPUT, { inputType: "" }, true);
1021+
1022+
if (prevented) {
1023+
this.value = valueBeforeClear;
1024+
return;
1025+
}
1026+
10191027
if (!this._isPhone) {
10201028
this.fireResetSelectionChange();
10211029
this.focus();
@@ -1275,6 +1283,9 @@ class Input extends UI5Element implements SuggestionComponent, IFormInputElement
12751283
}
12761284

12771285
fireEventByAction(action: INPUT_ACTIONS, e: InputEvent) {
1286+
const valueBeforeInput = this.value;
1287+
const inputRef = this.getInputDOMRefSync();
1288+
12781289
if (this.disabled || this.readonly) {
12791290
return;
12801291
}
@@ -1288,7 +1299,13 @@ class Input extends UI5Element implements SuggestionComponent, IFormInputElement
12881299

12891300
if (isUserInput) { // input
12901301
const inputType = e.inputType || "";
1291-
this.fireEvent<InputEventDetail>(INPUT_EVENTS.INPUT, { inputType });
1302+
const prevented = !this.fireEvent<InputEventDetail>(INPUT_EVENTS.INPUT, { inputType }, true);
1303+
1304+
if (prevented) {
1305+
this.value = valueBeforeInput;
1306+
inputRef && (inputRef.value = valueBeforeInput);
1307+
}
1308+
12921309
// Angular two way data binding
12931310
this.fireEvent("value-changed");
12941311
this.fireResetSelectionChange();

packages/main/test/pages/Input.html

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,11 @@ <h3>Input - open suggestions picker</h3>
519519

520520
<ui5-button id="dynamic-value-state-trigger">Trigger dynamic value state</ui5-button>
521521

522+
<br>
523+
524+
<ui5-input id="prevent-input-event"></ui5-input>
525+
<ui5-input id="prevent-input-event-clear-icon" value="Test" show-clear-icon></ui5-input>
526+
522527
<script>
523528
const dynamicValueState = document.getElementById("dynamic-value-state");
524529
const dynamicValueStateTrigger = document.getElementById("dynamic-value-state-trigger");
@@ -1057,6 +1062,16 @@ <h3>Input - open suggestions picker</h3>
10571062
inputNumber3ChangeCount.value = (parseInt(inputNumber3ChangeCount.value) + 1).toString();
10581063
});
10591064

1065+
document.getElementById("prevent-input-event").addEventListener("ui5-input", event => {
1066+
if (event.target.value.length > 3) {
1067+
event.preventDefault();
1068+
}
1069+
});
1070+
1071+
document.getElementById("prevent-input-event-clear-icon").addEventListener("ui5-input", event => {
1072+
event.preventDefault();
1073+
});
1074+
10601075
</script>
10611076
</body>
10621077

packages/main/test/specs/Input.spec.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,6 +1196,30 @@ describe("Input general interaction", () => {
11961196
assert.strictEqual(await changeCount.getHTML(false), "1", "The change event is still called once");
11971197
assert.strictEqual(await suggestionsInput.getValue(), "Afghanistan", "Input's value should be the text of the selected item");
11981198
});
1199+
1200+
it("Tests prevented input event", async () => {
1201+
const input = await $("#prevent-input-event");
1202+
const innerInput = await input.shadow$("input");
1203+
1204+
await input.click();
1205+
1206+
await innerInput.keys("a");
1207+
await innerInput.keys("b");
1208+
await innerInput.keys("c");
1209+
await innerInput.keys("d");
1210+
1211+
// forth input should be prevented
1212+
assert.strictEqual(await input.getValue(), "abc", "The value is correct");
1213+
});
1214+
1215+
it("Tests prevented input event with clear icon", async () => {
1216+
const input = await $("#prevent-input-event-clear-icon");
1217+
const clearIcon = await input.shadow$(".ui5-input-clear-icon-wrapper");
1218+
1219+
await clearIcon.click();
1220+
1221+
assert.strictEqual(await input.getValue(), "Test", "The value is not cleared");
1222+
});
11991223
});
12001224

12011225
describe("Input arrow navigation", () => {

0 commit comments

Comments
 (0)