MCPcopy Create free account
hub / github.com/angular/components / ngAfterViewInit

Method ngAfterViewInit

src/cdk/stepper/stepper.ts:424–491  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

422 }
423
424 ngAfterViewInit() {
425 // If the step headers are defined outside of the `ngFor` that renders the steps, like in the
426 // Material stepper, they won't appear in the `QueryList` in the same order as they're
427 // rendered in the DOM which will lead to incorrect keyboard navigation. We need to sort
428 // them manually to ensure that they're correct. Alternatively, we can change the Material
429 // template to inline the headers in the `ngFor`, but that'll result in a lot of
430 // code duplication. See #23539.
431 this._stepHeader.changes
432 .pipe(startWith(this._stepHeader), takeUntil(this._destroyed))
433 .subscribe((headers: QueryList<CdkStepHeader>) => {
434 this._sortedHeaders.reset(
435 headers.toArray().sort((a, b) => {
436 const documentPosition = a._elementRef.nativeElement.compareDocumentPosition(
437 b._elementRef.nativeElement,
438 );
439
440 // `compareDocumentPosition` returns a bitmask so we have to use a bitwise operator.
441 // https://developer.mozilla.org/en-US/docs/Web/API/Node/compareDocumentPosition
442 // tslint:disable-next-line:no-bitwise
443 return documentPosition & Node.DOCUMENT_POSITION_FOLLOWING ? -1 : 1;
444 }),
445 );
446 this._sortedHeaders.notifyOnChanges();
447 });
448
449 // Note that while the step headers are content children by default, any components that
450 // extend this one might have them as view children. We initialize the keyboard handling in
451 // AfterViewInit so we're guaranteed for both view and content children to be defined.
452 this._keyManager = new FocusKeyManager<FocusableOption>(this._sortedHeaders)
453 .withWrap()
454 .withHomeAndEnd()
455 .withVerticalOrientation(this._orientation === 'vertical');
456
457 // The selected index may have changed between when the component was created and when the
458 // key manager was initialized. Use `updateActiveItem` so it's correct, but it doesn't steal
459 // away focus from the user.
460 this._keyManager.updateActiveItem(this.selectedIndex);
461
462 (this._dir ? (this._dir.change as Observable<Direction>) : observableOf<Direction>())
463 .pipe(startWith(this._layoutDirection()), takeUntil(this._destroyed))
464 .subscribe(direction => this._keyManager?.withHorizontalOrientation(direction));
465
466 this._keyManager.updateActiveItem(this.selectedIndex);
467
468 // No need to `takeUntil` here, because we're the ones destroying `steps`.
469 this.steps.changes.subscribe(() => {
470 if (!this.selected) {
471 this._selectedIndex.set(Math.max(this.selectedIndex - 1, 0));
472 }
473 });
474
475 // The logic which asserts that the selected index is within bounds doesn't run before the
476 // steps are initialized, because we don't how many steps there are yet so we may have an
477 // invalid index on init. If that's the case, auto-correct to the default so we don't throw.
478 if (!this._isValidIndex(this.selectedIndex)) {
479 this._selectedIndex.set(0);
480 }
481

Callers

nothing calls this directly

Calls 12

_layoutDirectionMethod · 0.95
_isValidIndexMethod · 0.95
withHomeAndEndMethod · 0.80
withWrapMethod · 0.80
updateActiveItemMethod · 0.80
setMethod · 0.80
_markAsInteractedMethod · 0.80
resetMethod · 0.65
sortMethod · 0.65
maxMethod · 0.45

Tested by

no test coverage detected