Skip to content

Commit d5e42cb

Browse files
crisbetommalerba
authored andcommitted
chore(slider): switch to OnPush change detection (#5551)
* Switches `md-slider` to `OnPush` change detection. * Fixes a few properties not reacting to external changes. Relates to #5035.
1 parent 5cafa2b commit d5e42cb

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

src/lib/slider/slider.ts

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ import {
1616
Optional,
1717
Output,
1818
Renderer2,
19-
ViewEncapsulation
19+
ViewEncapsulation,
20+
ChangeDetectionStrategy,
21+
ChangeDetectorRef,
2022
} from '@angular/core';
2123
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from '@angular/forms';
2224
import {coerceBooleanProperty, coerceNumberProperty, HammerInput} from '../core';
@@ -118,31 +120,33 @@ export const _MdSliderMixinBase = mixinDisabled(MdSliderBase);
118120
styleUrls: ['slider.css'],
119121
inputs: ['disabled'],
120122
encapsulation: ViewEncapsulation.None,
123+
changeDetection: ChangeDetectionStrategy.OnPush,
121124
})
122125
export class MdSlider extends _MdSliderMixinBase
123126
implements ControlValueAccessor, OnDestroy, CanDisable {
124127
/** Whether the slider is inverted. */
125128
@Input()
126129
get invert() { return this._invert; }
127-
set invert(value: any) { this._invert = coerceBooleanProperty(value); }
130+
set invert(value: any) {
131+
this._invert = coerceBooleanProperty(value);
132+
}
128133
private _invert = false;
129134

130135
/** The maximum value that the slider can have. */
131136
@Input()
132-
get max() {
133-
return this._max;
134-
}
137+
get max() { return this._max; }
135138
set max(v: number) {
136139
this._max = coerceNumberProperty(v, this._max);
137140
this._percent = this._calculatePercentage(this._value);
141+
142+
// Since this also modifies the percentage, we need to let the change detection know.
143+
this._changeDetectorRef.markForCheck();
138144
}
139145
private _max: number = 100;
140146

141147
/** The minimum value that the slider can have. */
142148
@Input()
143-
get min() {
144-
return this._min;
145-
}
149+
get min() { return this._min; }
146150
set min(v: number) {
147151
this._min = coerceNumberProperty(v, this._min);
148152

@@ -151,6 +155,9 @@ export class MdSlider extends _MdSliderMixinBase
151155
this.value = this._min;
152156
}
153157
this._percent = this._calculatePercentage(this._value);
158+
159+
// Since this also modifies the percentage, we need to let the change detection know.
160+
this._changeDetectorRef.markForCheck();
154161
}
155162
private _min: number = 0;
156163

@@ -163,6 +170,9 @@ export class MdSlider extends _MdSliderMixinBase
163170
if (this._step % 1 !== 0) {
164171
this._roundLabelTo = this._step.toString().split('.').pop()!.length;
165172
}
173+
174+
// Since this could modify the label, we need to notify the change detection.
175+
this._changeDetectorRef.markForCheck();
166176
}
167177
private _step: number = 1;
168178

@@ -209,15 +219,22 @@ export class MdSlider extends _MdSliderMixinBase
209219
return this._value;
210220
}
211221
set value(v: number | null) {
212-
this._value = coerceNumberProperty(v, this._value || 0);
213-
this._percent = this._calculatePercentage(this._value);
222+
if (v !== this._value) {
223+
this._value = coerceNumberProperty(v, this._value || 0);
224+
this._percent = this._calculatePercentage(this._value);
225+
226+
// Since this also modifies the percentage, we need to let the change detection know.
227+
this._changeDetectorRef.markForCheck();
228+
}
214229
}
215230
private _value: number | null = null;
216231

217232
/** Whether the slider is vertical. */
218233
@Input()
219234
get vertical() { return this._vertical; }
220-
set vertical(value: any) { this._vertical = coerceBooleanProperty(value); }
235+
set vertical(value: any) {
236+
this._vertical = coerceBooleanProperty(value);
237+
}
221238
private _vertical = false;
222239

223240
@Input() color: 'primary' | 'accent' | 'warn' = 'accent';
@@ -392,9 +409,11 @@ export class MdSlider extends _MdSliderMixinBase
392409

393410
constructor(renderer: Renderer2, private _elementRef: ElementRef,
394411
private _focusOriginMonitor: FocusOriginMonitor,
412+
private _changeDetectorRef: ChangeDetectorRef,
395413
@Optional() private _dir: Directionality) {
396414
super();
397-
this._focusOriginMonitor.monitor(this._elementRef.nativeElement, renderer, true)
415+
this._focusOriginMonitor
416+
.monitor(this._elementRef.nativeElement, renderer, true)
398417
.subscribe((origin: FocusOrigin) => this._isActive = !!origin && origin !== 'keyboard');
399418
this._renderer = new SliderRenderer(this._elementRef);
400419
}

0 commit comments

Comments
 (0)