Skip to content

Commit

Permalink
fix(stepper): update state when steps change (#8398)
Browse files Browse the repository at this point in the history
  • Loading branch information
actra-gschuster authored and jelbourn committed Nov 20, 2017
1 parent f6bd9b0 commit 2bc0b41
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
12 changes: 11 additions & 1 deletion src/cdk/stepper/stepper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ import {
ChangeDetectionStrategy,
ChangeDetectorRef,
OnChanges,
OnDestroy
} from '@angular/core';
import {LEFT_ARROW, RIGHT_ARROW, ENTER, SPACE} from '@angular/cdk/keycodes';
import {CdkStepLabel} from './step-label';
import {coerceBooleanProperty} from '@angular/cdk/coercion';
import {AbstractControl} from '@angular/forms';
import {Direction, Directionality} from '@angular/cdk/bidi';
import {Subject} from 'rxjs/Subject';

/** Used to generate unique ID for each stepper component. */
let nextId = 0;
Expand Down Expand Up @@ -132,7 +134,10 @@ export class CdkStep implements OnChanges {
selector: '[cdkStepper]',
exportAs: 'cdkStepper',
})
export class CdkStepper {
export class CdkStepper implements OnDestroy {
/** Emits when the component is destroyed. */
protected _destroyed = new Subject<void>();

/** The list of step components that the stepper is holding. */
@ContentChildren(CdkStep) _steps: QueryList<CdkStep>;

Expand Down Expand Up @@ -186,6 +191,11 @@ export class CdkStepper {
this._groupId = nextId++;
}

ngOnDestroy() {
this._destroyed.next();
this._destroyed.complete();
}

/** Selects and focuses the next step in list. */
next(): void {
this.selectedIndex = Math.min(this._selectedIndex + 1, this._steps.length - 1);
Expand Down
9 changes: 8 additions & 1 deletion src/lib/stepper/stepper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import {animate, state, style, transition, trigger} from '@angular/animations';
import {CdkStep, CdkStepper} from '@angular/cdk/stepper';
import {
AfterContentInit,
Component,
ContentChild,
ContentChildren,
Expand All @@ -26,6 +27,7 @@ import {FormControl, FormGroupDirective, NgForm} from '@angular/forms';
import {ErrorStateMatcher} from '@angular/material/core';
import {MatStepHeader} from './step-header';
import {MatStepLabel} from './step-label';
import {takeUntil} from 'rxjs/operators/takeUntil';

/** Workaround for https://github.com/angular/angular/issues/17849 */
export const _MatStep = CdkStep;
Expand Down Expand Up @@ -66,12 +68,17 @@ export class MatStep extends _MatStep implements ErrorStateMatcher {
@Directive({
selector: '[matStepper]'
})
export class MatStepper extends _MatStepper {
export class MatStepper extends _MatStepper implements AfterContentInit {
/** The list of step headers of the steps in the stepper. */
@ViewChildren(MatStepHeader, {read: ElementRef}) _stepHeader: QueryList<ElementRef>;

/** Steps that the stepper holds. */
@ContentChildren(MatStep) _steps: QueryList<MatStep>;

ngAfterContentInit() {
// Mark the component for change detection whenever the content children query changes
this._steps.changes.pipe(takeUntil(this._destroyed)).subscribe(() => this._stateChanged());
}
}

@Component({
Expand Down

0 comments on commit 2bc0b41

Please sign in to comment.