@@ -12,7 +12,7 @@ var Autocomplete = React.createClass({
12
12
displayName : 'Autocomplete' ,
13
13
14
14
propTypes : {
15
- initialValue : React . PropTypes . any ,
15
+ value : React . PropTypes . any ,
16
16
onChange : React . PropTypes . func ,
17
17
onSelect : React . PropTypes . func ,
18
18
shouldItemRender : React . PropTypes . func ,
@@ -24,6 +24,7 @@ var Autocomplete = React.createClass({
24
24
25
25
getDefaultProps : function getDefaultProps ( ) {
26
26
return {
27
+ value : '' ,
27
28
inputProps : { } ,
28
29
labelText : '' ,
29
30
onChange : function onChange ( ) { } ,
@@ -49,7 +50,6 @@ var Autocomplete = React.createClass({
49
50
// TODO: don't cheat, let it flow to the bottom
50
51
getInitialState : function getInitialState ( ) {
51
52
return {
52
- value : this . props . initialValue || '' ,
53
53
isOpen : false ,
54
54
highlightedIndex : null
55
55
} ;
@@ -95,14 +95,8 @@ var Autocomplete = React.createClass({
95
95
} ,
96
96
97
97
handleChange : function handleChange ( event ) {
98
- var _this = this ;
99
-
100
98
this . _performAutoCompleteOnKeyUp = true ;
101
- this . setState ( {
102
- value : event . target . value
103
- } , function ( ) {
104
- _this . props . onChange ( event , _this . state . value ) ;
105
- } ) ;
99
+ this . props . onChange ( event , event . target . value ) ;
106
100
} ,
107
101
108
102
handleKeyUp : function handleKeyUp ( ) {
@@ -138,7 +132,7 @@ var Autocomplete = React.createClass({
138
132
} ,
139
133
140
134
Enter : function Enter ( event ) {
141
- var _this2 = this ;
135
+ var _this = this ;
142
136
143
137
if ( this . state . isOpen === false ) {
144
138
// menu is closed so there is no selection to accept -> do nothing
@@ -148,19 +142,19 @@ var Autocomplete = React.createClass({
148
142
this . setState ( {
149
143
isOpen : false
150
144
} , function ( ) {
151
- _this2 . refs . input . select ( ) ;
145
+ _this . refs . input . select ( ) ;
152
146
} ) ;
153
147
} else {
154
148
// text entered + menu item has been highlighted + enter is hit -> update value to that of selected menu item, close the menu
155
149
var item = this . getFilteredItems ( ) [ this . state . highlightedIndex ] ;
150
+ var value = this . props . getItemValue ( item ) ;
156
151
this . setState ( {
157
- value : this . props . getItemValue ( item ) ,
158
152
isOpen : false ,
159
153
highlightedIndex : null
160
154
} , function ( ) {
161
155
//this.refs.input.focus() // TODO: file issue
162
- _this2 . refs . input . setSelectionRange ( _this2 . state . value . length , _this2 . state . value . length ) ;
163
- _this2 . props . onSelect ( _this2 . state . value , item ) ;
156
+ _this . refs . input . setSelectionRange ( value . length , value . length ) ;
157
+ _this . props . onSelect ( value , item ) ;
164
158
} ) ;
165
159
}
166
160
} ,
@@ -174,41 +168,41 @@ var Autocomplete = React.createClass({
174
168
} ,
175
169
176
170
getFilteredItems : function getFilteredItems ( ) {
177
- var _this3 = this ;
171
+ var _this2 = this ;
178
172
179
173
var items = this . props . items ;
180
174
181
175
if ( this . props . shouldItemRender ) {
182
176
items = items . filter ( function ( item ) {
183
- return _this3 . props . shouldItemRender ( item , _this3 . state . value ) ;
177
+ return _this2 . props . shouldItemRender ( item , _this2 . props . value ) ;
184
178
} ) ;
185
179
}
186
180
187
181
if ( this . props . sortItems ) {
188
182
items . sort ( function ( a , b ) {
189
- return _this3 . props . sortItems ( a , b , _this3 . state . value ) ;
183
+ return _this2 . props . sortItems ( a , b , _this2 . props . value ) ;
190
184
} ) ;
191
185
}
192
186
193
187
return items ;
194
188
} ,
195
189
196
190
maybeAutoCompleteText : function maybeAutoCompleteText ( ) {
197
- var _this4 = this ;
191
+ var _this3 = this ;
198
192
199
- if ( this . state . value === '' ) return ;
193
+ if ( this . props . value === '' ) return ;
200
194
var highlightedIndex = this . state . highlightedIndex ;
201
195
202
196
var items = this . getFilteredItems ( ) ;
203
197
if ( items . length === 0 ) return ;
204
198
var matchedItem = highlightedIndex !== null ? items [ highlightedIndex ] : items [ 0 ] ;
205
199
var itemValue = this . props . getItemValue ( matchedItem ) ;
206
- var itemValueDoesMatch = itemValue . toLowerCase ( ) . indexOf ( this . state . value . toLowerCase ( ) ) === 0 ;
200
+ var itemValueDoesMatch = itemValue . toLowerCase ( ) . indexOf ( this . props . value . toLowerCase ( ) ) === 0 ;
207
201
if ( itemValueDoesMatch ) {
208
202
var node = this . refs . input ;
209
203
var setSelection = function setSelection ( ) {
210
204
node . value = itemValue ;
211
- node . setSelectionRange ( _this4 . state . value . length , itemValue . length ) ;
205
+ node . setSelectionRange ( _this3 . props . value . length , itemValue . length ) ;
212
206
} ;
213
207
if ( highlightedIndex === null ) this . setState ( { highlightedIndex : 0 } , setSelection ) ; else setSelection ( ) ;
214
208
}
@@ -233,16 +227,16 @@ var Autocomplete = React.createClass({
233
227
} ,
234
228
235
229
selectItemFromMouse : function selectItemFromMouse ( item ) {
236
- var _this5 = this ;
230
+ var _this4 = this ;
237
231
232
+ var value = this . props . getItemValue ( item ) ;
238
233
this . setState ( {
239
- value : this . props . getItemValue ( item ) ,
240
234
isOpen : false ,
241
235
highlightedIndex : null
242
236
} , function ( ) {
243
- _this5 . props . onSelect ( _this5 . state . value , item ) ;
244
- _this5 . refs . input . focus ( ) ;
245
- _this5 . setIgnoreBlur ( false ) ;
237
+ _this4 . props . onSelect ( value , item ) ;
238
+ _this4 . refs . input . focus ( ) ;
239
+ _this4 . setIgnoreBlur ( false ) ;
246
240
} ) ;
247
241
} ,
248
242
@@ -251,19 +245,19 @@ var Autocomplete = React.createClass({
251
245
} ,
252
246
253
247
renderMenu : function renderMenu ( ) {
254
- var _this6 = this ;
248
+ var _this5 = this ;
255
249
256
250
var items = this . getFilteredItems ( ) . map ( function ( item , index ) {
257
- var element = _this6 . props . renderItem ( item , _this6 . state . highlightedIndex === index , { cursor : 'default' } ) ;
251
+ var element = _this5 . props . renderItem ( item , _this5 . state . highlightedIndex === index , { cursor : 'default' } ) ;
258
252
return React . cloneElement ( element , {
259
253
onMouseDown : function onMouseDown ( ) {
260
- return _this6 . setIgnoreBlur ( true ) ;
254
+ return _this5 . setIgnoreBlur ( true ) ;
261
255
} ,
262
256
onMouseEnter : function onMouseEnter ( ) {
263
- return _this6 . highlightItemFromMouse ( index ) ;
257
+ return _this5 . highlightItemFromMouse ( index ) ;
264
258
} ,
265
259
onClick : function onClick ( ) {
266
- return _this6 . selectItemFromMouse ( item ) ;
260
+ return _this5 . selectItemFromMouse ( item ) ;
267
261
} ,
268
262
ref : 'item-' + index
269
263
} ) ;
@@ -273,7 +267,7 @@ var Autocomplete = React.createClass({
273
267
top : this . state . menuTop ,
274
268
minWidth : this . state . menuWidth
275
269
} ;
276
- var menu = this . props . renderMenu ( items , this . state . value , style ) ;
270
+ var menu = this . props . renderMenu ( items , this . props . value , style ) ;
277
271
return React . cloneElement ( menu , { ref : 'menu' } ) ;
278
272
} ,
279
273
@@ -295,7 +289,7 @@ var Autocomplete = React.createClass({
295
289
} ,
296
290
297
291
render : function render ( ) {
298
- var _this7 = this ;
292
+ var _this6 = this ;
299
293
300
294
if ( this . props . debug ) {
301
295
// you don't like it, you love it
@@ -319,16 +313,16 @@ var Autocomplete = React.createClass({
319
313
onFocus : this . handleInputFocus ,
320
314
onBlur : this . handleInputBlur ,
321
315
onChange : function ( event ) {
322
- return _this7 . handleChange ( event ) ;
316
+ return _this6 . handleChange ( event ) ;
323
317
} ,
324
318
onKeyDown : function ( event ) {
325
- return _this7 . handleKeyDown ( event ) ;
319
+ return _this6 . handleKeyDown ( event ) ;
326
320
} ,
327
321
onKeyUp : function ( event ) {
328
- return _this7 . handleKeyUp ( event ) ;
322
+ return _this6 . handleKeyUp ( event ) ;
329
323
} ,
330
324
onClick : this . handleInputClick ,
331
- value : this . state . value ,
325
+ value : this . props . value ,
332
326
id : this . id
333
327
} ) ) ,
334
328
this . state . isOpen && this . renderMenu ( ) ,
0 commit comments