@@ -259,7 +259,7 @@ export class ListKeyManager<T extends ListKeyManagerOption> {
259
259
* currently active item and the new active item. It will calculate differently
260
260
* depending on whether wrap mode is turned on.
261
261
*/
262
- private _setActiveItemByDelta ( delta : number , items = this . _items . toArray ( ) ) : void {
262
+ private _setActiveItemByDelta ( delta : - 1 | 1 , items = this . _items . toArray ( ) ) : void {
263
263
this . _wrap ? this . _setActiveInWrapMode ( delta , items )
264
264
: this . _setActiveInDefaultMode ( delta , items ) ;
265
265
}
@@ -269,16 +269,15 @@ export class ListKeyManager<T extends ListKeyManagerOption> {
269
269
* down the list until it finds an item that is not disabled, and it will wrap if it
270
270
* encounters either end of the list.
271
271
*/
272
- private _setActiveInWrapMode ( delta : number , items : T [ ] ) : void {
273
- // when active item would leave menu, wrap to beginning or end
274
- this . _activeItemIndex =
275
- ( this . _activeItemIndex + delta + items . length ) % items . length ;
276
-
277
- // skip all disabled menu items recursively until an enabled one is reached
278
- if ( items [ this . _activeItemIndex ] . disabled ) {
279
- this . _setActiveInWrapMode ( delta , items ) ;
280
- } else {
281
- this . setActiveItem ( this . _activeItemIndex ) ;
272
+ private _setActiveInWrapMode ( delta : - 1 | 1 , items : T [ ] ) : void {
273
+ for ( let i = 1 ; i <= items . length ; i ++ ) {
274
+ const index = ( this . _activeItemIndex + ( delta * i ) + items . length ) % items . length ;
275
+ const item = items [ index ] ;
276
+
277
+ if ( ! item . disabled ) {
278
+ this . setActiveItem ( index ) ;
279
+ return ;
280
+ }
282
281
}
283
282
}
284
283
@@ -287,7 +286,7 @@ export class ListKeyManager<T extends ListKeyManagerOption> {
287
286
* continue to move down the list until it finds an item that is not disabled. If
288
287
* it encounters either end of the list, it will stop and not wrap.
289
288
*/
290
- private _setActiveInDefaultMode ( delta : number , items : T [ ] ) : void {
289
+ private _setActiveInDefaultMode ( delta : - 1 | 1 , items : T [ ] ) : void {
291
290
this . _setActiveItemByIndex ( this . _activeItemIndex + delta , delta , items ) ;
292
291
}
293
292
@@ -296,13 +295,18 @@ export class ListKeyManager<T extends ListKeyManagerOption> {
296
295
* item is disabled, it will move in the fallbackDelta direction until it either
297
296
* finds an enabled item or encounters the end of the list.
298
297
*/
299
- private _setActiveItemByIndex ( index : number , fallbackDelta : number ,
300
- items = this . _items . toArray ( ) ) : void {
301
- if ( ! items [ index ] ) { return ; }
298
+ private _setActiveItemByIndex ( index : number , fallbackDelta : - 1 | 1 ,
299
+ items = this . _items . toArray ( ) ) : void {
300
+ if ( ! items [ index ] ) {
301
+ return ;
302
+ }
302
303
303
304
while ( items [ index ] . disabled ) {
304
305
index += fallbackDelta ;
305
- if ( ! items [ index ] ) { return ; }
306
+
307
+ if ( ! items [ index ] ) {
308
+ return ;
309
+ }
306
310
}
307
311
308
312
this . setActiveItem ( index ) ;
0 commit comments