Skip to content

Commit 54222a3

Browse files
committed
feat(connectors): forward widgetParams to renderFn
1 parent 704a455 commit 54222a3

File tree

18 files changed

+236
-159
lines changed

18 files changed

+236
-159
lines changed

src/connectors/clear-all/connectClearAll.js

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const clearAll = ({helper, clearAttributes, hasRefinements}) => () => {
3939
* @property {boolean} hasRefinements
4040
* @property {function} createURL
4141
* @property {InstantSearch} instantSearchInstance
42+
* @property {Object} widgetParams all original options forwarded to rendering
4243
*/
4344

4445
/**
@@ -50,35 +51,41 @@ const clearAll = ({helper, clearAttributes, hasRefinements}) => () => {
5051
export default function connectClearAll(renderFn) {
5152
checkRendering(renderFn, usage);
5253

53-
return ({excludeAttributes = []}) => ({
54-
init({helper, instantSearchInstance, createURL}) {
55-
const clearAttributes = getRefinements({}, helper.state)
54+
return (widgetParams = {}) => {
55+
const {excludeAttributes = []} = widgetParams;
56+
57+
return {
58+
init({helper, instantSearchInstance, createURL}) {
59+
const clearAttributes = getRefinements({}, helper.state)
5660
.map(one => one.attributeName)
5761
.filter(one => excludeAttributes.indexOf(one) === -1);
58-
const hasRefinements = clearAttributes.length !== 0;
59-
const preparedCreateURL = () => createURL(clearRefinementsFromState(helper.state));
62+
const hasRefinements = clearAttributes.length !== 0;
63+
const preparedCreateURL = () => createURL(clearRefinementsFromState(helper.state));
6064

61-
renderFn({
62-
clearAll: () => {},
63-
hasRefinements,
64-
createURL: preparedCreateURL,
65-
instantSearchInstance,
66-
}, true);
67-
},
65+
renderFn({
66+
clearAll: () => {},
67+
hasRefinements,
68+
createURL: preparedCreateURL,
69+
instantSearchInstance,
70+
widgetParams,
71+
}, true);
72+
},
6873

69-
render({results, state, createURL, helper, instantSearchInstance}) {
70-
const clearAttributes = getRefinements(results, state)
74+
render({results, state, createURL, helper, instantSearchInstance}) {
75+
const clearAttributes = getRefinements(results, state)
7176
.map(one => one.attributeName)
7277
.filter(one => excludeAttributes.indexOf(one) === -1);
73-
const hasRefinements = clearAttributes.length !== 0;
74-
const preparedCreateURL = () => createURL(clearRefinementsFromState(state));
78+
const hasRefinements = clearAttributes.length !== 0;
79+
const preparedCreateURL = () => createURL(clearRefinementsFromState(state));
7580

76-
renderFn({
77-
clearAll: clearAll({helper, clearAttributes, hasRefinements}),
78-
hasRefinements,
79-
createURL: preparedCreateURL,
80-
instantSearchInstance,
81-
}, false);
82-
},
83-
});
81+
renderFn({
82+
clearAll: clearAll({helper, clearAttributes, hasRefinements}),
83+
hasRefinements,
84+
createURL: preparedCreateURL,
85+
instantSearchInstance,
86+
widgetParams,
87+
}, false);
88+
},
89+
};
90+
};
8491
}

src/connectors/current-refined-values/connectCurrentRefinedValues.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ connectCurrentRefinedValues({
3333
* @property {string[]} clearRefinementURLs,individual url where a single refinement is cleared
3434
* @property {Refinements[]} refinements,all the current refinements
3535
* @property {InstantsSearch} instantSearchInstance the instance of instantsearch.js
36+
* @property {Object} widgetParams all original options forwarded to rendering
3637
*/
3738

3839
/**
@@ -56,10 +57,8 @@ connectCurrentRefinedValues({
5657
* @param {function(CurrentRefinedValuesRenderingOptions)} renderCurrentRefinedValues the custom rendering function
5758
* @return {function(CurrentRefinedValuesWidgetOptions): CurrentRefinedValuesWidget} a function that creates CurrentRefinedValues widget
5859
*/
59-
const connectCurrentRefinedValues = renderCurrentRefinedValues => ({
60-
attributes = [],
61-
onlyListedAttributes = false,
62-
}) => {
60+
const connectCurrentRefinedValues = renderCurrentRefinedValues => (widgetParams = {}) => {
61+
const {attributes = [], onlyListedAttributes = false} = widgetParams;
6362
const attributesOK = isArray(attributes) &&
6463
reduce(
6564
attributes,
@@ -108,6 +107,7 @@ const connectCurrentRefinedValues = renderCurrentRefinedValues => ({
108107
clearRefinementURLs,
109108
refinements,
110109
instantSearchInstance,
110+
widgetParams,
111111
}, true);
112112
},
113113
render({results, helper, state, createURL, instantSearchInstance}) {
@@ -125,6 +125,7 @@ const connectCurrentRefinedValues = renderCurrentRefinedValues => ({
125125
clearRefinementURLs,
126126
refinements,
127127
instantSearchInstance,
128+
widgetParams,
128129
}, false);
129130
},
130131
};

src/connectors/hierarchical-menu/connectHierarchicalMenu.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Full documentation available at https://community.algolia.com/instantsearch.js/c
4141
* @property {Object[]} items
4242
* @property {function} refine
4343
* @property {InstantSearch} instantSearchInstance
44+
* @property {Object} widgetParams all original options forwarded to rendering
4445
*/
4546

4647
/**
@@ -51,14 +52,16 @@ Full documentation available at https://community.algolia.com/instantsearch.js/c
5152
export default function connectHierarchicalMenu(renderFn) {
5253
checkRendering(renderFn, usage);
5354

54-
return ({
55-
attributes,
56-
separator = ' > ',
57-
rootPath = null,
58-
showParentLevel = true,
59-
limit = 10,
60-
sortBy = ['name:asc'],
61-
} = {}) => {
55+
return (widgetParams = {}) => {
56+
const {
57+
attributes,
58+
separator = ' > ',
59+
rootPath = null,
60+
showParentLevel = true,
61+
limit = 10,
62+
sortBy = ['name:asc'],
63+
} = widgetParams;
64+
6265
if (!attributes || !attributes.length) {
6366
throw new Error(usage);
6467
}
@@ -98,6 +101,7 @@ export default function connectHierarchicalMenu(renderFn) {
98101
items: [],
99102
refine: this._refine,
100103
instantSearchInstance,
104+
widgetParams,
101105
}, true);
102106
},
103107

@@ -130,6 +134,7 @@ export default function connectHierarchicalMenu(renderFn) {
130134
items,
131135
refine: this._refine,
132136
instantSearchInstance,
137+
widgetParams,
133138
}, false);
134139
},
135140
};

src/connectors/hits-per-page-selector/connectHitsPerPageSelector.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@ hitsPerPageSelector({
2020
options,
2121
})`;
2222

23-
const connectHitsPerPageSelector = renderHitsPerPageSelector => ({
24-
options: userOptions,
25-
} = {}) => {
23+
const connectHitsPerPageSelector = renderHitsPerPageSelector => (widgetParams = {}) => {
24+
const {options: userOptions} = widgetParams;
2625
let options = userOptions;
2726

2827
if (!options) {
@@ -66,6 +65,7 @@ with \`value: hitsPerPage\` (hitsPerPage: ${state.hitsPerPage})`
6665
options,
6766
setValue: this.setHitsPerPage,
6867
hasNoResults: true,
68+
widgetParams,
6969
}, true);
7070
},
7171

@@ -78,6 +78,7 @@ with \`value: hitsPerPage\` (hitsPerPage: ${state.hitsPerPage})`
7878
options,
7979
setValue: this.setHitsPerPage,
8080
hasNoResults,
81+
widgetParams,
8182
}, false);
8283
},
8384
};

src/connectors/hits/connectHits.js

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Full documentation available at https://community.algolia.com/instantsearch.js/c
2626
* @property {Object[]} hits
2727
* @property {Object} results
2828
* @property {InstantSearch} instantSearchInstance
29+
* @property {Object} widgetParams all original options forwarded to rendering
2930
*/
3031

3132
/**
@@ -36,25 +37,31 @@ Full documentation available at https://community.algolia.com/instantsearch.js/c
3637
export default function connectHits(renderFn) {
3738
checkRendering(renderFn, usage);
3839

39-
return ({hitsPerPage = 20}) => ({
40-
getConfiguration() {
41-
return {hitsPerPage};
42-
},
43-
44-
init({instantSearchInstance}) {
45-
renderFn({
46-
hits: [],
47-
results: undefined,
48-
instantSearchInstance,
49-
}, true);
50-
},
51-
52-
render({results, instantSearchInstance}) {
53-
renderFn({
54-
hits: results.hits,
55-
results,
56-
instantSearchInstance,
57-
}, false);
58-
},
59-
});
40+
return (widgetOptions = {}) => {
41+
const {hitsPerPage = 20} = widgetOptions;
42+
43+
return {
44+
getConfiguration() {
45+
return {hitsPerPage};
46+
},
47+
48+
init({instantSearchInstance}) {
49+
renderFn({
50+
hits: [],
51+
results: undefined,
52+
instantSearchInstance,
53+
widgetOptions,
54+
}, true);
55+
},
56+
57+
render({results, instantSearchInstance}) {
58+
renderFn({
59+
hits: results.hits,
60+
results,
61+
instantSearchInstance,
62+
widgetOptions,
63+
}, false);
64+
},
65+
};
66+
};
6067
}

src/connectors/infinite-hits/connectInfiniteHits.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,8 @@ Full documentation available at https://community.algolia.com/instantsearch.js/c
4040
export default function connectInfiniteHits(renderFn) {
4141
checkRendering(renderFn, usage);
4242

43-
return ({
44-
hitsPerPage = 20,
45-
}) => {
43+
return widgetParams => {
44+
const {hitsPerPage = 20} = widgetParams;
4645
let hitsCache = [];
4746
const getShowMore = helper => () => helper.nextPage().search();
4847

@@ -60,6 +59,7 @@ export default function connectInfiniteHits(renderFn) {
6059
showMore: this.showMore,
6160
isLastPage: true,
6261
instantSearchInstance,
62+
widgetParams,
6363
}, true);
6464
},
6565

@@ -78,6 +78,7 @@ export default function connectInfiniteHits(renderFn) {
7878
showMore: this.showMore,
7979
isLastPage,
8080
instantSearchInstance,
81+
widgetParams,
8182
}, false);
8283
},
8384
};

src/connectors/menu/connectMenu.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Full documentation available at https://community.algolia.com/instantsearch.js/c
3939
* @property {AlgoliaSearchHelper} helper
4040
* @property {InstantSearch} instantSearchInstance
4141
* @property {boolean} canRefine
42+
* @property {Object} widgetParams all original options forwarded to rendering
4243
*/
4344

4445
/**
@@ -49,11 +50,13 @@ Full documentation available at https://community.algolia.com/instantsearch.js/c
4950
export default function connectMenu(renderFn) {
5051
checkRendering(renderFn, usage);
5152

52-
return ({
53-
attributeName,
54-
limit = 10,
55-
sortBy = ['count:desc', 'name:asc'],
56-
}) => {
53+
return widgetParams => {
54+
const {
55+
attributeName,
56+
limit = 10,
57+
sortBy = ['count:desc', 'name:asc'],
58+
} = widgetParams;
59+
5760
if (!attributeName) {
5861
throw new Error(usage);
5962
}
@@ -91,6 +94,7 @@ export default function connectMenu(renderFn) {
9194
helper: this._helper,
9295
instantSearchInstance,
9396
canRefine: false,
97+
widgetParams,
9498
}, true);
9599
},
96100

@@ -105,6 +109,7 @@ export default function connectMenu(renderFn) {
105109
helper: this._helper,
106110
instantSearchInstance,
107111
canRefine: items.length > 0,
112+
widgetParams,
108113
}, false);
109114
},
110115
};

src/connectors/numeric-refinement-list/connectNumericRefinementList.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@ connectNumericRefinementList(renderer)({
3535
attributeName,
3636
options
3737
})`;
38-
const connectNumericRefinementList = numericRefinementListRendering => ({
39-
attributeName,
40-
options,
41-
}) => {
38+
const connectNumericRefinementList = numericRefinementListRendering => widgetParams => {
39+
const {
40+
attributeName,
41+
options,
42+
} = widgetParams;
43+
4244
if (!attributeName || !options) {
4345
throw new Error(usage);
4446
}
@@ -66,6 +68,7 @@ const connectNumericRefinementList = numericRefinementListRendering => ({
6668
hasNoResults: true,
6769
toggleRefinement: this._toggleRefinement,
6870
instantSearchInstance,
71+
widgetParams,
6972
}, true);
7073
},
7174
render({results, state, instantSearchInstance}) {
@@ -83,6 +86,7 @@ const connectNumericRefinementList = numericRefinementListRendering => ({
8386
hasNoResults: results.nbHits === 0,
8487
toggleRefinement: this._toggleRefinement,
8588
instantSearchInstance,
89+
widgetParams,
8690
}, false);
8791
},
8892
};

src/connectors/numeric-selector/connectNumericSelector.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Full documentation available at https://community.algolia.com/instantsearch.js/c
3636
* @property {function} setValue
3737
* @property {boolean} hasNoResults
3838
* @property {InstantSearch} instantSearchInstance
39+
* @property {Object} widgetParams all original options forwarded to rendering
3940
*/
4041

4142
/**
@@ -46,11 +47,13 @@ Full documentation available at https://community.algolia.com/instantsearch.js/c
4647
export default function connectNumericSelector(renderFn) {
4748
checkRendering(renderFn, usage);
4849

49-
return ({
50-
attributeName,
51-
options,
52-
operator = '=',
53-
}) => {
50+
return widgetParams => {
51+
const {
52+
attributeName,
53+
options,
54+
operator = '=',
55+
} = widgetParams;
56+
5457
if (!attributeName || !options) {
5558
throw new Error(usage);
5659
}
@@ -81,6 +84,7 @@ export default function connectNumericSelector(renderFn) {
8184
setValue: this._refine,
8285
hasNoResults: true,
8386
instantSearchInstance,
87+
widgetParams,
8488
}, true);
8589
},
8690

@@ -91,6 +95,7 @@ export default function connectNumericSelector(renderFn) {
9195
setValue: this._refine,
9296
hasNoResults: results.nbHits === 0,
9397
instantSearchInstance,
98+
widgetParams,
9499
}, false);
95100
},
96101

0 commit comments

Comments
 (0)