@@ -58,7 +58,7 @@ class SearchBar extends Component {
5858
5959 handleSuggestionsUpdate ( requestNo , data ) {
6060 if ( requestNo === this . state . maxRequestNo ) {
61- this . setState ( { loading : false , suggestions : data } )
61+ this . setState ( { loading : false , suggestions : data , selectedSuggestionIdx : null } )
6262 }
6363 }
6464
@@ -71,7 +71,8 @@ class SearchBar extends Component {
7171 searchValue : this . refs . searchValue . value ,
7272 requestNo : rc ,
7373 maxRequestNo : rc ,
74- loading : true
74+ loading : true ,
75+ searchState : 'focused'
7576 }
7677 } ,
7778 function ( ) {
@@ -93,11 +94,45 @@ class SearchBar extends Component {
9394
9495 onKeyUp ( evt ) {
9596 const eventKey = evt . keyCode
97+ evt . stopPropagation ( )
98+ evt . preventDefault ( )
9699 // if return is pressed
97100 if ( eventKey === 13 ) {
98101 this . setState ( { searchState : 'filled' } , function ( ) {
99102 this . search ( )
100103 } )
104+ } else if ( eventKey === 39 ) { // right arrow key is pressed
105+ const suggestion = this . state . suggestions . length > 0 ? this . state . suggestions [ 0 ] : null
106+ if ( suggestion ) {
107+ this . refs . searchValue . value = suggestion
108+ // trigger the change event handler
109+ this . onChange ( )
110+ }
111+ } else if ( eventKey === 38 ) { // up arrow key
112+ const currSelectedIdx = this . state . selectedSuggestionIdx
113+ if ( currSelectedIdx ) { // index is none of (undefined, null, 0)
114+ const suggestionIdx = currSelectedIdx - 1
115+ const suggestion = this . state . suggestions [ suggestionIdx ]
116+ this . refs . searchValue . value = suggestion
117+ this . setState ( {
118+ selectedSuggestionIdx : suggestionIdx ,
119+ searchValue : suggestion
120+ } )
121+ }
122+ } else if ( eventKey === 40 ) { // down arrow key
123+ const currSelectedIdx = this . state . selectedSuggestionIdx
124+ // index is none of (undefined, null, 0)
125+ if ( typeof currSelectedIdx === 'undefined'
126+ || currSelectedIdx === null
127+ || this . state . suggestions . length > currSelectedIdx + 1 ) {
128+ const suggestionIdx = typeof currSelectedIdx === 'number' ? currSelectedIdx + 1 : 0
129+ const suggestion = this . state . suggestions [ suggestionIdx ]
130+ this . refs . searchValue . value = suggestion
131+ this . setState ( {
132+ selectedSuggestionIdx : suggestionIdx ,
133+ searchValue : suggestion
134+ } )
135+ }
101136 }
102137 }
103138
@@ -140,7 +175,7 @@ class SearchBar extends Component {
140175
141176 const results = this . state . loading === true
142177 ? < div className = "loading-suggestions" > < Loader /> </ div >
143- : < SearchSuggestions recentSearch = { recentList } popularSearch = { popularList } onSuggestionSelect = { this . handleSuggestionSelect } />
178+ : < SearchSuggestions recentSearch = { recentList } searchTerm = { this . state . searchValue } popularSearch = { popularList } onSuggestionSelect = { this . handleSuggestionSelect } />
144179 return (
145180 < div className = { sbClasses } >
146181 < input className = "search-bar__text" onFocus = { this . onFocus } onChange = { this . onChange } onKeyUp = { this . onKeyUp } ref = "searchValue" value = { this . state . searchValue } />
0 commit comments