react-instantsearch icon indicating copy to clipboard operation
react-instantsearch copied to clipboard

Custom `RefinementList` brings more results on `transformItems` than the `limit` set

Open furlanrapha opened this issue 6 years ago • 9 comments

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:

  1. Create 2 RefinementList using connectRefinementList.
  2. Set 1 to use limit 10 and the other one to use 20.
  3. Add to both this method:
transformItems={items => {
   console.log(items.length);
   return items;
}}
  1. See that the console will print 20 for 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

furlanrapha avatar Feb 12 '20 21:02 furlanrapha

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)

Haroenv avatar Feb 13 '20 09:02 Haroenv

Is there something where given too many items to tranformItems breaks your code? It would be interesting to see that in a sandbox

Haroenv avatar Feb 13 '20 09:02 Haroenv

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.

furlanrapha avatar Feb 13 '20 14:02 furlanrapha

Yes, that would help. You don't need to slice manually, the connectRefinementList already slices :)

Haroenv avatar Feb 13 '20 15:02 Haroenv

Just updated the same sandbox so you can see my issue.

furlanrapha avatar Feb 13 '20 15:02 furlanrapha

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

Haroenv avatar Feb 13 '20 16:02 Haroenv

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

furlanrapha avatar Feb 13 '20 16:02 furlanrapha

That would be a breaking change, sorry!

Haroenv avatar Feb 13 '20 23:02 Haroenv

No problem @Haroenv, thanks for your fast response on this!

furlanrapha avatar Feb 14 '20 14:02 furlanrapha