Skip to content

Provide an option to track options from underlying select element #1003

@francoislehoux-okta

Description

@francoislehoux-okta

Is your feature request related to a problem? Please describe.
There is currently no clear way to have choices track the options from the underlying select element. We can use .setChoices but we have to manually parse every. Most of this logic already exist within the constructor/init methods.

Say, you are using choicesjs within react, when the children changes dynamically, you want an easy way to have those reflected in the rendered component.

It seems this has been asked often but was never considered or OKd (issue getting automatically closed after X days).

Describe the solution you'd like
Could be:

  • A new option when instantiating Choices
  • A new refresh method which would update choices (groups and options) based on the underlying element

Describe alternatives you've considered
This "works" but feels out of place when outside of the library itself.

const options: any[] = [];
if (choices.current.passedElement.options) {
  choices.current.passedElement.options.forEach((option) => {
    options.push({
      value: option.value,
      label: option.innerHTML,
      selected: !!option.selected,
      disabled: option.disabled || option.parentNode.disabled,
      placeholder:
        option.value === '' || option.hasAttribute('placeholder'),
      customProperties: option.dataset['custom-properties'],
    });
  });
}
choices.current.setChoices(options);

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions