Skip to content

Commit 5b755e1

Browse files
committed
fix(slider): not reacting to directionality changes
Fixes the slider not updating if its direction is changed dynamically. This is something I came across while testing #6641.
1 parent 70bd5fc commit 5b755e1

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/lib/slider/slider.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -958,6 +958,21 @@ describe('MdSlider without forms', () => {
958958
expect(sliderInstance.value).toBe(30);
959959
});
960960

961+
it('should re-render when the direction changes', () => {
962+
const thumbContainer =
963+
sliderNativeElement.querySelector('.mat-slider-thumb-container')! as HTMLElement;
964+
965+
dispatchClickEventSequence(sliderNativeElement, 0.7);
966+
fixture.detectChanges();
967+
968+
expect(thumbContainer.style.transform).toContain('translateX(-30%)');
969+
970+
testComponent.dir = 'rtl';
971+
fixture.detectChanges();
972+
973+
expect(thumbContainer.style.transform).toContain('translateX(-70%)');
974+
});
975+
961976
it('should increment inverted slider by 1 on right arrow pressed', () => {
962977
testComponent.invert = true;
963978
fixture.detectChanges();

src/lib/slider/slider.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import {HammerInput} from '../core';
3838
import {FocusOrigin, FocusOriginMonitor} from '../core/style/focus-origin-monitor';
3939
import {CanDisable, mixinDisabled} from '../core/common-behaviors/disabled';
4040
import {CanColor, mixinColor} from '../core/common-behaviors/color';
41+
import {Subscription} from 'rxjs/Subscription';
4142

4243

4344
/**
@@ -276,6 +277,9 @@ export class MdSlider extends _MdSliderMixinBase
276277
*/
277278
_isActive: boolean = false;
278279

280+
/** Subscription to changes in the user's direction. */
281+
private _dirChange: Subscription | undefined;
282+
279283
/**
280284
* Whether the axis of the slider is inverted.
281285
* (i.e. whether moving the thumb in the positive x or y direction decreases the slider's value).
@@ -413,10 +417,18 @@ export class MdSlider extends _MdSliderMixinBase
413417
this._focusOriginMonitor
414418
.monitor(this._elementRef.nativeElement, renderer, true)
415419
.subscribe((origin: FocusOrigin) => this._isActive = !!origin && origin !== 'keyboard');
420+
421+
if (_dir) {
422+
this._dirChange = _dir.change.subscribe(() => _changeDetectorRef.markForCheck());
423+
}
416424
}
417425

418426
ngOnDestroy() {
419427
this._focusOriginMonitor.stopMonitoring(this._elementRef.nativeElement);
428+
429+
if (this._dirChange) {
430+
this._dirChange.unsubscribe();
431+
}
420432
}
421433

422434
_onMouseenter() {

0 commit comments

Comments
 (0)