@@ -47,24 +47,38 @@ class Dropdown extends React.Component {
4747 }
4848 listChild [ childSelectionIndex ] . focus ( )
4949 }
50+ let searchKey = ''
51+ let timer
5052 const focusOnCharacter = ( value ) => {
53+ searchKey += value
54+ if ( timer ) {
55+ clearTimeout ( timer )
56+ }
57+ timer = setTimeout ( ( ) => { searchKey = '' } , 500 )
5158 const listChild = this . listRef . getElementsByTagName ( 'li' )
5259 if ( listChild . length === 0 ) {
5360 return
5461 }
5562 const length = listChild . length
5663 for ( let i = 0 ; i < length ; i ++ ) {
57- const textContent = listChild [ i ] . textContent
64+ let textContent = listChild [ i ] . textContent
5865 if ( textContent && textContent . length > 0 ) {
59- const firstChar = textContent . charAt ( 0 )
60- if ( firstChar === value || firstChar === value . toLowerCase ( ) ) {
66+ textContent = textContent . toLowerCase ( )
67+ const search = searchKey . toLowerCase ( )
68+ if ( textContent . startsWith ( search ) ) {
6169 listChild [ i ] . focus ( )
6270 return true
6371 }
6472 }
6573 }
6674 return false
6775 }
76+ const onFocus = ( ) => {
77+ this . containerRef . classList . add ( 'focused' )
78+ }
79+ const onBlur = ( ) => {
80+ this . containerRef . classList . remove ( 'focused' )
81+ }
6882 const onKeydown = ( e ) => {
6983 if ( ! handleKeyboardNavigation ) {
7084 return
@@ -100,12 +114,13 @@ class Dropdown extends React.Component {
100114 focusOnNextChild ( )
101115 } else if ( keyCode === 38 ) {
102116 focusOnPreviousChild ( )
103- } else if ( keyCode === 13 ) {
117+ } else if ( keyCode === 13 ) { // enter
104118 const listChild = this . listRef . getElementsByTagName ( 'li' )
105119 if ( listChild . length === 0 ) {
106120 return
107121 }
108122 listChild [ childSelectionIndex ] . click ( )
123+ this . handleKeyboardRef . focus ( )
109124 }
110125 e . preventDefault ( )
111126 } else {
@@ -117,13 +132,15 @@ class Dropdown extends React.Component {
117132 }
118133
119134 const setListRef = ( c ) => this . listRef = c
135+ const setContainerRef = ( c ) => this . containerRef = c
136+ const setHandleKeyboardRef = ( c ) => this . handleKeyboardRef = c
120137
121138 const childrenWithProps = React . Children . map ( children , child =>
122139 React . cloneElement ( child , { onKeyDown : onChildKeydown } )
123140 )
124141 return (
125- < div className = { ddClasses } onClick = { noAutoclose ? ( ) => { } : handleClick } >
126- { handleKeyboardNavigation && ( < a tabIndex = "0" onKeyDown = { onKeydown } className = "handle-keyboard" href = "javascript:;" > </ a > ) }
142+ < div ref = { setContainerRef } className = { ddClasses } onClick = { noAutoclose ? ( ) => { } : handleClick } >
143+ { handleKeyboardNavigation && ( < a ref = { setHandleKeyboardRef } tabIndex = "0" onFocus = { onFocus } onBlur = { onBlur } onKeyDown = { onKeydown } className = "handle-keyboard" href = "javascript:;" > </ a > ) }
127144 {
128145 childrenWithProps . map ( ( child , index ) => {
129146 if ( child . props . className . indexOf ( 'dropdown-menu-header' ) > - 1 )
0 commit comments