@@ -6,6 +6,13 @@ import pure from 'recompose/pure';
6
6
import { connect } from 'react-redux' ;
7
7
import { withApollo } from 'react-apollo' ;
8
8
import { withRouter } from 'react-router' ;
9
+ import {
10
+ ESC ,
11
+ BACKSPACE ,
12
+ ENTER ,
13
+ ARROW_DOWN ,
14
+ ARROW_UP ,
15
+ } from 'src/helpers/keycodes' ;
9
16
import { Spinner } from '../../../../components/globals' ;
10
17
import { throttle } from '../../../../helpers/utils' ;
11
18
import { searchUsersQuery } from 'shared/graphql/queries/search/searchUsers' ;
@@ -104,8 +111,7 @@ class Search extends React.Component<Props, State> {
104
111
focusedSearchResult
105
112
) ;
106
113
107
- // if person presses esc, clear all results, stop loading
108
- if ( e . keyCode === 27 ) {
114
+ if ( e . keyCode === ESC ) {
109
115
this . setState ( {
110
116
searchResults : [ ] ,
111
117
searchIsLoading : false ,
@@ -114,15 +120,13 @@ class Search extends React.Component<Props, State> {
114
120
return ;
115
121
}
116
122
117
- // backspace
118
- if ( e . keyCode === 8 ) {
123
+ if ( e . keyCode === BACKSPACE ) {
119
124
if ( searchString . length > 0 ) return ;
120
125
// $FlowFixMe
121
126
return input && input . focus ( ) ;
122
127
}
123
128
124
- //escape
125
- if ( e . keyCode === 27 ) {
129
+ if ( e . keyCode === ESC ) {
126
130
this . setState ( {
127
131
searchResults : [ ] ,
128
132
searchIsLoading : false ,
@@ -132,8 +136,7 @@ class Search extends React.Component<Props, State> {
132
136
return input && input . focus ( ) ;
133
137
}
134
138
135
- // down
136
- if ( e . keyCode === 40 ) {
139
+ if ( e . keyCode === ARROW_DOWN ) {
137
140
if ( indexOfFocusedSearchResult === searchResults . length - 1 ) return ;
138
141
if ( searchResults . length === 1 ) return ;
139
142
@@ -145,8 +148,7 @@ class Search extends React.Component<Props, State> {
145
148
return ;
146
149
}
147
150
148
- // up
149
- if ( e . keyCode === 38 ) {
151
+ if ( e . keyCode === ARROW_UP ) {
150
152
// 1
151
153
if ( indexOfFocusedSearchResult === 0 ) return ;
152
154
if ( searchResults . length === 1 ) return ;
@@ -159,8 +161,7 @@ class Search extends React.Component<Props, State> {
159
161
return ;
160
162
}
161
163
162
- // enter
163
- if ( e . keyCode === 13 ) {
164
+ if ( e . keyCode === ENTER ) {
164
165
if ( ! searchResults [ indexOfFocusedSearchResult ] ) return ;
165
166
return this . goToUser ( searchResults [ indexOfFocusedSearchResult ] . username ) ;
166
167
}
@@ -269,4 +270,9 @@ class Search extends React.Component<Props, State> {
269
270
}
270
271
}
271
272
272
- export default compose ( withApollo , withRouter , connect ( ) , pure ) ( Search ) ;
273
+ export default compose (
274
+ withApollo ,
275
+ withRouter ,
276
+ connect ( ) ,
277
+ pure
278
+ ) ( Search ) ;
0 commit comments