@@ -261,7 +261,7 @@ export class CdkStepper implements AfterContentInit, AfterViewInit, OnDestroy {
261
261
protected readonly _destroyed = new Subject < void > ( ) ;
262
262
263
263
/** Used for managing keyboard focus. */
264
- private _keyManager : FocusKeyManager < FocusableOption > ;
264
+ private _keyManager : FocusKeyManager < FocusableOption > | undefined ;
265
265
266
266
/** Full list of steps inside the stepper, including inside nested steppers. */
267
267
@ContentChildren ( CdkStep , { descendants : true } ) _steps : QueryList < CdkStep > ;
@@ -384,9 +384,14 @@ export class CdkStepper implements AfterContentInit, AfterViewInit, OnDestroy {
384
384
. withHomeAndEnd ( )
385
385
. withVerticalOrientation ( this . _orientation === 'vertical' ) ;
386
386
387
+ // The selected index may have changed between when the component was created and when the
388
+ // key manager was initialized. Use `updateActiveItem` so it's correct, but it doesn't steal
389
+ // away focus from the user.
390
+ this . _keyManager . updateActiveItem ( this . selectedIndex ) ;
391
+
387
392
( this . _dir ? ( this . _dir . change as Observable < Direction > ) : observableOf < Direction > ( ) )
388
393
. pipe ( startWith ( this . _layoutDirection ( ) ) , takeUntil ( this . _destroyed ) )
389
- . subscribe ( direction => this . _keyManager . withHorizontalOrientation ( direction ) ) ;
394
+ . subscribe ( direction => this . _keyManager ? .withHorizontalOrientation ( direction ) ) ;
390
395
391
396
this . _keyManager . updateActiveItem ( this . _selectedIndex ) ;
392
397
@@ -526,9 +531,11 @@ export class CdkStepper implements AfterContentInit, AfterViewInit, OnDestroy {
526
531
// lost when the active step content is hidden. We can't be more granular with the check
527
532
// (e.g. checking whether focus is inside the active step), because we don't have a
528
533
// reference to the elements that are rendering out the content.
529
- this . _containsFocus ( )
530
- ? this . _keyManager . setActiveItem ( newIndex )
531
- : this . _keyManager . updateActiveItem ( newIndex ) ;
534
+ if ( this . _keyManager ) {
535
+ this . _containsFocus ( )
536
+ ? this . _keyManager . setActiveItem ( newIndex )
537
+ : this . _keyManager . updateActiveItem ( newIndex ) ;
538
+ }
532
539
533
540
this . _selectedIndex = newIndex ;
534
541
this . selectedIndexChange . emit ( this . _selectedIndex ) ;
@@ -541,14 +548,14 @@ export class CdkStepper implements AfterContentInit, AfterViewInit, OnDestroy {
541
548
const manager = this . _keyManager ;
542
549
543
550
if (
544
- manager . activeItemIndex != null &&
551
+ manager ? .activeItemIndex != null &&
545
552
! hasModifier &&
546
553
( keyCode === SPACE || keyCode === ENTER )
547
554
) {
548
555
this . selectedIndex = manager . activeItemIndex ;
549
556
event . preventDefault ( ) ;
550
557
} else {
551
- manager . setFocusOrigin ( 'keyboard' ) . onKeydown ( event ) ;
558
+ manager ? .setFocusOrigin ( 'keyboard' ) . onKeydown ( event ) ;
552
559
}
553
560
}
554
561
0 commit comments