@@ -14,6 +14,7 @@ import type {
1414
1515// external libraries 
1616import  {  isFunction  }  from  'lodash' ; 
17+ import  createRef  from  'create-react-ref/lib/createRef' ; 
1718
1819// internal utility functions 
1920import  {  createEmptyContext ,  withTheme  }  from  './HOC/withTheme' ; 
@@ -95,6 +96,8 @@ class OptionsBase extends Component<Props, State> {
9596
9697    const  {  context ,  themeId ,  theme ,  themeOverrides  }  =  props ; 
9798
99+     this . searchInputRef  =  createRef ( ) ; 
100+ 
98101    this . state  =  { 
99102      composedTheme : composeTheme ( 
100103        addThemeId ( theme  ||  context . theme ,  themeId ) , 
@@ -115,9 +118,9 @@ class OptionsBase extends Component<Props, State> {
115118  componentDidUpdate ( prevProps : Props )  { 
116119    if  ( prevProps  !==  this . props )  { 
117120      if  ( ! prevProps . isOpen  &&  this . props . isOpen )  { 
118-         document . addEventListener ( 'keydown' ,   this . _handleKeyDown ,   false ) ; 
121+         this . onOpen ( ) ; 
119122      }  else  if  ( prevProps . isOpen  &&  ! this . props . isOpen )  { 
120-         document . removeEventListener ( 'keydown' ,   this . _handleKeyDown ,   false ) ; 
123+         this . onClose ( ) ; 
121124      } 
122125      didThemePropsChange ( prevProps ,  this . props ,  this . setState . bind ( this ) ) ; 
123126    } 
@@ -127,6 +130,16 @@ class OptionsBase extends Component<Props, State> {
127130    document . removeEventListener ( 'keydown' ,  this . _handleKeyDown ,  false ) ; 
128131  } 
129132
133+   onOpen  =  ( )  =>  { 
134+     document . addEventListener ( 'keydown' ,  this . _handleKeyDown ,  false ) ; 
135+     const  {  current : input  }  =  this . searchInputRef ; 
136+     if  ( input  &&  input . focus )  input . focus ( ) ; 
137+   } 
138+ 
139+   onClose  =  ( )  =>  { 
140+     document . removeEventListener ( 'keydown' ,  this . _handleKeyDown ,  false ) ; 
141+   } 
142+ 
130143  close  =  ( )  =>  { 
131144    const  {  isOpen,  onClose,  resetOnClose,  toggleOpen }  =  this . props ; 
132145    if  ( isOpen  &&  toggleOpen )  toggleOpen ( ) ; 
@@ -250,7 +263,7 @@ class OptionsBase extends Component<Props, State> {
250263  // ========= PRIVATE HELPERS ========= 
251264
252265  _handleSelectionOnKeyDown  =  ( event : SyntheticKeyboardEvent < > )  =>  { 
253-     const  {   options }   =  this . props ; 
266+     const  options =  this . getFilteredOptions ( ) ; 
254267    if  ( options . length )  { 
255268      const  {  isOpeningUpward }  =  this . props ; 
256269      const  currentIndex  =  this . state . highlightedOptionIndex ; 
@@ -371,6 +384,7 @@ class OptionsBase extends Component<Props, State> {
371384        isSelectedOption = { this . isSelectedOption } 
372385        options = { this . getFilteredOptions ( ) } 
373386        optionsRef = { optionsRef } 
387+         searchInputRef = { this . searchInputRef } 
374388        searchValue = { searchValue } 
375389        setHighlightedOptionIndex = { this . setHighlightedOptionIndex } 
376390        targetRef = { targetRef } 
0 commit comments