-
Notifications
You must be signed in to change notification settings - Fork 278
Expand file tree
/
Copy pathInput.ts
More file actions
2126 lines (1778 loc) · 60 KB
/
Input.ts
File metadata and controls
2126 lines (1778 loc) · 60 KB
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
/* eslint-disable spaced-comment */
import UI5Element from "@ui5/webcomponents-base/dist/UI5Element.js";
import type { UI5CustomEvent } from "@ui5/webcomponents-base";
import property from "@ui5/webcomponents-base/dist/decorators/property.js";
import customElement from "@ui5/webcomponents-base/dist/decorators/customElement.js";
import slot from "@ui5/webcomponents-base/dist/decorators/slot.js";
import event from "@ui5/webcomponents-base/dist/decorators/event-strict.js";
import jsxRenderer from "@ui5/webcomponents-base/dist/renderer/JsxRenderer.js";
import type {
AriaAutoComplete,
AriaRole,
AriaHasPopup,
ClassMap,
} from "@ui5/webcomponents-base/dist/types.js";
import ResizeHandler from "@ui5/webcomponents-base/dist/delegate/ResizeHandler.js";
import { getScopedVarName } from "@ui5/webcomponents-base/dist/CustomElementsScope.js";
import type { ResizeObserverCallback } from "@ui5/webcomponents-base/dist/delegate/ResizeHandler.js";
// @ts-expect-error
import encodeXML from "@ui5/webcomponents-base/dist/sap/base/security/encodeXML.js";
import {
isPhone,
isAndroid,
isMac,
} from "@ui5/webcomponents-base/dist/Device.js";
import ValueState from "@ui5/webcomponents-base/dist/types/ValueState.js";
import {
isUp,
isDown,
isSpace,
isEnter,
isBackSpace,
isDelete,
isEscape,
isTabNext,
isPageUp,
isPageDown,
isHome,
isEnd,
isCtrlAltF8,
} from "@ui5/webcomponents-base/dist/Keys.js";
import { attachListeners } from "@ui5/webcomponents-base/dist/util/valueStateNavigation.js";
import arraysAreEqual from "@ui5/webcomponents-base/dist/util/arraysAreEqual.js";
import type I18nBundle from "@ui5/webcomponents-base/dist/i18nBundle.js";
import i18n from "@ui5/webcomponents-base/dist/decorators/i18n.js";
import { submitForm } from "@ui5/webcomponents-base/dist/features/InputElementsFormSupport.js";
import type { IFormInputElement } from "@ui5/webcomponents-base/dist/features/InputElementsFormSupport.js";
import {
getAssociatedLabelForTexts,
getAllAccessibleNameRefTexts,
registerUI5Element,
deregisterUI5Element,
getEffectiveAriaDescriptionText,
getAllAccessibleDescriptionRefTexts,
} from "@ui5/webcomponents-base/dist/util/AccessibilityTextsHelper.js";
import { getCaretPosition, setCaretPosition } from "@ui5/webcomponents-base/dist/util/Caret.js";
import getActiveElement from "@ui5/webcomponents-base/dist/util/getActiveElement.js";
import type SuggestionItem from "./SuggestionItem.js";
import type { SuggestionComponent } from "./features/InputSuggestions.js";
import type InputSuggestions from "./features/InputSuggestions.js";
import InputType from "./types/InputType.js";
import type Popover from "./Popover.js";
import type Icon from "./Icon.js";
import type { IIcon } from "./Icon.js";
// Templates
import InputTemplate from "./InputTemplate.js";
import * as Filters from "./Filters.js";
import {
VALUE_STATE_SUCCESS,
VALUE_STATE_INFORMATION,
VALUE_STATE_ERROR,
VALUE_STATE_WARNING,
VALUE_STATE_TYPE_SUCCESS,
VALUE_STATE_TYPE_INFORMATION,
VALUE_STATE_TYPE_ERROR,
VALUE_STATE_TYPE_WARNING,
VALUE_STATE_LINK,
VALUE_STATE_LINKS,
VALUE_STATE_LINK_MAC,
VALUE_STATE_LINKS_MAC,
INPUT_SUGGESTIONS,
INPUT_SUGGESTIONS_TITLE,
INPUT_SUGGESTIONS_ONE_HIT,
INPUT_SUGGESTIONS_MORE_HITS,
INPUT_SUGGESTIONS_NO_HIT,
INPUT_CLEAR_ICON_ACC_NAME,
INPUT_AVALIABLE_VALUES,
INPUT_SUGGESTIONS_OK_BUTTON,
INPUT_SUGGESTIONS_CANCEL_BUTTON,
} from "./generated/i18n/i18n-defaults.js";
// Styles
import inputStyles from "./generated/themes/Input.css.js";
import ResponsivePopoverCommonCss from "./generated/themes/ResponsivePopoverCommon.css.js";
import ValueStateMessageCss from "./generated/themes/ValueStateMessage.css.js";
import SuggestionsCss from "./generated/themes/Suggestions.css.js";
import type { ListItemClickEventDetail, ListSelectionChangeEventDetail } from "./List.js";
import type ResponsivePopover from "./ResponsivePopover.js";
import type InputKeyHint from "./types/InputKeyHint.js";
import type InputComposition from "./features/InputComposition.js";
import InputSuggestionsFilter from "./types/InputSuggestionsFilter.js";
/**
* Interface for components that represent a suggestion item, usable in `ui5-input`
* @public
*/
interface IInputSuggestionItem extends UI5Element {
focused: boolean;
additionalText?: string;
items?: IInputSuggestionItem[];
}
interface IInputSuggestionItemSelectable extends IInputSuggestionItem {
text?: string;
selected: boolean;
}
type NativeInputAttributes = {
min?: number,
max?: number,
step?: number
}
type InputAccInfo = {
ariaRoledescription?: string,
ariaDescribedBy?: string,
ariaHasPopup?: AriaHasPopup,
ariaAutoComplete?: AriaAutoComplete,
role?: AriaRole,
ariaControls?: string,
ariaRequired?: boolean,
ariaExpanded?: boolean,
ariaDescription?: string,
ariaLabel?: string,
ariaInvalid?: boolean,
}
// all sementic events
enum INPUT_EVENTS {
CHANGE = "change",
INPUT = "input",
SELECTION_CHANGE = "selection-change",
}
// all user interactions
enum INPUT_ACTIONS {
ACTION_ENTER = "enter",
ACTION_USER_INPUT = "input",
}
type InputEventDetail = {
inputType: string;
}
type InputSelectionChangeEventDetail = {
item: IInputSuggestionItem | null;
}
type InputSuggestionScrollEventDetail = {
scrollTop: number;
scrollContainer: HTMLElement;
}
/**
* @class
* ### Overview
*
* The `ui5-input` component allows the user to enter and edit text or numeric values in one line.
*
* Additionally, you can provide `suggestionItems`,
* that are displayed in a popover right under the input.
*
* The text field can be editable or read-only (`readonly` property),
* and it can be enabled or disabled (`disabled` property).
* To visualize semantic states, such as "Negative" or "Critical", the `valueState` 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.
*
* ### Keyboard Handling
* The `ui5-input` provides the following keyboard shortcuts:
*
* - [Escape] - Closes the suggestion list, if open. If closed or not enabled, cancels changes and reverts to the value which the Input field had when it got the focus.
* - [Enter] or [Return] - If suggestion list is open takes over the current matching item and closes it. If value state or group header is focused, does nothing.
* - [Down] - Focuses the next matching item in the suggestion list. Selection-change event is fired.
* - [Up] - Focuses the previous matching item in the suggestion list. Selection-change event is fired.
* - [Home] - If focus is in the text input, moves caret before the first character. If focus is in the list, highlights the first item and updates the input accordingly.
* - [End] - If focus is in the text input, moves caret after the last character. If focus is in the list, highlights the last item and updates the input accordingly.
* - [Page Up] - If focus is in the list, moves highlight up by page size (10 items by default). If focus is in the input, does nothing.
* - [Page Down] - If focus is in the list, moves highlight down by page size (10 items by default). If focus is in the input, does nothing.
* - [Ctrl]+[Alt]+[F8] or [Command]+[Option]+[F8] - Focuses the first link in the value state message, if available. Pressing [Tab] moves the focus to the next link in the value state message, or closes the value state message if there are no more links.
*
* ### ES6 Module Import
*
* `import "@ui5/webcomponents/dist/Input.js";`
*
* @constructor
* @extends UI5Element
* @public
* @csspart root - Used to style the root DOM element of the Input component
* @csspart input - Used to style the native input element
* @csspart clear-icon - Used to style the clear icon, which can be pressed to clear user input text
*/
@customElement({
tag: "ui5-input",
languageAware: true,
formAssociated: true,
renderer: jsxRenderer,
template: InputTemplate,
styles: [
inputStyles,
ResponsivePopoverCommonCss,
ValueStateMessageCss,
SuggestionsCss,
],
})
/**
* Fired when the input operation has finished by pressing Enter or on focusout.
* @public
*/
@event("change", {
bubbles: true,
})
/**
* Fired when the value of the component changes at each keystroke,
* and when a suggestion item has been selected.
* @public
*/
@event("input", {
bubbles: true,
cancelable: true,
})
/**
* Fired when some text has been selected.
*
* @since 2.0.0
* @public
*/
@event("select", {
bubbles: true,
})
/**
* Fired when the user navigates to a suggestion item via the ARROW keys,
* as a preview, before the final selection.
* @param {HTMLElement} item The previewed suggestion item.
* @public
* @since 2.0.0
*/
@event("selection-change", {
bubbles: true,
})
/**
* Fires when a suggestion item is autocompleted in the input.
*
* @private
*/
@event("type-ahead", {
bubbles: true,
})
/**
* Fired when the user scrolls the suggestion popover.
* @param {Integer} scrollTop The current scroll position.
* @param {HTMLElement} scrollContainer The scroll container.
* @protected
* @since 1.0.0-rc.8
*/
@event("suggestion-scroll", {
bubbles: true,
})
/**
* Fired when the suggestions picker is open.
* @public
* @since 2.0.0
*/
@event("open", {
bubbles: true,
})
/**
* Fired when the suggestions picker is closed.
* @public
* @since 2.0.0
*/
@event("close")
class Input extends UI5Element implements SuggestionComponent, IFormInputElement {
eventDetails!: {
"change": InputEventDetail,
"input": InputEventDetail,
"select": void,
"selection-change": InputSelectionChangeEventDetail,
"type-ahead": void,
"suggestion-scroll": InputSuggestionScrollEventDetail,
"open": void,
"close": void,
}
/**
* Defines whether the component is in disabled state.
*
* **Note:** A disabled component is completely noninteractive.
* @default false
* @public
*/
@property({ type: Boolean })
disabled = false;
/**
* Defines if characters within the suggestions are to be highlighted
* in case the input value matches parts of the suggestions text.
*
* **Note:** takes effect when `showSuggestions` is set to `true`
* @default false
* @private
* @since 1.0.0-rc.8
*/
@property({ type: Boolean })
highlight = false;
/**
* Defines a short hint intended to aid the user with data entry when the
* component has no value.
* @default undefined
* @public
*/
@property()
placeholder?: string;
/**
* Defines whether the component is read-only.
*
* **Note:** A read-only component is not editable,
* but still provides visual feedback upon user interaction.
* @default false
* @public
*/
@property({ type: Boolean })
readonly = false;
/**
* Defines whether the component is required.
* @default false
* @public
* @since 1.0.0-rc.3
*/
@property({ type: Boolean })
required = false;
/**
* Defines whether the value will be autcompleted to match an item
* @default false
* @public
* @since 1.4.0
*/
@property({ type: Boolean })
noTypeahead = false;
/**
* Defines the HTML type of the component.
*
* **Notes:**
*
* - The particular effect of this property differs depending on the browser
* and the current language settings, especially for type `Number`.
* - The property is mostly intended to be used with touch devices
* that use different soft keyboard layouts depending on the given input type.
* @default "Text"
* @public
*/
@property()
type: `${InputType}` = "Text";
/**
* Defines the value of the component.
*
* **Note:** The property is updated upon typing.
* @default ""
* @formEvents change input
* @formProperty
* @public
*/
@property()
value = "";
/**
* Defines the inner stored value of the component.
*
* **Note:** The property is updated upon typing. In some special cases the old value is kept (e.g. deleting the value after the dot in a float)
* @default ""
* @private
*/
@property({ noAttribute: true })
_innerValue = "";
/**
* Defines the value state of the component.
* @default "None"
* @public
*/
@property()
valueState: `${ValueState}` = "None";
/**
* Determines the name by which the component will be identified upon submission in an HTML form.
*
* **Note:** This property is only applicable within the context of an HTML Form element.
* @default undefined
* @public
*/
@property()
name?: string;
/**
* Defines whether the component should show suggestions, if such are present.
*
* @default false
* @public
*/
@property({ type: Boolean })
showSuggestions = false;
/**
* Sets the maximum number of characters available in the input field.
*
* **Note:** This property is not compatible with the ui5-input type InputType.Number. If the ui5-input type is set to Number, the maxlength value is ignored.
* @default undefined
* @since 1.0.0-rc.5
* @public
*/
@property({ type: Number })
maxlength?: number;
/**
* Defines the accessible ARIA name of the component.
* @default undefined
* @public
* @since 1.0.0-rc.15
*/
@property()
accessibleName?: string;
/**
* Receives id(or many ids) of the elements that label the input.
* @default undefined
* @public
* @since 1.0.0-rc.15
*/
@property()
accessibleNameRef?: string;
/**
* Defines the accessible description of the component.
* @default undefined
* @public
* @since 2.9.0
*/
@property()
accessibleDescription?: string;
/**
* Receives id(or many ids) of the elements that describe the input.
* @default undefined
* @public
* @since 2.9.0
*/
@property()
accessibleDescriptionRef?: string;
/**
* Defines whether the clear icon of the input will be shown.
* @default false
* @public
* @since 1.2.0
*/
@property({ type: Boolean })
showClearIcon = false;
/**
* Defines whether the suggestions picker is open.
* The picker will not open if the `showSuggestions` property is set to `false`, the input is disabled or the input is readonly.
* The picker will close automatically and `close` event will be fired if the input is not in the viewport.
* @default false
* @public
* @since 2.0.0
*/
@property({ type: Boolean })
open = false;
/**
* Defines the filter type of the component.
* @default "None"
* @public
*/
@property()
filter: `${InputSuggestionsFilter}` = InputSuggestionsFilter.None;
/**
* Defines whether the clear icon is visible.
* @default false
* @private
* @since 1.2.0
*/
@property({ type: Boolean })
_effectiveShowClearIcon = false;
/**
* @private
*/
@property({ type: Boolean })
focused = false;
/**
* Used to define enterkeyhint of the inner input.
* https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/enterkeyhint
*
* @private
*/
@property()
hint?: `${InputKeyHint}`;
@property({ type: Boolean })
valueStateOpen = false;
@property({ type: Object })
_inputAccInfo: InputAccInfo = {};
@property({ type: Object })
_nativeInputAttributes: NativeInputAttributes = {};
@property({ type: Number })
_inputWidth?: number;
@property({ type: Number })
_listWidth?: number;
@property({ type: Boolean, noAttribute: true })
_inputIconFocused = false;
/**
* Constantly updated value of texts collected from the associated labels
* @private
*/
@property({ noAttribute: true })
_associatedLabelsTexts?: string;
/**
* Constantly updated value of texts collected from the accessibleNameRef elements
* @private
*/
@property({ noAttribute: true })
_accessibleLabelsRefTexts?: string;
/**
* Constantly updated value of texts collected from the associated labels
* @private
*/
@property({ noAttribute: true })
_associatedDescriptionRefTexts?: string;
/**
* @private
*/
@property({ type: Object })
Suggestions?: InputSuggestions;
/**
* @private
*/
@property({ type: Array })
_linksListenersArray: Array<(args: any) => void> = [];
/**
* Indicates whether IME composition is currently active
* @default false
* @private
*/
@property({ type: Boolean, noAttribute: true })
_isComposing = false;
/**
* Defines the suggestion items.
*
* **Note:** The suggestions would be displayed only if the `showSuggestions`
* property is set to `true`.
*
* **Note:** The `<ui5-suggestion-item>`, `<ui5-suggestion-item-group>` and `ui5-suggestion-item-custom` are recommended to be used as suggestion items.
*
* @public
*/
@slot({ type: HTMLElement, "default": true })
suggestionItems!: Array<IInputSuggestionItem>;
/**
* Defines the icon to be displayed in the component.
* @public
*/
@slot()
icon!: Array<IIcon>;
/**
* Defines the value state message that will be displayed as pop up under the component.
* The value state message slot should contain only one root element.
*
* **Note:** If not specified, a default text (in the respective language) will be displayed.
*
* **Note:** The `valueStateMessage` would be displayed,
* when the component is in `Information`, `Critical` or `Negative` value state.
*
* **Note:** If the component has `suggestionItems`,
* the `valueStateMessage` would be displayed as part of the same popover, if used on desktop, or dialog - on phone.
* @since 1.0.0-rc.6
* @public
*/
@slot({
type: HTMLElement,
invalidateOnChildChange: true,
})
valueStateMessage!: Array<HTMLElement>;
hasSuggestionItemSelected: boolean;
valueBeforeItemSelection: string;
valueBeforeSelectionStart: string;
previousValue: string;
firstRendering: boolean;
typedInValue: string;
lastConfirmedValue: string
isTyping: boolean
_handleResizeBound: ResizeObserverCallback;
_keepInnerValue: boolean;
_shouldAutocomplete?: boolean;
_enterKeyDown?: boolean;
_isKeyNavigation?: boolean;
_indexOfSelectedItem: number;
_selectedText?: string;
_clearIconClicked?: boolean;
_focusedAfterClear: boolean;
_changeToBeFired?: boolean; // used to wait change event firing after suggestion item selection
_performTextSelection?: boolean;
_isLatestValueFromSuggestions: boolean;
_isChangeTriggeredBySuggestion: boolean;
_valueStateLinks: Array<HTMLElement>;
_composition?: InputComposition;
@i18n("@ui5/webcomponents")
static i18nBundle: I18nBundle;
static composition: typeof InputComposition;
/**
* Indicates whether link navigation is being handled.
* @default false
* @private
* @since 2.11.0
*/
_handleLinkNavigation: boolean = false;
get formValidityMessage() {
return this.nativeInput?.validationMessage;
}
get _effectiveShowSuggestions() {
return !!(this.showSuggestions && this.Suggestions);
}
get formValidity(): ValidityStateFlags {
return {
valueMissing: this.nativeInput?.validity.valueMissing,
typeMismatch: this.required && this.nativeInput?.validity.typeMismatch,
patternMismatch: this.nativeInput?.validity.patternMismatch,
};
}
async formElementAnchor() {
return this.getFocusDomRefAsync();
}
get formFormattedValue(): FormData | string | null {
return this.value;
}
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 [Escape].
this.valueBeforeSelectionStart = "";
// tracks the value between focus in and focus out to detect that change event should be fired.
this.previousValue = "";
// Indicates, if the component is rendering for first time.
this.firstRendering = true;
// The typed in value.
this.typedInValue = "";
// The last value confirmed by the user with "ENTER"
this.lastConfirmedValue = "";
// Indicates, if the user is typing. Gets reset once popup is closed
this.isTyping = false;
// Indicates whether the value of the input is comming from a suggestion item
this._isLatestValueFromSuggestions = false;
this._isChangeTriggeredBySuggestion = false;
this._indexOfSelectedItem = -1;
this._handleResizeBound = this._handleResize.bind(this);
this._keepInnerValue = false;
this._focusedAfterClear = false;
this._valueStateLinks = [];
}
onEnterDOM() {
ResizeHandler.register(this, this._handleResizeBound);
registerUI5Element(this, this._updateAssociatedLabelsTexts.bind(this));
this._enableComposition();
}
onExitDOM() {
ResizeHandler.deregister(this, this._handleResizeBound);
deregisterUI5Element(this);
this._removeLinksEventListeners();
this._composition?.removeEventListeners();
}
_highlightSuggestionItem(item: SuggestionItem) {
item.markupText = this.typedInValue ? this.Suggestions?.hightlightInput((item.text || ""), this.typedInValue) : encodeXML(item.text || "");
}
_isGroupItem(item: IInputSuggestionItem) {
return item.hasAttribute("ui5-suggestion-item-group");
}
onBeforeRendering() {
if (!this._keepInnerValue) {
this._innerValue = this.value === null ? "" : this.value;
}
if (this.showSuggestions) {
this.enableSuggestions();
this._flattenItems.forEach(item => {
if (item.hasAttribute("ui5-suggestion-item")) {
this._highlightSuggestionItem(item as SuggestionItem);
} else if (this._isGroupItem(item)) {
item.items?.forEach(nestedItem => {
this._highlightSuggestionItem(nestedItem as SuggestionItem);
});
}
});
}
this._effectiveShowClearIcon = (this.showClearIcon && !!this.value && !this.readonly && !this.disabled);
this.style.setProperty(getScopedVarName("--_ui5-input-icons-count"), `${this.iconsCount}`);
const hasItems = !!this._flattenItems.length;
const hasValue = !!this.value;
const isFocused = this.shadowRoot!.querySelector("input") === getActiveElement();
const preventOpenPicker = this.disabled || this.readonly;
const shouldOpenSuggestions = !preventOpenPicker && !this._isPhone && hasItems && (this.open || (hasValue && isFocused && this.isTyping));
if (preventOpenPicker) {
this.open = false;
} else if (!this._isPhone) {
this.open = hasItems && (this.open || (hasValue && isFocused && this.isTyping));
}
if (this.shouldDisplayOnlyValueStateMessage && !shouldOpenSuggestions) {
this.openValueStatePopover();
} else {
this.closeValueStatePopover();
}
const value = this.value;
const innerInput = this.getInputDOMRefSync();
if (!innerInput || !value) {
return;
}
if (this.filter !== InputSuggestionsFilter.None) {
this._filterItems(this.typedInValue);
}
const autoCompletedChars = innerInput.selectionEnd! - innerInput.selectionStart!;
// Typehead causes issues on Android devices, so we disable it for now
// If there is already a selection the autocomplete has already been performed
if (this._shouldAutocomplete && !isAndroid() && !autoCompletedChars && !this._isKeyNavigation) {
const item = this._getFirstMatchingItem(value);
if (item) {
if (!this._isComposing) {
this._handleTypeAhead(item);
}
this._selectMatchingItem(item);
}
}
}
onAfterRendering() {
const innerInput = this.getInputDOMRefSync()!;
if (this.showSuggestions && this.Suggestions?._getPicker()) {
this._listWidth = this.Suggestions._getListWidth();
// disabled ItemNavigation from the list since we are not using it
this.Suggestions._getList()._itemNavigation._getItems = () => [];
}
if (this._performTextSelection) {
// this is required to syncronize lit-html input's value and user's input
// lit-html does not sync its stored value for the value property when the user is typing
if (innerInput.value !== this._innerValue) {
innerInput.value = this._innerValue;
}
if (this.typedInValue.length && this.value.length) {
// "Contains" filtering requires custom selection range handling.
// Example: "e" → "Belgium" (item does not start with typed value, so select all).
if (this.filter === InputSuggestionsFilter.Contains) {
this._adjustContainsSelectionRange();
} else {
innerInput.setSelectionRange(this.typedInValue.length, this.value.length);
}
}
this.fireDecoratorEvent("type-ahead");
}
this._performTextSelection = false;
if (!arraysAreEqual(this._valueStateLinks, this.linksInAriaValueStateHiddenText)) {
this._removeLinksEventListeners();
this._addLinksEventListeners();
this._valueStateLinks = this.linksInAriaValueStateHiddenText;
}
}
_adjustContainsSelectionRange() {
const innerInput = this.getInputDOMRefSync()!;
const visibleItems = this.Suggestions?._getItems().filter(item => !item.hidden) as IInputSuggestionItemSelectable[];
const currentItem = visibleItems?.find(item => { return item.selected || item.focused; });
const groupItems = this._flattenItems.filter(item => this._isGroupItem(item));
if (currentItem && !groupItems.includes(currentItem)) {
const doesItemStartWithTypedValue = currentItem?.text?.toLowerCase().startsWith(this.typedInValue.toLowerCase());
if (doesItemStartWithTypedValue) {
innerInput.setSelectionRange(this.typedInValue.length, this.value.length);
} else {
innerInput.setSelectionRange(0, this.value.length);
}
}
}
_onkeydown(e: KeyboardEvent) {
this._isKeyNavigation = true;
this._shouldAutocomplete = !this.noTypeahead && !(isBackSpace(e) || isDelete(e) || isEscape(e));
if (isUp(e)) {
return this._handleUp(e);
}
if (isDown(e)) {
return this._handleDown(e);
}
if (isSpace(e)) {
return this._handleSpace(e);
}
if (isTabNext(e)) {
return this._handleTab();
}
if (isEnter(e)) {
const isValueUnchanged = this.previousValue === this.getInputDOMRefSync()!.value;
const shouldSubmit = this._internals.form && this._internals.form.querySelectorAll("[ui5-input]").length === 1;
this._enterKeyDown = true;
if (isValueUnchanged && shouldSubmit) {
submitForm(this);
}
return this._handleEnter(e);
}
if (isPageUp(e)) {
return this._handlePageUp(e);
}
if (isPageDown(e)) {
return this._handlePageDown(e);
}
if (isHome(e)) {
return this._handleHome(e);
}
if (isEnd(e)) {
return this._handleEnd(e);
}
if (isEscape(e)) {
return this._handleEscape();
}
if (isCtrlAltF8(e)) {
return this._handleCtrlAltF8();
}
if (this.showSuggestions) {
this._clearPopoverFocusAndSelection();
}
this._isKeyNavigation = false;
}
_onkeyup(e: KeyboardEvent) {
// The native Delete event does not update the value property "on time".
// So, the (native) change event is always fired with the old value
if (isDelete(e)) {
this.value = (e.target as HTMLInputElement).value;
}
this._enterKeyDown = false;
}
get currentItemIndex() {
const allItems = this.Suggestions?._getItems() as IInputSuggestionItemSelectable[];
const visibleItems = allItems.filter(item => !item.hidden);
const currentItem = visibleItems.find(item => { return item.selected || item.focused; });
const indexOfCurrentItem = currentItem ? visibleItems.indexOf(currentItem) : -1;
return indexOfCurrentItem;
}
_handleUp(e: KeyboardEvent) {
if (this.Suggestions?.isOpened()) {
this.Suggestions.onUp(e, this.currentItemIndex);
}
}
_handleDown(e: KeyboardEvent) {
if (this.Suggestions?.isOpened()) {
this.Suggestions.onDown(e, this.currentItemIndex);
}
}
_handleSpace(e: KeyboardEvent) {
if (this.Suggestions) {
this.Suggestions.onSpace(e);
}
}
_handleTab() {
if (this.Suggestions && (this.previousValue !== this.value)) {
this.Suggestions.onTab();
}
}
_handleCtrlAltF8() {
this._handleLinkNavigation = true;
const links = this.linksInAriaValueStateHiddenText;
if (links.length) {
links[0].focus();
}
}
_addLinksEventListeners() {
const links = this.linksInAriaValueStateHiddenText;
links.forEach((link, index) => {
this._linksListenersArray.push((e: KeyboardEvent) => {
attachListeners(e, links, index, {
closeValueState: () => {
if (this.Suggestions?.isOpened()) {
this.Suggestions?.close();
}
if (this.valueStateOpen) {
this.closeValueStatePopover();
}