Skip to content

Commit

Permalink
fix(slider): Fix range slider pips and value 0 (#2350)
Browse files Browse the repository at this point in the history
* fix(Slider): correctly disable pips
* fix(connectRangeSlider): dont apply numeric refinement
* test(range-slider): test pips & value 0
* fix(connectRangeSlider): correctly refine when no userSet min/max

fix #2343
  • Loading branch information
Maxime Janton authored and bobylito committed Sep 20, 2017
1 parent bc947ec commit fa0dc09
Show file tree
Hide file tree
Showing 6 changed files with 255 additions and 30 deletions.
61 changes: 41 additions & 20 deletions dev/app/init-builtin-widgets.js
Original file line number Diff line number Diff line change
Expand Up @@ -487,27 +487,48 @@ export default () => {
})
);

storiesOf('RangeSlider').add(
'default',
wrapWithHits(container => {
window.search.addWidget(
instantsearch.widgets.rangeSlider({
container,
attributeName: 'price',
templates: {
header: 'Price',
},
max: 500,
step: 10,
tooltips: {
format(rawValue) {
return `$${Math.round(rawValue).toLocaleString()}`;
storiesOf('RangeSlider')
.add(
'default',
wrapWithHits(container => {
window.search.addWidget(
instantsearch.widgets.rangeSlider({
container,
attributeName: 'price',
templates: {
header: 'Price',
},
},
})
);
})
);
max: 500,
step: 10,
tooltips: {
format(rawValue) {
return `$${Math.round(rawValue).toLocaleString()}`;
},
},
})
);
})
)
.add(
'without pips',
wrapWithHits(container => {
window.search.addWidget(
instantsearch.widgets.rangeSlider({
container,
attributeName: 'price',
min: 0,
max: 500,
pips: false,
step: 10,
tooltips: {
format(rawValue) {
return `$${Math.round(rawValue).toLocaleString()}`;
},
},
})
);
})
);

storiesOf('HierarchicalMenu')
.add(
Expand Down
12 changes: 4 additions & 8 deletions src/components/Slider/Slider.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,9 @@ class Slider extends Component {

return (
<div {...props} className={className}>
{tooltips
? <div className="ais-range-slider--tooltip">
{value}
</div>
: null}
{tooltips ? (
<div className="ais-range-slider--tooltip">{value}</div>
) : null}
</div>
);
};
Expand All @@ -101,9 +99,7 @@ class Slider extends Component {

const snapPoints = this.computeSnapPoints({ min, max, step });
const pitPoints =
pips === true || pips === undefined || pips === false
? this.computeDefaultPitPoints({ min, max })
: pips;
pips === false ? [] : this.computeDefaultPitPoints({ min, max });

return (
<div className={this.isDisabled ? 'ais-range-slider--disabled' : ''}>
Expand Down
18 changes: 18 additions & 0 deletions src/components/Slider/__tests__/Slider-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,22 @@ describe('Slider', () => {
.toJSON();
expect(tree).toMatchSnapshot();
});

it('should render without pips', () => {
const tree = renderer
.create(
<Slider
refine={() => undefined}
min={0}
max={500}
values={[0, 0]}
pips={false}
step={2}
tooltips={true}
shouldAutoHideContainer={false}
/>
)
.toJSON();
expect(tree).toMatchSnapshot();
});
});
97 changes: 97 additions & 0 deletions src/components/Slider/__tests__/__snapshots__/Slider-test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -464,3 +464,100 @@ exports[`Slider should render correctly 1`] = `
</div>
</div>
`;

exports[`Slider should render without pips 1`] = `
<div
style={
Object {
"display": "",
}
}
>
<div
className="ais-root"
>
<div
className="ais-body"
>
<div
className=""
>
<div
className="rheostat rheostat-horizontal "
onClick={[Function]}
style={
Object {
"position": "relative",
}
}
>
<div
className="rheostat-background"
/>
<div
aria-disabled={false}
aria-valuemax={500}
aria-valuemin={0}
aria-valuenow={0}
className="ais-range-slider--handle rheostat-handle ais-range-slider--handle-lower"
data-handle-key={0}
onClick={undefined}
onKeyDown={[Function]}
onMouseDown={[Function]}
onTouchStart={[Function]}
role="slider"
style={
Object {
"left": "0%",
"position": "absolute",
}
}
tabIndex={0}
>
<div
className="ais-range-slider--tooltip"
>
0
</div>
</div>
<div
aria-disabled={false}
aria-valuemax={500}
aria-valuemin={0}
aria-valuenow={0}
className="ais-range-slider--handle rheostat-handle ais-range-slider--handle-upper"
data-handle-key={1}
onClick={undefined}
onKeyDown={[Function]}
onMouseDown={[Function]}
onTouchStart={[Function]}
role="slider"
style={
Object {
"left": "0%",
"position": "absolute",
}
}
tabIndex={0}
>
<div
className="ais-range-slider--tooltip"
>
0
</div>
</div>
<div
className="rheostat-progress"
style={
Object {
"left": "0%",
"width": "0%",
}
}
/>
</div>
</div>
</div>
</div>
</div>
`;
77 changes: 77 additions & 0 deletions src/connectors/range-slider/__tests__/connectRangeSlider-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,81 @@ describe('connectRangeSlider', () => {
expect(helper.search.callCount).toBe(2);
}
});

it('should add numeric refinement when refining min boundary', () => {
const rendering = sinon.stub();
const makeWidget = connectRangeSlider(rendering);

const attributeName = 'price';
const widget = makeWidget({ attributeName, min: 0, max: 500 });

const helper = jsHelper(fakeClient, '', widget.getConfiguration());
helper.search = sinon.stub();

widget.init({
helper,
state: helper.state,
createURL: () => '#',
onHistoryChange: () => {},
});

{
// first rendering
expect(helper.getNumericRefinement('price', '>=')).toEqual([0]);
expect(helper.getNumericRefinement('price', '<=')).toEqual([500]);

const renderOptions = rendering.lastCall.args[0];
const { refine } = renderOptions;
refine([10, 30]);

expect(helper.getNumericRefinement('price', '>=')).toEqual([10]);
expect(helper.getNumericRefinement('price', '<=')).toEqual([30]);
expect(helper.search.callCount).toBe(1);

refine([0, undefined]);
expect(helper.getNumericRefinement('price', '>=')).toEqual([0]);
expect(helper.getNumericRefinement('price', '<=')).toEqual(undefined);
}
});

it('should refine on boundaries when no min/max defined', () => {
const rendering = sinon.stub();
const makeWidget = connectRangeSlider(rendering);

const attributeName = 'price';
const widget = makeWidget({ attributeName });

const helper = jsHelper(fakeClient, '', widget.getConfiguration());
helper.search = sinon.stub();

widget.init({
helper,
state: helper.state,
createURL: () => '#',
onHistoryChange: () => {},
});

{
expect(helper.getNumericRefinement('price', '>=')).toEqual(undefined);
expect(helper.getNumericRefinement('price', '<=')).toEqual(undefined);

const renderOptions = rendering.lastCall.args[0];
const { refine } = renderOptions;

refine([undefined, 100]);
expect(helper.getNumericRefinement('price', '>=')).toEqual(undefined);
expect(helper.getNumericRefinement('price', '<=')).toEqual([100]);
expect(helper.search.callCount).toBe(1);

refine([0, undefined]);
expect(helper.getNumericRefinement('price', '>=')).toEqual([0]);
expect(helper.getNumericRefinement('price', '<=')).toEqual(undefined);
expect(helper.search.callCount).toBe(2);

refine([0, 100]);
expect(helper.getNumericRefinement('price', '>=')).toEqual([0]);
expect(helper.getNumericRefinement('price', '<=')).toEqual([100]);
expect(helper.search.callCount).toBe(3);
}
});
});
20 changes: 18 additions & 2 deletions src/connectors/range-slider/connectRangeSlider.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,30 @@ export default function connectRangeSlider(renderFn) {
currentValues[1] !== newValues[1]
) {
helper.clearRefinements(attributeName);
if (!bounds.min || newValues[0] > bounds.min) {

const hasMin = bounds.min !== null && bounds.min !== undefined;
const minValueChanged =
newValues[0] !== null && newValues[0] !== undefined;

if (
(hasMin && minValueChanged && bounds.min < newValues[0]) ||
(!hasMin && minValueChanged)
) {
helper.addNumericRefinement(
attributeName,
'>=',
formatToNumber(newValues[0])
);
}
if (!bounds.max || newValues[1] < bounds.max) {

const hasMax = bounds.max !== null && bounds.max !== undefined;
const maxValueChanged =
newValues[1] !== null && newValues[1] !== undefined;

if (
(hasMax && maxValueChanged && bounds.max > newValues[1]) ||
(!hasMax && maxValueChanged)
) {
helper.addNumericRefinement(
attributeName,
'<=',
Expand Down

0 comments on commit fa0dc09

Please sign in to comment.