@@ -242,10 +242,19 @@ export class TreeKeyManager<T extends TreeKeyManagerItem> {
242242 this . _getItems ( )
243243 . pipe ( take ( 1 ) )
244244 . 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 ) ;
245247 const activeItem = items [ index ] ;
246248
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 ;
249258 this . _activeItemIndex = index ;
250259
251260 this . _activeItem ?. focus ( ) ;
@@ -271,13 +280,25 @@ export class TreeKeyManager<T extends TreeKeyManagerItem> {
271280
272281 //// Navigational methods
273282
274- private _focusFirstItem ( ) { }
283+ private _focusFirstItem ( ) {
284+ this . _setActiveItem ( 0 ) ;
285+ }
275286
276- private _focusLastItem ( ) { }
287+ private _focusLastItem ( ) {
288+ this . _getItems ( )
289+ . pipe ( take ( 1 ) )
290+ . subscribe ( items => {
291+ this . _setActiveItem ( items . length - 1 ) ;
292+ } ) ;
293+ }
277294
278- private _focusPreviousItem ( ) { }
295+ private _focusPreviousItem ( ) {
296+ this . _setActiveItem ( this . _activeItemIndex - 1 ) ;
297+ }
279298
280- private _focusNextItem ( ) { }
299+ private _focusNextItem ( ) {
300+ this . _setActiveItem ( this . _activeItemIndex + 1 ) ;
301+ }
281302
282303 /**
283304 * If the item is already expanded, we collapse the item. Otherwise, we will focus the parent.
0 commit comments