Skip to content

Commit 987897c

Browse files
crisbetokara
authored andcommitted
fix(slider): round decimals in the thumb label (#2527)
Fixes #2511
1 parent d445007 commit 987897c

File tree

3 files changed

+50
-4
lines changed

3 files changed

+50
-4
lines changed

src/lib/slider/slider.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<div class="md-slider-thumb-container" [ngStyle]="thumbContainerStyles">
1010
<div class="md-slider-thumb"></div>
1111
<div class="md-slider-thumb-label">
12-
<span class="md-slider-thumb-label-text">{{value}}</span>
12+
<span class="md-slider-thumb-label-text">{{displayValue}}</span>
1313
</div>
1414
</div>
1515
</div>

src/lib/slider/slider.spec.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,29 @@ describe('MdSlider', () => {
431431
// The closest snap is at the end of the slider.
432432
expect(trackFillElement.style.transform).toContain('scaleX(1)');
433433
});
434+
435+
it('should round the value inside the label based on the provided step', () => {
436+
let testStep = (step: number, expected: string) => {
437+
fixture.componentInstance.step = step;
438+
fixture.detectChanges();
439+
dispatchSlideEventSequence(sliderNativeElement, 0, 0.333333, gestureConfig);
440+
expect(sliderDebugElement.componentInstance.displayValue.toString()).toBe(expected);
441+
};
442+
443+
testStep(1, '33');
444+
testStep(0.1, '33.3');
445+
testStep(0.01, '33.33');
446+
testStep(0.001, '33.333');
447+
});
448+
449+
it('should not add decimals to the value if it is a whole number', () => {
450+
fixture.componentInstance.step = 0.1;
451+
fixture.detectChanges();
452+
453+
dispatchSlideEventSequence(sliderNativeElement, 0, 1, gestureConfig);
454+
455+
expect(sliderDebugElement.componentInstance.displayValue).toBe(100);
456+
});
434457
});
435458

436459
describe('slider with auto ticks', () => {
@@ -1162,10 +1185,12 @@ class SliderWithMinAndMax {
11621185
class SliderWithValue { }
11631186

11641187
@Component({
1165-
template: `<md-slider step="25"></md-slider>`,
1188+
template: `<md-slider [step]="step"></md-slider>`,
11661189
styles: [styles],
11671190
})
1168-
class SliderWithStep { }
1191+
class SliderWithStep {
1192+
step = 25;
1193+
}
11691194

11701195
@Component({
11711196
template: `<md-slider step="5" tickInterval="auto"></md-slider>`,

src/lib/slider/slider.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,21 @@ export class MdSlider implements ControlValueAccessor {
147147
*/
148148
_isActive: boolean = false;
149149

150+
/** Decimal places to round to, based on the step amount. */
151+
private _roundLabelTo: number;
152+
150153
private _step: number = 1;
151154

152155
/** The values at which the thumb will snap. */
153156
@Input()
154157
get step() { return this._step; }
155-
set step(v) { this._step = coerceNumberProperty(v, this._step); }
158+
set step(v) {
159+
this._step = coerceNumberProperty(v, this._step);
160+
161+
if (this._step % 1 !== 0) {
162+
this._roundLabelTo = this._step.toString().split('.').pop().length;
163+
}
164+
}
156165

157166
private _tickInterval: 'auto' | number = 0;
158167

@@ -238,6 +247,18 @@ export class MdSlider implements ControlValueAccessor {
238247
set vertical(value: any) { this._vertical = coerceBooleanProperty(value); }
239248
private _vertical = false;
240249

250+
/** The value to be used for display purposes. */
251+
get displayValue(): string|number {
252+
// Note that this could be improved further by rounding something like 0.999 to 1 or
253+
// 0.899 to 0.9, however it is very performance sensitive, because it gets called on
254+
// every change detection cycle.
255+
if (this._roundLabelTo && this.value % 1 !== 0) {
256+
return this.value.toFixed(this._roundLabelTo);
257+
}
258+
259+
return this.value;
260+
}
261+
241262
/**
242263
* Whether the axis of the slider is inverted.
243264
* (i.e. whether moving the thumb in the positive x or y direction decreases the slider's value).

0 commit comments

Comments
 (0)