Skip to content

fix(button): preserve value on form reset#2536

Open
kaigritun wants to merge 1 commit intoshoelace-style:nextfrom
kaigritun:fix/button-value-form-reset
Open

fix(button): preserve value on form reset#2536
kaigritun wants to merge 1 commit intoshoelace-style:nextfrom
kaigritun:fix/button-value-form-reset

Conversation

@kaigritun
Copy link

Summary

Fixes #2524

Problem

When a form containing <sl-button> elements with name/value attributes is reset, the button values were being cleared instead of being preserved.

For example:

<form>
  <sl-button type='submit' name='rtype' value='month'>List by month</sl-button>
  <sl-button type='reset'>Reset</sl-button>
</form>

After clicking the reset button, the submit button's value would become empty.

Root Cause

The FormControlController.handleFormReset() method calls setValue(host, defaultValue(host)) to restore form controls to their default values. However, sl-button didn't have a defaultValue property, so defaultValue(host) returned undefined, which was then set as the value.

Fix

Added a defaultValue property to sl-button that is initialized to the button's value in firstUpdated(). This ensures that:

  1. The initial value from the HTML attribute is captured as the default
  2. Form reset restores the button to this initial value

This matches the native HTML behavior where button values are fixed in the markup and don't change on form reset.

Testing

Added a test case to verify the button value is preserved after form reset.

Fixes shoelace-style#2524

The button's value was being cleared when the form was reset because
sl-button didn't have a defaultValue property. The FormControlController
uses defaultValue to restore values on form reset, but without it,
undefined was being set.

Added a defaultValue property that is initialized to the button's value
in firstUpdated(). This ensures form reset restores the original value
rather than clearing it.

Also added a test to verify the behavior.
@vercel
Copy link

vercel bot commented Feb 12, 2026

@kaigritun is attempting to deploy a commit to the Font Awesome Team on Vercel.

A member of the Team first needs to authorize it.

@KonnorRogers
Copy link
Collaborator

Funnily enough, native buttons don't have a defaultValue and are not subject to button resets, so they should just "stay as they are" and not try to revert.

There's a whole heuristic around what the browser does, but the TLDR is that: "if its user-editable IE: textarea, input, etc it has a value / defaultValue, for buttons, checkboxes, etc there is no defaultValue (checkboxes have defaultChecked and options have defaultSelected, but no defaultValue, its a fun time...)

@kaigritun
Copy link
Author

Thanks for the context! You're right - I checked the spec and native <button> elements don't participate in form reset for their value attribute (only inputs, textareas, and select do).

So the correct fix would be to not reset the value at all, which means removing the formResetCallback() that sets this.value = '' rather than adding defaultValue.

Want me to update the PR to just remove that line instead? Or would you prefer to close this and handle it differently?

@KonnorRogers
Copy link
Collaborator

i dont believe shoelace has a formResetCallback(), instead, what I would do is modify this check here:

https://github.com/kaigritun/shoelace/blob/5c531816841935ae4caee706ba1c125a0a2de3c3/src/internal/form.ts#L255

to be more like:

-    this.options.setValue(this.host, this.options.defaultValue(this.host));
+    if ("defaultValue" in this.host) {
+      this.options.setValue(this.host, this.options.defaultValue(this.host));
+    }

and then remove the defaultValue property from sl-button. value should probably also reflect, but that isn't necessarily pertinent to this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

<sl-button> value is cleared with form reset

2 participants