Skip to content

Commit c2c6e04

Browse files
crisbetokara
authored andcommitted
fix(stepper): switch to OnPush change detection (#7119)
1 parent 4d278dd commit c2c6e04

File tree

5 files changed

+35
-7
lines changed

5 files changed

+35
-7
lines changed

src/cdk/stepper/stepper.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ import {
2424
ViewEncapsulation,
2525
Optional,
2626
Inject,
27-
forwardRef
27+
forwardRef,
28+
ChangeDetectionStrategy,
29+
ChangeDetectorRef,
30+
OnChanges,
2831
} from '@angular/core';
2932
import {LEFT_ARROW, RIGHT_ARROW, ENTER, SPACE} from '@angular/cdk/keycodes';
3033
import {CdkStepLabel} from './step-label';
@@ -63,8 +66,9 @@ export class StepperSelectionEvent {
6366
templateUrl: 'step.html',
6467
encapsulation: ViewEncapsulation.None,
6568
preserveWhitespaces: false,
69+
changeDetection: ChangeDetectionStrategy.OnPush,
6670
})
67-
export class CdkStep {
71+
export class CdkStep implements OnChanges {
6872
/** Template for step label if it exists. */
6973
@ContentChild(CdkStepLabel) stepLabel: CdkStepLabel;
7074

@@ -74,12 +78,11 @@ export class CdkStep {
7478
/** The top level abstract control of the step. */
7579
@Input() stepControl: AbstractControl;
7680

77-
/** Whether user has seen the expanded step content or not . */
81+
/** Whether user has seen the expanded step content or not. */
7882
interacted = false;
7983

8084
/** Label of the step. */
81-
@Input()
82-
label: string;
85+
@Input() label: string;
8386

8487
@Input()
8588
get editable() { return this._editable; }
@@ -116,6 +119,12 @@ export class CdkStep {
116119
select(): void {
117120
this._stepper.selected = this;
118121
}
122+
123+
ngOnChanges() {
124+
// Since basically all inputs of the MdStep get proxied through the view down to the
125+
// underlying MdStepHeader, we have to make sure that change detection runs correctly.
126+
this._stepper._stateChanged();
127+
}
119128
}
120129

121130
@Directive({
@@ -166,7 +175,9 @@ export class CdkStepper {
166175
/** Used to track unique ID for each stepper component. */
167176
_groupId: number;
168177

169-
constructor(@Optional() private _dir: Directionality) {
178+
constructor(
179+
@Optional() private _dir: Directionality,
180+
private _changeDetectorRef: ChangeDetectorRef) {
170181
this._groupId = nextId++;
171182
}
172183

@@ -190,6 +201,11 @@ export class CdkStepper {
190201
return `mat-step-content-${this._groupId}-${i}`;
191202
}
192203

204+
/** Marks the component to be change detected. */
205+
_stateChanged() {
206+
this._changeDetectorRef.markForCheck();
207+
}
208+
193209
/** Returns position state of the step with the given index. */
194210
_getAnimationDirection(index: number): StepContentPositionState {
195211
const position = index - this._selectedIndex;
@@ -220,6 +236,7 @@ export class CdkStepper {
220236
previouslySelectedStep: stepsArray[this._selectedIndex],
221237
});
222238
this._selectedIndex = newIndex;
239+
this._stateChanged();
223240
}
224241

225242
_onKeydown(event: KeyboardEvent) {

src/lib/dialog/dialog-container.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
ChangeDetectorRef,
1818
ViewChild,
1919
ViewEncapsulation,
20+
ChangeDetectionStrategy,
2021
} from '@angular/core';
2122
import {animate, AnimationEvent, state, style, transition, trigger} from '@angular/animations';
2223
import {DOCUMENT} from '@angular/platform-browser';
@@ -51,6 +52,9 @@ export function throwMatDialogContentAlreadyAttachedError() {
5152
styleUrls: ['dialog.css'],
5253
encapsulation: ViewEncapsulation.None,
5354
preserveWhitespaces: false,
55+
// Using OnPush for dialogs caused some G3 sync issues. Disabled until we can track them down.
56+
// tslint:disable-next-line:validate-decorators
57+
changeDetection: ChangeDetectionStrategy.Default,
5458
animations: [
5559
trigger('slideDialog', [
5660
// Note: The `enter` animation doesn't transition to something like `translate3d(0, 0, 0)

src/lib/stepper/step-header.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
OnDestroy,
1717
ElementRef,
1818
Renderer2,
19+
ChangeDetectionStrategy,
1920
} from '@angular/core';
2021
import {MatStepLabel} from './step-label';
2122
import {MatStepperIntl} from './stepper-intl';
@@ -33,6 +34,7 @@ import {Subscription} from 'rxjs/Subscription';
3334
},
3435
encapsulation: ViewEncapsulation.None,
3536
preserveWhitespaces: false,
37+
changeDetection: ChangeDetectionStrategy.OnPush,
3638
})
3739
export class MatStepHeader implements OnDestroy {
3840
private _intlSubscription: Subscription;

src/lib/stepper/stepper.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
SkipSelf,
2121
ViewChildren,
2222
ViewEncapsulation,
23+
ChangeDetectionStrategy,
2324
} from '@angular/core';
2425
import {NgControl, FormGroupDirective, NgForm} from '@angular/forms';
2526
import {ErrorStateMatcher} from '@angular/material/core';
@@ -38,6 +39,7 @@ export const _MatStepper = CdkStepper;
3839
encapsulation: ViewEncapsulation.None,
3940
exportAs: 'matStep',
4041
preserveWhitespaces: false,
42+
changeDetection: ChangeDetectionStrategy.OnPush,
4143
})
4244
export class MatStep extends _MatStep implements ErrorStateMatcher {
4345
/** Content for step label given by <ng-template matStepLabel>. */
@@ -94,6 +96,7 @@ export class MatStepper extends _MatStepper {
9496
providers: [{provide: MatStepper, useExisting: MatHorizontalStepper}],
9597
encapsulation: ViewEncapsulation.None,
9698
preserveWhitespaces: false,
99+
changeDetection: ChangeDetectionStrategy.OnPush,
97100
})
98101
export class MatHorizontalStepper extends MatStepper { }
99102

@@ -119,5 +122,6 @@ export class MatHorizontalStepper extends MatStepper { }
119122
providers: [{provide: MatStepper, useExisting: MatVerticalStepper}],
120123
encapsulation: ViewEncapsulation.None,
121124
preserveWhitespaces: false,
125+
changeDetection: ChangeDetectionStrategy.OnPush,
122126
})
123127
export class MatVerticalStepper extends MatStepper { }

tslint.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@
9191
"Component": {
9292
"encapsulation": "\\.None$",
9393
"moduleId": "^module\\.id$",
94-
"preserveWhitespaces": "false$"
94+
"preserveWhitespaces": "false$",
95+
"changeDetection": "\\.OnPush$"
9596
}
9697
}, "src/+(lib|cdk)/**/!(*.spec).ts"],
9798
"require-license-banner": [

0 commit comments

Comments
 (0)