Skip to content

Commit 511fdfd

Browse files
Maxime Jantonvvo
authored andcommitted
fix(Slider): display disabled slider when min === max (#2041)
fixes #2037
1 parent 7b26d75 commit 511fdfd

File tree

3 files changed

+41
-8
lines changed

3 files changed

+41
-8
lines changed

src/components/Slider/Slider.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,16 @@ class Slider extends React.Component {
2323
}
2424

2525
render() {
26-
if (this.props.range.min === this.props.range.max) {
27-
// There's no need to try to render the Slider, it will not be usable
28-
// and will throw
29-
return null;
30-
}
26+
// display a `disabled` state of the `NoUiSlider` when range.min === range.max
27+
const {range: {min, max}} = this.props;
28+
const isDisabled = min === max;
29+
30+
// when range.min === range.max, we only want to add a little more to the max
31+
// to display the same value in the UI, but the `NoUiSlider` wont
32+
// throw an error since they are not the same value.
33+
const range = isDisabled
34+
? {min, max: min + 0.0001}
35+
: {min, max};
3136

3237
// setup pips
3338
let pips;
@@ -48,12 +53,14 @@ class Slider extends React.Component {
4853
<Nouislider
4954
// NoUiSlider also accepts a cssClasses prop, but we don't want to
5055
// provide one.
51-
{...omit(this.props, ['cssClasses'])}
56+
{...omit(this.props, ['cssClasses', 'range'])}
5257
animate={false}
5358
behaviour={'snap'}
5459
connect
5560
cssPrefix={cssPrefix}
5661
onChange={this.handleChange}
62+
range={range}
63+
disabled={isDisabled}
5764
pips={pips}
5865
/>
5966
);

src/components/Slider/__tests__/Slider-test.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,27 @@ describe('Slider', () => {
4747
);
4848
});
4949

50-
it('should not render anything when ranges are equal', () => {
50+
it('should render <NouisLider disabled="true" /> when ranges are equal', () => {
5151
props.range.min = props.range.max = 8;
5252
const out = render();
53-
expect(out).toEqual(null);
53+
expect(out).toEqualJSX(
54+
<Nouislider
55+
animate={ false }
56+
behaviour="snap"
57+
connect
58+
cssPrefix="ais-range-slider--"
59+
format={ {to: () => {}, from: () => {}} }
60+
onChange={ () => {} }
61+
pips={ {
62+
density: 3,
63+
mode: 'positions',
64+
stepped: true,
65+
values: [0, 50, 100],
66+
} }
67+
range={ {min: props.range.min, max: props.range.min + 0.0001} }
68+
disabled
69+
/>
70+
);
5471
});
5572

5673
function render() {

src/css/default/_range-slider.scss

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ $range-slider-handle-bg: $white;
1616
height: $range-slider-target-height;
1717
margin-top: 2em;
1818
margin-bottom: 2em;
19+
20+
&[disabled="true"] {
21+
cursor: not-allowed;
22+
23+
.ais-range-slider--handle {
24+
border-color: $range-slider-marker-bg;
25+
cursor: not-allowed;
26+
}
27+
}
1928
}
2029

2130
@include element(base) {

0 commit comments

Comments
 (0)