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

Restore select to initial value #1023

Closed
pieecia opened this issue Mar 21, 2022 · 12 comments
Closed

Restore select to initial value #1023

pieecia opened this issue Mar 21, 2022 · 12 comments
Labels

Comments

@pieecia
Copy link

pieecia commented Mar 21, 2022

hey,

I checked the live preview in the documentation, the button to reset the form (button type="reset") causes select not to keep the default value, select is empty

this feature works fine in version 9.0.1, the bug has appeared since version 9.1.0

how to reproduce the issue:

Zrzut ekranu 2022-03-21 o 11 26 45

@pieecia pieecia added the bug label Mar 21, 2022
@marcelaerts
Copy link

Is there a solution for this issue?

@yanmorinokamca
Copy link

We experienced the same problem. We call form.reset() between each page load.
Seems like form.reset() call Choices._onFormReset -> resetTo with _initialState.
_initialState doesn't contains the preset values from constructor.
And the rootReducer will clone the action state in "RESET_TO".

My guess is that _initialState should contains the data of _preset* at line 297 (before the bind calls)

@masiorama
Copy link

I am experiencing the same issue. Did anybody find a solution?

@masiorama
Copy link

masiorama commented Sep 9, 2022

No feedback on this? There is at least another one issue about this problem, with no answer #1053

@masiorama
Copy link

@yanmorinokamca I followed you analysis looking at the code and I guess you got it right... did you find any solution?

@masiorama
Copy link

masiorama commented Sep 9, 2022

As a temporary workaround in my application I managed to intercept reset event on the form and then repopulate all the options by hand:

document.getElementById('formId').select.addEventListener('reset', (ev) => {
    choices.setChoices(async () => {
        return this.options.map(({ value, label }) => ({
            value,
            label,
            selected: false,
        }));
    });
})

This should be unnecessary but at least my app does work. I hope this will be fixed at some point.

@yanmorinokamca
Copy link

@yanmorinokamca I followed you analysis looking at the code and I guess you got it right... did you find any solution?

Hi, we didn't have time to debug the library, so we replaced the form.reset() with a custom reset of each field.

@masiorama
Copy link

@yanmorinokamca I followed you analysis looking at the code and I guess you got it right... did you find any solution?

Hi, we didn't have time to debug the library, so we replaced the form.reset() with a custom reset of each field.

I see. Unfortunately I cannot explicitly reset each field because it is a component that has to handle different forms, with many different fields. Thanks for your reply.

@Sogl
Copy link

Sogl commented Dec 18, 2022

@masiorama I tried your code and saw that the event is not triggered:

const choicesTagsObj = new Choices('#choices-tags', {
            maxItemCount: 4,
            removeItemButton: true,
            allowHTML: false,
            position: 'bottom',
            itemSelectText: 'Push',
            maxItemText: (maxItemCount) => {
                return `Only ${maxItemCount} can be selected`;
            },
        });

document.getElementById('choices-tags').addEventListener('reset', (ev) => {
     console.log('reset');
})

what's the problem?

After some minutes

Some points:

  1. Reset event works only for a form, not for a select element.
  2. Choices deletes options structure from a select, so you need to copy it into var before create new Choices element.

Working code:

let choicesTags = document.getElementById("choices-tags");
//copy full select node with values
let choicesTagsBefore = choicesTags.cloneNode(true);
//convert to array
choicesTagsBefore = Array.from(choicesTagsBefore.options); 

const choicesTagsObj = new Choices(choicesTags, {
    maxItemCount: 4,
    removeItemButton: true,
    allowHTML: false,
    position: 'bottom',
    itemSelectText: 'Push',
    maxItemText: (maxItemCount) => {
        return `Only ${maxItemCount} can be selected`;
    },
});

//fill again after form reset (Choices.js bug workaround)
document.getElementById('therapy-form').addEventListener('reset', (ev) => {
    // console.log('reset');
    choicesTagsObj.setChoices(async () => {
        return choicesTagsBefore.map(({ value, label }) => ({
            value,
            label,
            selected: false,
        }));
    });
})

@tagliala
Copy link
Contributor

tagliala commented Dec 19, 2022

this feature works fine in version 9.0.1, the bug has appeared since version 9.1.0

Bisected to: 68313da

(which is the conversion to TypeScript 🥲)


Tracked down to

-    // Set initial state (We need to clone the state because some reducers
-    // modify the inner objects properties in the state) 🤢
-    this._initialState = cloneObject(this._store.state);

after

this._createStructure();

tagliala referenced this issue Dec 23, 2022
* Typescript config setup

* Add type annotations to components

* Further type additions

* And more...

* Add types to actions

* Add types to templates

* Further type checks

* Further type additons

* Install fuse latest

* Housekeeping

* Remove old type definitions

* Fix remaning type issues

* Fix some failing tests

* Remove types workflow

* Fix failing unit tests

* Resolve back space event regression

* Convert cypress files to .ts

* Fix eslint issues

* Remove cachebusting urls

* Resolve delete button bug

* Resolve regression bugs

* Fix lint script

* Fix lint workflow

* Pass args instead of object to keyboard handlers

* Flatten misc reducer

* Resolve keyboad action test failures

* Use Pick instead of Partial

* Use interfaces in action tests

* Update firefox image

* Incorporate #791

* Incorporate #788
@Mopster
Copy link

Mopster commented Mar 16, 2023

I've encountered this issue now and hoping to see this fixed soon.
I tried the above temporary solution, but I lose my option groups.

Xon added a commit to Xon/Choices.js that referenced this issue Jul 17, 2024
@Xon Xon mentioned this issue Aug 6, 2024
9 tasks
@Xon
Copy link
Collaborator

Xon commented Aug 22, 2024

Implemented as part of #1166

@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

No branches or pull requests

8 participants