From 08917b6af33f1c82f8288653ae7a59cc68c295a8 Mon Sep 17 00:00:00 2001 From: Samuel Vaillant Date: Mon, 5 Feb 2018 12:45:44 +0100 Subject: [PATCH] fix(connectRange): use the same behaviour for currentRefinement in getMetadata (#923) --- .../src/connectors/connectRange.js | 25 ++++++++++++------- .../src/connectors/connectRange.test.js | 8 +++--- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/packages/react-instantsearch/src/connectors/connectRange.js b/packages/react-instantsearch/src/connectors/connectRange.js index 045d8ff933..95f235dea4 100644 --- a/packages/react-instantsearch/src/connectors/connectRange.js +++ b/packages/react-instantsearch/src/connectors/connectRange.js @@ -107,6 +107,13 @@ function getCurrentRefinement(props, searchState, currentRange, context) { return refinement; } +function getCurrentRefinementWithRange(refinement, range) { + return { + min: refinement.min !== undefined ? refinement.min : range.min, + max: refinement.max !== undefined ? refinement.max : range.max, + }; +} + function nextValueForRefinement(hasBound, isReset, range, value) { let next; if (!hasBound && range === value) { @@ -217,7 +224,7 @@ export default createConnector({ max: rangeMax, }; - const { min: valueMin, max: valueMax } = getCurrentRefinement( + const currentRefinement = getCurrentRefinement( props, searchState, this._currentRange, @@ -228,10 +235,10 @@ export default createConnector({ min: rangeMin, max: rangeMax, canRefine: count.length > 0, - currentRefinement: { - min: valueMin === undefined ? rangeMin : valueMin, - max: valueMax === undefined ? rangeMax : valueMax, - }, + currentRefinement: getCurrentRefinementWithRange( + currentRefinement, + this._currentRange + ), count, precision, }; @@ -300,10 +307,10 @@ export default createConnector({ attributeName: props.attributeName, value: nextState => refine(props, nextState, {}, this._currentRange, this.context), - currentRefinement: { - min: minValue, - max: maxValue, - }, + currentRefinement: getCurrentRefinementWithRange( + { min: minValue, max: maxValue }, + { min: minRange, max: maxRange } + ), }); } diff --git a/packages/react-instantsearch/src/connectors/connectRange.test.js b/packages/react-instantsearch/src/connectors/connectRange.test.js index 0d0609d0c4..8880d839a8 100644 --- a/packages/react-instantsearch/src/connectors/connectRange.test.js +++ b/packages/react-instantsearch/src/connectors/connectRange.test.js @@ -470,7 +470,7 @@ describe('connectRange', () => { { label: '5 <= wot', attributeName: 'wot', - currentRefinement: { min: 5, max: undefined }, + currentRefinement: { min: 5, max: 100 }, // Ignore clear, we test it later value: metadata.items[0].value, }, @@ -505,7 +505,7 @@ describe('connectRange', () => { { label: 'wot <= 10', attributeName: 'wot', - currentRefinement: { min: undefined, max: 10 }, + currentRefinement: { min: 0, max: 10 }, value: metadata.items[0].value, }, ], @@ -753,7 +753,7 @@ describe('connectRange', () => { { label: '5 <= wot', attributeName: 'wot', - currentRefinement: { min: 5, max: undefined }, + currentRefinement: { min: 5, max: 100 }, // Ignore clear, we test it later value: metadata.items[0].value, }, @@ -792,7 +792,7 @@ describe('connectRange', () => { { label: 'wot <= 10', attributeName: 'wot', - currentRefinement: { min: undefined, max: 10 }, + currentRefinement: { min: 0, max: 10 }, value: metadata.items[0].value, }, ],