Skip to content

Commit 8c93cd2

Browse files
committed
add option submits_with to button
1 parent aae5040 commit 8c93cd2

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,6 +1065,20 @@ will be rendered as
10651065

10661066
(some unimportant HTML attributes have been removed for simplicity)
10671067

1068+
### Turbo submits with
1069+
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.
1070+
```erb
1071+
<%= f.button "Save", submits_with: :spinner %>
1072+
```
1073+
1074+
```html
1075+
<button class="btn btn-secondary" data-turbo-submits-with="<div class="spinner-border d-block mx-auto"; role="status" style="--bs-spinner-width: 1lh; --bs-spinner-height: 1lh;"></div>" name="button" type="submit">Save</button>
1076+
```
1077+
1078+
Use `submits_with: :spinner` to render a default bootstrap spinner or pass your own HTML.
1079+
This only works on `f.button` or `f.primary` not on `f.submit` and forces `render_as_button: true` on `f.primary`.
1080+
1081+
10681082
## Rich Text Areas AKA Trix Editor
10691083

10701084
![Example 38](demo/doc/screenshots/bootstrap/readme/38_example.png "Example 38")

lib/bootstrap_form/inputs/submit.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ module BootstrapForm
44
module Inputs
55
module Submit
66
def button(value=nil, options={}, &)
7+
if options.key? :submits_with
8+
options[:data] = { turbo_submits_with: setup_turbo_submit(options[:submits_with]) }
9+
options.except! :submits_with
10+
end
711
value = setup_css_class "btn btn-secondary", value, options
812
super
913
end
@@ -16,7 +20,7 @@ def submit(value=nil, options={})
1620
def primary(value=nil, options={}, &block)
1721
value = setup_css_class "btn btn-primary", value, options
1822

19-
if options[:render_as_button] || block
23+
if options[:render_as_button] || options[:submits_with] || block
2024
options.except! :render_as_button
2125
button(value, options, &block)
2226
else
@@ -39,6 +43,17 @@ def setup_css_class(the_class, value, options)
3943
end
4044
value
4145
end
46+
47+
def setup_turbo_submit(submits_with)
48+
case submits_with
49+
when :spinner, "spinner"
50+
<<~HTML.strip
51+
<div class="spinner-border d-block mx-auto" role="status" style="--bs-spinner-width: 1lh; --bs-spinner-height: 1lh;"></div>
52+
HTML
53+
else
54+
submits_with.to_s
55+
end
56+
end
4257
end
4358
end
4459
end

test/bootstrap_other_components_test.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,22 @@ class BootstrapOtherComponentsTest < ActionView::TestCase
152152
@builder.button("<span>I'm HTML!</span> in a button!".html_safe, extra_class: "test-button")
153153
end
154154

155+
test "regular button has turbo-submits-with deafault spinner" do
156+
expected = <<~HTML
157+
<button class="btn btn-secondary" data-turbo-submits-with="<div class=&quot;spinner-border d-block mx-auto&quot; role=&quot;status&quot; style=&quot;--bs-spinner-width: 1lh; --bs-spinner-height: 1lh;&quot;></div>" name="button" type="submit">Submit with Spinner</button>
158+
HTML
159+
assert_equivalent_html expected,
160+
@builder.button("Submit with Spinner", submits_with: :spinner)
161+
end
162+
163+
test "regular button has turbo-submits-with custom HTML" do
164+
expected = <<~HTML
165+
<button class="btn btn-secondary" data-turbo-submits-with="<span>Loading...</span>" name="button" type="submit">Submit with Spinner</button>
166+
HTML
167+
assert_equivalent_html expected,
168+
@builder.button("Submit with Spinner", submits_with: "<span>Loading...</span>".html_safe)
169+
end
170+
155171
test "submit button defaults to rails action name" do
156172
expected = '<input class="btn btn-secondary" name="commit" type="submit" value="Create User" />'
157173
assert_equivalent_html expected, @builder.submit

0 commit comments

Comments
 (0)