@@ -98,6 +98,7 @@ export class MatListOption extends _MatListOptionMixinBase
98
98
99
99
private _selected = false ;
100
100
private _disabled = false ;
101
+ private _hasFocus = false ;
101
102
102
103
@ContentChild ( MatListAvatarCssMatStyler ) _avatar : MatListAvatarCssMatStyler ;
103
104
@ContentChild ( MatListIconCssMatStyler ) _icon : MatListIconCssMatStyler ;
@@ -172,7 +173,13 @@ export class MatListOption extends _MatListOptionMixinBase
172
173
Promise . resolve ( ) . then ( ( ) => this . selected = false ) ;
173
174
}
174
175
175
- this . selectionList . _removeOptionFromList ( this ) ;
176
+ const hadFocus = this . _hasFocus ;
177
+ const newActiveItem = this . selectionList . _removeOptionFromList ( this ) ;
178
+
179
+ // Only move focus if this option was focused at the time it was destroyed.
180
+ if ( hadFocus && newActiveItem ) {
181
+ newActiveItem . focus ( ) ;
182
+ }
176
183
}
177
184
178
185
/** Toggles the selection state of the option. */
@@ -209,10 +216,12 @@ export class MatListOption extends _MatListOptionMixinBase
209
216
210
217
_handleFocus ( ) {
211
218
this . selectionList . _setFocusedOption ( this ) ;
219
+ this . _hasFocus = true ;
212
220
}
213
221
214
222
_handleBlur ( ) {
215
223
this . selectionList . _onTouched ( ) ;
224
+ this . _hasFocus = false ;
216
225
}
217
226
218
227
/** Retrieves the DOM element of the component host. */
@@ -385,18 +394,23 @@ export class MatSelectionList extends _MatSelectionListMixinBase implements Focu
385
394
this . _keyManager . updateActiveItemIndex ( this . _getOptionIndex ( option ) ) ;
386
395
}
387
396
388
- /** Removes an option from the selection list and updates the active item. */
389
- _removeOptionFromList ( option : MatListOption ) {
397
+ /**
398
+ * Removes an option from the selection list and updates the active item.
399
+ * @returns Currently-active item.
400
+ */
401
+ _removeOptionFromList ( option : MatListOption ) : MatListOption | null {
390
402
const optionIndex = this . _getOptionIndex ( option ) ;
391
403
392
404
if ( optionIndex > - 1 && this . _keyManager . activeItemIndex === optionIndex ) {
393
405
// Check whether the option is the last item
394
406
if ( optionIndex > 0 ) {
395
- this . _keyManager . setPreviousItemActive ( ) ;
407
+ this . _keyManager . updateActiveItemIndex ( optionIndex - 1 ) ;
396
408
} else if ( optionIndex === 0 && this . options . length > 1 ) {
397
- this . _keyManager . setNextItemActive ( ) ;
409
+ this . _keyManager . updateActiveItemIndex ( Math . min ( optionIndex + 1 , this . options . length - 1 ) ) ;
398
410
}
399
411
}
412
+
413
+ return this . _keyManager . activeItem ;
400
414
}
401
415
402
416
/** Passes relevant key presses to our key manager. */
0 commit comments