Skip to content

Commit a736791

Browse files
committed
fix(stepper): switch to OnPush change detection
* Adds OnPush change detection to all stepper-related components. * Enables the tslint rule that requires OnPush for all components.
1 parent df808b8 commit a736791

File tree

5 files changed

+43
-12
lines changed

5 files changed

+43
-12
lines changed

src/cdk/stepper/stepper.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ import {
2424
ViewEncapsulation,
2525
Optional,
2626
Inject,
27-
forwardRef
27+
forwardRef,
28+
ChangeDetectionStrategy,
29+
ChangeDetectorRef,
2830
} from '@angular/core';
2931
import {LEFT_ARROW, RIGHT_ARROW, ENTER, SPACE} from '@angular/cdk/keycodes';
3032
import {CdkStepLabel} from './step-label';
@@ -60,7 +62,8 @@ export class StepperSelectionEvent {
6062
moduleId: module.id,
6163
selector: 'cdk-step',
6264
templateUrl: 'step.html',
63-
encapsulation: ViewEncapsulation.None
65+
encapsulation: ViewEncapsulation.None,
66+
changeDetection: ChangeDetectionStrategy.OnPush,
6467
})
6568
export class CdkStep {
6669
/** Template for step label if it exists. */
@@ -72,12 +75,11 @@ export class CdkStep {
7275
/** The top level abstract control of the step. */
7376
@Input() stepControl: AbstractControl;
7477

75-
/** Whether user has seen the expanded step content or not . */
78+
/** Whether user has seen the expanded step content or not. */
7679
interacted = false;
7780

7881
/** Label of the step. */
79-
@Input()
80-
label: string;
82+
@Input() label: string;
8183

8284
@Input()
8385
get editable() { return this._editable; }
@@ -163,7 +165,9 @@ export class CdkStepper {
163165
/** Used to track unique ID for each stepper component. */
164166
_groupId: number;
165167

166-
constructor(@Optional() private _dir: Directionality) {
168+
constructor(
169+
@Optional() private _dir: Directionality,
170+
private _changeDetectorRef: ChangeDetectorRef) {
167171
this._groupId = nextId++;
168172
}
169173

@@ -217,6 +221,7 @@ export class CdkStepper {
217221
previouslySelectedStep: stepsArray[this._selectedIndex],
218222
});
219223
this._selectedIndex = newIndex;
224+
this._changeDetectorRef.markForCheck();
220225
}
221226

222227
_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';
@@ -50,6 +51,9 @@ export function throwMdDialogContentAlreadyAttachedError() {
5051
templateUrl: 'dialog-container.html',
5152
styleUrls: ['dialog.css'],
5253
encapsulation: ViewEncapsulation.None,
54+
// Using OnPush for dialogs caused some G3 sync issues. Disabled until we can track them down.
55+
// tslint:disable-next-line:validate-decorators
56+
changeDetection: ChangeDetectionStrategy.Default,
5357
animations: [
5458
trigger('slideDialog', [
5559
// Note: The `enter` animation doesn't transition to something like `translate3d(0, 0, 0)

src/lib/stepper/step-header.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@
77
*/
88

99
import {coerceBooleanProperty, coerceNumberProperty} from '@angular/cdk/coercion';
10-
import {Component, Input, ViewEncapsulation} from '@angular/core';
10+
import {
11+
Component,
12+
Input,
13+
ViewEncapsulation,
14+
ChangeDetectionStrategy,
15+
ChangeDetectorRef,
16+
} from '@angular/core';
1117
import {MATERIAL_COMPATIBILITY_MODE} from '@angular/material/core';
1218
import {MdStepLabel} from './step-label';
1319

@@ -22,14 +28,23 @@ import {MdStepLabel} from './step-label';
2228
'role': 'tab',
2329
},
2430
encapsulation: ViewEncapsulation.None,
31+
changeDetection: ChangeDetectionStrategy.OnPush,
2532
providers: [{provide: MATERIAL_COMPATIBILITY_MODE, useValue: false}],
2633
})
2734
export class MdStepHeader {
2835
/** Icon for the given step. */
2936
@Input() icon: string;
3037

3138
/** Label of the given step. */
32-
@Input() label: MdStepLabel | string;
39+
@Input()
40+
get label() { return this._label; }
41+
set label(newLabel: MdStepLabel | string) {
42+
if (newLabel !== this._label) {
43+
this._label = newLabel;
44+
this._changeDetectorRef.markForCheck();
45+
}
46+
}
47+
private _label: MdStepLabel | string;
3348

3449
/** Index of the given step. */
3550
@Input()
@@ -63,6 +78,8 @@ export class MdStepHeader {
6378
}
6479
private _optional: boolean;
6580

81+
constructor(private _changeDetectorRef: ChangeDetectorRef) {}
82+
6683
/** Returns string label of given step if it is a text label. */
6784
_stringLabel(): string | null {
6885
return this.label instanceof MdStepLabel ? null : this.label;

src/lib/stepper/stepper.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
SkipSelf,
2222
ViewChildren,
2323
ViewEncapsulation,
24+
ChangeDetectionStrategy,
2425
} from '@angular/core';
2526
import {FormControl, FormGroupDirective, NgForm} from '@angular/forms';
2627
import {
@@ -41,7 +42,8 @@ export const _MdStepper = CdkStepper;
4142
selector: 'md-step, mat-step',
4243
templateUrl: 'step.html',
4344
providers: [{provide: MD_ERROR_GLOBAL_OPTIONS, useExisting: MdStep}],
44-
encapsulation: ViewEncapsulation.None
45+
encapsulation: ViewEncapsulation.None,
46+
changeDetection: ChangeDetectionStrategy.OnPush,
4547
})
4648
export class MdStep extends _MdStep implements ErrorOptions {
4749
/** Content for step label given by <ng-template matStepLabel> or <ng-template mdStepLabel>. */
@@ -104,7 +106,8 @@ export class MdStepper extends _MdStepper {
104106
])
105107
],
106108
providers: [{provide: MdStepper, useExisting: MdHorizontalStepper}],
107-
encapsulation: ViewEncapsulation.None
109+
encapsulation: ViewEncapsulation.None,
110+
changeDetection: ChangeDetectionStrategy.OnPush,
108111
})
109112
export class MdHorizontalStepper extends MdStepper { }
110113

@@ -127,6 +130,7 @@ export class MdHorizontalStepper extends MdStepper { }
127130
])
128131
],
129132
providers: [{provide: MdStepper, useExisting: MdVerticalStepper}],
130-
encapsulation: ViewEncapsulation.None
133+
encapsulation: ViewEncapsulation.None,
134+
changeDetection: ChangeDetectionStrategy.OnPush,
131135
})
132136
export class MdVerticalStepper extends MdStepper { }

tslint.json

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

0 commit comments

Comments
 (0)