Skip to content

Commit bea43db

Browse files
samoussbobylito
authored andcommitted
fix(range-slider): Fix slider boundaries (#2408)
* fix(connectRangeSlider): update getConfiguration for handle with previous configuration * feat(RangeSlider): add stories for disabled * refactor(connectRangeSlider): add function for compute range * refactor(connectRangeSlider): update function for compute refinement from stats * refactor(connectRangeSlider): add test for getConfiguration function * refactor(connectRangeSlider): extract condition for bounds * style(connectRangeSlider): just refactor the style of the function * refactor(connectRangeSlider): extract refine on widget * fix(connectRangeSlider): trggier refinement when refine value is equal to bounds * refactor(connectRangeSlider): rename variable for avoid confusion * refactor(Slider): always call refine with values * refactor(Slider): rename test for consistency * fix(range-slider-widget): update test according to the new refine function * refactor: reoder widget functions * docs(connectRangeSlider): update incorrect options name and signature function * refactor(connectRangeSlider): restore initial behaviour for fallback refinements values * feat(range-slider): add stories * refactor(connectRangeSlider): restore Infinity defaut refinement * refactor(range-slider): restore start override in widget * fix(connectRangeSlider): update wrong destructuring * fix(connectorRangeSlider): dont refine on bounds * feat(range-slider): add stories for min only and max only bounds * fix(connectRangeSlider): update expectations * refactor(connectRangeSlider): use destructuring in refinement * feat(connectRangeSlider): add function for test if able to refine * feat(connectRangeSlider): allow disable slider without alterate the results * refactor(connectRangeSlider): remove compute refinement from stats * fix(range-slider): restore initial behaviour * refactor(connectRangeSlider): rename variables * feat(range-slider-stories): increase step value for step stories * fix(connectRangeSlider): use matches iteratee shorthand * refactor: avoid usage of useless fat arrow * fix(connectRangeSlider): avoid refine out of range * refactor(connectRangeSlider): use isFinite instead of condittion * refactor(connectRangeSlider): use ternary instead of let bindings * chore(dev-novel): Add more meaningful values to min & max story * refactor(Slider): rename named export to RawSlider * refactor(connectRangeSlider): rename is(Min|Max)Bounds to has(Min|Max)Bound * refactor(connectRangeSlider): rename (min|max)Bounds to (min|max)Bound * refactor(connectRangeSlider): avoid using _isAbleToRefine * refactor(connectRangeSlider): use array as return value for _getCurrentRefinement * refactor(connectRangeSlider): rewrite test without mock helper * refactor(range-slider-test): reorder test for coherence with Jest output * fix(range-slider): clamp values to range * fix(conncetRangeSlider): avoid to round refinement values Fix #2386 - The boundaries were ignored in `getConfiguration` for the first render - They were ignored when a refinement was triggered and reset
1 parent 6207b93 commit bea43db

File tree

9 files changed

+798
-198
lines changed

9 files changed

+798
-198
lines changed

dev/app/init-builtin-widgets.js

Lines changed: 99 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -511,8 +511,44 @@ export default () => {
511511
templates: {
512512
header: 'Price',
513513
},
514-
max: 500,
515-
step: 10,
514+
tooltips: {
515+
format(rawValue) {
516+
return `$${Math.round(rawValue).toLocaleString()}`;
517+
},
518+
},
519+
})
520+
);
521+
})
522+
)
523+
.add(
524+
'disabled',
525+
wrapWithHits(container => {
526+
window.search.addWidget(
527+
instantsearch.widgets.rangeSlider({
528+
container,
529+
attributeName: 'price',
530+
templates: {
531+
header: 'Price',
532+
},
533+
min: 100,
534+
max: 50,
535+
tooltips: {
536+
format(rawValue) {
537+
return `$${Math.round(rawValue).toLocaleString()}`;
538+
},
539+
},
540+
})
541+
);
542+
})
543+
)
544+
.add(
545+
'with step',
546+
wrapWithHits(container => {
547+
window.search.addWidget(
548+
instantsearch.widgets.rangeSlider({
549+
container,
550+
attributeName: 'price',
551+
step: 500,
516552
tooltips: {
517553
format(rawValue) {
518554
return `$${Math.round(rawValue).toLocaleString()}`;
@@ -529,10 +565,68 @@ export default () => {
529565
instantsearch.widgets.rangeSlider({
530566
container,
531567
attributeName: 'price',
532-
min: 0,
533-
max: 500,
534568
pips: false,
535-
step: 10,
569+
tooltips: {
570+
format(rawValue) {
571+
return `$${Math.round(rawValue).toLocaleString()}`;
572+
},
573+
},
574+
})
575+
);
576+
})
577+
)
578+
.add(
579+
'with min boundaries',
580+
wrapWithHits(container => {
581+
window.search.addWidget(
582+
instantsearch.widgets.rangeSlider({
583+
container,
584+
attributeName: 'price',
585+
templates: {
586+
header: 'Price',
587+
},
588+
min: 36,
589+
tooltips: {
590+
format(rawValue) {
591+
return `$${Math.round(rawValue).toLocaleString()}`;
592+
},
593+
},
594+
})
595+
);
596+
})
597+
)
598+
.add(
599+
'with max boundaries',
600+
wrapWithHits(container => {
601+
window.search.addWidget(
602+
instantsearch.widgets.rangeSlider({
603+
container,
604+
attributeName: 'price',
605+
templates: {
606+
header: 'Price',
607+
},
608+
max: 36,
609+
tooltips: {
610+
format(rawValue) {
611+
return `$${Math.round(rawValue).toLocaleString()}`;
612+
},
613+
},
614+
})
615+
);
616+
})
617+
)
618+
.add(
619+
'with min / max boundaries',
620+
wrapWithHits(container => {
621+
window.search.addWidget(
622+
instantsearch.widgets.rangeSlider({
623+
container,
624+
attributeName: 'price',
625+
templates: {
626+
header: 'Price',
627+
},
628+
min: 10,
629+
max: 500,
536630
tooltips: {
537631
format(rawValue) {
538632
return `$${Math.round(rawValue).toLocaleString()}`;

src/components/Slider/Slider.js

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ import times from 'lodash/times';
22
import range from 'lodash/range';
33
import has from 'lodash/has';
44

5-
import PropTypes from 'prop-types';
6-
75
import React, { Component } from 'react';
6+
import PropTypes from 'prop-types';
87
import Rheostat from 'rheostat';
98
import cx from 'classnames';
109

@@ -13,7 +12,7 @@ import Pit from './Pit.js';
1312
import autoHideContainerHOC from '../../decorators/autoHideContainer.js';
1413
import headerFooterHOC from '../../decorators/headerFooter.js';
1514

16-
class Slider extends Component {
15+
export class RawSlider extends Component {
1716
static propTypes = {
1817
refine: PropTypes.func.isRequired,
1918
min: PropTypes.number.isRequired,
@@ -31,17 +30,9 @@ class Slider extends Component {
3130
return this.props.min >= this.props.max;
3231
}
3332

34-
handleChange = ({ min, max, values }) => {
35-
// when Slider is disabled, we alter `min, max` values
36-
// in order to render a "disabled state" Slider. Since we alter
37-
// theses values, Rheostat trigger a "change" event which trigger a new
38-
// search to Algolia with wrong values.
33+
handleChange = ({ values }) => {
3934
if (!this.isDisabled) {
40-
const { refine } = this.props;
41-
refine([
42-
min === values[0] ? undefined : values[0],
43-
max === values[1] ? undefined : values[1],
44-
]);
35+
this.props.refine(values);
4536
}
4637
};
4738

@@ -120,4 +111,4 @@ class Slider extends Component {
120111
}
121112
}
122113

123-
export default autoHideContainerHOC(headerFooterHOC(Slider));
114+
export default autoHideContainerHOC(headerFooterHOC(RawSlider));

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

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import React from 'react';
22
import renderer from 'react-test-renderer';
3+
import { shallow } from 'enzyme';
34

4-
import Slider from '../Slider';
5+
import Slider, { RawSlider } from '../Slider';
56

67
describe('Slider', () => {
7-
it('should render correctly', () => {
8+
it('expect to render correctly', () => {
89
const tree = renderer
910
.create(
1011
<Slider
@@ -22,7 +23,7 @@ describe('Slider', () => {
2223
expect(tree).toMatchSnapshot();
2324
});
2425

25-
it('should render without pips', () => {
26+
it('expect to render without pips', () => {
2627
const tree = renderer
2728
.create(
2829
<Slider
@@ -39,4 +40,25 @@ describe('Slider', () => {
3940
.toJSON();
4041
expect(tree).toMatchSnapshot();
4142
});
43+
44+
it('expect to call handleChange on change', () => {
45+
const props = {
46+
refine: jest.fn(),
47+
min: 0,
48+
max: 500,
49+
values: [0, 0],
50+
pips: true,
51+
step: 2,
52+
tooltips: true,
53+
shouldAutoHideContainer: false,
54+
};
55+
56+
shallow(<RawSlider {...props} />)
57+
.find('Rheostat')
58+
.simulate('change', {
59+
values: [0, 100],
60+
});
61+
62+
expect(props.refine).toHaveBeenCalledWith([0, 100]);
63+
});
4264
});

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`Slider should render correctly 1`] = `
3+
exports[`Slider expect to render correctly 1`] = `
44
<div
55
style={
66
Object {
@@ -465,7 +465,7 @@ exports[`Slider should render correctly 1`] = `
465465
</div>
466466
`;
467467

468-
exports[`Slider should render without pips 1`] = `
468+
exports[`Slider expect to render without pips 1`] = `
469469
<div
470470
style={
471471
Object {

0 commit comments

Comments
 (0)