@@ -102,19 +102,9 @@ export class TreeKeyManager<T extends TreeKeyManagerItem> implements TreeKeyMana
102102 // items aren't being collected via `ViewChildren` or `ContentChildren`).
103103 if ( items instanceof QueryList ) {
104104 this . _items = items . toArray ( ) ;
105- items . changes . subscribe ( ( newItems : QueryList < T > ) => {
106- this . _items = newItems . toArray ( ) ;
107- this . _typeahead ?. setItems ( this . _items ) ;
108- this . _updateActiveItemIndex ( this . _items ) ;
109- this . _initializeFocus ( ) ;
110- } ) ;
105+ items . changes . subscribe ( ( newItems : QueryList < T > ) => this . _itemsChanged ( newItems . toArray ( ) ) ) ;
111106 } else if ( isObservable ( items ) ) {
112- items . subscribe ( newItems => {
113- this . _items = newItems ;
114- this . _typeahead ?. setItems ( newItems ) ;
115- this . _updateActiveItemIndex ( newItems ) ;
116- this . _initializeFocus ( ) ;
117- } ) ;
107+ items . subscribe ( newItems => this . _itemsChanged ( newItems ) ) ;
118108 } else {
119109 this . _items = items ;
120110 this . _initializeFocus ( ) ;
@@ -219,6 +209,19 @@ export class TreeKeyManager<T extends TreeKeyManagerItem> implements TreeKeyMana
219209 return this . _activeItem ;
220210 }
221211
212+ /** Called when the list of items has changed. */
213+ private _itemsChanged ( newItems : T [ ] ) {
214+ if ( this . _hasInitialFocused && this . _activeItem && ! newItems . includes ( this . _activeItem ) ) {
215+ this . _activeItem = null ;
216+ this . _hasInitialFocused = false ;
217+ }
218+
219+ this . _items = newItems ;
220+ this . _typeahead ?. setItems ( this . _items ) ;
221+ this . _updateActiveItemIndex ( this . _items ) ;
222+ this . _initializeFocus ( ) ;
223+ }
224+
222225 /** Focus the first available item. */
223226 private _focusFirstItem ( ) : void {
224227 this . focusItem ( this . _findNextAvailableItemIndex ( - 1 ) ) ;
0 commit comments