-
Notifications
You must be signed in to change notification settings - Fork 6.8k
fix(slider): constraints value between min and max #1567
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
a53a129
da2af1e
c9ef34c
920c875
ffbc295
1ad457b
43786b8
35f0044
333b11e
6f322cf
c0e6f83
b56f520
c6b4c22
9422793
dbfd235
2e651e7
95b2a34
8e16992
2ebb46f
f0e3e26
fdc0ac8
65401a3
9eaf7e4
c63b9f4
f10ac7c
8e7f80d
8c1a791
e33360d
86758f3
f43bacf
4570998
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,9 @@ describe('MdSlider', () => { | |
SliderWithSetTickInterval, | ||
SliderWithThumbLabel, | ||
SliderWithTwoWayBinding, | ||
SliderWithValueSmallerThanMin, | ||
SliderWithValueGreaterThanMax, | ||
SliderWithValueBetweenMinAndMax | ||
], | ||
providers: [ | ||
{provide: HAMMER_GESTURE_CONFIG, useFactory: () => { | ||
|
@@ -621,6 +624,66 @@ describe('MdSlider', () => { | |
|
||
// TODO: Add tests for ng-pristine, ng-touched, ng-invalid. | ||
}); | ||
|
||
describe('slider with set min and max and a value smaller than min', () => { | ||
let fixture: ComponentFixture<SliderWithValueSmallerThanMin>; | ||
let sliderDebugElement: DebugElement; | ||
let sliderInstance: MdSlider; | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(SliderWithValueSmallerThanMin); | ||
fixture.detectChanges(); | ||
|
||
sliderDebugElement = fixture.debugElement.query(By.directive(MdSlider)); | ||
sliderInstance = sliderDebugElement.injector.get(MdSlider); | ||
}); | ||
|
||
it('should set the value to the min value', () => { | ||
expect(sliderInstance.value).toBe(4); | ||
expect(sliderInstance.min).toBe(4); | ||
expect(sliderInstance.max).toBe(6); | ||
}); | ||
}); | ||
|
||
describe('slider with set min and max and a value greater than max', () => { | ||
let fixture: ComponentFixture<SliderWithValueGreaterThanMax>; | ||
let sliderDebugElement: DebugElement; | ||
let sliderInstance: MdSlider; | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(SliderWithValueGreaterThanMax); | ||
fixture.detectChanges(); | ||
|
||
sliderDebugElement = fixture.debugElement.query(By.directive(MdSlider)); | ||
sliderInstance = sliderDebugElement.injector.get(MdSlider); | ||
}); | ||
|
||
it('should set the value to the max value', () => { | ||
expect(sliderInstance.value).toBe(6); | ||
expect(sliderInstance.min).toBe(4); | ||
expect(sliderInstance.max).toBe(6); | ||
}); | ||
}); | ||
|
||
describe('slider with set min and max and a value between min and max', () => { | ||
let fixture: ComponentFixture<SliderWithValueBetweenMinAndMax>; | ||
let sliderDebugElement: DebugElement; | ||
let sliderInstance: MdSlider; | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(SliderWithValueBetweenMinAndMax); | ||
fixture.detectChanges(); | ||
|
||
sliderDebugElement = fixture.debugElement.query(By.directive(MdSlider)); | ||
sliderInstance = sliderDebugElement.injector.get(MdSlider); | ||
}); | ||
|
||
it('should set the value to the max value', () => { | ||
expect(sliderInstance.value).toBe(5); | ||
expect(sliderInstance.min).toBe(4); | ||
expect(sliderInstance.max).toBe(6); | ||
}); | ||
}); | ||
}); | ||
|
||
// The transition has to be removed in order to test the updated positions without setTimeout. | ||
|
@@ -678,6 +741,27 @@ class SliderWithTwoWayBinding { | |
control = new FormControl(''); | ||
} | ||
|
||
@Component({ | ||
template: `<md-slider value="3" min="4" max="6"></md-slider>`, | ||
styles: [noTransitionStyle], | ||
encapsulation: ViewEncapsulation.None | ||
}) | ||
class SliderWithValueSmallerThanMin { } | ||
|
||
@Component({ | ||
template: `<md-slider value="7" min="4" max="6"></md-slider>`, | ||
styles: [noTransitionStyle], | ||
encapsulation: ViewEncapsulation.None | ||
}) | ||
class SliderWithValueGreaterThanMax { } | ||
|
||
@Component({ | ||
template: `<md-slider value="5" min="4" max="6"></md-slider>`, | ||
styles: [noTransitionStyle], | ||
encapsulation: ViewEncapsulation.None | ||
}) | ||
class SliderWithValueBetweenMinAndMax { } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need this component any more. |
||
|
||
/** | ||
* Dispatches a click event from an element. | ||
* Note: The mouse event truncates the position for the click. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -151,7 +151,7 @@ export class MdSlider implements AfterContentInit, ControlValueAccessor { | |
return; | ||
} | ||
|
||
this._value = Number(v); | ||
this._value = Math.min(Math.max(Number(v), this.min), this.max); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm thinking that it makes more sense to clamp the rendered position of the thumb than the actual value. Having an invalid value is a state that the control should allow, where the user will likely want to show a validation error. An easy way to do this would be to have a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jelbourn I've created a new patch in this idea. |
||
this._isInitialized = true; | ||
this._controlValueAccessorChangeFn(this._value); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This behavior is already captured by existing unit tests in the "slider with set min and max" section.