Skip to content

Make SpinnerButton form-aware for invalid form submissions#7803

Merged
aduth merged 8 commits intomainfrom
aduth-spinner-button-form
Mar 24, 2023
Merged

Make SpinnerButton form-aware for invalid form submissions#7803
aduth merged 8 commits intomainfrom
aduth-spinner-button-form

Conversation

@aduth
Copy link
Contributor

@aduth aduth commented Feb 9, 2023

🛠 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.

Comment on lines 65 to 72
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor

@eric-gade eric-gade Feb 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or better yet:

attributeChangedCallback(name, oldVal, newVal){
    if(name === 'spinner-active'){
      this.button.toggleAttribute('disabled', !!newVal);
    }
  }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@aduth aduth changed the title Make SpinnerButton form aware for invalid form submissions Make SpinnerButton form-aware for invalid form submissions Feb 10, 2023
@aduth aduth marked this pull request as ready for review February 10, 2023 20:58
Copy link
Contributor Author

@aduth aduth Feb 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

https://codepen.io/aduth/pen/rNZodWj?editors=1111

Copy link
Contributor

@zachmargolis zachmargolis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

aduth added 8 commits March 24, 2023 12:43
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
@aduth aduth force-pushed the aduth-spinner-button-form branch from ed9c23e to 75d4ec5 Compare March 24, 2023 16:46
@aduth aduth merged commit f1daadd into main Mar 24, 2023
@aduth aduth deleted the aduth-spinner-button-form branch March 24, 2023 18:21
@aduth aduth mentioned this pull request Mar 27, 2023
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.

3 participants