Skip to content

[Feature]: Support Default Values in Form ControlsΒ #9230

@ITenthusiasm

Description

@ITenthusiasm

Describe the problem

Unfortunately, as of today, Svelte does not support default values1 for form controls. For example, Svelte renders

<input value="default-value" />

as

<input />
<textarea>default text</textarea>

as

<textarea></textarea>

and

<select>
  <option>1</option>
  <option selected>2</option>
</select>

as

<select>
  <option value="1">1</option>
  <option value="2">2</option>
</select>

(It's also a little odd that the value attribute is added here when it wasn't specified in the HTML. But for now, I'm only interested in the missing selected attribute.)

This dissonance between Svelte and HTML results in an unintuitive experience for developers intending to use Svelte as a superset of HTML, and it acts a roadblock to important features in forms. One example is that this behavior prevents form resets from behaving correctly, as pointed out in #8220. Another example is that it prevents developers from using the defaultValue/defaultChecked/defaultSelected attributes of fields to identify "dirty controls" -- a common use case in forms.

When using native HTML/CSS/JS, these default* properties can be used to check which fields are dirty. The same code works just fine in Solid.js and Vue. (It's worth noting that neither Vue nor Solid add the value attribute to the options if it wasn't specified.) Amazingly, React almost completely supports this use case as well. Only select elements don't work because they don't support the selected attribute currently. So I was a little surprised to see that Svelte doesn't support this use case at all -- not even for inputs. (If you see things turn red in Svelte's demo, it's because JS is comparing field values with non-existing default values the entire time, not with anything that's an actual default value. So everything stays red -- except the checkbox, for obvious reasons.)

It would be fantastic if Svelte could support default values. I would even be bold enough to say that this is a necessary feature (since not having the feature is technically a regression from browser behavior).

Describe the proposed solution

During SSR and CSR, Svelte should render all form controls with the exact markup that they're given. That way, default values will be supported out of the box, and no attributes will unexpectedly disappear from (or force themselves upon) form controls.

Alternatives considered

Using state is a possible way around this problem, but it's more complicated and likely less performant for obvious reasons. Besides that, I don't think there's an alternative solution to preserving the developer's intended markup for form fields.

Importance

I cannot use Svelte without it

Footnotes

  1. It seems that svelte uses JS to simulate giving a form control a default value. However, this is not a "true" default value. It's more like an onMount side effect that still leaves the developer without the browser's features surrounding default values. Hope that isn't too confusing. ↩

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions