diff --git a/README.md b/README.md index 1899ff7b..ae30ec81 100644 --- a/README.md +++ b/README.md @@ -1068,6 +1068,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/helpers/bootstrap.rb b/lib/bootstrap_form/helpers/bootstrap.rb index 1cf39ce4..ba1c002b 100644 --- a/lib/bootstrap_form/helpers/bootstrap.rb +++ b/lib/bootstrap_form/helpers/bootstrap.rb @@ -96,6 +96,14 @@ def static_class "form-control-plaintext" end + def spinner + tag.div(class: "text-center") do + tag.div(class: "spinner-border spinner-border-sm", role: "status") do + tag.span("Loading...", class: "visually-hidden") + end + end + end + private def attach_input(options, key) diff --git a/lib/bootstrap_form/inputs/submit.rb b/lib/bootstrap_form/inputs/submit.rb index 24c196d2..5017aba3 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.dig(:data, :turbo_submits_with) || block options.except! :render_as_button button(value, options, &block) else diff --git a/test/bootstrap_other_components_test.rb b/test/bootstrap_other_components_test.rb index d7339533..4aa84b52 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 default spinner" do + expected = <<~HTML + + HTML + assert_equivalent_html expected, + @builder.button("Submit with Spinner", data: { turbo_submits_with: @builder.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