From 8c93cd201f3319451bf5ef38c4df34b9348746de Mon Sep 17 00:00:00 2001 From: Jonas Pardeyke Date: Thu, 27 Nov 2025 10:53:49 +0100 Subject: [PATCH 1/5] add option submits_with to button --- README.md | 14 ++++++++++++++ lib/bootstrap_form/inputs/submit.rb | 17 ++++++++++++++++- test/bootstrap_other_components_test.rb | 16 ++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) 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 ![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..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 From eba4510148ea47dee9e62c7ec8212bdeb03d1977 Mon Sep 17 00:00:00 2001 From: Jonas Pardeyke Date: Thu, 27 Nov 2025 11:21:35 +0100 Subject: [PATCH 2/5] use spinner by default --- lib/bootstrap_form/inputs/submit.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/bootstrap_form/inputs/submit.rb b/lib/bootstrap_form/inputs/submit.rb index 8789a73c..8de7fbd0 100644 --- a/lib/bootstrap_form/inputs/submit.rb +++ b/lib/bootstrap_form/inputs/submit.rb @@ -4,10 +4,12 @@ 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 + # if options.key? :submits_with + # options[:data] = { turbo_submits_with: setup_turbo_submit(options[:submits_with]) } + # options.except! :submits_with + # end + options[:data] = { turbo_submits_with: setup_turbo_submit(:spinner) } + value = setup_css_class "btn btn-secondary", value, options super end From b72780fbd9bfbbdedd869576d97be7c83046e6e3 Mon Sep 17 00:00:00 2001 From: Jonas Pardeyke Date: Thu, 27 Nov 2025 11:22:52 +0100 Subject: [PATCH 3/5] undo --- lib/bootstrap_form/inputs/submit.rb | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/lib/bootstrap_form/inputs/submit.rb b/lib/bootstrap_form/inputs/submit.rb index 8de7fbd0..8789a73c 100644 --- a/lib/bootstrap_form/inputs/submit.rb +++ b/lib/bootstrap_form/inputs/submit.rb @@ -4,12 +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 - options[:data] = { turbo_submits_with: setup_turbo_submit(:spinner) } - + 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 From fa254f53a8943789ec435e363f5dc8178e8af753 Mon Sep 17 00:00:00 2001 From: Jonas Pardeyke Date: Wed, 10 Dec 2025 15:58:48 +0100 Subject: [PATCH 4/5] -let users put in the data attribute themselves -update readme -todo: add f.spinner --- README.md | 13 +++++++------ lib/bootstrap_form/inputs/submit.rb | 8 ++------ 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 54e908bb..d4088150 100644 --- a/README.md +++ b/README.md @@ -1066,18 +1066,19 @@ 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. +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", submits_with: :spinner %> +<%= f.button "Save", data: { turbo_submits_with: f.spinner } %> + +<%= f.button "Save", data: { turbo_submits_with: "Loading..." } %> ``` ```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 8789a73c..76908902 100644 --- a/lib/bootstrap_form/inputs/submit.rb +++ b/lib/bootstrap_form/inputs/submit.rb @@ -4,10 +4,6 @@ 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 @@ -20,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] || options[:submits_with] || block + if options[:render_as_button] || options[:data][:turbo_submits_with] || block options.except! :render_as_button button(value, options, &block) else @@ -47,7 +43,7 @@ def setup_css_class(the_class, value, options) def setup_turbo_submit(submits_with) case submits_with when :spinner, "spinner" - <<~HTML.strip + raw(<<~HTML.strip)
HTML else From 693b4559a0287ab4005c3660dc58fb24d5091c1d Mon Sep 17 00:00:00 2001 From: Jonas Pardeyke Date: Wed, 10 Dec 2025 16:07:49 +0100 Subject: [PATCH 5/5] updated tests --- test/bootstrap_other_components_test.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/bootstrap_other_components_test.rb b/test/bootstrap_other_components_test.rb index 94a906a3..9bfaa01b 100644 --- a/test/bootstrap_other_components_test.rb +++ b/test/bootstrap_other_components_test.rb @@ -154,10 +154,10 @@ class BootstrapOtherComponentsTest < ActionView::TestCase 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) + @builder.button("Submit with Spinner", data: { turbo_submits_with: f.spinner }) end test "regular button has turbo-submits-with custom HTML" do @@ -165,7 +165,7 @@ class BootstrapOtherComponentsTest < ActionView::TestCase HTML assert_equivalent_html expected, - @builder.button("Submit with Spinner", submits_with: "Loading...".html_safe) + @builder.button("Submit with Spinner", data: { turbo_submits_with: "Loading..." }) end test "submit button defaults to rails action name" do