Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Destroy does not preserve the selected value #1088

Closed
tagliala opened this issue Dec 24, 2022 · 1 comment · Fixed by #1166
Closed

Destroy does not preserve the selected value #1088

tagliala opened this issue Dec 24, 2022 · 1 comment · Fixed by #1166
Labels

Comments

@tagliala
Copy link
Contributor

tagliala commented Dec 24, 2022

Describe the bug
destroy does not respect selected value. This causes issues when using with libraries like Turbo and Stimulus, where you want to destroy() when leaving the page or disconnecting the controller

To Reproduce
Steps to reproduce the behavior:

  1. Go to https://jsfiddle.net/tagliala/yetk9sgh/3/
  2. Click on the page to invoke destroy

Expected behavior
Option 3 (selected) selected

Screenshots
n/a

Desktop (please complete the following information):

  • OS: macOS
  • Browser Chrome, Firefox, Safari
  • Version 108, 108, 16.2

Smartphone (please complete the following information):

  • Device: iPhone 12
  • OS: iOS 16.2
  • Browser Safari
  • Version 16.2

Additional context
Related to #1055

As a workaround for simple inputs, I'm using this in the disconnect() controller

  disconnect () {
    // FIXME: workaround for Choices-js/Choices#1088
    const selectedValues = this.choices._currentState.items.map((item) => item.value)

    this.choices.destroy()

    selectedValues.forEach((selectedValue) => {
      const option = this.choicesTarget.querySelector(`option[value="${selectedValue}"]`)
      if (!option) { return }

      option.setAttribute('selected', 'selected')
    })
  }
@tagliala tagliala added the bug label Dec 24, 2022
@creativetags
Copy link

I've run into this problem too. Would be great to have a way to destroy() and keep selected values.

@Xon Xon linked a pull request Aug 6, 2024 that will close this issue
9 tasks
@Xon Xon closed this as completed Aug 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants