-
-
Notifications
You must be signed in to change notification settings - Fork 787
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(form): add reset functionality #799
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
Thanks for tackling this. I like your approach and I'll spend more time testing it out soon. One thing I noticed is it's missing some controller options on a few form controls. At a glance, I don't think they're required since they should use the Form Controller's default values — but I just want to confirm that they're all covered. Here's a list of all controls that submit:
|
I had a chance to test each form control individually. All of the aforementioned controls reset correctly except for the following: Checkboxes don't reset to their initial checked state. Uncheck the checkbox, then hit reset. The checkbox is still unchecked. <form>
<sl-checkbox checked>Click me</sl-checkbox>
<br><br>
<sl-button type="reset" variant="primary">Reset</sl-button>
</form> Radios don't reset to their initial checked state. Select a different option, then hit reset. No radios are checked. <form>
<sl-radio-group label="Select an option">
<sl-radio name="option" value="1" checked>Option 1</sl-radio>
<sl-radio name="option" value="2">Option 2</sl-radio>
<sl-radio name="option" value="3">Option 3</sl-radio>
</sl-radio-group>
<br /><br />
<sl-button type="reset" variant="primary">Reset</sl-button>
</form> Radio buttons don't reset to their initial checked state. Select a different option, then hit reset. No radio buttons are checked. <form>
<sl-radio-group label="Select an option">
<sl-radio-button name="a" value="1" checked>Option 1</sl-radio-button>
<sl-radio-button name="a" value="2">Option 2</sl-radio-button>
<sl-radio-button name="a" value="3">Option 3</sl-radio-button>
</sl-radio-group>
<br /><br />
<sl-button type="reset" variant="primary">Reset</sl-button>
</form> Switches don't reset to their initial checked state. Uncheck the switch, then hit reset. The checkbox is still unchecked. <form>
<sl-switch checked>Switch</sl-switch>
<br /><br />
<sl-button type="reset" variant="primary">Reset</sl-button>
</form> I also noticed that Let me know what you think! |
Hi @claviska, yes I think that exposing A possible limitation I see here is that the I'll take a look at the components that are not resetting properly, and I'll also investigate a bit further about this topic |
💯 Those should continue to reflect to remain consistent with the rest of the library. We’ll need to work around that, possibly with an extra property. |
I think I found a possible solution. The spec says that the
For the second, we just have to update the property value and that's all. For the first, we could listen the // pseudo code
class Element {
attributeChangedCallback(name, oldValue, newValue) {
if (name === 'checked') {
if (this.checked !== newValue)
this.defaultChecked = newValue;
}
}
} The code above could be abstracted in a decorator for simplicity (that I'm going to push soon) class Element {
@property({ type: Boolean, reflect: true })
checked = false;
@defaultValue('checked')
defaultChecked = false;
} What do you think? |
this is the idea implemented in the checkbox component |
I added the defaultValue/defaultChecked properties to all the components that need it. This solved also the issues I had with some of them. I implemented some unit tests as well. |
Great work on this. Thanks for taking the time to submit this, and especially for adding tests. There are a couple of sort order lint rules and a failing focus test in WebKit, but I'll handle those. |
This PR adds the form reset functionality to SL components.
Two new options has been added to FormSubmitController:
defaultValue
- returns the control's default valuesetValue
- a function used to set the control's valueThe idea is to set the value originally defined in the component attribute when the form's reset event is fired (spec). To reset the form through the SL button, this implementation uses the same approach used for the submission.