fix(button): preserve value on form reset#2536
fix(button): preserve value on form reset#2536kaigritun wants to merge 1 commit intoshoelace-style:nextfrom
Conversation
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.
|
@kaigritun is attempting to deploy a commit to the Font Awesome Team on Vercel. A member of the Team first needs to authorize it. |
|
Funnily enough, native buttons don't have a 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...) |
|
Thanks for the context! You're right - I checked the spec and native So the correct fix would be to not reset the value at all, which means removing the Want me to update the PR to just remove that line instead? Or would you prefer to close this and handle it differently? |
|
i dont believe shoelace has a 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 |
Summary
Fixes #2524
Problem
When a form containing
<sl-button>elements withname/valueattributes is reset, the button values were being cleared instead of being preserved.For example:
After clicking the reset button, the submit button's
valuewould become empty.Root Cause
The
FormControlController.handleFormReset()method callssetValue(host, defaultValue(host))to restore form controls to their default values. However,sl-buttondidn't have adefaultValueproperty, sodefaultValue(host)returnedundefined, which was then set as the value.Fix
Added a
defaultValueproperty tosl-buttonthat is initialized to the button'svalueinfirstUpdated(). This ensures that: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.