Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<submit>` 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: "<span>Loading...</span>" } %>
```

```html
<button class="btn btn-secondary" data-turbo-submits-with="<span>Loading...</span>" name="button" type="submit">Save</button>
```


## Rich Text Areas AKA Trix Editor

![Example 38](demo/doc/screenshots/bootstrap/readme/38_example.png "Example 38")
Expand Down
8 changes: 8 additions & 0 deletions lib/bootstrap_form/helpers/bootstrap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/bootstrap_form/inputs/submit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions test/bootstrap_other_components_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,22 @@ class BootstrapOtherComponentsTest < ActionView::TestCase
@builder.button("<span>I'm HTML!</span> in a button!".html_safe, extra_class: "test-button")
end

test "regular button has turbo-submits-with default spinner" do
expected = <<~HTML
<button class="btn btn-secondary" data-turbo-submits-with='&lt;div class="text-center"&gt;&lt;div class="spinner-border spinner-border-sm" role="status"&gt;&lt;span class="visually-hidden"&gt;Loading...&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;' name="button" type="submit">Submit with Spinner</button>
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
<button class="btn btn-secondary" data-turbo-submits-with="<span>Loading...</span>" name="button" type="submit">Submit with Spinner</button>
HTML
assert_equivalent_html expected,
@builder.button("Submit with Spinner", data: { turbo_submits_with: "<span>Loading...</span>" })
end

test "submit button defaults to rails action name" do
expected = '<input class="btn btn-secondary" name="commit" type="submit" value="Create User" />'
assert_equivalent_html expected, @builder.submit
Expand Down