0

I have this nested optgroup:

<select>
<optgroup label="A">
<optgroup label="B">
     <option>C</option>
     <option>D</option>
     <option>G</option>
</optgroup>

<optgroup label="J">
     <option>K</option>
     <option>L</option>
     <option>M</option>

</optgroup>
</optgroup>
</select>

but the result is:

enter image description here

1
  • what is the expected result? what have you tried? why is this related to react? Commented Jul 11, 2021 at 8:16

2 Answers 2

0

You can use Select from react-select.

import Select from "react-select";

const options = [
  {
    label: "A",
    options: [
      {
        label: "B",
        options: [
          {
            label: "C",
            value: 1
          },
          {
            label: "D",
            value: 2
          },
          {
            label: "G",
            value: 3
          }
        ]
      },
      {
        label: "J",
        options: [
          {
            label: "K",
            value: 4
          },
          {
            label: "L",
            value: 5
          },
          {
            label: "M",
            value: 6
          }
        ]
      }
    ]
  }
];

export const NestedOptGroup = () => <Select name="options" options={options} />

There is a working example of this react-select feature: https://codesandbox.io/s/react-codesandboxer-example-8xxyx?file=/index.js

Source: How can I create nested option groups in React Select (V2)?

Sign up to request clarification or add additional context in comments.

Comments

0

you can use only 1 sub group, like this

  <select>
    <optgroup label="B">
      <option>C</option>
      <option>D</option>
      <option>G</option>
    </optgroup>
    <optgroup label="J">
      <option>K</option>
      <option>L</option>
      <option>M</option>
    </optgroup>
  </select>

if you wish add more subgroups you need to hack the identation here have other answers for this: Nesting optgroups in a dropdownlist/select

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.