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

Limiting number of options displayed in dropdown

Open super-cache-money opened this issue 11 years ago • 22 comments

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

super-cache-money avatar Apr 14 '15 03:04 super-cache-money

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

dcousens avatar May 07 '15 03:05 dcousens

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.

ArcEglos avatar Jun 09 '15 12:06 ArcEglos

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.

nslobodchuk avatar Oct 18 '15 11:10 nslobodchuk

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?

chriddyp avatar Feb 27 '16 18:02 chriddyp

+1

sttevan88 avatar Nov 15 '16 14:11 sttevan88

+1

onelz avatar Jun 21 '17 21:06 onelz

+1

ramonvicelli avatar Jun 30 '17 18:06 ramonvicelli

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.

aappddeevv avatar Jan 17 '18 18:01 aappddeevv

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

peterbartos avatar May 15 '18 12:05 peterbartos

+1

sahilanguralla avatar Sep 10 '18 08:09 sahilanguralla

+1

curliq avatar Nov 04 '18 22:11 curliq

Any updates on this?

javierojeda94 avatar Nov 20 '18 19:11 javierojeda94

+1

shaayaansayed avatar Jan 24 '19 20:01 shaayaansayed

You can use the props maxMenuHeight={200}

douxsey avatar Feb 27 '19 01:02 douxsey

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: https://github.com/JedWatson/react-select/issues/2711

toninofox avatar Feb 28 '19 16:02 toninofox

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)}
/>

cbmek avatar Mar 07 '19 14:03 cbmek

Quick temp solution:

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

vitexikora avatar Apr 01 '19 14:04 vitexikora

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 }} />

nhevia avatar Sep 09 '19 05:09 nhevia

@vitexikora that solution works great. Thanks!

KonstantinDinev avatar Sep 12 '19 10:09 KonstantinDinev

@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

Carchuli avatar Jan 13 '20 15:01 Carchuli