@@ -221,6 +221,7 @@ export const CodeAutocompleteField = ({
221221 const currentCm = React . useRef < EditorView > ( undefined ) ;
222222 currentCm . current = cm ;
223223 const isFocused = React . useRef ( false ) ;
224+ const suggestionsDismissed = React . useRef ( false ) ;
224225 const autoSuggestionDivRef = React . useRef < HTMLDivElement > ( null ) ;
225226 /** Mutable editor state, since this needs to be current in scope of the SingleLineEditorComponent. */
226227 const [ editorState ] = React . useState < {
@@ -229,6 +230,15 @@ export const CodeAutocompleteField = ({
229230 cm ?: EditorView ;
230231 dropdownShown : boolean ;
231232 } > ( { index : 0 , suggestions : [ ] , dropdownShown : false } ) ;
233+
234+ const setDropdownShown = React . useCallback (
235+ ( shown : boolean ) => {
236+ editorState . dropdownShown = shown ;
237+ setShouldShowDropdown ( shown ) ;
238+ } ,
239+ [ editorState ] ,
240+ ) ;
241+
232242 /** This is for the AutoSuggestionList component in order to re-render. */
233243 const [ focusedIndex , setFocusedIndex ] = React . useState ( 0 ) ;
234244 const selectedTextRanges = React . useRef < IRange [ ] > ( [ ] ) ;
@@ -266,10 +276,6 @@ export const CodeAutocompleteField = ({
266276 typeof editorState ?. cm ?. dispatch === "function" ? editorState ?. cm ?. dispatch : ( ) => { }
267277 ) as EditorView [ "dispatch" ] ;
268278
269- React . useEffect ( ( ) => {
270- editorState . dropdownShown = shouldShowDropdown ;
271- } , [ shouldShowDropdown , editorState ] ) ;
272-
273279 // Handle replacement highlighting
274280 useEffect ( ( ) => {
275281 if ( highlightedElement && cm ) {
@@ -331,7 +337,7 @@ export const CodeAutocompleteField = ({
331337 suggestionResponse ?. replacementResults ?. length === 1 &&
332338 ! suggestionResponse ?. replacementResults [ 0 ] ?. replacements ?. length
333339 ) {
334- setShouldShowDropdown ( false ) ;
340+ setDropdownShown ( false ) ;
335341 }
336342 if ( suggestionResponse ?. replacementResults ?. length ) {
337343 suggestionResponse . replacementResults . forEach (
@@ -352,7 +358,7 @@ export const CodeAutocompleteField = ({
352358 setSuggestions ( [ ] ) ;
353359 }
354360 setCurrentIndex ( 0 ) ;
355- } , [ suggestionResponse , editorState ] ) ;
361+ } , [ suggestionResponse , editorState , setDropdownShown ] ) ;
356362
357363 const getOffsetRange = ( cm : EditorView , from : number , to : number ) => {
358364 if ( ! cm ) return { fromOffset : 0 , toOffset : 0 } ;
@@ -462,8 +468,8 @@ export const CodeAutocompleteField = ({
462468 cursorPosition . current = cursor - offsetFromFirstLine ;
463469 // cursor change is fired after onChange, so we put the auto-complete logic here
464470 //get value at line
465- if ( isFocused . current ) {
466- setShouldShowDropdown ( true ) ;
471+ if ( isFocused . current && ! suggestionsDismissed . current ) {
472+ setDropdownShown ( true ) ;
467473 handleEditorInputChange . cancel ( ) ;
468474 handleEditorInputChange ( value . current , cursorPosition . current ) ;
469475 }
@@ -480,6 +486,20 @@ export const CodeAutocompleteField = ({
480486 } ;
481487
482488 const handleInputEditorKeyPress = ( event : KeyboardEvent ) => {
489+ if ( event . key === OVERWRITTEN_KEYS . Escape ) {
490+ if ( editorState . dropdownShown ) {
491+ suggestionsDismissed . current = true ;
492+
493+ event . preventDefault ( ) ;
494+ handleEscapePressed ( ) ;
495+ }
496+ // A closed dropdown lets CodeMirror handle Escape so the next Tab can leave the editor.
497+ return true ;
498+ }
499+ if ( event . key === OVERWRITTEN_KEYS . Tab && ! editorState . dropdownShown ) {
500+ return true ;
501+ }
502+ suggestionsDismissed . current = false ;
483503 const overWrittenKeys : Array < string > = Object . values ( OVERWRITTEN_KEYS ) ;
484504 if ( overWrittenKeys . includes ( event . key ) && ( useTabForCompletions || event . key !== OVERWRITTEN_KEYS . Tab ) ) {
485505 //don't prevent when enter should create new line (multiline config) and dropdown isn't shown
@@ -498,7 +518,7 @@ export const CodeAutocompleteField = ({
498518
499519 const closeDropDown = ( ) => {
500520 setHighlightedElement ( undefined ) ;
501- setShouldShowDropdown ( false ) ;
521+ setDropdownShown ( false ) ;
502522 } ;
503523
504524 const handleDropdownChange = ( selectedSuggestion : CodeAutocompleteFieldSuggestionWithReplacementInfo ) => {
@@ -525,19 +545,20 @@ export const CodeAutocompleteField = ({
525545 }
526546 } ;
527547
528- const handleInputEditorClear = ( ) => {
529- dispatch ( {
530- changes : { from : 0 , to : cm ? .state . doc . length , insert : "" } ,
548+ const handleInputEditorClear = React . useCallback ( ( ) => {
549+ currentCm . current ?. dispatch ( {
550+ changes : { from : 0 , to : currentCm . current . state . doc . length , insert : "" } ,
531551 } ) ;
532552 cursorPosition . current = 0 ;
533553 handleChange ( "" ) ;
534- cm ?. focus ( ) ;
535- } ;
554+ currentCm . current ?. focus ( ) ;
555+ } , [ handleChange ] ) ;
536556
537557 const handleInputFocus = ( focusState : boolean ) => {
538558 onFocusChange ?.( focusState ) ;
539559 if ( focusState ) {
540- setShouldShowDropdown ( true ) ;
560+ suggestionsDismissed . current = false ;
561+ setDropdownShown ( true ) ;
541562 } else {
542563 closeDropDown ( ) ;
543564 }
@@ -556,6 +577,7 @@ export const CodeAutocompleteField = ({
556577 } ;
557578
558579 const handleInputMouseDown = React . useCallback ( ( editor : EditorView ) => {
580+ suggestionsDismissed . current = false ;
559581 const cursor = editorState . cm ?. state . selection . main . head ;
560582 const currentLine = editorState . cm ?. state . doc . lineAt ( cursor ?? 0 ) . number ;
561583 const clickedLine = editor ?. state . doc . lineAt ( cursor ?? 0 ) . number ;
@@ -670,6 +692,7 @@ export const CodeAutocompleteField = ({
670692 showScrollBar ,
671693 multiline ,
672694 handleInputMouseDown ,
695+ height ,
673696 readOnly ,
674697 effectiveIntent ,
675698 ] ) ;
@@ -711,7 +734,7 @@ export const CodeAutocompleteField = ({
711734 { ! ! value . current && (
712735 < span className = { BlueprintClassNames . INPUT_ACTION } ref = { inputActionsDisplayed } >
713736 < IconButton
714- data-test-id = { "value-path-clear-btn" }
737+ data-test-id = "value-path-clear-btn"
715738 name = "operation-clear"
716739 text = { clearIconText }
717740 disabled = { readOnly }
0 commit comments