diff --git a/README.md b/README.md index b233c2d3..d4088150 100644 --- a/README.md +++ b/README.md @@ -1065,6 +1065,21 @@ 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. This only works on `f.button` or `f.primary` not on `f.submit` and forces `render_as_button: true` on `f.primary` since `` tags only allow text content. + +You can have `bootstrap_form` put up a default bootstrap spinner while the request is processing with the following: +```erb +<%= f.button "Save", data: { turbo_submits_with: f.spinner } %> + +<%= f.button "Save", data: { turbo_submits_with: "Loading..." } %> +``` + +```html + +``` + + ## Rich Text Areas AKA Trix Editor ![Example 38](demo/doc/screenshots/bootstrap/readme/38_example.png "Example 38") diff --git a/lib/bootstrap_form/inputs/submit.rb b/lib/bootstrap_form/inputs/submit.rb index 24c196d2..76908902 100644 --- a/lib/bootstrap_form/inputs/submit.rb +++ b/lib/bootstrap_form/inputs/submit.rb @@ -16,7 +16,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[:data][:turbo_submits_with] || block options.except! :render_as_button button(value, options, &block) else @@ -39,6 +39,17 @@ def setup_css_class(the_class, value, options) end value end + + def setup_turbo_submit(submits_with) + case submits_with + when :spinner, "spinner" + raw(<<~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..9bfaa01b 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", data: { turbo_submits_with: f.spinner }) + end + + test "regular button has turbo-submits-with custom HTML" do + expected = <<~HTML + + HTML + assert_equivalent_html expected, + @builder.button("Submit with Spinner", data: { turbo_submits_with: "Loading..." }) + end + test "submit button defaults to rails action name" do expected = '' assert_equivalent_html expected, @builder.submit