Skip to content
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 displayed in dropdown #126

Closed
super-cache-money opened this issue Apr 14, 2015 · 22 comments
Closed

Limiting number of options displayed in dropdown #126

super-cache-money opened this issue Apr 14, 2015 · 22 comments

Comments

@super-cache-money
Copy link

For example, how would one only show the 5 closest matches to input text in the dropdown, despite having 1000 options which are searchable.

@dcousens
Copy link
Collaborator

dcousens commented May 7, 2015

@super-cache-money how would they be sorted?

@dcousens dcousens self-assigned this May 7, 2015
@ArcEglos
Copy link

ArcEglos commented Jun 9, 2015

If you are only concerned about the display this css may help you out:

.Select-option:nth-child(n+6) {
  display: none;
}
.Select-option:last-child,
.Select-option:nth-child(5) {
  border-bottom-right-radius: 4px;
  border-bottom-left-radius: 4px;
}

I know this isn't pretty at all but as long as this doesn't impact your performance too hard it may be a quick-and-dirty workaround until this is implemented.

@dcousens dcousens removed their assignment Aug 13, 2015
@nslobodchuk
Copy link

I'm in a similar situation. I've got 4000 options and would like to either limit the number of options displayed or start filtering only if the input is longer than 3 characters. Do you know if there's a way to do this?
Thanks in advance.

@chriddyp
Copy link

I'm in a similar case where I have just 6 options and I'd like to display all 6 options at once instead of requiring the user to scroll down just to see 1 more item.

Is CSS still the best way to do this?

@sttevan88
Copy link

+1

2 similar comments
@onelz
Copy link

onelz commented Jun 21, 2017

+1

@ramonvicelli
Copy link

+1

@aappddeevv
Copy link

aappddeevv commented Jan 17, 2018

Oh, I just hit this as well. To fit visually, I need to limit it to the dropdown just showing 2-3 options and scroll the rest. The above CSS will not display the options though. It looks like setting .Select-menu { max-height: 50px;} seems to constrain the dropdown size and preserve scrolling. Of course, this won't work well if you have more than one select and you need different sizes. I know that's slightly different than the problem statement in the first comment.

peterbartos pushed a commit to peterbartos/react-select that referenced this issue May 15, 2018
…ction

results count. It shows the first `filterMaxResuls` items. FIX JedWatson#126
peterbartos pushed a commit to peterbartos/react-select that referenced this issue May 15, 2018
…ction

results count. It shows the first `filterMaxResuls` items. FIX JedWatson#126
@peterbartos
Copy link

I created a PR which should provide filterMaxResults functionality in #2611

peterbartos pushed a commit to peterbartos/react-select that referenced this issue May 15, 2018
…ction

results count. It shows the first `filterMaxResults` items. FIX JedWatson#126
peterbartos pushed a commit to peterbartos/react-select that referenced this issue May 15, 2018
…ction

results count. It shows the first `filterMaxResults` items. FIX JedWatson#126
@sahilanguralla
Copy link

+1

1 similar comment
@curliq
Copy link

curliq commented Nov 4, 2018

+1

@javierojeda94
Copy link

Any updates on this?

@shaayaansayed
Copy link

+1

@douxsey
Copy link

douxsey commented Feb 27, 2019

You can use the props maxMenuHeight={200}

@toninofox
Copy link

toninofox commented Feb 28, 2019

In the meantime I am using in my code this (awful) workaround:

const MenuList = props => {
  return (
    <components.MenuList {...props}>      
      { Arrays.isArray(props.children) ? props.children.slice(0, 10) : props.children }
    </components.MenuList>
  );
};

Where 10 is my limit

export default () => (
  <Select
    defaultValue={colourOptions[1]}
    options={colourOptions}
    components={{ MenuList }}
  />
);

In this way you will still be able to search inside the list but you will display just the first 10 elements of the list

UPDATE:
This will create a lag hovering the element of the list due to another issue:
#2711

@cbmek
Copy link

cbmek commented Mar 7, 2019

Why not just go with AsyncSelect?

Example with limit:

<AsyncSelect
    loadOptions={(inputValue, callback) => {
        callback(selectOptions.slice(0, 50))
    }}
    defaultOptions={true}
    value={value}
    onChange={(val) => this.onValueChanged(val)}
/>

Example with limit and simple serach:

<AsyncSelect
    loadOptions={(inputValue, callback) => {
        callback(selectOptions.filter(x => x.label.toLowerCase().includes(inputValue.toLowerCase())).slice(0, 50))
    }}
    defaultOptions={true}
    value={value}
    onChange={(val) => this.onValueChanged(val)}
/>

@vitexikora
Copy link

vitexikora commented Apr 1, 2019

Quick temp solution:

const resultLimit = 10
let i = 0
return <Select filterOption={({label}, query) => label.indexOf(query) >= 0 && i++ < resultLimit} onInputChange={() => { i = 0 }} />

@nhevia
Copy link

nhevia commented Sep 9, 2019

Solution provided by @vitexikora worked for me, but I'd like to point out that 'label' will be the visual option for the user, so I'd rather use value, which should be sanitized and/or normalized.

const resultLimit = 10
let i = 0
return <Select filterOption={({value}, query) => value.indexOf(query.toLowerCase()) >= 0 && i++ < resultLimit} onInputChange={() => { i = 0 }} />

@KonstantinDinev
Copy link

@vitexikora that solution works great. Thanks!

@Carchuli
Copy link

@vitexikora solution worked for me also! Im dealing with 50k options, and with this solution I can search between that all records. The only issue is that it only displays the 10 first values, I will try to make it async using onMenuScrollToBottom prop to make it 'async' and load 10 more values

@jossmac jossmac removed the v1 label Mar 17, 2020
@jossmac
Copy link
Collaborator

jossmac commented Mar 17, 2020

Version 1 of react-select is no longer supported.

In the best interest of the community we've decided to spend the time we have available on the latest version.

We apologise for any inconvenience.

Please see:

  1. v1 to v2 upgrade guide
  2. v1 documentation and examples

@sankita15
Copy link

sankita15 commented Jun 25, 2020

In case anyone is looking for dropdown menu with a limited number of options and scroll; use the maxMenuHeight prop:

<Select
  maxMenuHeight={175}
  options={options}
  value={value}
/>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.