Make SpinnerButton form-aware for invalid form submissions#7803
Conversation
There was a problem hiding this comment.
If you make toggleSpinner an observedAttribute / callback you wouldn't need to set the attributes here -- they could just be reactive. Not sure if there's any real advantage thought because it might be more verbose in the end.
There was a problem hiding this comment.
Are you thinking something like a spinner-active attribute for the element that we'd reactively disable/enable the button, in place of what we currently use the .spinner-button--spinner-active CSS class for? I quite like that.
There was a problem hiding this comment.
Yeah, though on second thought might be cleaner to keep the toggleSpinner's setting of the button disabled attributes, like so:
static get observedAttributes(){
return [
'spinner-active'
];
}
attributeChangedCallback(name, oldVal, newVal){
if(name === 'spinner-active'){
this.toggleSpinner(!!newVal);
}
}
toggleSpinner(isActive){
if(!isActive){
this.button.removeAttribute("disabled");
} else {
this.button.setAttribute("disabled", "");
}
}And now you can just use spinner-button[spinner-active] as the selector for animation, rather than a class
There was a problem hiding this comment.
Or better yet:
attributeChangedCallback(name, oldVal, newVal){
if(name === 'spinner-active'){
this.button.toggleAttribute('disabled', !!newVal);
}
}There was a problem hiding this comment.
I like this plan. I'd probably want the separate method since we'll still need to deal with the delayed display of the "action message". I might propose to separate out that refactoring to a quick follow-on pull request, to keep this one a bit more focused on submit handling.
There was a problem hiding this comment.
This dependency on an external element when the node is connected is a bit clunky to deal with, but unfortunately the form's submit event is the most reliable way to know that all of the client-side validation has passed, which is critical both to know when captcha test should be run, as well as when the spinner button's animation should start.
There was a problem hiding this comment.
It's an edge-case, but I was curious the specific behavior of custom elements when being moved around the page while already live / attached. It looks like they will call disconnectedCallback and then connectedCallback in its new position, which will behave how we want it to.
app/javascript/packages/spinner-button/spinner-button-element.spec.ts
Outdated
Show resolved
Hide resolved
changelog: Internal, Spinner Button, Improve reliability of spinner button in validated form contexts
No longer calling form.submit directly, so observe form submission event
Now handled internal to SpinnerButton
It's used this way when SpinnerButton is rendered with Rails button_to implementatino
Handled in SpinnerButton
ed9c23e to
75d4ec5
Compare
🛠 Summary of changes
As a follow-on to #7761, this aims to allow SpinnerButton to be self-aware of form validation issues, so that the button will not show the spinning animation if the form is prevented from submitting.
In #7761, the CaptchaSubmitButtonElement was explicitly preventing the SpinnerButton from spinning when the form was invalid, but this could be managed by the SpinnerButton itself.