From f5e2c5ce0a2cb817d18cecd13202a40e8cc065b6 Mon Sep 17 00:00:00 2001 From: Pedro Date: Thu, 12 Nov 2026 22:31:15 -0300 Subject: [PATCH] Fix createSelect().enable() resetting the selected option --- src/dom/dom.js | 3 +-- test/unit/dom/dom.js | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/dom/dom.js b/src/dom/dom.js index a8ff324891..6f011aae08 100644 --- a/src/dom/dom.js +++ b/src/dom/dom.js @@ -995,6 +995,7 @@ function dom(p5, fn) { * - `mySelect.disable(option)` marks a given option as disabled. * - `mySelect.enable()` marks the whole dropdown element as enabled. * - `mySelect.enable(option)` marks a given option as enabled. + * - Enabling a dropdown or an option doesn't change which option is selected. * * @method createSelect * @param {Boolean} [multiple] support multiple selections. @@ -1226,14 +1227,12 @@ function dom(p5, fn) { for (let i = 0; i < this.elt.length; i++) { if (this.elt[i].value.toString() === value) { this.elt[i].disabled = false; - this.elt[i].selected = false; } } } else { this.elt.disabled = false; for (let i = 0; i < this.elt.length; i++) { this.elt[i].disabled = false; - this.elt[i].selected = false; } } return this; diff --git a/test/unit/dom/dom.js b/test/unit/dom/dom.js index d74ff4dc30..1a65e5b266 100644 --- a/test/unit/dom/dom.js +++ b/test/unit/dom/dom.js @@ -701,6 +701,31 @@ suite('DOM', function () { testElement.disable(options[disabledIndex]); assert.isTrue(testElement.elt.options[disabledIndex].disabled); assert.isFalse(testElement.elt.options[disabledIndex].selected); + }); + test('calling enable() should not change the selected option', function () { + const testElement = mockP5Prototype.createSelect(); + const options = ['red', 'yellow', 'blue']; + for (const optionName of options) testElement.option(optionName); + + testElement.selected('blue'); + testElement.disable(); + testElement.enable(); + + assert.isFalse(testElement.elt.disabled); + assert.equal(testElement.value(), 'blue'); + assert.equal(testElement.selected(), 'blue'); + }); + + test('calling enable(option) should not change the selected option', function () { + const testElement = mockP5Prototype.createSelect(); + const options = ['red', 'yellow', 'blue']; + for (const optionName of options) testElement.option(optionName); + + testElement.selected('blue'); + testElement.enable('blue'); + + assert.isFalse(testElement.elt.options[2].disabled); + assert.equal(testElement.value(), 'blue'); }); });