I'm starting to use Hotwire in my Rails app and I implemented something similar to the tutorial video where a form gets submitted and refresh another part of the page. Like in the video, I had to implement my own reset form controller:
import {Controller} from "stimulus"
export default class extends Controller {
reset() {
this.element.reset()
}
}
to reset the values in the form. The form looks something like:
<%= form_with(model: @invitation,
data: {controller: "reset-form",
action: "turbo:submit-end->reset-form#reset"}) do |form| %>
<!-- other form elements -->
<%= form.submit "Invite" %>
<% end %>
which generates in button like this:
<input type="submit" name="commit" value="Invite" data-disable-with="Invite">
Because there's a data-disable-with
, when you press the button, it gets disabled to avoid double-click submissions, which is great. The problem is that this.element.reset()
doesn't reset it.
What's the proper way of dealing with this?
I'm not searching for a workaround, I know many, but I'm searching for the clean solution to this problem.
Is this disabling of a button caused by UJS? does it mean UJS shouldn't be used in Stimulus application?
I can re-enable the button from the reset
JavaScript function but if the input button is specified like this:
<%= form.submit "Invite", data: {disable_with: "Inviting, please wait..."} %>
then the original label (value
) of the button is lost and I don't have a way to re-establish it, which makes me think whatever is implementing this functionality (UJS?) it's not designed for hotwire/spa applications, and expects a full reload of the page.
I could just throw:
config.action_view.automatically_disable_submit_tag = false
and implement my own double-click prevention with a Stimulus controller, but that feels wrong. The problem is not with the attribute data-disable-with
but how it was implemented.