Skip to content

Commit 9e896fb

Browse files
committed
Fix (slider): Update slider position and ticks when the min and max
values change.
1 parent 8e7f80d commit 9e896fb

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

src/demo-app/slider/slider-demo.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ <h1>Default Slider</h1>
33
{{slidey.value}}
44

55
<h1>Slider with Min and Max</h1>
6-
<md-slider min="5" max="7" #slider2></md-slider>
6+
<input [(ngModel)]="min">
7+
<md-slider [min]="min" [max]="max" tick-interval="5" #slider2></md-slider>
78
{{slider2.value}}
9+
<input [(ngModel)]="max">
810

911
<h1>Disabled Slider</h1>
1012
<md-slider disabled #slider3></md-slider>

src/demo-app/slider/slider-demo.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ import {Component} from '@angular/core';
99
export class SliderDemo {
1010
demo: number;
1111
val: number = 50;
12+
min: number = 0;
13+
max: number = 100;
1214
}

src/lib/slider/slider.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ export class MdSlider implements AfterContentInit, ControlValueAccessor {
126126
if (!this._isInitialized) {
127127
this.value = this._min;
128128
}
129+
this._initTicksAndSlider();
129130
}
130131

131132
@Input()
@@ -136,6 +137,7 @@ export class MdSlider implements AfterContentInit, ControlValueAccessor {
136137

137138
set max(v: number) {
138139
this._max = Number(v);
140+
this._initTicksAndSlider();
139141
}
140142

141143
@Input()
@@ -171,8 +173,7 @@ export class MdSlider implements AfterContentInit, ControlValueAccessor {
171173
// This needs to be called after content init because the value can be set to the min if the
172174
// value itself isn't set. If this happens, the control value accessor needs to be updated.
173175
this._controlValueAccessorChangeFn(this.value);
174-
this.snapThumbToValue();
175-
this._updateTickSeparation();
176+
this._initTicksAndSlider();
176177
}
177178

178179
/** TODO: internal */
@@ -271,11 +272,23 @@ export class MdSlider implements AfterContentInit, ControlValueAccessor {
271272
}
272273
}
273274

275+
/**
276+
* Initializes the tick and slider positions.
277+
* @private
278+
*/
279+
private _initTicksAndSlider() {
280+
this.snapThumbToValue();
281+
this._updateTickSeparation();
282+
}
283+
274284
/**
275285
* Calculates the separation in pixels of tick marks. If there is no tick interval or the interval
276286
* is set to something other than a number or 'auto', nothing happens.
277287
*/
278288
private _updateTickSeparation() {
289+
if (!this._sliderDimensions) {
290+
return;
291+
}
279292
if (this._tickInterval == 'auto') {
280293
this._updateAutoTickSeparation();
281294
} else if (Number(this._tickInterval)) {
@@ -428,8 +441,10 @@ export class SliderRenderer {
428441
* Draws ticks onto the tick container.
429442
*/
430443
drawTicks(tickSeparation: number) {
444+
let sliderTrackContainer =
445+
<HTMLElement>this._sliderElement.querySelector('.md-slider-track-container');
446+
let tickContainerWidth = sliderTrackContainer.getBoundingClientRect().width;
431447
let tickContainer = <HTMLElement>this._sliderElement.querySelector('.md-slider-tick-container');
432-
let tickContainerWidth = tickContainer.getBoundingClientRect().width;
433448
// An extra element for the last tick is needed because the linear gradient cannot be told to
434449
// always draw a tick at the end of the gradient. To get around this, there is a second
435450
// container for ticks that has a single tick mark on the very right edge.
@@ -447,6 +462,8 @@ export class SliderRenderer {
447462
// separation), don't show it by decreasing the width of the tick container element.
448463
if (tickContainerWidth % tickSeparation < (tickSeparation / 2)) {
449464
tickContainer.style.width = tickContainerWidth - tickSeparation + 'px';
465+
} else {
466+
tickContainer.style.width = '';
450467
}
451468
}
452469
}

0 commit comments

Comments
 (0)