diff --git a/cypress/integration/select-one.spec.js b/cypress/integration/select-one.spec.js index c44c907..3d2c35d 100644 --- a/cypress/integration/select-one.spec.js +++ b/cypress/integration/select-one.spec.js @@ -1022,5 +1022,39 @@ describe('Choices - select one', () => { }); }); }); + + describe('re-initialising a choices instance', () => { + it('preserves the choices list', () => { + cy.get('[data-test-hook=new-destroy-init]') + .find('.choices__list--dropdown .choices__list') + .children() + .should('have.length', 3); + + cy.get('[data-test-hook=new-destroy-init]') + .find('button.destroy') + .click(); + cy.get('[data-test-hook=new-destroy-init]') + .find('button.init') + .click(); + + cy.get('[data-test-hook=new-destroy-init]') + .find('.choices__list--dropdown .choices__list') + .children() + .should('have.length', 3); + }); + }); + + describe('destroying the choices instance', () => { + it('preserves the original select element', () => { + cy.get('[data-test-hook=new-destroy-init]') + .find('button.destroy') + .click(); + + cy.get('[data-test-hook=new-destroy-init]') + .find('select') + .children() + .should('have.length', 3); + }); + }); }); }); diff --git a/public/test/select-one/index.html b/public/test/select-one/index.html index 4532b15..b567b28 100644 --- a/public/test/select-one/index.html +++ b/public/test/select-one/index.html @@ -359,6 +359,21 @@ + +
+ + + + +
diff --git a/src/scripts/choices.js b/src/scripts/choices.js index 38f6551..1d69104 100644 --- a/src/scripts/choices.js +++ b/src/scripts/choices.js @@ -183,6 +183,8 @@ class Choices { }; // Assign preset groups from passed element this._presetGroups = this.passedElement.optionGroups; + // Assign preset options from passed element + this._presetOptions = this.passedElement.options; // Assign preset choices from passed object this._presetChoices = this.config.choices; // Assign preset items from passed object first @@ -283,11 +285,12 @@ class Choices { this.passedElement.reveal(); this.containerOuter.unwrap(this.passedElement.element); + this.clearStore(); + if (this._isSelectElement) { - this.passedElement.options = this._presetChoices; + this.passedElement.options = this._presetOptions; } - this.clearStore(); this._templates = null; this.initialised = false; }