Custom `RefinementList` brings more results on `transformItems` than the `limit` set
When using more than 1 custom RefinementList with connectRefinementList the property limit is actually being set for all RefinementList's making transformItems returning wrong results.
To Reproduce 🔍
Steps to reproduce the behavior:
- Create 2
RefinementListusingconnectRefinementList. - Set 1 to use
limit10and the other one to use20. - Add to both this method:
transformItems={items => {
console.log(items.length);
return items;
}}
- See that the console will print
20for both.
Sandbox env.: https://codesandbox.io/s/react-instantsearch-custom-refinement-list-i6nt4
Expected behavior 💭
Should bring only 10 items when transformItems is executed for the first RefinementList and 20 for the second one.
Environment:
- OS: MacOS
- Browser: Chrome
- Version: 6.3.0
This is happening because maxValuesPerFacet is global, and thus those facets are requested anyway, so we give them to transformItems.
The list gets pruned to your requested limit here: https://github.com/algolia/react-instantsearch/blob/2e8214fd19d5c618cb3013790193747329899a9f/packages/react-instantsearch-core/src/connectors/connectRefinementList.js#L185 (taking show more into account too)
Is there something where given too many items to tranformItems breaks your code? It would be interesting to see that in a sandbox
Yes, we are trying to sort alphabetically, so considering is coming more than the default for a specific RefinementList, we are doing the sort but the thing is that only the 10 is appearing later. So taking into account what you said, I will need to sort by count first, slice 10, and then sort alphabetically. Right?
Will try to create a sandbox sample.
Yes, that would help. You don't need to slice manually, the connectRefinementList already slices :)
Just updated the same sandbox so you can see my issue.
To clarify, you want to show the limit most items, but those not sorted by count, but alphabetic? Usually we assume that whatever sorting you choose will make will sort the most important items at the top.
In your case I would indeed look at slicing before sorting
Yes, I want this behaviour:
Items > sort by count > get 10 (which is the limit for our RefinementList) > sort by alphabetically
I have about 6 custom RefinementList's on the same page, we increased the limit of one to be 40 and that broke another one that was only sorting alphabetically (you can see this issue on the Sandbox).
Question, can't we slice before we execute the transformItems method?
https://github.com/algolia/react-instantsearch/blob/2e8214fd19d5c618cb3013790193747329899a9f/packages/react-instantsearch-core/src/connectors/connectRefinementList.js#L180
That would be a breaking change, sorry!
No problem @Haroenv, thanks for your fast response on this!