Skip to content

Commit 7c5647e

Browse files
authored
feat(ui5-textarea): implement input event (#543)
1 parent 366a0b3 commit 7c5647e

File tree

2 files changed

+45
-8
lines changed

2 files changed

+45
-8
lines changed

packages/main/src/TextArea.hbs

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
maxlength="{{_exceededTextProps.calcedMaxLength}}"
2929
aria-labelledby={{ariaLabelledBy}}
3030
.value="{{value}}"
31-
@change="{{_listeners.change}}"
31+
@input="{{_handleInput}}"
32+
@change="{{_handleChange}}"
3233
data-sap-focus-ref>
3334
</textarea>
3435

packages/main/src/TextArea.js

+43-7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import litRender from "@ui5/webcomponents-base/dist/renderer/LitRenderer.js";
33
import Integer from "@ui5/webcomponents-base/dist/types/Integer.js";
44
import { fetchI18nBundle, getI18nBundle } from "@ui5/webcomponents-base/dist/i18nBundle.js";
55
import { getFeature } from "@ui5/webcomponents-base/dist/FeaturesRegistry.js";
6+
import { isIE } from "@ui5/webcomponents-core/dist/sap/ui/Device.js";
67
import TextAreaTemplate from "./generated/templates/TextAreaTemplate.lit.js";
78

89
import { TEXTAREA_CHARACTERS_LEFT, TEXTAREA_CHARACTERS_EXCEEDED } from "./generated/i18n/i18n-defaults.js";
@@ -189,9 +190,6 @@ const metadata = {
189190
type: String,
190191
noAttribute: true,
191192
},
192-
_listeners: {
193-
type: Object,
194-
},
195193
},
196194
events: /** @lends sap.ui.webcomponents.main.TextArea.prototype */ {
197195
/**
@@ -201,6 +199,16 @@ const metadata = {
201199
* @public
202200
*/
203201
change: {},
202+
203+
/**
204+
* Fired when the value of the <code>ui5-textarea</code> changes at each keystroke or when
205+
* something is pasted.
206+
*
207+
* @event
208+
* @since 1.0.0-rc.5
209+
* @public
210+
*/
211+
input: {},
204212
},
205213
_eventHandlersByConvention: true,
206214
};
@@ -250,10 +258,6 @@ class TextArea extends UI5Element {
250258
super();
251259

252260
this.i18nBundle = getI18nBundle("@ui5/webcomponents");
253-
254-
this._listeners = {
255-
change: this._handleChange.bind(this),
256-
};
257261
}
258262

259263
onBeforeRendering() {
@@ -295,6 +299,14 @@ class TextArea extends UI5Element {
295299
this.value = inputValue;
296300
}
297301

302+
onkeydown() {
303+
this._keyDown = true;
304+
}
305+
306+
onkeyup() {
307+
this._keyDown = false;
308+
}
309+
298310
onfocusin() {
299311
this.focused = true;
300312
}
@@ -307,6 +319,30 @@ class TextArea extends UI5Element {
307319
this.fireEvent("change", {});
308320
}
309321

322+
_handleInput(event) {
323+
const nativeTextarea = this.getInputDomRef();
324+
325+
/* skip calling change event when an textarea with a placeholder is focused on IE
326+
- value of the host and the internal textarea should be different in case of actual input
327+
- input is called when a key is pressed => keyup should not be called yet
328+
*/
329+
const skipFiring = (this.getInputDomRef().value === this.value) && isIE() && !this._keyDown && !!this.placeholder;
330+
if (event.target === nativeTextarea) {
331+
// stop the native event, as the semantic "input" would be fired.
332+
event.stopImmediatePropagation();
333+
}
334+
335+
if (skipFiring) {
336+
return;
337+
}
338+
339+
this.value = nativeTextarea.value;
340+
this.fireEvent("input", {});
341+
342+
// Angular two way data binding
343+
this.fireEvent("value-changed");
344+
}
345+
310346
_tokenizeText(value) {
311347
const tokenizedText = value.replace(/&/gm, "&amp;").replace(/"/gm, "&quot;").replace(/"/gm, "&#39;").replace(/</gm, "&lt;")
312348
.replace(/>/gm, "&gt;")

0 commit comments

Comments
 (0)