fix: don't reset <select> elements to the first option after a remote form submission#16107
fix: don't reset <select> elements to the first option after a remote form submission#16107RazinShafayet2007 wants to merge 9 commits into
<select> elements to the first option after a remote form submission#16107Conversation
🦋 Changeset detectedLatest commit: 5cc79d5 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
| if (select.multiple) { | ||
| if (default_options.length > 0) { | ||
| for (const opt of default_options) fd.append(name, opt.value); | ||
| } else if (submitted_data && submitted_data[name] !== undefined) { |
There was a problem hiding this comment.
In read_default_values, the captured submitted value is looked up with the raw DOM element name, but submitted_data keys were normalized by convert_formdata (stripped n:/b: prefix and [] suffix, nested paths), so the lookup always misses for prefixed/suffixed/nested select names and falls back to the stale DOM.
9f463c8 to
8e9b2eb
Compare
|
This is pretty complicated for what should just be (also, this PR won't work for when |
|
@ottomated You were right, there was a real gap in I agree it's not the prettiest but the complexity comes from working around the DOM's limits, |
Fixes #16093
After a successful remote-form submission, the framework called native
form.reset()to clear the form. For<select>elements, native reset restores whichever<option>has theselectedattribute, or falls back to the first option if none does — and since most<select>markup doesn't declare an explicit default, every successful submission caused the select to snap back to its first option, discarding what the user actually picked.This PR replaces that reset call with
read_default_values(), which resolves each field to its correct post-submit state explicitly rather than delegating to the browser. Text inputs, checkboxes, radios, and textareas keep the same behavior as before (defaultValue/defaultChecked). For<select>, a genuine, non-disabled declared default is restored just likedefaultValueon a text input, while a select with no usable default — none declared, or only a disabled placeholder like<option selected disabled>— preserves the submitted value instead, captured at submit time to avoid a race condition with the DOM. The same logic applies to multi-select. Tests cover all four resulting behaviors, including multi-select restoring every declared default.Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
Tests
pnpm testand lint the project withpnpm lintandpnpm checkChangesets
pnpm changesetand following the prompts. Changesets that add features should beminorand those that fix bugs should bepatch. Please prefix changeset messages withfeat:,fix:, orchore:.Edits