@@ -37,9 +37,7 @@ class SLDSLookup extends React.Component {
37
37
focusIndex :null ,
38
38
selectedIndex : null ,
39
39
listLength :this . props . items . length ,
40
- items :[ ] ,
41
- errors :[ ] ,
42
- messages :[ ] ,
40
+ items :[ ]
43
41
} ;
44
42
}
45
43
@@ -63,13 +61,13 @@ class SLDSLookup extends React.Component {
63
61
// 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.
64
62
// Adding/subtracting 1 from focusIndex to account for fixed action items (searchRecords and addNewItem buttons)
65
63
increaseIndex ( ) {
66
- let items = this . state . listLength ;
67
- this . setState ( { focusIndex : this . state . focusIndex <= items ? this . state . focusIndex + 1 : 0 } )
64
+ let numFocusable = this . getNumFocusableItems ( ) ;
65
+ this . setState ( { focusIndex : this . state . focusIndex < numFocusable - 1 ? this . state . focusIndex + 1 : 0 } )
68
66
}
69
67
70
68
decreaseIndex ( ) {
71
- let items = this . state . listLength ;
72
- this . setState ( { focusIndex : this . state . focusIndex > 0 ? this . state . focusIndex - 1 : items } )
69
+ let numFocusable = this . getNumFocusableItems ( ) ;
70
+ this . setState ( { focusIndex : this . state . focusIndex > 0 ? this . state . focusIndex - 1 : numFocusable - 1 } )
73
71
}
74
72
75
73
setFocus ( id ) {
@@ -80,16 +78,33 @@ class SLDSLookup extends React.Component {
80
78
if ( qty !== this . state . listLength ) this . setState ( { listLength :qty } ) ;
81
79
}
82
80
81
+ getNumFocusableItems ( ) {
82
+ let offset = 0
83
+ if ( this . refs . footer )
84
+ offset += 1
85
+ if ( this . refs . header )
86
+ offset += 1
87
+ return this . state . listLength + offset
88
+ }
89
+
83
90
//=================================================
84
91
// Select menu item (onClick or on key enter/space)
85
92
selectItem ( itemId ) {
86
- const index = itemId . replace ( 'item-' , '' ) ;
87
- this . setState ( {
88
- selectedIndex : index ,
89
- searchTerm : null
90
- } ) ;
91
- const data = this . state . items [ index ] . data ;
92
- if ( this . props . onItemSelect ) this . props . onItemSelect ( data ) ;
93
+ if ( itemId ) {
94
+ const index = itemId . replace ( 'item-' , '' ) ;
95
+ this . selectItemByIndex ( index ) ;
96
+ }
97
+ }
98
+
99
+ selectItemByIndex ( index ) {
100
+ if ( index >= 0 && index < this . state . items . length ) {
101
+ this . setState ( {
102
+ selectedIndex : index ,
103
+ searchTerm : null
104
+ } ) ;
105
+ const data = this . state . items [ index ] . data ;
106
+ if ( this . props . onItemSelect ) this . props . onItemSelect ( data ) ;
107
+ }
93
108
}
94
109
95
110
handleDeleteSelected ( ) {
@@ -149,20 +164,19 @@ class SLDSLookup extends React.Component {
149
164
//If user hits up key, advance aria activedescendant to previous item
150
165
else if ( event . keyCode === KEYS . UP ) {
151
166
EventUtil . trapImmediate ( event ) ;
152
- this . state . focusIndex === null ? this . setState ( { focusIndex : this . state . listLength + 1 } ) : this . decreaseIndex ( ) ;
167
+ let numFocusable = this . getNumFocusableItems ( )
168
+ this . state . focusIndex === null ? this . setState ( { focusIndex : numFocusable - 1 } ) : this . decreaseIndex ( ) ;
153
169
}
154
170
//If user hits enter/space key, select current activedescendant item
155
171
else if ( ( event . keyCode === KEYS . ENTER || event . keyCode === KEYS . SPACE ) && this . state . focusIndex !== null ) {
156
172
EventUtil . trapImmediate ( event ) ;
157
173
//If the focus is on the first fixed Action Item in Menu, click it
158
174
if ( this . refs . header && this . state . focusIndex === 0 ) {
159
- // document.getElementById('menuContainer').firstChild.children[0].click();
160
175
React . findDOMNode ( this . refs . header ) . click ( ) ;
161
176
}
162
177
//If the focus is on the last fixed Action Item in Menu, click it
163
178
else if ( this . refs . footer && this . state . focusIndex === ( this . state . listLength + 1 ) ) {
164
179
React . findDOMNode ( this . refs . footer ) . click ( ) ;
165
- // document.getElementById('menuContainer').lastChild.children[0].click();
166
180
}
167
181
//If not, then select menu item
168
182
else {
@@ -223,12 +237,13 @@ class SLDSLookup extends React.Component {
223
237
searchTerm = { this . state . searchTerm }
224
238
label = { this . props . label }
225
239
type = { this . props . type }
240
+ iconCategory = { this . props . iconCategory }
226
241
focusIndex = { this . state . focusIndex }
227
242
listLength = { this . state . listLength }
228
243
items = { this . state . items }
229
244
emptyMessage = { this . props . emptyMessage }
230
- messages = { this . state . messages }
231
- errors = { this . state . errors }
245
+ messages = { this . props . messages }
246
+ errors = { this . props . errors }
232
247
filterWith = { this . props . filterWith }
233
248
getListLength = { this . getListLength . bind ( this ) }
234
249
setFocus = { this . setFocus . bind ( this ) }
@@ -298,12 +313,6 @@ class SLDSLookup extends React.Component {
298
313
if ( newProps . items ) {
299
314
this . modifyItems ( newProps . items ) ;
300
315
}
301
- if ( newProps . message ) {
302
- this . setState ( { message : newProps . message } ) ;
303
- }
304
- if ( newProps . error ) {
305
- this . setState ( { errors : newProps . error } ) ;
306
- }
307
316
}
308
317
309
318
render ( ) {
@@ -349,12 +358,12 @@ class SLDSLookup extends React.Component {
349
358
350
359
SLDSLookup . propTypes = {
351
360
items : React . PropTypes . array ,
352
- errors : React . PropTypes . arrayOf ( React . PropTypes . string ) ,
353
361
emptyMessage : React . PropTypes . string ,
354
362
messages : React . PropTypes . arrayOf ( React . PropTypes . string ) ,
355
363
errors : React . PropTypes . arrayOf ( React . PropTypes . string ) ,
356
364
label : React . PropTypes . string ,
357
365
type : React . PropTypes . string ,
366
+ iconCategory : React . PropTypes . string ,
358
367
filterWith : React . PropTypes . func ,
359
368
onItemSelect : React . PropTypes . func ,
360
369
onItemUnselect : React . PropTypes . func ,
0 commit comments