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

Fix #573 #574

Merged
merged 16 commits into from
Nov 17, 2019
Merged

Fix #573 #574

merged 16 commits into from
Nov 17, 2019

Conversation

kzkn
Copy link
Contributor

@kzkn kzkn commented Apr 9, 2019

Description

This PR fixes #573. It contains following fixes:

  • Restores preset choices after destroy
  • Loads preset choices only once for each choice after init

How Has This Been Tested?

E2E tests are added.

cypress/integration/select-one.spec.js Outdated Show resolved Hide resolved
cypress/integration/select-one.spec.js Outdated Show resolved Hide resolved
cypress/integration/select-one.spec.js Outdated Show resolved Hide resolved
public/test/select-one.html Outdated Show resolved Hide resolved
public/test/select-one.html Outdated Show resolved Hide resolved
public/test/select-one.html Outdated Show resolved Hide resolved
public/test/select-one.html Outdated Show resolved Hide resolved
src/scripts/choices.js Outdated Show resolved Hide resolved
@jshjohnson
Copy link
Collaborator

Thanks for this - i've left some comments 👍

@perifer
Copy link

perifer commented May 21, 2019

Tested this, and can confirm that it works in my app. Great job!

kzkn added 2 commits May 22, 2019 00:10
 - Save `passedElement.options` values as `this._presetOptions`
 - Restore saved `this._presetOptions` to `passedElement.options` on `destroy`
 - It avoids restoring options in `this.config.choices`
@franke-mc
Copy link

@jshjohnson: Maybe you can comment on the current status of this?

@perifer
Copy link

perifer commented Aug 21, 2019

I'd love to see this resolved. Anything I can do to help?

@kzkn
Copy link
Contributor Author

kzkn commented Aug 21, 2019

@jshjohnson re-review please?

cypress/integration/select-one.spec.js Outdated Show resolved Hide resolved
cypress/integration/select-one.spec.js Outdated Show resolved Hide resolved
public/test/select-one.html Outdated Show resolved Hide resolved
@jshjohnson jshjohnson added the bugfix Pull request that fixes an existing bug label Oct 30, 2019
@kzkn kzkn requested a review from jshjohnson November 1, 2019 03:44
@kzkn
Copy link
Contributor Author

kzkn commented Nov 2, 2019

@jshjohnson
I had fixed new indicated items from you and a conflict with master branch.

Then, I have gotten a new ambiguity.
Should the Choices selection be reproduced in the original DOM after calling destroy?

<select>
<option selected>one</option>
<option>two</option>
<option>three</option>
</select>

<script>
const select = document.querySelector('select')
console.log(select.value) // => one
const choices = new Choices(select)
// select "two" on UI
choices.destroy()
console.log(select.value) // what?
</script>

How do you think it should work?

Thanks!

@jshjohnson jshjohnson mentioned this pull request Nov 3, 2019
8 tasks
@jshjohnson
Copy link
Collaborator

jshjohnson commented Nov 14, 2019

Should the Choices selection be reproduced in the original DOM after calling destroy?

For a first pass I would say no. We can revisit this is people want to though. P.s. you have some conflicts!

@kzkn
Copy link
Contributor Author

kzkn commented Nov 15, 2019

@jshjohnson

For a first pass I would say no. We can revisit this is people want to though.

Thanks! I got it.

P.s. you have some conflicts!

I have fixed.

@filip-jk
Copy link

filip-jk commented Nov 15, 2019

@jshjohnson Can't wait for this to be released! Thanks!

@@ -144,6 +144,7 @@ class Choices {
classNames: this.config.classNames,
template: data => this._templates.option(data),
});
this._presetOptions = this.passedElement.options;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be moved above line 186 please? That's where all the "preset" variables are defined

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I have fixed.

@jshjohnson jshjohnson merged commit 0e8e42e into Choices-js:master Nov 17, 2019
@kzkn kzkn deleted the fix-573 branch November 18, 2019 06:55
@filip-jk
Copy link

@kzkn Thank you, Sir!

@jshjohnson Thank you, as well! Is there any chance to get this updated version on NPM? :)

@jshjohnson
Copy link
Collaborator

@filip-jk this has been released with 9.0.1 🎉 thanks @kzkn

@filip-jk
Copy link

@jshjohnson Ace, thanks!

@filip-jk
Copy link

@jshjohnson Sorry for bugging you about this but I think there is an issue with the destroy() method. It doesn't seem to pass the selected value?

I'm on 9.0.1 and let's say, the select I initialize Choices on has the selected value of "3". I destroy that Choices object and, unfortunately, the selected value on the tag is not "3". It gets "reset" I believe.

@jshjohnson
Copy link
Collaborator

That's expected @filip-jk #574 (comment)

@filip-jk
Copy link

@jshjohnson I'm confused. I'm initializing Choices on a select that has a selected value of "5". Choices take that selected value and I can see it in the rendered Choices dropdown menu. Once I destroy it, the select gets totally reset so it's not really the states the Choices was initialized on, right?

If that's expected than the description of the method is wrong:

"Kills the instance of Choices, removes all event listeners and returns passed input to its initial state."

It's not the initial state if the selected option/value is different.

@jshjohnson
Copy link
Collaborator

Yeah you're right, I would expect the originally selected option to persist. I'll investigate

@tagliala
Copy link
Contributor

tagliala commented Feb 8, 2022

Hi, is it possible to reopen this issue? I'm experiencing the same issue described by @filip-jk

The use case is in a stimulus.js + turbo application, I will try to provide a reduced test case, but I'm going to describe it

  1. Open a page with a Choices.js input with a selected value of 3
  2. Go back in history (that will call destroy on the Choices.js instance)
  3. Navigate forward to the page of Choices.js (that will trigger a init on the Choices.js instance)
  4. I expect 3 to be selected, but the selection is lost

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugfix Pull request that fixes an existing bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

new -> destroy does not restore preset choices
6 participants