Skip to content

Commit b083280

Browse files
committed
Fix (slider): Update slider position and ticks when the min and max
values change.
1 parent add0d23 commit b083280

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
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
@@ -8,4 +8,6 @@ import {Component} from '@angular/core';
88
})
99
export class SliderDemo {
1010
demo: number;
11+
min: number = 0;
12+
max: number = 100;
1113
}

src/lib/slider/slider.ts

Lines changed: 23 additions & 4 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()
@@ -170,8 +172,7 @@ export class MdSlider implements AfterContentInit, ControlValueAccessor {
170172
// This needs to be called after content init because the value can be set to the min if the
171173
// value itself isn't set. If this happens, the control value accessor needs to be updated.
172174
this._controlValueAccessorChangeFn(this.value);
173-
this.snapThumbToValue();
174-
this._updateTickSeparation();
175+
this._initTicksAndSlider();
175176
}
176177

177178
/** TODO: internal */
@@ -265,14 +266,28 @@ export class MdSlider implements AfterContentInit, ControlValueAccessor {
265266
*/
266267
snapThumbToValue() {
267268
this.updatePercentFromValue();
268-
this._renderer.updateThumbAndFillPosition(this._percent, this._sliderDimensions.width);
269+
if (this._sliderDimensions) {
270+
this._renderer.updateThumbAndFillPosition(this._percent, this._sliderDimensions.width);
271+
}
272+
}
273+
274+
/**
275+
* Initializes the tick and slider positions.
276+
* @private
277+
*/
278+
private _initTicksAndSlider() {
279+
this.snapThumbToValue();
280+
this._updateTickSeparation();
269281
}
270282

271283
/**
272284
* Calculates the separation in pixels of tick marks. If there is no tick interval or the interval
273285
* is set to something other than a number or 'auto', nothing happens.
274286
*/
275287
private _updateTickSeparation() {
288+
if (!this._sliderDimensions) {
289+
return;
290+
}
276291
if (this._tickInterval == 'auto') {
277292
this._updateAutoTickSeparation();
278293
} else if (Number(this._tickInterval)) {
@@ -425,8 +440,10 @@ export class SliderRenderer {
425440
* Draws ticks onto the tick container.
426441
*/
427442
drawTicks(tickSeparation: number) {
443+
let sliderTrackContainer =
444+
<HTMLElement>this._sliderElement.querySelector('.md-slider-track-container');
445+
let tickContainerWidth = sliderTrackContainer.getBoundingClientRect().width;
428446
let tickContainer = <HTMLElement>this._sliderElement.querySelector('.md-slider-tick-container');
429-
let tickContainerWidth = tickContainer.getBoundingClientRect().width;
430447
// An extra element for the last tick is needed because the linear gradient cannot be told to
431448
// always draw a tick at the end of the gradient. To get around this, there is a second
432449
// container for ticks that has a single tick mark on the very right edge.
@@ -444,6 +461,8 @@ export class SliderRenderer {
444461
// separation), don't show it by decreasing the width of the tick container element.
445462
if (tickContainerWidth % tickSeparation < (tickSeparation / 2)) {
446463
tickContainer.style.width = tickContainerWidth - tickSeparation + 'px';
464+
} else {
465+
tickContainer.style.width = '';
447466
}
448467
}
449468
}

0 commit comments

Comments
 (0)