Skip to content

Commit

Permalink
feat(connector): update doc, move setValue to refine in SortBySelector
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandre Stanislawski committed Mar 24, 2017
1 parent e26e8e2 commit 2486f36
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ describe('connectSortBySelector', () => {
{ // first rendering
expect(helper.state.index).toBe(indices[0].name);
const renderOptions = rendering.lastCall.args[0];
const {setValue, currentValue} = renderOptions;
const {refine, currentValue} = renderOptions;
expect(currentValue).toBe(helper.state.index);
setValue('bip');
refine('bip');
expect(helper.state.index).toBe('bip');
expect(helper.search.callCount).toBe(1);
}
Expand All @@ -117,9 +117,9 @@ describe('connectSortBySelector', () => {
{ // Second rendering
expect(helper.state.index).toBe('bip');
const renderOptions = rendering.lastCall.args[0];
const {setValue, currentValue} = renderOptions;
const {refine, currentValue} = renderOptions;
expect(currentValue).toBe('bip');
setValue('bop');
refine('bop');
expect(helper.state.index).toBe('bop');
expect(helper.search.callCount).toBe(2);
}
Expand Down
22 changes: 13 additions & 9 deletions src/connectors/sort-by-selector/connectSortBySelector.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ var customSortBySelector = connectSortBySelector(function render(params, isFirst
// params = {
// currentValue,
// options,
// setValue,
// refine,
// hasNoResults,
// instantSearchInstance
// }
});
search.addWidget(
Expand All @@ -24,10 +25,11 @@ Full documentation available at https://community.algolia.com/instantsearch.js/c

/**
* @typedef {Object} SortBySelectorRenderingOptions
* @property {string} currentValue
* @property {Object[]} options
* @property {function} setValue
* @property {boolean} hasNoResults
* @property {string} currentValue the currently selected index
* @property {Object[]} options all the available indices
* @property {function} refine switch indices and do a new search
* @property {boolean} hasNoResults a boolean that indicates if there were no results during that last search
* @property {InstantSearch} instantSearchInstance the instance of instantsearch on which the widget is attached
*/

/**
Expand All @@ -46,7 +48,7 @@ export default function connectSortBySelector(renderFn) {
const selectorOptions = indices.map(({label, name}) => ({label, value: name}));

return {
init({helper}) {
init({helper, instantSearchInstance}) {
const currentIndex = helper.getIndex();
const isIndexInList = indices.find(({name}) => name === currentIndex);

Expand All @@ -61,17 +63,19 @@ export default function connectSortBySelector(renderFn) {
renderFn({
currentValue: currentIndex,
options: selectorOptions,
setValue: this.setIndex,
refine: this.setIndex,
hasNoResults: true,
instantSearchInstance,
}, true);
},

render({helper, results}) {
render({helper, results, instantSearchInstance}) {
renderFn({
currentValue: helper.getIndex(),
options: selectorOptions,
setValue: this.setIndex,
refine: this.setIndex,
hasNoResults: results.nbHits === 0,
instantSearchInstance,
}, false);
},
};
Expand Down
4 changes: 2 additions & 2 deletions src/widgets/sort-by-selector/sort-by-selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const renderer = ({
}) => ({
currentValue,
options,
setValue,
refine,
hasNoResults,
}, isFirstRendering) => {
if (isFirstRendering) return;
Expand All @@ -27,7 +27,7 @@ const renderer = ({
cssClasses={cssClasses}
currentValue={currentValue}
options={options}
setValue={setValue}
setValue={refine}
shouldAutoHideContainer={shouldAutoHideContainer}
/>,
containerNode
Expand Down

0 comments on commit 2486f36

Please sign in to comment.