Dropdown filter with 'options' defined as an array of primitives

UI Components for React
Post Reply
AlainD.
Posts: 26
Joined: 14 Jun 2018, 22:00

15 Jul 2021, 15:06

Hello,

It seems that the filter feature does not work when the 'options' used is an array of primitives, such as an array of strings.
For instance:

Code: Select all

export default function MyComponent() {
  const [selected, setSelected] = useState<string>('');
  const options: string[] = ['a', 'b', 'c'];
  
  return (
    <Dropdown
      options={options}
      value={selected}
      onChange={(e) => setSelected(e.value)}
      placeholder="Select an option"
    />
  );
}
Is it a bug or do I need to specify some other options to Dropdown to make it work?

Thank you

mcandu
Posts: 107
Joined: 20 Jan 2021, 16:31

16 Jul 2021, 10:22

Hi, you should specify optionLabel and add value of this property as a property of object in the array.

Code: Select all

export default function MyComponent() {
  const [selected, setSelected] = useState<any>(null);
  const options: Object[] = [{name: 'a'} , {name: 'b'}, {name: 'c'}];
  
  return (
    <Dropdown
      options={options}
      value={selected}
      onChange={(e) => setSelected(e.value)}
      placeholder="Select an option"
      optionLabel="name"
    />
  );
}

Post Reply

Return to “PrimeReact”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 8 guests