@@ -15,9 +15,9 @@ const IMPERATIVE_API = [
15
15
'setRangeText' ,
16
16
]
17
17
18
- let Autocomplete = React . createClass ( {
18
+ class Autocomplete extends React . Component {
19
19
20
- propTypes : {
20
+ static propTypes = {
21
21
/**
22
22
* The items to display in the dropdown menu
23
23
*/
@@ -120,10 +120,9 @@ let Autocomplete = React.createClass({
120
120
*/
121
121
open : PropTypes . bool ,
122
122
debug : PropTypes . bool ,
123
- } ,
123
+ }
124
124
125
- getDefaultProps ( ) {
126
- return {
125
+ static defaultProps = {
127
126
value : '' ,
128
127
wrapperProps : { } ,
129
128
wrapperStyle : {
@@ -148,22 +147,29 @@ let Autocomplete = React.createClass({
148
147
autoHighlight : true ,
149
148
onMenuVisibilityChange ( ) { } ,
150
149
}
151
- } ,
152
150
153
- getInitialState ( ) {
154
- return {
151
+ constructor ( props ) {
152
+ super ( props )
153
+ this . state = {
155
154
isOpen : false ,
156
155
highlightedIndex : null ,
157
156
}
158
- } ,
157
+ this . exposeAPI = this . exposeAPI . bind ( this )
158
+ this . handleInputFocus = this . handleInputFocus . bind ( this )
159
+ this . handleInputBlur = this . handleInputBlur . bind ( this )
160
+ this . handleChange = this . handleChange . bind ( this )
161
+ this . handleKeyDown = this . handleKeyDown . bind ( this )
162
+ this . handleKeyUp = this . handleKeyUp . bind ( this )
163
+ this . handleInputClick = this . handleInputClick . bind ( this )
164
+ }
159
165
160
166
componentWillMount ( ) {
161
167
// this.refs is frozen, so we need to assign a new object to it
162
168
this . refs = { }
163
169
this . _ignoreBlur = false
164
170
this . _performAutoCompleteOnUpdate = false
165
171
this . _performAutoCompleteOnKeyUp = false
166
- } ,
172
+ }
167
173
168
174
componentWillReceiveProps ( nextProps ) {
169
175
this . _performAutoCompleteOnUpdate = true
@@ -176,13 +182,13 @@ let Autocomplete = React.createClass({
176
182
this . state . highlightedIndex >= nextProps . items . length ) {
177
183
this . setState ( { highlightedIndex : null } )
178
184
}
179
- } ,
185
+ }
180
186
181
187
componentDidMount ( ) {
182
188
if ( this . isOpen ( ) ) {
183
189
this . setMenuPositions ( )
184
190
}
185
- } ,
191
+ }
186
192
187
193
componentDidUpdate ( prevProps , prevState ) {
188
194
if ( ( this . state . isOpen && ! prevState . isOpen ) || ( 'open' in this . props && this . props . open && ! prevProps . open ) )
@@ -201,12 +207,12 @@ let Autocomplete = React.createClass({
201
207
if ( this . _ignoreBlur ) {
202
208
this . refs . input . focus ( )
203
209
}
204
- } ,
210
+ }
205
211
206
212
exposeAPI ( el ) {
207
213
this . refs . input = el
208
214
IMPERATIVE_API . forEach ( ev => this [ ev ] = ( el && el [ ev ] && el [ ev ] . bind ( el ) ) )
209
- } ,
215
+ }
210
216
211
217
maybeScrollItemIntoView ( ) {
212
218
if ( this . isOpen ( ) && this . state . highlightedIndex !== null ) {
@@ -220,32 +226,32 @@ let Autocomplete = React.createClass({
220
226
)
221
227
}
222
228
}
223
- } ,
229
+ }
224
230
225
231
handleKeyDown ( event ) {
226
- if ( this . keyDownHandlers [ event . key ] )
227
- this . keyDownHandlers [ event . key ] . call ( this , event )
232
+ if ( Autocomplete . keyDownHandlers [ event . key ] )
233
+ Autocomplete . keyDownHandlers [ event . key ] . call ( this , event )
228
234
else if ( ! this . isOpen ( ) ) {
229
235
this . setState ( {
230
236
isOpen : true
231
237
} )
232
238
}
233
- } ,
239
+ }
234
240
235
241
handleChange ( event ) {
236
242
this . _performAutoCompleteOnKeyUp = true
237
243
this . setState ( { highlightedIndex : null } )
238
244
this . props . onChange ( event , event . target . value )
239
- } ,
245
+ }
240
246
241
247
handleKeyUp ( ) {
242
248
if ( this . _performAutoCompleteOnKeyUp ) {
243
249
this . _performAutoCompleteOnKeyUp = false
244
250
this . maybeAutoCompleteText ( )
245
251
}
246
- } ,
252
+ }
247
253
248
- keyDownHandlers : {
254
+ static keyDownHandlers = {
249
255
ArrowDown ( event ) {
250
256
event . preventDefault ( )
251
257
const itemsLength = this . getFilteredItems ( ) . length
@@ -323,7 +329,7 @@ let Autocomplete = React.createClass({
323
329
// In case the user is currently hovering over the menu
324
330
this . setIgnoreBlur ( false )
325
331
} ,
326
- } ,
332
+ }
327
333
328
334
getFilteredItems ( ) {
329
335
let items = this . props . items
@@ -341,7 +347,7 @@ let Autocomplete = React.createClass({
341
347
}
342
348
343
349
return items
344
- } ,
350
+ }
345
351
346
352
maybeAutoCompleteText ( ) {
347
353
if ( ! this . props . autoHighlight || this . props . value === '' )
@@ -358,7 +364,7 @@ let Autocomplete = React.createClass({
358
364
) === 0 )
359
365
if ( itemValueDoesMatch && highlightedIndex === null )
360
366
this . setState ( { highlightedIndex : 0 } )
361
- } ,
367
+ }
362
368
363
369
setMenuPositions ( ) {
364
370
const node = this . refs . input
@@ -372,11 +378,11 @@ let Autocomplete = React.createClass({
372
378
menuLeft : rect . left + marginLeft ,
373
379
menuWidth : rect . width + marginLeft + marginRight
374
380
} )
375
- } ,
381
+ }
376
382
377
383
highlightItemFromMouse ( index ) {
378
384
this . setState ( { highlightedIndex : index } )
379
- } ,
385
+ }
380
386
381
387
selectItemFromMouse ( item ) {
382
388
const value = this . props . getItemValue ( item )
@@ -389,11 +395,11 @@ let Autocomplete = React.createClass({
389
395
this . setIgnoreBlur ( false )
390
396
this . props . onSelect ( value , item )
391
397
} )
392
- } ,
398
+ }
393
399
394
400
setIgnoreBlur ( ignore ) {
395
401
this . _ignoreBlur = ignore
396
- } ,
402
+ }
397
403
398
404
renderMenu ( ) {
399
405
const items = this . getFilteredItems ( ) . map ( ( item , index ) => {
@@ -420,7 +426,7 @@ let Autocomplete = React.createClass({
420
426
onMouseEnter : ( ) => this . setIgnoreBlur ( true ) ,
421
427
onMouseLeave : ( ) => this . setIgnoreBlur ( false ) ,
422
428
} )
423
- } ,
429
+ }
424
430
425
431
handleInputBlur ( event ) {
426
432
if ( this . _ignoreBlur ) {
@@ -434,7 +440,7 @@ let Autocomplete = React.createClass({
434
440
if ( onBlur ) {
435
441
onBlur ( event )
436
442
}
437
- } ,
443
+ }
438
444
439
445
handleInputFocus ( event ) {
440
446
if ( this . _ignoreBlur ) {
@@ -445,28 +451,28 @@ let Autocomplete = React.createClass({
445
451
if ( onFocus ) {
446
452
onFocus ( event )
447
453
}
448
- } ,
454
+ }
449
455
450
456
isInputFocused ( ) {
451
457
const el = this . refs . input
452
458
return el . ownerDocument && ( el === el . ownerDocument . activeElement )
453
- } ,
459
+ }
454
460
455
461
handleInputClick ( ) {
456
462
// Input will not be focused if it's disabled
457
463
if ( this . isInputFocused ( ) && ! this . isOpen ( ) )
458
464
this . setState ( { isOpen : true } )
459
- } ,
465
+ }
460
466
461
467
composeEventHandlers ( internal , external ) {
462
468
return external
463
469
? e => { internal ( e ) ; external ( e ) }
464
470
: internal
465
- } ,
471
+ }
466
472
467
473
isOpen ( ) {
468
474
return 'open' in this . props ? this . props . open : this . state . isOpen
469
- } ,
475
+ }
470
476
471
477
render ( ) {
472
478
if ( this . props . debug ) { // you don't like it, you love it
@@ -504,7 +510,7 @@ let Autocomplete = React.createClass({
504
510
</ div >
505
511
)
506
512
}
507
- } )
513
+ }
508
514
509
515
module . exports = Autocomplete
510
516
0 commit comments