@@ -7,8 +7,8 @@ import ee from 'browser/main/lib/eventEmitter'
7
7
import NewNoteButton from 'browser/main/NewNoteButton'
8
8
import i18n from 'browser/lib/i18n'
9
9
import debounce from 'lodash/debounce'
10
+ import CInput from 'react-composition-input'
10
11
import { push } from 'connected-react-router'
11
- import queryString from 'query-string'
12
12
13
13
class TopBar extends React . Component {
14
14
constructor ( props ) {
@@ -17,21 +17,29 @@ class TopBar extends React.Component {
17
17
this . state = {
18
18
search : '' ,
19
19
searchOptions : [ ] ,
20
- isSearching : false ,
21
- isAlphabet : false ,
22
- isIME : false ,
23
- isConfirmTranslation : false
20
+ isSearching : false
24
21
}
25
22
23
+ const { dispatch } = this . props
24
+
26
25
this . focusSearchHandler = ( ) => {
27
26
this . handleOnSearchFocus ( )
28
27
}
29
28
30
29
this . codeInitHandler = this . handleCodeInit . bind ( this )
31
- this . updateKeyword = this . updateKeyword . bind ( this )
30
+ this . handleKeyDown = this . handleKeyDown . bind ( this )
31
+ this . handleSearchFocus = this . handleSearchFocus . bind ( this )
32
+ this . handleSearchBlur = this . handleSearchBlur . bind ( this )
33
+ this . handleSearchChange = this . handleSearchChange . bind ( this )
32
34
this . handleSearchClearButton = this . handleSearchClearButton . bind ( this )
33
35
34
- this . updateKeyword = debounce ( this . updateKeyword , 1000 / 60 , {
36
+ this . debouncedUpdateKeyword = debounce ( ( keyword ) => {
37
+ dispatch ( push ( `/searched/${ encodeURIComponent ( keyword ) } ` ) )
38
+ this . setState ( {
39
+ search : keyword
40
+ } )
41
+ ee . emit ( 'top:search' , keyword )
42
+ } , 1000 / 60 , {
35
43
maxWait : 1000 / 8
36
44
} )
37
45
}
@@ -66,11 +74,10 @@ class TopBar extends React.Component {
66
74
}
67
75
68
76
handleKeyDown ( e ) {
69
- // reset states
70
- this . setState ( {
71
- isAlphabet : false ,
72
- isIME : false
73
- } )
77
+ // Re-apply search field on ENTER key
78
+ if ( e . keyCode === 13 ) {
79
+ this . debouncedUpdateKeyword ( e . target . value )
80
+ }
74
81
75
82
// Clear search on ESC
76
83
if ( e . keyCode === 27 ) {
@@ -88,59 +95,19 @@ class TopBar extends React.Component {
88
95
ee . emit ( 'list:prior' )
89
96
e . preventDefault ( )
90
97
}
91
-
92
- // When the key is an alphabet, del, enter or ctr
93
- if ( e . keyCode <= 90 || e . keyCode >= 186 && e . keyCode <= 222 ) {
94
- this . setState ( {
95
- isAlphabet : true
96
- } )
97
- // When the key is an IME input (Japanese, Chinese)
98
- } else if ( e . keyCode === 229 ) {
99
- this . setState ( {
100
- isIME : true
101
- } )
102
- }
103
- }
104
-
105
- handleKeyUp ( e ) {
106
- // reset states
107
- this . setState ( {
108
- isConfirmTranslation : false
109
- } )
110
-
111
- // When the key is translation confirmation (Enter, Space)
112
- if ( this . state . isIME && ( e . keyCode === 32 || e . keyCode === 13 ) ) {
113
- this . setState ( {
114
- isConfirmTranslation : true
115
- } )
116
- const keyword = this . refs . searchInput . value
117
- this . updateKeyword ( keyword )
118
- }
119
98
}
120
99
121
100
handleSearchChange ( e ) {
122
- if ( this . state . isAlphabet || this . state . isConfirmTranslation ) {
123
- const keyword = this . refs . searchInput . value
124
- this . updateKeyword ( keyword )
125
- } else {
126
- e . preventDefault ( )
127
- }
128
- }
129
-
130
- updateKeyword ( keyword ) {
131
- const { dispatch } = this . props
132
- dispatch ( push ( `/searched/${ encodeURIComponent ( keyword ) } ` ) )
133
- this . setState ( {
134
- search : keyword
135
- } )
136
- ee . emit ( 'top:search' , keyword )
101
+ const keyword = e . target . value
102
+ this . debouncedUpdateKeyword ( keyword )
137
103
}
138
104
139
105
handleSearchFocus ( e ) {
140
106
this . setState ( {
141
107
isSearching : true
142
108
} )
143
109
}
110
+
144
111
handleSearchBlur ( e ) {
145
112
e . stopPropagation ( )
146
113
@@ -170,7 +137,7 @@ class TopBar extends React.Component {
170
137
}
171
138
172
139
handleCodeInit ( ) {
173
- ee . emit ( 'top:search' , this . refs . searchInput . value )
140
+ ee . emit ( 'top:search' , this . refs . searchInput . value || '' )
174
141
}
175
142
176
143
render ( ) {
@@ -183,24 +150,23 @@ class TopBar extends React.Component {
183
150
< div styleName = 'control' >
184
151
< div styleName = 'control-search' >
185
152
< div styleName = 'control-search-input'
186
- onFocus = { ( e ) => this . handleSearchFocus ( e ) }
187
- onBlur = { ( e ) => this . handleSearchBlur ( e ) }
153
+ onFocus = { this . handleSearchFocus }
154
+ onBlur = { this . handleSearchBlur }
188
155
tabIndex = '-1'
189
156
ref = 'search'
190
157
>
191
- < input
158
+ < CInput
192
159
ref = 'searchInput'
193
160
value = { this . state . search }
194
- onChange = { ( e ) => this . handleSearchChange ( e ) }
195
- onKeyDown = { ( e ) => this . handleKeyDown ( e ) }
196
- onKeyUp = { ( e ) => this . handleKeyUp ( e ) }
161
+ onInputChange = { this . handleSearchChange }
162
+ onKeyDown = { this . handleKeyDown }
197
163
placeholder = { i18n . __ ( 'Search' ) }
198
164
type = 'text'
199
165
className = 'searchInput'
200
166
/>
201
167
{ this . state . search !== '' &&
202
168
< button styleName = 'control-search-input-clear'
203
- onClick = { ( e ) => this . handleSearchClearButton ( e ) }
169
+ onClick = { this . handleSearchClearButton }
204
170
>
205
171
< i className = 'fa fa-fw fa-times' />
206
172
< span styleName = 'control-search-input-clear-tooltip' > { i18n . __ ( 'Clear Search' ) } </ span >
0 commit comments