@@ -11,9 +11,7 @@ import { TextInput } from 'components/core/text-input/index';
1111/* eslint react/sort-comp: 0 */
1212
1313function matchStateToTerm ( item , value ) {
14- return (
15- this . getItemValue ( item ) . toLowerCase ( ) . indexOf ( value . toLowerCase ( ) ) !== - 1
16- ) ;
14+ return this . getItemValue ( item ) . toLowerCase ( ) . indexOf ( value . toLowerCase ( ) ) !== - 1 ;
1715}
1816
1917type Props = {
@@ -35,17 +33,16 @@ type Props = {
3533/* eslint-disable no-unused-vars */
3634
3735type State = {
38- value : string | number ,
36+ value : string | number ,
3937 isOpen : boolean ,
4038 menuDirection : string ,
41- highlightedIndex : null | number ,
39+ highlightedIndex : null | number ,
4240 changingStarted ?: boolean ,
4341} ;
4442
4543/* eslint-enable no-unused-vars */
4644
4745class Autocomplete extends Component {
48-
4946 props : Props ;
5047
5148 state : State = {
@@ -75,12 +72,7 @@ class Autocomplete extends Component {
7572 const value = this . getItemValue ( item ) ;
7673 const key = this . getItemKey ( item ) ;
7774
78- return (
79- < div
80- styleName = { isHighlighted ? 'item-highlighted' : 'item' }
81- key = { key }
82- > { value } </ div >
83- ) ;
75+ return < div styleName = { isHighlighted ? 'item-highlighted' : 'item' } key = { key } > { value } </ div > ;
8476 } ,
8577 } ;
8678
@@ -115,11 +107,14 @@ class Autocomplete extends Component {
115107 }
116108
117109 finishFiltering ( ) {
118- this . setState ( {
119- changingStarted : false ,
120- } , ( ) => {
121- this . maybeSelectItem ( ) ;
122- } ) ;
110+ this . setState (
111+ {
112+ changingStarted : false ,
113+ } ,
114+ ( ) => {
115+ this . maybeSelectItem ( ) ;
116+ }
117+ ) ;
123118 }
124119
125120 @autobind
@@ -130,7 +125,7 @@ class Autocomplete extends Component {
130125 maybeSelectItem ( ) {
131126 const props = this . props ;
132127
133- const exactlyItem = _ . find ( props . items , ( item ) => {
128+ const exactlyItem = _ . find ( props . items , item => {
134129 return props . compareValues ( props . getItemValue ( item ) , this . state . value ) ;
135130 } ) ;
136131
@@ -176,12 +171,15 @@ class Autocomplete extends Component {
176171 event . stopPropagation ( ) ;
177172
178173 this . _performAutoCompleteOnKeyUp = true ;
179- this . setState ( {
180- value : event . target . value ,
181- changingStarted : true ,
182- } , ( ) => {
183- this . props . onChange ( this . state . value ) ;
184- } ) ;
174+ this . setState (
175+ {
176+ value : event . target . value ,
177+ changingStarted : true ,
178+ } ,
179+ ( ) => {
180+ this . props . onChange ( this . state . value ) ;
181+ }
182+ ) ;
185183 }
186184
187185 @autobind
@@ -201,10 +199,9 @@ class Autocomplete extends Component {
201199 ArrowDown ( event ) {
202200 event . preventDefault ( ) ;
203201 const { highlightedIndex } = this . state ;
204- const index = (
205- highlightedIndex === null ||
206- highlightedIndex === this . getFilteredItems ( ) . length - 1
207- ) ? 0 : highlightedIndex + 1 ;
202+ const index = highlightedIndex === null || highlightedIndex === this . getFilteredItems ( ) . length - 1
203+ ? 0
204+ : highlightedIndex + 1 ;
208205 this . _performAutoCompleteOnKeyUp = true ;
209206 this . setState ( {
210207 highlightedIndex : index ,
@@ -214,10 +211,9 @@ class Autocomplete extends Component {
214211 ArrowUp ( event ) {
215212 event . preventDefault ( ) ;
216213 const { highlightedIndex } = this . state ;
217- const index = (
218- highlightedIndex === 0 ||
219- highlightedIndex === null
220- ) ? this . getFilteredItems ( ) . length - 1 : highlightedIndex - 1 ;
214+ const index = highlightedIndex === 0 || highlightedIndex === null
215+ ? this . getFilteredItems ( ) . length - 1
216+ : highlightedIndex - 1 ;
221217 this . _performAutoCompleteOnKeyUp = true ;
222218 this . setState ( {
223219 highlightedIndex : index ,
@@ -231,34 +227,37 @@ class Autocomplete extends Component {
231227 // already selected this, do nothing
232228 } else if ( this . state . highlightedIndex == null ) {
233229 // hit enter after focus but before typing anything so no autocomplete attempt yet
234- this . setState ( {
235- isOpen : false ,
236- } , ( ) => {
237- this . getInput ( ) . select ( ) ;
238- } ) ;
230+ this . setState (
231+ {
232+ isOpen : false ,
233+ } ,
234+ ( ) => {
235+ this . getInput ( ) . select ( ) ;
236+ }
237+ ) ;
239238 } else {
240239 const item = this . getFilteredItems ( ) [ this . state . highlightedIndex ] ;
241240
242- this . setState ( {
243- value : this . props . getItemValue ( item ) ,
244- isOpen : false ,
245- highlightedIndex : null ,
246- } , ( ) => {
247- // this.getInput().focus() // TODO: file issue
248- const input = this . getInput ( ) ;
249- if ( input . setSelectionRange ) {
250- try {
251- input . setSelectionRange (
252- this . state . value . length ,
253- this . state . value . length
254- ) ;
255- } catch ( ex ) {
256- // ignore
241+ this . setState (
242+ {
243+ value : this . props . getItemValue ( item ) ,
244+ isOpen : false ,
245+ highlightedIndex : null ,
246+ } ,
247+ ( ) => {
248+ // this.getInput().focus() // TODO: file issue
249+ const input = this . getInput ( ) ;
250+ if ( input . setSelectionRange ) {
251+ try {
252+ input . setSelectionRange ( this . state . value . length , this . state . value . length ) ;
253+ } catch ( ex ) {
254+ // ignore
255+ }
257256 }
258- }
259257
260- this . props . onSelect ( item , this . state . value ) ;
261- } ) ;
258+ this . props . onSelect ( item , this . state . value ) ;
259+ }
260+ ) ;
262261 }
263262 } ,
264263 Escape ( ) {
@@ -270,39 +269,32 @@ class Autocomplete extends Component {
270269 } ;
271270 }
272271
273- getFilteredItems ( ) {
272+ getFilteredItems ( ) {
274273 let items = this . props . items ;
275274
276275 if ( this . props . shouldItemRender && this . state . changingStarted ) {
277- items = items . filter ( item => (
278- this . props . shouldItemRender ( item , this . state . value )
279- ) ) ;
276+ items = items . filter ( item => this . props . shouldItemRender ( item , this . state . value ) ) ;
280277 }
281278
282279 if ( this . props . sortItems ) {
283- items . sort ( ( a , b ) => (
284- this . sortItems ( a , b )
285- ) ) ;
280+ items . sort ( ( a , b ) => this . sortItems ( a , b ) ) ;
286281 }
287282
288283 return items ;
289284 }
290285
291286 itemValueMatches ( value ) {
292- return ( value . toString ( ) . toLowerCase ( ) . indexOf (
293- this . state . value . toString ( ) . toLowerCase ( )
294- ) === 0 ) ;
287+ return value . toString ( ) . toLowerCase ( ) . indexOf ( this . state . value . toString ( ) . toLowerCase ( ) ) === 0 ;
295288 }
296289
297- maybeAutoCompleteText ( ) {
290+ maybeAutoCompleteText ( ) {
298291 if ( this . state . value === '' ) return ;
299292
300293 const { highlightedIndex } = this . state ;
301294 const items = this . getFilteredItems ( ) ;
302295 if ( items . length === 0 ) return ;
303296
304- const matchedItem = highlightedIndex !== null ?
305- items [ highlightedIndex ] : items [ 0 ] ;
297+ const matchedItem = highlightedIndex !== null ? items [ highlightedIndex ] : items [ 0 ] ;
306298 const itemValue = this . props . getItemValue ( matchedItem ) ;
307299
308300 if ( this . itemValueMatches ( itemValue ) ) {
@@ -312,7 +304,7 @@ class Autocomplete extends Component {
312304 }
313305 }
314306
315- setMenuOrientation ( ) {
307+ setMenuOrientation ( ) {
316308 const menuNode = this . refs . menu ;
317309 const inputNode = this . getInput ( ) ;
318310 const viewportHeight = window . innerHeight ;
@@ -337,33 +329,32 @@ class Autocomplete extends Component {
337329 } ) ;
338330 }
339331
340- highlightItemFromMouse ( index ) {
332+ highlightItemFromMouse ( index ) {
341333 this . setState ( { highlightedIndex : index } ) ;
342334 }
343335
344- selectItemFromMouse ( item ) {
345- this . setState ( {
346- value : this . props . getItemValue ( item ) ,
347- isOpen : false ,
348- highlightedIndex : null ,
349- } , ( ) => {
350- this . props . onSelect ( item , this . state . value ) ;
351- this . getInput ( ) . focus ( ) ;
352- this . setIgnoreBlur ( false ) ;
353- } ) ;
336+ selectItemFromMouse ( item ) {
337+ this . setState (
338+ {
339+ value : this . props . getItemValue ( item ) ,
340+ isOpen : false ,
341+ highlightedIndex : null ,
342+ } ,
343+ ( ) => {
344+ this . props . onSelect ( item , this . state . value ) ;
345+ this . getInput ( ) . focus ( ) ;
346+ this . setIgnoreBlur ( false ) ;
347+ }
348+ ) ;
354349 }
355350
356- setIgnoreBlur ( ignore ) {
351+ setIgnoreBlur ( ignore ) {
357352 this . _ignoreBlur = ignore ;
358353 }
359354
360355 get menu ( ) {
361356 const items = this . getFilteredItems ( ) . map ( ( item , index ) => {
362- const element = this . props . renderItem (
363- item ,
364- this . state . highlightedIndex === index ,
365- { cursor : 'default' }
366- ) ;
357+ const element = this . props . renderItem ( item , this . state . highlightedIndex === index , { cursor : 'default' } ) ;
367358 return React . cloneElement ( element , {
368359 onMouseDown : ( ) => this . setIgnoreBlur ( true ) ,
369360 onMouseEnter : ( ) => this . highlightItemFromMouse ( index ) ,
@@ -377,7 +368,7 @@ class Autocomplete extends Component {
377368 }
378369
379370 @autobind
380- handleInputBlur ( ) {
371+ handleInputBlur ( ) {
381372 if ( this . _ignoreBlur ) return ;
382373
383374 this . setState ( {
@@ -387,24 +378,24 @@ class Autocomplete extends Component {
387378 }
388379
389380 @autobind
390- handleInputFocus ( ) {
381+ handleInputFocus ( ) {
391382 if ( this . _ignoreBlur ) return ;
392383
393384 this . setState ( { isOpen : true } ) ;
394385 }
395386
396387 @autobind
397- handleInputClick ( ) {
388+ handleInputClick ( ) {
398389 if ( this . state . isOpen === false ) {
399390 this . setState ( { isOpen : true } ) ;
400391 }
401392 }
402393
403- render ( ) {
394+ render ( ) {
404395 const { inputProps } = this . props ;
405396
406397 return (
407- < div ref = "container" styleName = "autocomplete" >
398+ < div ref = "container" styleName = "autocomplete" >
408399 < TextInput
409400 type = "text"
410401 { ...inputProps }
0 commit comments