11'use strict' ;
22
3+ Object . defineProperty ( exports , "__esModule" , {
4+ value : true
5+ } ) ;
6+
37var _react = require ( 'react' ) ;
48
59var _react2 = _interopRequireDefault ( _react ) ;
@@ -16,14 +20,10 @@ var _TypeaheadMenu = require('./TypeaheadMenu.react');
1620
1721var _TypeaheadMenu2 = _interopRequireDefault ( _TypeaheadMenu ) ;
1822
19- var _reactDom = require ( 'react-dom' ) ;
20-
2123var _lodash = require ( 'lodash' ) ;
2224
2325var _keyCode = require ( './keyCode' ) ;
2426
25- var _keyCode2 = _interopRequireDefault ( _keyCode ) ;
26-
2727var _reactOnclickoutside = require ( 'react-onclickoutside' ) ;
2828
2929var _reactOnclickoutside2 = _interopRequireDefault ( _reactOnclickoutside ) ;
@@ -74,20 +74,18 @@ var Typeahead = _react2.default.createClass({
7474 selected : [ ]
7575 } ;
7676 } ,
77-
7877 getInitialState : function getInitialState ( ) {
7978 var _props = this . props ;
8079 var defaultSelected = _props . defaultSelected ;
8180 var selected = _props . selected ;
8281
8382 return {
84- focusedMenuItem : null ,
83+ activeIndex : 0 ,
8584 selected : ! ( 0 , _lodash . isEmpty ) ( defaultSelected ) ? defaultSelected : selected ,
8685 showMenu : false ,
8786 text : ''
8887 } ;
8988 } ,
90-
9189 componentWillReceiveProps : function componentWillReceiveProps ( nextProps ) {
9290 if ( ! ( 0 , _lodash . isEqual ) ( this . props . selected , nextProps . selected ) ) {
9391 // If new selections are passed in via props, treat the component as a
@@ -99,13 +97,13 @@ var Typeahead = _react2.default.createClass({
9997 this . setState ( { text : '' } ) ;
10098 }
10199 } ,
102-
103100 render : function render ( ) {
104101 var _props2 = this . props ;
105102 var labelKey = _props2 . labelKey ;
106103 var multiple = _props2 . multiple ;
107104 var options = _props2 . options ;
108105 var _state = this . state ;
106+ var activeIndex = _state . activeIndex ;
109107 var selected = _state . selected ;
110108 var text = _state . text ;
111109
@@ -116,16 +114,15 @@ var Typeahead = _react2.default.createClass({
116114 return ! ( option [ labelKey ] . toLowerCase ( ) . indexOf ( text . toLowerCase ( ) ) === - 1 || multiple && ( 0 , _lodash . find ) ( selected , option ) ) ;
117115 } ) ;
118116
119- var menu ;
117+ var menu = undefined ;
120118 if ( this . state . showMenu ) {
121119 menu = _react2 . default . createElement ( _TypeaheadMenu2 . default , {
120+ activeIndex : activeIndex ,
122121 emptyLabel : this . props . emptyLabel ,
123122 labelKey : labelKey ,
124123 maxHeight : this . props . maxHeight ,
125124 onClick : this . _handleAddOption ,
126- onKeyDown : this . _handleKeydown ,
127- options : filteredOptions ,
128- ref : 'menu'
125+ options : filteredOptions
129126 } ) ;
130127 }
131128
@@ -148,7 +145,7 @@ var Typeahead = _react2.default.createClass({
148145 onAdd : this . _handleAddOption ,
149146 onChange : this . _handleTextChange ,
150147 onFocus : this . _handleFocus ,
151- onKeyDown : this . _handleKeydown ,
148+ onKeyDown : this . _handleKeydown . bind ( null , filteredOptions ) ,
152149 onRemove : this . _handleRemoveOption ,
153150 placeholder : this . props . placeholder ,
154151 ref : 'input' ,
@@ -158,87 +155,64 @@ var Typeahead = _react2.default.createClass({
158155 menu
159156 ) ;
160157 } ,
161-
162158 _handleFocus : function _handleFocus ( ) {
163159 this . setState ( { showMenu : true } ) ;
164160 } ,
165-
166161 _handleTextChange : function _handleTextChange ( e ) {
167162 this . setState ( {
163+ activeIndex : 0 ,
168164 showMenu : true ,
169165 text : e . target . value
170166 } ) ;
171167 } ,
172-
173- _handleKeydown : function _handleKeydown ( e ) {
174- var focusedMenuItem = ( 0 , _lodash . clone ) ( this . state . focusedMenuItem ) ;
168+ _handleKeydown : function _handleKeydown ( options , e ) {
169+ var activeIndex = this . state . activeIndex ;
175170
176171 switch ( e . keyCode ) {
177- case _keyCode2 . default . UP :
178- case _keyCode2 . default . DOWN :
179- case _keyCode2 . default . TAB :
180- // Prevent page from scrolling when pressing up or down.
172+ case _keyCode . BACKSPACE :
173+ // Don't let the browser go back.
174+ e . stopPropagation ( ) ;
175+ break ;
176+ case _keyCode . UP :
177+ // Prevent page from scrolling.
181178 e . preventDefault ( ) ;
182179
183- // Look for the menu. It won't be there if there are no results.
184- var menu = this . refs . menu && ( 0 , _reactDom . findDOMNode ) ( this . refs . menu ) ;
185- if ( ! menu ) {
186- return ;
187- }
188-
189- if ( e . keyCode === _keyCode2 . default . UP ) {
190- if ( ! focusedMenuItem ) {
191- // The input is focused and the user pressed the down key; select
192- // the first menu item.
193- focusedMenuItem = menu . lastChild ;
194- } else {
195- focusedMenuItem = focusedMenuItem . previousSibling || null ;
196- }
197- } else {
198- // keyCode.DOWN
199- if ( ! focusedMenuItem ) {
200- // The input is focused and the user pressed the down key; select
201- // the first menu item.
202- focusedMenuItem = menu . firstChild ;
203- } else {
204- focusedMenuItem = focusedMenuItem . nextSibling || null ;
205- }
180+ activeIndex -- ;
181+ if ( activeIndex < 0 ) {
182+ activeIndex = options . length - 1 ;
206183 }
184+ this . setState ( { activeIndex : activeIndex } ) ;
185+ break ;
186+ case _keyCode . DOWN :
187+ case _keyCode . TAB :
188+ // Prevent page from scrolling.
189+ e . preventDefault ( ) ;
207190
208- if ( focusedMenuItem ) {
209- // Select the link in the menu item.
210- focusedMenuItem . firstChild . focus ( ) ;
211- } else {
212- // If there's no focused item, it means we're at the beginning or the
213- // end of the menu. Focus the input.
214- ( 0 , _reactDom . findDOMNode ) ( this . refs . input ) . focus ( ) ;
191+ activeIndex ++ ;
192+ if ( activeIndex === options . length ) {
193+ activeIndex = 0 ;
215194 }
216-
217- this . setState ( { focusedMenuItem : focusedMenuItem } ) ;
195+ this . setState ( { activeIndex : activeIndex } ) ;
218196 break ;
219- case _keyCode2 . default . ESC :
197+ case _keyCode . ESC :
220198 // Prevent things like unintentionally closing dialogs.
221199 e . stopPropagation ( ) ;
222200 this . _hideDropdown ( ) ;
223201 break ;
224- case _keyCode2 . default . RETURN :
225- if ( focusedMenuItem ) {
226- // Simulate clicking on the anchor.
227- focusedMenuItem . firstChild . click ( ) ;
228- this . _hideDropdown ( ) ;
229- }
202+ case _keyCode . RETURN :
203+ var selected = options [ activeIndex ] ;
204+ selected && this . _handleAddOption ( selected ) ;
230205 break ;
231206 }
232207 } ,
233-
234208 _handleAddOption : function _handleAddOption ( selectedOption ) {
235209 var _props3 = this . props ;
236210 var multiple = _props3 . multiple ;
237211 var labelKey = _props3 . labelKey ;
238212 var onChange = _props3 . onChange ;
239213
240- var selected ;
241- var text ;
214+ var selected = undefined ;
215+ var text = undefined ;
242216
243217 if ( multiple ) {
244218 // If multiple selections are allowed, add the new selection to the
@@ -253,21 +227,22 @@ var Typeahead = _react2.default.createClass({
253227 }
254228
255229 this . setState ( {
230+ activeIndex : 0 ,
256231 selected : selected ,
257232 showMenu : false ,
258233 text : text
259234 } ) ;
260235
261236 onChange && onChange ( selected ) ;
262237 } ,
263-
264238 _handleRemoveOption : function _handleRemoveOption ( removedOption ) {
265239 var selected = this . state . selected . slice ( ) ;
266240 selected = selected . filter ( function ( option ) {
267241 return ! ( 0 , _lodash . isEqual ) ( option , removedOption ) ;
268242 } ) ;
269243
270244 this . setState ( {
245+ activeIndex : 0 ,
271246 selected : selected ,
272247 showMenu : false
273248 } ) ;
@@ -281,13 +256,12 @@ var Typeahead = _react2.default.createClass({
281256 handleClickOutside : function handleClickOutside ( e ) {
282257 this . _hideDropdown ( ) ;
283258 } ,
284-
285259 _hideDropdown : function _hideDropdown ( ) {
286260 this . setState ( {
287- showMenu : false ,
288- focusedMenuItem : null
261+ activeIndex : 0 ,
262+ showMenu : false
289263 } ) ;
290264 }
291265} ) ;
292266
293- module . exports = Typeahead ;
267+ exports . default = Typeahead ;
0 commit comments