6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
9
- import { FocusableOption , FocusKeyManager } from '@angular/cdk/a11y' ;
9
+ import { FocusableOption , FocusKeyManager , FocusMonitor } from '@angular/cdk/a11y' ;
10
10
import { BooleanInput , coerceBooleanProperty } from '@angular/cdk/coercion' ;
11
11
import { SelectionModel } from '@angular/cdk/collections' ;
12
12
import {
@@ -319,7 +319,6 @@ export class MatListOption extends _MatListOptionMixinBase implements AfterConte
319
319
host : {
320
320
'role' : 'listbox' ,
321
321
'class' : 'mat-selection-list mat-list-base' ,
322
- '(focus)' : '_onFocus()' ,
323
322
'(keydown)' : '_keydown($event)' ,
324
323
'[attr.aria-multiselectable]' : 'multiple' ,
325
324
'[attr.aria-disabled]' : 'disabled.toString()' ,
@@ -417,7 +416,9 @@ export class MatSelectionList extends _MatSelectionListMixinBase implements CanD
417
416
constructor ( private _element : ElementRef < HTMLElement > ,
418
417
// @breaking -change 11.0.0 Remove `tabIndex` parameter.
419
418
@Attribute ( 'tabindex' ) tabIndex : string ,
420
- private _changeDetector : ChangeDetectorRef ) {
419
+ private _changeDetector : ChangeDetectorRef ,
420
+ // @breaking -change 11.0.0 `_focusMonitor` parameter to become required.
421
+ private _focusMonitor ?: FocusMonitor ) {
421
422
super ( ) ;
422
423
}
423
424
@@ -460,6 +461,23 @@ export class MatSelectionList extends _MatSelectionListMixinBase implements CanD
460
461
}
461
462
}
462
463
} ) ;
464
+
465
+ // @breaking -change 11.0.0 Remove null assertion once _focusMonitor is required.
466
+ this . _focusMonitor ?. monitor ( this . _element )
467
+ . pipe ( takeUntil ( this . _destroyed ) )
468
+ . subscribe ( origin => {
469
+ if ( origin === 'keyboard' || origin === 'program' ) {
470
+ const activeIndex = this . _keyManager . activeItemIndex ;
471
+
472
+ if ( ! activeIndex || activeIndex === - 1 ) {
473
+ // If there is no active index, set focus to the first option.
474
+ this . _keyManager . setFirstItemActive ( ) ;
475
+ } else {
476
+ // Otherwise, set focus to the active option.
477
+ this . _keyManager . setActiveItem ( activeIndex ) ;
478
+ }
479
+ }
480
+ } ) ;
463
481
}
464
482
465
483
ngOnChanges ( changes : SimpleChanges ) {
@@ -473,6 +491,8 @@ export class MatSelectionList extends _MatSelectionListMixinBase implements CanD
473
491
}
474
492
475
493
ngOnDestroy ( ) {
494
+ // @breaking -change 11.0.0 Remove null assertion once _focusMonitor is required.
495
+ this . _focusMonitor ?. stopMonitoring ( this . _element ) ;
476
496
this . _destroyed . next ( ) ;
477
497
this . _destroyed . complete ( ) ;
478
498
this . _isDestroyed = true ;
@@ -575,22 +595,6 @@ export class MatSelectionList extends _MatSelectionListMixinBase implements CanD
575
595
this . selectionChange . emit ( new MatSelectionListChange ( this , option ) ) ;
576
596
}
577
597
578
- /**
579
- * When the selection list is focused, we want to move focus to an option within the list. Do this
580
- * by setting the appropriate option to be active.
581
- */
582
- _onFocus ( ) : void {
583
- const activeIndex = this . _keyManager . activeItemIndex ;
584
-
585
- if ( ! activeIndex || ( activeIndex === - 1 ) ) {
586
- // If there is no active index, set focus to the first option.
587
- this . _keyManager . setFirstItemActive ( ) ;
588
- } else {
589
- // Otherwise, set focus to the active option.
590
- this . _keyManager . setActiveItem ( activeIndex ) ;
591
- }
592
- }
593
-
594
598
/** Implemented as part of ControlValueAccessor. */
595
599
writeValue ( values : string [ ] ) : void {
596
600
this . _value = values ;
0 commit comments