diff --git a/README.md b/README.md index b233c2d3..54e908bb 100644 --- a/README.md +++ b/README.md @@ -1065,6 +1065,20 @@ will be rendered as (some unimportant HTML attributes have been removed for simplicity) +### Turbo submits with +There is a turbo attribute [(data-turbo-submits-with)](https://turbo.hotwired.dev/reference/attributes) that replaces the content of a submit button while the request is processing. +```erb +<%= f.button "Save", submits_with: :spinner %> +``` + +```html + +``` + +Use `submits_with: :spinner` to render a default bootstrap spinner or pass your own HTML. +This only works on `f.button` or `f.primary` not on `f.submit` and forces `render_as_button: true` on `f.primary`. + + ## Rich Text Areas AKA Trix Editor  diff --git a/lib/bootstrap_form/inputs/submit.rb b/lib/bootstrap_form/inputs/submit.rb index 24c196d2..8789a73c 100644 --- a/lib/bootstrap_form/inputs/submit.rb +++ b/lib/bootstrap_form/inputs/submit.rb @@ -4,6 +4,10 @@ module BootstrapForm module Inputs module Submit def button(value=nil, options={}, &) + if options.key? :submits_with + options[:data] = { turbo_submits_with: setup_turbo_submit(options[:submits_with]) } + options.except! :submits_with + end value = setup_css_class "btn btn-secondary", value, options super end @@ -16,7 +20,7 @@ def submit(value=nil, options={}) def primary(value=nil, options={}, &block) value = setup_css_class "btn btn-primary", value, options - if options[:render_as_button] || block + if options[:render_as_button] || options[:submits_with] || block options.except! :render_as_button button(value, options, &block) else @@ -39,6 +43,17 @@ def setup_css_class(the_class, value, options) end value end + + def setup_turbo_submit(submits_with) + case submits_with + when :spinner, "spinner" + <<~HTML.strip +
+ HTML + else + submits_with.to_s + end + end end end end diff --git a/test/bootstrap_other_components_test.rb b/test/bootstrap_other_components_test.rb index d7339533..94a906a3 100644 --- a/test/bootstrap_other_components_test.rb +++ b/test/bootstrap_other_components_test.rb @@ -152,6 +152,22 @@ class BootstrapOtherComponentsTest < ActionView::TestCase @builder.button("I'm HTML! in a button!".html_safe, extra_class: "test-button") end + test "regular button has turbo-submits-with deafault spinner" do + expected = <<~HTML + + HTML + assert_equivalent_html expected, + @builder.button("Submit with Spinner", submits_with: :spinner) + end + + test "regular button has turbo-submits-with custom HTML" do + expected = <<~HTML + + HTML + assert_equivalent_html expected, + @builder.button("Submit with Spinner", submits_with: "Loading...".html_safe) + end + test "submit button defaults to rails action name" do expected = '' assert_equivalent_html expected, @builder.submit