Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limiting number of options rendered in dropdown #4516

Closed
h3rmanj opened this issue Mar 23, 2021 · 5 comments
Closed

Limiting number of options rendered in dropdown #4516

h3rmanj opened this issue Mar 23, 2021 · 5 comments

Comments

@h3rmanj
Copy link

h3rmanj commented Mar 23, 2021

Continuation of #126, which got closed when react-select moved from v1 (?).

There should be a way to limit the number of options that are rendered to avoid sluggish (at best, even with high-end PCs) behavior when dealing with large lists.
Example: https://codesandbox.io/s/react-codesandboxer-example-forked-sijty

The workaround posted in #126 (comment) no longer works, and this needs to be addressed again.

I've made a PR in #4515 (which is a spiritual successor to #2611, also closed due to being v1), that simply adds a prop to limit the amount of options rendered. That solution might be too simple though, but I'm creating a feature request so it can be discussed here.

This issue also addresses the root cause of #4504

@ebonow
Copy link
Collaborator

ebonow commented Jun 25, 2021

Greetings @h3rmanj and sorry for the late reply.

I could be missing something but you should be able modify your MenuList component to handle this.

Codesandbox demo

const MenuList = ({ children, ...props }) => {
  return (
    <components.MenuList {...props}>
      {Array.isArray(children) 
        ? children.slice(0, props.selectProps?.maxOptions) /* Options */
        : children /* NoOptionsLabel */
       }
    </components.MenuList>
  );
};

<Select components={{ MenuList }} maxOptions={5} />

@h3rmanj
Copy link
Author

h3rmanj commented Jul 9, 2021

Thanks! That seems like a good solution to me.

I do still think it needs to be "officially" addressed though. I know this is a common problem in projects in my company. Reading through #127 and other issues, it seems like something many devs run into as well.

So if it's not going to be supported in a prop, I'd still like to see a suggested solution in the docs.

I'll close my two PR's though since there is a major rewrite in progress.

@rafaelquintanilha
Copy link

Greetings @h3rmanj and sorry for the late reply.

I could be missing something but you should be able modify your MenuList component to handle this.

Codesandbox demo

const MenuList = ({ children, ...props }) => {
  return (
    <components.MenuList {...props}>
      {Array.isArray(children) 
        ? children.slice(0, props.selectProps?.maxOptions) /* Options */
        : children /* NoOptionsLabel */
       }
    </components.MenuList>
  );
};

<Select components={{ MenuList }} maxOptions={5} />

@ebonow thanks for your solution. While this works for limiting the number of options displayed, filterOption will run nonetheless. That means that in large datasets with any kind of filtering, even if we are limiting to only a subset of the options, it will still be computationally expensive.

In the past, I solved this issue by limiting how many times filterOption can run, but now this function is being called multiple times per option (as correctly pointed by @h3rmanj in another PR) and this solution no longer works.

Any plans (or ideas) to address this issue?

@alamothe
Copy link

@ebonow The solution works but it is inefficient, since all options will still be rendered, even though almost all of them are discarded.

@fkaempfer
Copy link

I simply used an async select (https://react-select.com/async) as a workaround to limit results. it works quite well and is efficient.

However limiting the number of options displayed at a time would certainly be useful

Repository owner locked and limited conversation to collaborators Oct 31, 2022
@dcousens dcousens converted this issue into discussion #5444 Oct 31, 2022

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants