@@ -242,10 +242,19 @@ export class TreeKeyManager<T extends TreeKeyManagerItem> {
242
242
this . _getItems ( )
243
243
. pipe ( take ( 1 ) )
244
244
. subscribe ( items => {
245
+ // Clamp the index between 0 and the length of the list.
246
+ index = Math . min ( Math . max ( index , 0 ) , items . length - 1 ) ;
245
247
const activeItem = items [ index ] ;
246
248
247
- // Explicitly check for `null` and `undefined` because other falsy values are valid.
248
- this . _activeItem = activeItem == null ? null : activeItem ;
249
+ // If we're just setting the same item, don't re-call activate or focus
250
+ if (
251
+ this . _activeItem !== null &&
252
+ this . _trackByFn ( activeItem ) === this . _trackByFn ( this . _activeItem )
253
+ ) {
254
+ return ;
255
+ }
256
+
257
+ this . _activeItem = activeItem ?? null ;
249
258
this . _activeItemIndex = index ;
250
259
251
260
this . _activeItem ?. focus ( ) ;
@@ -271,13 +280,25 @@ export class TreeKeyManager<T extends TreeKeyManagerItem> {
271
280
272
281
//// Navigational methods
273
282
274
- private _focusFirstItem ( ) { }
283
+ private _focusFirstItem ( ) {
284
+ this . _setActiveItem ( 0 ) ;
285
+ }
275
286
276
- private _focusLastItem ( ) { }
287
+ private _focusLastItem ( ) {
288
+ this . _getItems ( )
289
+ . pipe ( take ( 1 ) )
290
+ . subscribe ( items => {
291
+ this . _setActiveItem ( items . length - 1 ) ;
292
+ } ) ;
293
+ }
277
294
278
- private _focusPreviousItem ( ) { }
295
+ private _focusPreviousItem ( ) {
296
+ this . _setActiveItem ( this . _activeItemIndex - 1 ) ;
297
+ }
279
298
280
- private _focusNextItem ( ) { }
299
+ private _focusNextItem ( ) {
300
+ this . _setActiveItem ( this . _activeItemIndex + 1 ) ;
301
+ }
281
302
282
303
/**
283
304
* If the item is already expanded, we collapse the item. Otherwise, we will focus the parent.
0 commit comments