Choices-js / Choices

A vanilla JS customisable select box/text input plugin ⚡️
https://choices-js.github.io/Choices/
MIT License
6.13k stars 605 forks source link

optgroup are always displayed last #1178

Open gmisiolek-sbm opened 2 weeks ago

gmisiolek-sbm commented 2 weeks ago

Describe the bug I have select with options and optgroups mixed together, which is displayed as I want as normal select, but when I want to use choices.js it puts all options first, then all groups below, ignoring order of options/optgroups.

To Reproduce Steps to reproduce the behavior:

  1. Add HTML:
    <select class="form-input" name="field_100">
    <option value="" selected="">select</option>
    <option>1</option>
    <option>2</option>
    <option>3</option>
    <optgroup label="4">
    <option>4.1</option>
    <option>4.2</option>
    </optgroup>
    <option>5</option>
    </select>
  2. Run choices on this element
  3. See invalid order

Expected behavior Options and optgroups should be displayed in order I've specified in html

Screenshots Normal select without choices applied: obraz Select with choices applied: obraz

Desktop (please complete the following information):

Smartphone (please complete the following information):

Additional context Add any other context about the problem here.

Xon commented 2 weeks ago

This is currently a limitation of the choices optgroup rendering code. It used to just drop options without an optgroup which is frankly worse.

The work-around would be to use a dummy optgroup to wrap the choices to force ordering.

I'm looking to improve this behavior but it will require some additional structural changes in the back-end but I wanted to finish v11.0.0 and the inevitable followup bugfixes.