-
Notifications
You must be signed in to change notification settings - Fork 358
add option submits_with to button #783
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
| <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> | ||
| ``` | ||
|
|
||
| Use `submits_with: :spinner` to render a default bootstrap spinner or pass your own HTML. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be nice to show an example where the user provides the HTML. |
||
| This only works on `f.button` or `f.primary` not on `f.submit` and forces `render_as_button: true` on `f.primary`. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would move this line up to near the top of this section, because some of us don't read the whole section before we start coding. ;-) |
||
|
|
||
|
|
||
| ## Rich Text Areas AKA Trix Editor | ||
|
|
||
|  | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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]) } | ||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should |
||||||||||||||
| 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 | ||||||||||||||
| <div class="spinner-border d-block mx-auto" role="status" style="--bs-spinner-width: 1lh; --bs-spinner-height: 1lh;"></div> | ||||||||||||||
| HTML | ||||||||||||||
|
Comment on lines
+50
to
+52
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you want to wrap this in
Suggested change
(I think the above is correct, but I don't guarantee it. :-) ) |
||||||||||||||
| else | ||||||||||||||
| submits_with.to_s | ||||||||||||||
| end | ||||||||||||||
| end | ||||||||||||||
| end | ||||||||||||||
| end | ||||||||||||||
| end | ||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 deafault spinner" do | ||
| expected = <<~HTML | ||
| <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">Submit with Spinner</button> | ||
| HTML | ||
|
Comment on lines
+156
to
+158
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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", submits_with: :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", submits_with: "<span>Loading...</span>".html_safe) | ||
| 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 | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.