Skip to content

Commit e468234

Browse files
committed
chore(slider): switch to OnPush change detection
* Switches `md-slider` to `OnPush` change detection. * Fixes a few properties not reacting to external changes. Relates to #5035.
1 parent ac3e21a commit e468234

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

src/lib/slider/slider.ts

Lines changed: 28 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,32 @@ 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+
this._changeDetectorRef.markForCheck();
133+
}
128134
private _invert = false;
129135

130136
/** The maximum value that the slider can have. */
131137
@Input()
132-
get max() {
133-
return this._max;
134-
}
138+
get max() { return this._max; }
135139
set max(v: number) {
136140
this._max = coerceNumberProperty(v, this._max);
137141
this._percent = this._calculatePercentage(this._value);
142+
this._changeDetectorRef.markForCheck();
138143
}
139144
private _max: number = 100;
140145

141146
/** The minimum value that the slider can have. */
142147
@Input()
143-
get min() {
144-
return this._min;
145-
}
148+
get min() { return this._min; }
146149
set min(v: number) {
147150
this._min = coerceNumberProperty(v, this._min);
148151

@@ -151,6 +154,7 @@ export class MdSlider extends _MdSliderMixinBase
151154
this.value = this._min;
152155
}
153156
this._percent = this._calculatePercentage(this._value);
157+
this._changeDetectorRef.markForCheck();
154158
}
155159
private _min: number = 0;
156160

@@ -163,6 +167,8 @@ export class MdSlider extends _MdSliderMixinBase
163167
if (this._step % 1 !== 0) {
164168
this._roundLabelTo = this._step.toString().split('.').pop()!.length;
165169
}
170+
171+
this._changeDetectorRef.markForCheck();
166172
}
167173
private _step: number = 1;
168174

@@ -191,6 +197,8 @@ export class MdSlider extends _MdSliderMixinBase
191197
} else {
192198
this._tickInterval = 0;
193199
}
200+
201+
this._changeDetectorRef.markForCheck();
194202
}
195203
private _tickInterval: 'auto' | number = 0;
196204

@@ -209,15 +217,21 @@ export class MdSlider extends _MdSliderMixinBase
209217
return this._value;
210218
}
211219
set value(v: number | null) {
212-
this._value = coerceNumberProperty(v, this._value || 0);
213-
this._percent = this._calculatePercentage(this._value);
220+
if (v !== this._value) {
221+
this._value = coerceNumberProperty(v, this._value || 0);
222+
this._percent = this._calculatePercentage(this._value);
223+
this._changeDetectorRef.markForCheck();
224+
}
214225
}
215226
private _value: number | null = null;
216227

217228
/** Whether the slider is vertical. */
218229
@Input()
219230
get vertical() { return this._vertical; }
220-
set vertical(value: any) { this._vertical = coerceBooleanProperty(value); }
231+
set vertical(value: any) {
232+
this._vertical = coerceBooleanProperty(value);
233+
this._changeDetectorRef.markForCheck();
234+
}
221235
private _vertical = false;
222236

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

393407
constructor(renderer: Renderer2, private _elementRef: ElementRef,
394408
private _focusOriginMonitor: FocusOriginMonitor,
409+
private _changeDetectorRef: ChangeDetectorRef,
395410
@Optional() private _dir: Directionality) {
396411
super();
397-
this._focusOriginMonitor.monitor(this._elementRef.nativeElement, renderer, true)
412+
this._focusOriginMonitor
413+
.monitor(this._elementRef.nativeElement, renderer, true)
398414
.subscribe((origin: FocusOrigin) => this._isActive = !!origin && origin !== 'keyboard');
399415
this._renderer = new SliderRenderer(this._elementRef);
400416
}

0 commit comments

Comments
 (0)