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

S.M_Emamian
  • 17,005
  • 37
  • 135
  • 254

2 Answers2

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)?

Arthur
  • 110
  • 3
  • 13
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

Black Hole
  • 1,277
  • 12
  • 16