@@ -46,12 +46,12 @@ class SLDSLookup extends React.Component {
46
46
// Using down/up keys, set Focus on list item and assign it to aria-activedescendant attribute in input.
47
47
// Need to keep track of filtered list length to be able to increment/decrement the focus index so it's contained to the number of available list items.
48
48
increaseIndex ( ) {
49
- let items = this . state . listLength - 1 ;
50
- this . setState ( { focusIndex : this . state . focusIndex < items ? this . state . focusIndex + 1 : 0 } )
49
+ let items = this . state . listLength ;
50
+ this . setState ( { focusIndex : this . state . focusIndex <= items ? this . state . focusIndex + 1 : 0 } )
51
51
}
52
52
53
53
decreaseIndex ( ) {
54
- let items = this . state . listLength - 1 ;
54
+ let items = this . state . listLength ;
55
55
this . setState ( { focusIndex : this . state . focusIndex > 0 ? this . state . focusIndex - 1 : items } )
56
56
}
57
57
@@ -85,7 +85,8 @@ class SLDSLookup extends React.Component {
85
85
handleClose ( ) {
86
86
this . setState ( {
87
87
isOpen :false ,
88
- focusIndex :null
88
+ focusIndex :null ,
89
+ currentFocus :null ,
89
90
} )
90
91
}
91
92
@@ -111,25 +112,22 @@ class SLDSLookup extends React.Component {
111
112
//If user hits esc key, close menu
112
113
event . keyCode === KEYS . ESCAPE ? this . handleClose ( ) : this . handleClick ( ) ;
113
114
114
- //If user hits tab key, move aria activedescendant to first menu item
115
- if ( event . keyCode === KEYS . TAB && this . state . focusIndex === null ) {
116
- this . setState ( { focusIndex : 0 } ) ;
117
- EventUtil . trapImmediate ( event ) ;
118
- }
119
115
//If user hits down key, advance aria activedescendant to next item
120
- else if ( event . keyCode === KEYS . DOWN && this . state . focusIndex !== null ) {
116
+ if ( event . keyCode === KEYS . DOWN ) {
121
117
EventUtil . trapImmediate ( event ) ;
122
- this . increaseIndex ( ) ;
118
+ this . state . focusIndex === null ? this . setState ( { focusIndex : 0 } ) : this . increaseIndex ( ) ;
123
119
}
124
120
//If user hits up key, advance aria activedescendant to previous item
125
- else if ( event . keyCode === KEYS . UP && this . state . focusIndex !== null ) {
121
+ else if ( event . keyCode === KEYS . UP ) {
126
122
EventUtil . trapImmediate ( event ) ;
127
- this . decreaseIndex ( ) ;
123
+ this . state . focusIndex === null ? this . setState ( { focusIndex : this . state . listLength } ) : this . decreaseIndex ( ) ;
128
124
}
129
125
//If user hits enter/space key, select current activedescendant item
130
126
else if ( ( event . keyCode === KEYS . ENTER || event . keyCode === KEYS . SPACE ) && this . state . focusIndex !== null ) {
131
127
EventUtil . trapImmediate ( event ) ;
132
- this . selectItem ( this . state . currentFocus ) ;
128
+ //If the focus is on the first or last item in the menu (search details or add item buttons), then close.
129
+ //If not, then select menu item
130
+ ( this . state . focusIndex === 0 || this . state . focusIndex === ( this . state . listLength + 1 ) ) ? this . handleClose ( ) : this . selectItem ( this . state . currentFocus ) ;
133
131
}
134
132
}
135
133
}
@@ -227,7 +225,7 @@ SLDSLookup.defaultProps = {
227
225
onItemSelect : function ( item ) {
228
226
//console.log('onItemSelect should be defined');
229
227
} ,
230
- addItem : function ( item ) {
228
+ addItem : function ( event ) {
231
229
//console.log('onItemSelect should be defined');
232
230
} ,
233
231
} ;
0 commit comments