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
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1068,6 +1068,32 @@ 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. Turbo supports it on both `<button>` and `<input type="submit">` elements, but an `<input>` can only show plain text, so HTML content such as a spinner needs a `<button>`. Use `f.button`, or `f.primary` with `render_as_button: true`, when the replacement content is HTML.

Plain text works with the normal submit input:
```erb
<%= f.primary "Save", data: { turbo_submits_with: "Saving..." } %>
```

```html
<input class="btn btn-primary" data-turbo-submits-with="Saving..." name="commit" type="submit" value="Save">
```

You can have `bootstrap_form` put up a default bootstrap spinner while the request is processing with `f.spinner`, or provide your own HTML:
```erb
<%= f.button "Save", data: { turbo_submits_with: f.spinner } %>

<%= f.primary "Save", render_as_button: true, 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
31 changes: 31 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
Comment on lines +156 to +158

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The expected HTML should not be escaped.

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 Expand Up @@ -182,6 +198,21 @@ class BootstrapOtherComponentsTest < ActionView::TestCase
assert_equivalent_html expected, @builder.primary("Submit Form", extra_class: "test-button")
end

test "primary button with plain-text turbo-submits-with still renders a submit input" do
expected = <<~HTML
<input class="btn btn-primary" data-turbo-submits-with="Saving..." name="commit" type="submit" value="Submit Form" />
HTML
assert_equivalent_html expected, @builder.primary("Submit Form", data: { turbo_submits_with: "Saving..." })
end

test "primary button with render_as_button and turbo-submits-with spinner renders a button" do
expected = <<~HTML
<button class="btn btn-primary" 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 Form</button>
HTML
assert_equivalent_html expected,
@builder.primary("Submit Form", render_as_button: true, data: { turbo_submits_with: @builder.spinner })
end

test "primary button can render as HTML button" do
expected = %q(<button class="btn btn-primary" name="button" type="submit"><span>I'm HTML!</span> Submit Form</button>)
assert_equivalent_html expected,
Expand Down