-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathInput.js
1280 lines (1080 loc) · 33.7 KB
/
Input.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import UI5Element from "@ui5/webcomponents-base/dist/UI5Element.js";
import litRender from "@ui5/webcomponents-base/dist/renderer/LitRenderer.js";
import ResizeHandler from "@ui5/webcomponents-base/dist/delegate/ResizeHandler.js";
import { renderFinished } from "@ui5/webcomponents-base/dist/Render.js";
import { isIE, isPhone, isSafari } from "@ui5/webcomponents-base/dist/Device.js";
import ValueState from "@ui5/webcomponents-base/dist/types/ValueState.js";
import { getFeature } from "@ui5/webcomponents-base/dist/FeaturesRegistry.js";
import {
isUp,
isDown,
isSpace,
isEnter,
isBackSpace,
isEscape,
} from "@ui5/webcomponents-base/dist/Keys.js";
import Integer from "@ui5/webcomponents-base/dist/types/Integer.js";
import { fetchI18nBundle, getI18nBundle } from "@ui5/webcomponents-base/dist/i18nBundle.js";
import { getEffectiveAriaLabelText } from "@ui5/webcomponents-base/dist/util/AriaLabelHelper.js";
import { getCaretPosition, setCaretPosition } from "@ui5/webcomponents-base/dist/util/Caret.js";
import "@ui5/webcomponents-icons/dist/decline.js";
import InputType from "./types/InputType.js";
import Popover from "./Popover.js";
// Templates
import InputTemplate from "./generated/templates/InputTemplate.lit.js";
import InputPopoverTemplate from "./generated/templates/InputPopoverTemplate.lit.js";
import {
VALUE_STATE_SUCCESS,
VALUE_STATE_INFORMATION,
VALUE_STATE_ERROR,
VALUE_STATE_WARNING,
INPUT_SUGGESTIONS,
INPUT_SUGGESTIONS_TITLE,
INPUT_SUGGESTIONS_ONE_HIT,
INPUT_SUGGESTIONS_MORE_HITS,
INPUT_SUGGESTIONS_NO_HIT,
} from "./generated/i18n/i18n-defaults.js";
// Styles
import styles from "./generated/themes/Input.css.js";
import ResponsivePopoverCommonCss from "./generated/themes/ResponsivePopoverCommon.css.js";
import ValueStateMessageCss from "./generated/themes/ValueStateMessage.css.js";
const rgxFloat = new RegExp(/(\+|-)?\d+(\.|,)\d+/);
/**
* @public
*/
const metadata = {
tag: "ui5-input",
languageAware: true,
managedSlots: true,
slots: /** @lends sap.ui.webcomponents.main.Input.prototype */ {
/**
* Defines the icon to be displayed in the component.
*
* @type {sap.ui.webcomponents.main.IIcon}
* @slot
* @public
*/
icon: {
type: HTMLElement,
},
/**
* Defines the suggestion items.
* <br><br>
* Example:
* <br><br>
* <ui5-input show-suggestions><br>
* <ui5-suggestion-item text="Item #1"></ui5-suggestion-item><br>
* <ui5-suggestion-item text="Item #2"></ui5-suggestion-item><br>
* </ui5-input>
* <br>
* <ui5-input show-suggestions>
* <ui5-suggestion-group-item text="Group #1"></ui5-suggestion-group-item>
* <ui5-suggestion-item text="Item #1"></ui5-suggestion-item>
* <ui5-suggestion-item text="Item #2"></ui5-suggestion-item>
* <ui5-suggestion-group-item text="Group #2"></ui5-suggestion-group-item>
* <ui5-suggestion-item text="Item #3"></ui5-suggestion-item>
* <ui5-suggestion-item text="Item #4"></ui5-suggestion-item>
* </ui5-input>
* <br><br>
* <b>Note:</b> The suggestion would be displayed only if the <code>showSuggestions</code>
* property is set to <code>true</code>.
* <br><br>
* <b>Note:</b> The <ui5-suggestion-item> and <ui5-suggestion-group-item> are recommended to be used as suggestion items.
* <br><br>
* <b>Note:</b> Importing the Input Suggestions Support feature:
* <br>
* <code>import "@ui5/webcomponents/dist/features/InputSuggestions.js";</code>
* <br>
* automatically imports the <ui5-suggestion-item> and <ui5-suggestion-group-item> for your convenience.
*
* @type {sap.ui.webcomponents.main.IInputSuggestionItem[]}
* @slot suggestionItems
* @public
*/
"default": {
propertyName: "suggestionItems",
type: HTMLElement,
},
/**
* The slot is used for native <code>input</code> HTML element to enable form submit,
* when <code>name</code> property is set.
* @type {HTMLElement[]}
* @private
*/
formSupport: {
type: HTMLElement,
},
/**
* Defines the value state message that will be displayed as pop up under the component.
* <br><br>
*
* <b>Note:</b> If not specified, a default text (in the respective language) will be displayed.
* <br>
* <b>Note:</b> The <code>valueStateMessage</code> would be displayed,
* when the component is in <code>Information</code>, <code>Warning</code> or <code>Error</code> value state.
* <br>
* <b>Note:</b> If the component has <code>suggestionItems</code>,
* the <code>valueStateMessage</code> would be displayed as part of the same popover, if used on desktop, or dialog - on phone.
* @type {HTMLElement[]}
* @since 1.0.0-rc.6
* @slot
* @public
*/
valueStateMessage: {
type: HTMLElement,
},
},
properties: /** @lends sap.ui.webcomponents.main.Input.prototype */ {
/**
* Defines whether the component is in disabled state.
* <br><br>
* <b>Note:</b> A disabled component is completely noninteractive.
*
* @type {boolean}
* @defaultvalue false
* @public
*/
disabled: {
type: Boolean,
},
/**
* Defines if characters within the suggestions are to be highlighted
* in case the input value matches parts of the suggestions text.
* <br><br>
* <b>Note:</b> takes effect when <code>showSuggestions</code> is set to <code>true</code>
*
* @type {boolean}
* @defaultvalue false
* @private
* @sicne 1.0.0-rc.8
*/
highlight: {
type: Boolean,
},
/**
* Defines a short hint intended to aid the user with data entry when the
* component has no value.
* @type {string}
* @defaultvalue ""
* @public
*/
placeholder: {
type: String,
},
/**
* Defines whether the component is read-only.
* <br><br>
* <b>Note:</b> A read-only component is not editable,
* but still provides visual feedback upon user interaction.
*
* @type {boolean}
* @defaultvalue false
* @public
*/
readonly: {
type: Boolean,
},
/**
* Defines whether the component is required.
*
* @type {boolean}
* @defaultvalue false
* @public
* @since 1.0.0-rc.3
*/
required: {
type: Boolean,
},
/**
* Defines the HTML type of the component.
* Available options are: <code>Text</code>, <code>Email</code>,
* <code>Number</code>, <code>Password</code>, <code>Tel</code>, and <code>URL</code>.
* <br><br>
* <b>Notes:</b>
* <ul>
* <li>The particular effect of this property differs depending on the browser
* and the current language settings, especially for type <code>Number</code>.</li>
* <li>The property is mostly intended to be used with touch devices
* that use different soft keyboard layouts depending on the given input type.</li>
* </ul>
*
* @type {InputType}
* @defaultvalue "Text"
* @public
*/
type: {
type: InputType,
defaultValue: InputType.Text,
},
/**
* Defines the value of the component.
* <br><br>
* <b>Note:</b> The property is updated upon typing.
*
* @type {string}
* @defaultvalue ""
* @public
*/
value: {
type: String,
},
/**
* Defines the value state of the component.
* <br><br>
* Available options are:
* <ul>
* <li><code>None</code></li>
* <li><code>Error</code></li>
* <li><code>Warning</code></li>
* <li><code>Success</code></li>
* <li><code>Information</code></li>
* </ul>
*
* @type {ValueState}
* @defaultvalue "None"
* @public
*/
valueState: {
type: ValueState,
defaultValue: ValueState.None,
},
/**
* Determines the name with which the component will be submitted in an HTML form.
*
* <br><br>
* <b>Important:</b> For the <code>name</code> property to have effect, you must add the following import to your project:
* <code>import "@ui5/webcomponents/dist/features/InputElementsFormSupport.js";</code>
*
* <br><br>
* <b>Note:</b> When set, a native <code>input</code> HTML element
* will be created inside the component so that it can be submitted as
* part of an HTML form. Do not use this property unless you need to submit a form.
*
* @type {string}
* @defaultvalue ""
* @public
*/
name: {
type: String,
},
/**
* Defines whether the component should show suggestions, if such are present.
* <br><br>
* <b>Note:</b> You need to import the <code>InputSuggestions</code> module
* from <code>"@ui5/webcomponents/dist/features/InputSuggestions.js"</code> to enable this functionality.
* @type {boolean}
* @defaultvalue false
* @public
*/
showSuggestions: {
type: Boolean,
},
/**
* Sets the maximum number of characters available in the input field.
*
* @type {Integer}
* @since 1.0.0-rc.5
* @public
*/
maxlength: {
type: Integer,
},
/**
* Defines the aria-label attribute for the input
*
* @type {String}
* @since 1.0.0-rc.8
* @private
* @defaultvalue ""
*/
ariaLabel: {
type: String,
},
/**
* Receives id(or many ids) of the elements that label the input
*
* @type {String}
* @defaultvalue ""
* @private
* @since 1.0.0-rc.8
*/
ariaLabelledby: {
type: String,
defaultValue: "",
},
/**
* @private
*/
focused: {
type: Boolean,
},
_input: {
type: Object,
},
_inputAccInfo: {
type: Object,
},
_nativeInputAttributes: {
type: Object,
},
_inputWidth: {
type: Integer,
},
_listWidth: {
type: Integer,
},
_isPopoverOpen: {
type: Boolean,
noAttribute: true,
},
_inputIconFocused: {
type: Boolean,
noAttribute: true,
},
},
events: /** @lends sap.ui.webcomponents.main.Input.prototype */ {
/**
* Fired when the input operation has finished by pressing Enter or on focusout.
*
* @event
* @public
*/
change: {},
/**
* Fired when the value of the component changes at each keystroke,
* and when a suggestion item has been selected.
*
* @event
* @public
*/
input: {},
/**
* Fired when a suggestion item, that is displayed in the suggestion popup, is selected.
*
* @event sap.ui.webcomponents.main.Input#suggestion-item-select
* @param {HTMLElement} item The selected item
* @public
*/
"suggestion-item-select": {
detail: {
item: { type: HTMLElement },
},
},
/**
* Fired when the user navigates to a suggestion item via the ARROW keys,
* as a preview, before the final selection.
*
* @event sap.ui.webcomponents.main.Input#suggestion-item-preview
* @param {HTMLElement} item The previewed suggestion item
* @param {HTMLElement} targetRef The DOM ref of the suggestion item.
* @public
* @since 1.0.0-rc.8
*/
"suggestion-item-preview": {
detail: {
item: { type: HTMLElement },
targetRef: { type: HTMLElement },
},
},
/**
* Fired when the user scrolls the suggestion popover.
*
* @event sap.ui.webcomponents.main.Input#suggestion-scroll
* @param {Integer} scrollTop The current scroll position
* @param {HTMLElement} scrollContainer The scroll container
* @public
* @since 1.0.0-rc.8
*/
"suggestion-scroll": {
detail: {
scrollTop: { type: Integer },
scrollContainer: { type: HTMLElement },
},
},
},
};
/**
* @class
* <h3 class="comment-api-title">Overview</h3>
*
* The <code>ui5-input</code> component allows the user to enter and edit text or numeric values in one line.
* <br>
* Additionally, you can provide <code>suggestionItems</code>,
* that are displayed in a popover right under the input.
* <br><br>
* The text field can be editable or read-only (<code>readonly</code> property),
* and it can be enabled or disabled (<code>enabled</code> property).
* To visualize semantic states, such as "error" or "warning", the <code>valueState</code> property is provided.
* When the user makes changes to the text, the change event is fired,
* which enables you to react on any text change.
* <br><br>
* <b>Note:</b> If you are using the <code>ui5-input</code> as a single npm module,
* don't forget to import the <code>InputSuggestions</code> module from
* "@ui5/webcomponents/dist/features/InputSuggestions.js"
* to enable the suggestions functionality.
*
* <h3>ES6 Module Import</h3>
*
* <code>import "@ui5/webcomponents/dist/Input.js";</code>
* <br>
* <code>import "@ui5/webcomponents/dist/features/InputSuggestions.js";</code> (optional - for input suggestions support)
*
* @constructor
* @author SAP SE
* @alias sap.ui.webcomponents.main.Input
* @extends sap.ui.webcomponents.base.UI5Element
* @tagname ui5-input
* @appenddocs SuggestionItem SuggestionGroupItem
* @implements sap.ui.webcomponents.main.IInput
* @public
*/
class Input extends UI5Element {
static get metadata() {
return metadata;
}
static get render() {
return litRender;
}
static get template() {
return InputTemplate;
}
static get staticAreaTemplate() {
return InputPopoverTemplate;
}
static get styles() {
return styles;
}
static get staticAreaStyles() {
return [ResponsivePopoverCommonCss, ValueStateMessageCss];
}
constructor() {
super();
// Indicates if there is selected suggestionItem.
this.hasSuggestionItemSelected = false;
// Represents the value before user moves selection from suggestion item to another
// and its value is updated after each move.
// Note: Used to register and fire "input" event upon [SPACE] or [ENTER].
// Note: The property "value" is updated upon selection move and can`t be used.
this.valueBeforeItemSelection = "";
// Represents the value before user moves selection between the suggestion items
// and its value remains the same when the user navigates up or down the list.
// Note: Used to cancel selection upon [ESC].
this.valueBeforeItemPreview = "";
// Indicates if the user selection has been canceled with [ESC].
this.suggestionSelectionCanceled = false;
// tracks the value between focus in and focus out to detect that change event should be fired.
this.previousValue = undefined;
// Indicates, if the component is rendering for first time.
this.firstRendering = true;
// The value that should be highlited.
this.highlightValue = "";
// Indicates, if the user pressed the BACKSPACE key.
this._backspaceKeyDown = false;
// all sementic events
this.EVENT_CHANGE = "change";
this.EVENT_INPUT = "input";
this.EVENT_SUGGESTION_ITEM_SELECT = "suggestion-item-select";
// all user interactions
this.ACTION_ENTER = "enter";
this.ACTION_USER_INPUT = "input";
// Suggestions array initialization
this.suggestionsTexts = [];
this.i18nBundle = getI18nBundle("@ui5/webcomponents");
this._handleResizeBound = this._handleResize.bind(this);
}
onEnterDOM() {
ResizeHandler.register(this, this._handleResizeBound);
}
onExitDOM() {
ResizeHandler.deregister(this, this._handleResizeBound);
}
onBeforeRendering() {
if (this.showSuggestions) {
this.enableSuggestions();
this.suggestionsTexts = this.Suggestions.defaultSlotProperties(this.highlightValue);
}
const FormSupport = getFeature("FormSupport");
if (FormSupport) {
FormSupport.syncNativeHiddenInput(this);
} else if (this.name) {
console.warn(`In order for the "name" property to have effect, you should also: import "@ui5/webcomponents/dist/features/InputElementsFormSupport.js";`); // eslint-disable-line
}
}
async onAfterRendering() {
if (!this.firstRendering && !isPhone() && this.Suggestions) {
const shouldOpenSuggestions = this.shouldOpenSuggestions();
this.Suggestions.toggle(shouldOpenSuggestions, {
preventFocusRestore: !this.hasSuggestionItemSelected,
});
await renderFinished();
this._listWidth = await this.Suggestions._getListWidth();
if (!isPhone() && shouldOpenSuggestions) {
// Set initial focus to the native input
(await this.getInputDOMRef()).focus();
}
}
if (!this.firstRendering && this.hasValueStateMessage) {
this.toggle(this.shouldDisplayOnlyValueStateMessage);
}
this.firstRendering = false;
}
_onkeydown(event) {
if (isUp(event)) {
return this._handleUp(event);
}
if (isDown(event)) {
return this._handleDown(event);
}
if (isSpace(event)) {
return this._handleSpace(event);
}
if (isEnter(event)) {
return this._handleEnter(event);
}
if (isEscape(event)) {
return this._handleEscape(event);
}
if (isBackSpace(event)) {
this._backspaceKeyDown = true;
this._selectedText = window.getSelection().toString();
}
if (this.showSuggestions) {
this.Suggestions._deselectItems();
}
this._keyDown = true;
}
_onkeyup(event) {
this._keyDown = false;
this._backspaceKeyDown = false;
}
/* Event handling */
_handleUp(event) {
if (this.Suggestions && this.Suggestions.isOpened()) {
this.Suggestions.onUp(event);
}
}
_handleDown(event) {
if (this.Suggestions && this.Suggestions.isOpened()) {
this.Suggestions.onDown(event);
}
}
_handleSpace(event) {
if (this.Suggestions) {
this.Suggestions.onSpace(event);
}
}
_handleEnter(event) {
const itemPressed = !!(this.Suggestions && this.Suggestions.onEnter(event));
if (!itemPressed) {
this.fireEventByAction(this.ACTION_ENTER);
}
}
_handleEscape() {
if (this.showSuggestions && this.Suggestions && this.Suggestions._isItemOnTarget()) {
// Restore the value.
this.value = this.valueBeforeItemPreview;
// Mark that the selection has been canceled, so the popover can close
// and not reopen, due to receiving focus.
this.suggestionSelectionCanceled = true;
}
}
async _onfocusin(event) {
await this.getInputDOMRef();
this.focused = true; // invalidating property
this.previousValue = this.value;
this.valueBeforeItemPreview = this.value;
this._inputIconFocused = event.target && event.target === this.querySelector("[ui5-icon]");
}
_onfocusout(event) {
const focusedOutToSuggestions = this.Suggestions && event.relatedTarget && event.relatedTarget.shadowRoot && event.relatedTarget.shadowRoot.contains(this.Suggestions.responsivePopover);
const focusedOutToValueStateMessage = event.relatedTarget && event.relatedTarget.shadowRoot && event.relatedTarget.shadowRoot.querySelector(".ui5-valuestatemessage-root");
// if focusout is triggered by pressing on suggestion item or value state message popover, skip invalidation, because re-rendering
// will happen before "itemPress" event, which will make item "active" state not visualized
if (focusedOutToSuggestions || focusedOutToValueStateMessage) {
event.stopImmediatePropagation();
return;
}
const toBeFocused = event.relatedTarget;
if (toBeFocused && toBeFocused.classList.contains(this._id)) {
return;
}
this.closePopover();
this.previousValue = "";
this.focused = false; // invalidating property
}
_click(event) {
if (isPhone() && !this.readonly && this.Suggestions) {
this.Suggestions.open(this);
this.isRespPopoverOpen = true;
}
}
_handleChange(event) {
this.fireEvent(this.EVENT_CHANGE);
}
_scroll(event) {
const detail = event.detail;
this.fireEvent("suggestion-scroll", {
scrollTop: detail.scrollTop,
scrollContainer: detail.targetRef,
});
}
async _handleInput(event) {
const inputDomRef = await this.getInputDOMRef();
const emptyValueFiredOnNumberInput = this.value && this.isTypeNumber && !inputDomRef.value;
this.suggestionSelectionCanceled = false;
if (emptyValueFiredOnNumberInput && !this._backspaceKeyDown) {
// For input with type="Number", if the delimiter is entered second time,
// the inner input is firing event with empty value
return;
}
if (emptyValueFiredOnNumberInput && this._backspaceKeyDown) {
// Issue: when the user removes the character(s) after the delimeter of numeric Input,
// the native input is firing event with an empty value and we have to manually handle this case,
// otherwise the entire input will be cleared as we sync the "value".
// There are tree scenarios:
// Example: type "123.4" and press BACKSPACE - the native input is firing event with empty value.
// Example: type "123.456", select/mark "456" and press BACKSPACE - the native input is firing event with empty value.
// Example: type "123.456", select/mark "123.456" and press BACKSPACE - the native input is firing event with empty value,
// but this time that's really the case.
// Perform manual handling in case of floating number
// and if the user did not select the entire input value
if (rgxFloat.test(this.value) && this._selectedText !== this.value) {
const newValue = this.removeFractionalPart(this.value);
// update state
this.value = newValue;
this.highlightValue = newValue;
this.valueBeforeItemPreview = newValue;
// fire events
this.fireEvent(this.EVENT_INPUT);
this.fireEvent("value-changed");
return;
}
}
if (event.target === inputDomRef) {
// stop the native event, as the semantic "input" would be fired.
event.stopImmediatePropagation();
}
/* skip calling change event when an input with a placeholder is focused on IE
- value of the host and the internal input should be differnt in case of actual input
- input is called when a key is pressed => keyup should not be called yet
*/
const skipFiring = (inputDomRef.value === this.value) && isIE() && !this._keyDown && !!this.placeholder;
!skipFiring && this.fireEventByAction(this.ACTION_USER_INPUT);
this.hasSuggestionItemSelected = false;
if (this.Suggestions) {
this.Suggestions.updateSelectedItemPosition(null);
}
}
_handleResize() {
this._inputWidth = this.offsetWidth;
}
_closeRespPopover(preventFocusRestore) {
this.Suggestions.close(preventFocusRestore);
}
async _afterOpenPopover() {
// Set initial focus to the native input
if (isPhone()) {
(await this.getInputDOMRef()).focus();
}
}
_afterClosePopover() {
this.announceSelectedItem();
// close device's keyboard and prevent further typing
if (isPhone()) {
this.blur();
}
}
toggle(isToggled) {
if (isToggled && !this.isRespPopoverOpen) {
this.openPopover();
} else {
this.closePopover();
}
}
/**
* Checks if the value state popover is open.
* @returns {boolean} true if the value state popover is open, false otherwise
*/
isValueStateOpened() {
return !!this._isPopoverOpen;
}
async openPopover() {
const popover = await this._getPopover();
if (popover) {
this._isPopoverOpen = true;
popover.openBy(this);
}
}
async closePopover() {
const popover = await this._getPopover();
popover && popover.close();
}
async _getPopover() {
const staticAreaItem = await this.getStaticAreaItemDomRef();
return staticAreaItem && staticAreaItem.querySelector("[ui5-popover]");
}
enableSuggestions() {
if (this.Suggestions) {
return;
}
const Suggestions = getFeature("InputSuggestions");
if (Suggestions) {
this.Suggestions = new Suggestions(this, "suggestionItems", true);
} else {
throw new Error(`You have to import "@ui5/webcomponents/dist/features/InputSuggestions.js" module to use ui5-input suggestions`);
}
}
shouldOpenSuggestions() {
return !!(this.suggestionItems.length
&& this.focused
&& this.showSuggestions
&& !this.hasSuggestionItemSelected
&& !this.suggestionSelectionCanceled);
}
selectSuggestion(item, keyboardUsed) {
if (item.group) {
return;
}
const itemText = item.text || item.textContent; // keep textContent for compatibility
const fireInput = keyboardUsed
? this.valueBeforeItemSelection !== itemText : this.value !== itemText;
this.hasSuggestionItemSelected = true;
if (fireInput) {
this.value = itemText;
this.valueBeforeItemSelection = itemText;
this.fireEvent(this.EVENT_INPUT);
this.fireEvent(this.EVENT_CHANGE);
}
this.valueBeforeItemPreview = "";
this.suggestionSelectionCanceled = false;
this.fireEvent(this.EVENT_SUGGESTION_ITEM_SELECT, { item });
}
previewSuggestion(item) {
this.valueBeforeItemSelection = this.value;
this.updateValueOnPreview(item);
this.announceSelectedItem();
this._previewItem = item;
}
/**
* Updates the input value on item preview.
* @param {Object} item The item that is on preview
*/
updateValueOnPreview(item) {
const noPreview = item.type === "Inactive" || item.group;
const itemValue = noPreview ? "" : (item.effectiveTitle || item.textContent);
this.value = itemValue;
}
/**
* The suggestion item on preview.
* @type { ui5-suggestion-item }
* @readonly
* @public
*/
get previewItem() {
if (!this._previewItem) {
return null;
}
return this.getSuggestionByListItem(this._previewItem);
}
async fireEventByAction(action) {
await this.getInputDOMRef();
if (this.disabled || this.readonly) {
return;
}
const inputValue = await this.getInputValue();
const isUserInput = action === this.ACTION_USER_INPUT;
const input = await this.getInputDOMRef();
const cursorPosition = input.selectionStart;
this.value = inputValue;
this.highlightValue = inputValue;
this.valueBeforeItemPreview = inputValue;
if (isSafari()) {
// When setting the value by hand, Safari moves the cursor when typing in the middle of the text (See #1761)
setTimeout(() => {
input.selectionStart = cursorPosition;
input.selectionEnd = cursorPosition;
}, 0);
}
if (isUserInput) { // input
this.fireEvent(this.EVENT_INPUT);
// Angular two way data binding
this.fireEvent("value-changed");
return;
}
// In IE, pressing the ENTER does not fire change
const valueChanged = (this.previousValue !== undefined) && (this.previousValue !== this.value);
if (isIE() && action === this.ACTION_ENTER && valueChanged) {
this.fireEvent(this.EVENT_CHANGE);
}
}
async getInputValue() {
const domRef = this.getDomRef();
if (domRef) {
return (await this.getInputDOMRef()).value;
}
return "";
}
async getInputDOMRef() {
if (isPhone() && this.Suggestions && this.suggestionItems.length) {
await this.Suggestions._respPopover();
return this.Suggestions && this.Suggestions.responsivePopover.querySelector(".ui5-input-inner-phone");
}
return this.nativeInput;
}
/**
* Returns a reference to the native input element
* @protected
*/
get nativeInput() {
return this.getDomRef() && this.getDomRef().querySelector(`input`);
}
get nativeInputWidth() {
return this.nativeInput && this.nativeInput.offsetWidth;
}
getLabelableElementId() {
return this.getInputId();
}
getSuggestionByListItem(item) {
const key = parseInt(item.getAttribute("data-ui5-key"));
return this.suggestionItems[key];
}
/**
* Returns if the suggestions popover is scrollable.
* The method returns <code>Promise</code> that resolves to true,
* if the popup is scrollable and false otherwise.
* @returns {Promise}
*/
isSuggestionsScrollable() {
if (!this.Suggestions) {
return Promise.resolve(false);
}
return this.Suggestions._isScrollable();
}