Skip to content

Commit 2bc0b41

Browse files
actra-gschusterjelbourn
authored andcommitted
fix(stepper): update state when steps change (#8398)
1 parent f6bd9b0 commit 2bc0b41

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

src/cdk/stepper/stepper.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@ import {
2828
ChangeDetectionStrategy,
2929
ChangeDetectorRef,
3030
OnChanges,
31+
OnDestroy
3132
} from '@angular/core';
3233
import {LEFT_ARROW, RIGHT_ARROW, ENTER, SPACE} from '@angular/cdk/keycodes';
3334
import {CdkStepLabel} from './step-label';
3435
import {coerceBooleanProperty} from '@angular/cdk/coercion';
3536
import {AbstractControl} from '@angular/forms';
3637
import {Direction, Directionality} from '@angular/cdk/bidi';
38+
import {Subject} from 'rxjs/Subject';
3739

3840
/** Used to generate unique ID for each stepper component. */
3941
let nextId = 0;
@@ -132,7 +134,10 @@ export class CdkStep implements OnChanges {
132134
selector: '[cdkStepper]',
133135
exportAs: 'cdkStepper',
134136
})
135-
export class CdkStepper {
137+
export class CdkStepper implements OnDestroy {
138+
/** Emits when the component is destroyed. */
139+
protected _destroyed = new Subject<void>();
140+
136141
/** The list of step components that the stepper is holding. */
137142
@ContentChildren(CdkStep) _steps: QueryList<CdkStep>;
138143

@@ -186,6 +191,11 @@ export class CdkStepper {
186191
this._groupId = nextId++;
187192
}
188193

194+
ngOnDestroy() {
195+
this._destroyed.next();
196+
this._destroyed.complete();
197+
}
198+
189199
/** Selects and focuses the next step in list. */
190200
next(): void {
191201
this.selectedIndex = Math.min(this._selectedIndex + 1, this._steps.length - 1);

src/lib/stepper/stepper.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import {animate, state, style, transition, trigger} from '@angular/animations';
1010
import {CdkStep, CdkStepper} from '@angular/cdk/stepper';
1111
import {
12+
AfterContentInit,
1213
Component,
1314
ContentChild,
1415
ContentChildren,
@@ -26,6 +27,7 @@ import {FormControl, FormGroupDirective, NgForm} from '@angular/forms';
2627
import {ErrorStateMatcher} from '@angular/material/core';
2728
import {MatStepHeader} from './step-header';
2829
import {MatStepLabel} from './step-label';
30+
import {takeUntil} from 'rxjs/operators/takeUntil';
2931

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

7375
/** Steps that the stepper holds. */
7476
@ContentChildren(MatStep) _steps: QueryList<MatStep>;
77+
78+
ngAfterContentInit() {
79+
// Mark the component for change detection whenever the content children query changes
80+
this._steps.changes.pipe(takeUntil(this._destroyed)).subscribe(() => this._stateChanged());
81+
}
7582
}
7683

7784
@Component({

0 commit comments

Comments
 (0)