React-select: Filter should only consider "label" not "value"

Created on 3 Feb 2019  路  5Comments  路  Source: JedWatson/react-select

The default filtering seems to match the entered text to both the value and label field of the option. This is a problem because the user is shown options whose label does not match the entered text at all.

In my case, "value" is the database id, while the label displays an account number. When users search for an account, they are shown unrelated accounts whose database ID's happen to include the entered account number. I know I can fix this with a custom filter, but it really should be the default

Please change the behavior so that search only considers the label.

Most helpful comment

I solved this problem with filterOptions:

<Select
  {...this.props}
  filterOption={createFilter({
    matchFrom: 'any',
    stringify: option => `${option.label}`,
  })}
  options={options}
/>

All 5 comments

https://github.com/JedWatson/react-select/issues/2007

So, it seems like this is the intended behavior, but why? Surely, the user expects the search to only apply to the visible label?

I agree, for the purpose of UX, the user expects to filter the label only as default.

Probably a better way

export const filterOption = (option: OptionType, inputValue: string): boolean =>
  (option.label.toString().match(inputValue) || []).length > 0;

<Select
  {...this.props}
  filterOption={filterOption}
  options={options}
 />

I solved this problem with filterOptions:

<Select
  {...this.props}
  filterOption={createFilter({
    matchFrom: 'any',
    stringify: option => `${option.label}`,
  })}
  options={options}
/>

@VadSiam this solution resolve for me, but will be better have an boolean in the props..
thanks for the solution

For UX reasons, I believe this should be a bug. Having a default of filter by label only makes more sense to me.
Thanks for the workarounds.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MalcolmDwyer picture MalcolmDwyer  路  3Comments

Meesam picture Meesam  路  3Comments

x-yuri picture x-yuri  路  3Comments

yrabinov picture yrabinov  路  3Comments

mbonaci picture mbonaci  路  3Comments