3
3
var _extends = Object . assign || function ( target ) { for ( var i = 1 ; i < arguments . length ; i ++ ) { var source = arguments [ i ] ; for ( var key in source ) { if ( Object . prototype . hasOwnProperty . call ( source , key ) ) { target [ key ] = source [ key ] ; } } } return target ; } ;
4
4
5
5
var React = require ( 'react' ) ;
6
+ var lodash = require ( 'lodash' ) ;
6
7
var scrollIntoView = require ( 'dom-scroll-into-view' ) ;
7
8
8
9
var _debugStates = [ ] ;
@@ -17,12 +18,14 @@ var Autocomplete = React.createClass({
17
18
shouldItemRender : React . PropTypes . func ,
18
19
renderItem : React . PropTypes . func . isRequired ,
19
20
menuStyle : React . PropTypes . object ,
20
- inputProps : React . PropTypes . object
21
+ inputProps : React . PropTypes . object ,
22
+ labelText : React . PropTypes . string
21
23
} ,
22
24
23
25
getDefaultProps : function getDefaultProps ( ) {
24
26
return {
25
27
inputProps : { } ,
28
+ labelText : '' ,
26
29
onChange : function onChange ( ) { } ,
27
30
onSelect : function onSelect ( value , item ) { } ,
28
31
renderMenu : function renderMenu ( items , value , style ) {
@@ -53,6 +56,7 @@ var Autocomplete = React.createClass({
53
56
} ,
54
57
55
58
componentWillMount : function componentWillMount ( ) {
59
+ this . id = lodash . uniqueId ( 'autocomplete-' ) ;
56
60
this . _ignoreBlur = false ;
57
61
this . _performAutoCompleteOnUpdate = false ;
58
62
this . _performAutoCompleteOnKeyUp = false ;
@@ -109,7 +113,7 @@ var Autocomplete = React.createClass({
109
113
} ,
110
114
111
115
keyDownHandlers : {
112
- ArrowDown : function ArrowDown ( ) {
116
+ ArrowDown : function ArrowDown ( event ) {
113
117
event . preventDefault ( ) ;
114
118
var highlightedIndex = this . state . highlightedIndex ;
115
119
@@ -137,16 +141,17 @@ var Autocomplete = React.createClass({
137
141
var _this2 = this ;
138
142
139
143
if ( this . state . isOpen === false ) {
140
- // already selected this, do nothing
144
+ // menu is closed so there is no selection to accept -> do nothing
141
145
return ;
142
146
} else if ( this . state . highlightedIndex == null ) {
143
- // hit enter after focus but before typing anything so no autocomplete attempt yet
147
+ // input has focus but no menu item is selected + enter is hit -> close the menu, highlight whatever's in input
144
148
this . setState ( {
145
149
isOpen : false
146
150
} , function ( ) {
147
151
_this2 . refs . input . select ( ) ;
148
152
} ) ;
149
153
} else {
154
+ // text entered + menu item has been highlighted + enter is hit -> update value to that of selected menu item, close the menu
150
155
var item = this . getFilteredItems ( ) [ this . state . highlightedIndex ] ;
151
156
this . setState ( {
152
157
value : this . props . getItemValue ( item ) ,
@@ -212,10 +217,10 @@ var Autocomplete = React.createClass({
212
217
setMenuPositions : function setMenuPositions ( ) {
213
218
var node = this . refs . input ;
214
219
var rect = node . getBoundingClientRect ( ) ;
215
- var computedStyle = getComputedStyle ( node ) ;
216
- var marginBottom = parseInt ( computedStyle . marginBottom , 10 ) ;
217
- var marginLeft = parseInt ( computedStyle . marginLeft , 10 ) ;
218
- var marginRight = parseInt ( computedStyle . marginRight , 10 ) ;
220
+ var computedStyle = global . window . getComputedStyle ( node ) ;
221
+ var marginBottom = parseInt ( computedStyle . marginBottom , 10 ) || 0 ;
222
+ var marginLeft = parseInt ( computedStyle . marginLeft , 10 ) || 0 ;
223
+ var marginRight = parseInt ( computedStyle . marginRight , 10 ) || 0 ;
219
224
this . setState ( {
220
225
menuTop : rect . bottom + marginBottom ,
221
226
menuLeft : rect . left + marginLeft ,
@@ -302,6 +307,11 @@ var Autocomplete = React.createClass({
302
307
return React . createElement (
303
308
'div' ,
304
309
{ style : { display : 'inline-block' } } ,
310
+ React . createElement (
311
+ 'label' ,
312
+ { htmlFor : this . id , ref : 'label' } ,
313
+ this . props . labelText
314
+ ) ,
305
315
React . createElement ( 'input' , _extends ( { } , this . props . inputProps , {
306
316
role : 'combobox' ,
307
317
'aria-autocomplete' : 'both' ,
@@ -318,7 +328,8 @@ var Autocomplete = React.createClass({
318
328
return _this7 . handleKeyUp ( event ) ;
319
329
} ,
320
330
onClick : this . handleInputClick ,
321
- value : this . state . value
331
+ value : this . state . value ,
332
+ id : this . id
322
333
} ) ) ,
323
334
this . state . isOpen && this . renderMenu ( ) ,
324
335
this . props . debug && React . createElement (
0 commit comments