@@ -11,6 +11,7 @@ import {
11
11
isSpace ,
12
12
isEnter ,
13
13
isBackSpace ,
14
+ isEscape ,
14
15
} from "@ui5/webcomponents-base/dist/Keys.js" ;
15
16
import Integer from "@ui5/webcomponents-base/dist/types/Integer.js" ;
16
17
import { fetchI18nBundle , getI18nBundle } from "@ui5/webcomponents-base/dist/i18nBundle.js" ;
@@ -494,11 +495,20 @@ class Input extends UI5Element {
494
495
// Indicates if there is selected suggestionItem.
495
496
this . hasSuggestionItemSelected = false ;
496
497
497
- // Represents the value before user moves selection between the suggestion items.
498
- // Used to register and fire "input" event upon [SPACE] or [ENTER].
499
- // Note: the property "value" is updated upon selection move and can`t be used.
498
+ // Represents the value before user moves selection from suggestion item to another
499
+ // and its value is updated after each move.
500
+ // Note: Used to register and fire "input" event upon [SPACE] or [ENTER].
501
+ // Note: The property "value" is updated upon selection move and can`t be used.
500
502
this . valueBeforeItemSelection = "" ;
501
503
504
+ // Represents the value before user moves selection between the suggestion items
505
+ // and its value remains the same when the user navigates up or down the list.
506
+ // Note: Used to cancel selection upon [ESC].
507
+ this . valueBeforeItemPreview = "" ;
508
+
509
+ // Indicates if the user selection has been canceled with [ESC].
510
+ this . suggestionSelectionCanceled = false ;
511
+
502
512
// tracks the value between focus in and focus out to detect that change event should be fired.
503
513
this . previousValue = undefined ;
504
514
@@ -591,6 +601,10 @@ class Input extends UI5Element {
591
601
return this . _handleEnter ( event ) ;
592
602
}
593
603
604
+ if ( isEscape ( event ) ) {
605
+ return this . _handleEscape ( event ) ;
606
+ }
607
+
594
608
this . _keyDown = true ;
595
609
}
596
610
@@ -624,6 +638,20 @@ class Input extends UI5Element {
624
638
}
625
639
}
626
640
641
+ _handleEscape ( ) {
642
+ if ( this . showSuggestions && this . Suggestions && this . Suggestions . _isItemOnTarget ( ) ) {
643
+ // Restore the value.
644
+ this . value = this . valueBeforeItemPreview ;
645
+
646
+ // Mark that the selection has been canceled, so the popover can close
647
+ // and not reopen, due to receiving focus.
648
+ this . suggestionSelectionCanceled = true ;
649
+
650
+ // Close suggestion popover
651
+ this . _closeRespPopover ( true ) ;
652
+ }
653
+ }
654
+
627
655
async _onfocusin ( event ) {
628
656
const inputDomRef = await this . getInputDOMRef ( ) ;
629
657
@@ -633,6 +661,7 @@ class Input extends UI5Element {
633
661
634
662
this . focused = true ; // invalidating property
635
663
this . previousValue = this . value ;
664
+ this . valueBeforeItemPreview = this . value ;
636
665
637
666
this . _inputIconFocused = event . target && event . target === this . querySelector ( "[ui5-icon]" ) ;
638
667
}
@@ -682,6 +711,8 @@ class Input extends UI5Element {
682
711
async _handleInput ( event ) {
683
712
const inputDomRef = await this . getInputDOMRef ( ) ;
684
713
714
+ this . suggestionSelectionCanceled = false ;
715
+
685
716
if ( this . value && this . type === InputType . Number && ! isBackSpace ( event ) && ! inputDomRef . value ) {
686
717
// For input with type="Number", if the delimiter is entered second time, the inner input is firing event with empty value
687
718
return ;
@@ -711,8 +742,8 @@ class Input extends UI5Element {
711
742
this . _inputWidth = this . offsetWidth ;
712
743
}
713
744
714
- _closeRespPopover ( ) {
715
- this . Suggestions . close ( ) ;
745
+ _closeRespPopover ( preventFocusRestore ) {
746
+ this . Suggestions . close ( preventFocusRestore ) ;
716
747
}
717
748
718
749
async _afterOpenPopover ( ) {
@@ -786,7 +817,8 @@ class Input extends UI5Element {
786
817
return ! ! ( this . suggestionItems . length
787
818
&& this . focused
788
819
&& this . showSuggestions
789
- && ! this . hasSuggestionItemSelected ) ;
820
+ && ! this . hasSuggestionItemSelected
821
+ && ! this . suggestionSelectionCanceled ) ;
790
822
}
791
823
792
824
selectSuggestion ( item , keyboardUsed ) {
@@ -807,6 +839,9 @@ class Input extends UI5Element {
807
839
this . fireEvent ( this . EVENT_CHANGE ) ;
808
840
}
809
841
842
+ this . valueBeforeItemPreview = "" ;
843
+ this . suggestionSelectionCanceled = false ;
844
+
810
845
this . fireEvent ( this . EVENT_SUGGESTION_ITEM_SELECT , { item } ) ;
811
846
}
812
847
@@ -857,6 +892,7 @@ class Input extends UI5Element {
857
892
858
893
this . value = inputValue ;
859
894
this . highlightValue = inputValue ;
895
+ this . valueBeforeItemPreview = inputValue ;
860
896
861
897
if ( isSafari ( ) ) {
862
898
// When setting the value by hand, Safari moves the cursor when typing in the middle of the text (See #1761)
0 commit comments