Skip to content

Commit b8d978a

Browse files
authoredMar 11, 2019
refactor(ui5-input): fire input, instead of liveChange (#159)
Fires 'input' event upon user typing, instead of the 'liveChange' event. BREAKING CHANGE: liveChange event is no longer fired, listen for the input event instead.
1 parent 0a8c8cd commit b8d978a

File tree

8 files changed

+44
-30
lines changed

8 files changed

+44
-30
lines changed
 

‎packages/main/src/DatePicker.hbs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
?readonly="{{ctr.readonly}}"
1313
value-state="{{ctr.valueState}}"
1414
@change="{{ctr._input.onChange}}"
15-
@liveChange="{{ctr._input.onLiveChange}}"
15+
@input="{{ctr._input.onLiveChange}}"
1616
data-sap-focus-ref
1717
>
1818
{{#if showIcon}}

‎packages/main/src/Input.hbs

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
?readonly="{{_readonly}}"
1515
.value="{{ctr.value}}"
1616
placeholder="{{ctr.placeholder}}"
17+
@input="{{ctr._input.onInput}}"
1718
data-sap-no-tab-ref
1819
data-sap-focus-ref
1920
/>

‎packages/main/src/Input.js

+27-14
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,10 @@ const metadata = {
180180
type: Boolean,
181181
},
182182

183+
_input: {
184+
type: Object,
185+
},
186+
183187
_popover: {
184188
type: Object,
185189
},
@@ -200,7 +204,7 @@ const metadata = {
200204
* @event
201205
* @public
202206
*/
203-
liveChange: {},
207+
input: {},
204208

205209
/**
206210
* Fired when user presses Enter key on the <code>ui5-input</code>.
@@ -288,7 +292,7 @@ class Input extends WebComponent {
288292
this.previousValue = undefined;
289293

290294
// Represents the value before user moves selection between the suggestion items.
291-
// Used to register and fire "liveChange" event upon [SPACE] or [ENTER].
295+
// Used to register and fire "input" event upon [SPACE] or [ENTER].
292296
// Note: the property "value" is updated upon selection move and can`t be used.
293297
this.valueBeforeItemSelection = "";
294298

@@ -298,15 +302,19 @@ class Input extends WebComponent {
298302
// all sementic events
299303
this.EVENT_SUBMIT = "submit";
300304
this.EVENT_CHANGE = "change";
301-
this.EVENT_LIVE_CHANGE = "liveChange";
305+
this.EVENT_INPUT = "input";
302306
this.EVENT_SUGGESTION_ITEM_SELECT = "suggestionItemSelect";
303307

304308
// all user interactions
305-
this.ACTION_INPUT = "input";
306309
this.ACTION_ENTER = "enter";
307310
this.ACTION_FOCUSOUT = "focusOut";
311+
this.ACTION_USER_INPUT = "input";
308312

309-
this._whenShadowRootReady().then(this.attachFocusHandler.bind(this));
313+
this._input = {
314+
onInput: this._onInput.bind(this),
315+
};
316+
317+
this._whenShadowRootReady().then(this.attachFocusHandlers.bind(this));
310318
}
311319

312320
onBeforeRendering() {
@@ -393,8 +401,13 @@ class Input extends WebComponent {
393401
this._focused = false; // invalidating property
394402
}
395403

396-
oninput() {
397-
this.fireEventByAction(this.ACTION_INPUT);
404+
_onInput(event) {
405+
if (event.target === this.getInputDOMRef()) {
406+
// stop the native event, as the semantic "input" would be fired.
407+
event.stopImmediatePropagation();
408+
}
409+
410+
this.fireEventByAction(this.ACTION_USER_INPUT);
398411
this.hasSuggestionItemSelected = false;
399412

400413
if (this.Suggestions) {
@@ -403,7 +416,7 @@ class Input extends WebComponent {
403416
}
404417

405418
/* Private Methods */
406-
attachFocusHandler() {
419+
attachFocusHandlers() {
407420
this.shadowRoot.addEventListener("focusout", this.onfocusout.bind(this));
408421
this.shadowRoot.addEventListener("focusin", this.onfocusin.bind(this));
409422
}
@@ -430,17 +443,17 @@ class Input extends WebComponent {
430443

431444
selectSuggestion(item, keyboardUsed) {
432445
const itemText = item._nodeText;
433-
const fireLiveChange = keyboardUsed
446+
const fireInput = keyboardUsed
434447
? this.valueBeforeItemSelection !== itemText : this.value !== itemText;
435448

436449
item.selected = false;
437450
this.hasSuggestionItemSelected = true;
438451
this.fireEvent(this.EVENT_SUGGESTION_ITEM_SELECT, { item });
439452

440-
if (fireLiveChange) {
453+
if (fireInput) {
441454
this.value = itemText;
442455
this.valueBeforeItemSelection = itemText;
443-
this.fireEvent(this.EVENT_LIVE_CHANGE);
456+
this.fireEvent(this.EVENT_INPUT);
444457
}
445458
}
446459

@@ -457,14 +470,14 @@ class Input extends WebComponent {
457470
const inputValue = this.getInputValue();
458471
const isSubmit = action === this.ACTION_ENTER;
459472
const isFocusOut = action === this.ACTION_FOCUSOUT;
460-
const isInput = action === this.ACTION_INPUT;
473+
const isUserInput = action === this.ACTION_USER_INPUT;
461474

462475
this.value = inputValue;
463476

464477
const valueChanged = (this.previousValue !== undefined) && (this.previousValue !== this.value);
465478

466-
if (isInput) { // liveChange
467-
this.fireEvent(this.EVENT_LIVE_CHANGE);
479+
if (isUserInput) { // input
480+
this.fireEvent(this.EVENT_INPUT);
468481
return;
469482
}
470483

‎packages/main/test/sap/ui/webcomponents/main/pages/Input.html

+7-7
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ <h3> Input with suggestions: type 'a'</h3>
1717
<ui5-label id="myLabel">Event [suggestionItemSelect] :: N/A</ui5-label><br/>
1818
<ui5-label id="myLabelChange">Event [change] :: N/A</ui5-label><br/>
1919
<ui5-label id="myLabelSubmit">Event [submit] :: N/A</ui5-label><br/>
20-
<ui5-label id="myLabelLiveChange">Event [liveChange] :: N/A</ui5-label><br/>
20+
<ui5-label id="myLabelLiveChange">Event [input] :: N/A</ui5-label><br/>
2121

2222
<ui5-input id="myInput"
2323
style="width:100%"
@@ -88,7 +88,7 @@ <h3> Input type 'URL'</h3>
8888
var labelLiveChange = document.getElementById('myLabelLiveChange');
8989
var labelSubmit = document.getElementById('myLabelSubmit');
9090

91-
input.addEventListener("liveChange", function (event) {
91+
input.addEventListener("input", function (event) {
9292
var value = event.target.value;
9393
var suggestionItems = [];
9494

@@ -110,7 +110,7 @@ <h3> Input type 'URL'</h3>
110110
input.appendChild(li);
111111
});
112112

113-
labelLiveChange.innerHTML = "Event [liveChange] :: " + value;
113+
labelLiveChange.innerHTML = "Event [input] :: " + value;
114114
});
115115

116116
input.addEventListener("suggestionItemSelect", function (event) {
@@ -129,17 +129,17 @@ <h3> Input type 'URL'</h3>
129129
});
130130

131131
var changeCounter = 0;
132-
var liveChangeCounter = 0;
132+
var inputCounter = 0;
133133
var suggestionSelectedCounter = 0;
134134

135135
input1.addEventListener("change", function (event) {
136136
changeCounter += 1;
137137
inputResult.value = changeCounter;
138138
});
139139

140-
input2.addEventListener("liveChange", function (event) {
141-
liveChangeCounter += 1;
142-
inputResult.value = liveChangeCounter;
140+
input2.addEventListener("input", function (event) {
141+
inputCounter += 1;
142+
inputResult.value = inputCounter;
143143
});
144144

145145
myInput2.addEventListener("suggestionItemSelect", function (event) {

‎packages/main/test/sap/ui/webcomponents/main/pages/Select.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ <h2> Change event counter holder</h2>
6969
});
7070

7171
// Input with Suggestions
72-
input.addEventListener("liveChange", function (event) {
72+
input.addEventListener("input", function (event) {
7373
var value = event.target.value;
7474
var suggestionItems = [];
7575

‎packages/main/test/sap/ui/webcomponents/main/pages/kitchen-scripts.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ document.addEventListener("DOMContentLoaded", function(event) {
155155
var sap_database_entries = [{ key: "Afg", text: "Anna" }, { key: "Arg", text: "Anelia" }, { key: "Alex", text: "Ally" }, { key: "Arm", text: "Boris" }, { key: "Alg", text: "Borg" }, { key: "And", text: "Cindy" }, { key: "Ang", text: "Sara" }, { key: "Ast", text: "Sally" }, { key: "Aus", text: "Daniel" }, { key: "Aze", text: "Don" }, { key: "Aruba", text: "Ema" }, { key: "Antigua", text: "Fred" }, { key: "Bel", text: "John" }, { key: "Bel", text: "Jonathan" }, { key: "Bg", text: "Zack" }, { key: "Bra", text: "Zara" }, { key: "Bra", text: "Wolly"}, { key: "Bra", text: "Will"}, { key: "Bra", text: "Quentin"}];
156156
var input = document.getElementById('user');
157157

158-
input.addEventListener("liveChange", function (event) {
158+
input.addEventListener("input", function (event) {
159159
var value = event.target.value;
160160
var suggestionItems = [];
161161

‎packages/main/test/sap/ui/webcomponents/main/samples/Input.sample.html

+4-4
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ <h3>Input With Suggestions (Note: the usage depends on the framework you are usi
8080

8181
var oInput = document.getElementById("suggestions-input");
8282

83-
oInput.addEventListener("liveChange", function(event) {
83+
oInput.addEventListener("input", function(event) {
8484
var value = event.target.value;
8585
var suggestionItems = [];
8686

@@ -114,8 +114,8 @@ <h3>Input With Suggestions (Note: the usage depends on the framework you are usi
114114
"England", "Finland", "France", "Germany", "Hungary", "Ireland", "Italy", "Kuwait", "Luxembourg", "Mexico", "Morocco", "Norway", "Paraguay", "Philippines", "Portugal", "Spain", "Sweden", "Sri Lanka", "Senegal", "United Kingdom", "USA" ];
115115

116116
var oInput = document.getElementById("suggestions-input");
117-
// Listen for the liveChange event
118-
oInput.addEventListener("liveChange", function() {
117+
// Listen for the input event
118+
oInput.addEventListener("input", function() {
119119
var value = event.target.value;
120120
var suggestionItems = Array();
121121
// Find the new suggestions
@@ -218,7 +218,7 @@ <h3>Input with Label</h3>
218218
searchIcon.addEventListener("press", function(){
219219
alert("Look for: " + searchCriteria);
220220
});
221-
searchInput.addEventListener("liveChange", function(e){
221+
searchInput.addEventListener("input", function(e){
222222
searchCriteria = e.target.value;
223223
});
224224
window.prettyPrint();

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ describe("Input general interaction", () => {
1818
assert.strictEqual(inputResult.getProperty("value"), "2", "change is called twice");
1919
});
2020

21-
it("fires liveChange", () => {
21+
it("fires input", () => {
2222
const input2 = browser.findElementDeep("#input2 >>> input");
2323
const inputResult = browser.findElementDeep("#inputResult >>> input");
2424

2525
input2.click();
2626
input2.keys("abc");
2727

28-
assert.strictEqual(inputResult.getProperty("value"), "3", "liveChange is fired 3 times");
28+
assert.strictEqual(inputResult.getProperty("value"), "3", "input is fired 3 times");
2929
});
3030

3131
it("handles suggestions", () => {

0 commit comments

Comments
 (0)