Skip to content

Commit 8715d10

Browse files
committed
adding a configuration option to use turbo_submits_with
1 parent aae5040 commit 8715d10

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ The current configuration options are:
233233
| Option | Default value | Description |
234234
|---------------------------|------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
235235
| `default_form_attributes` | {} | `bootstrap_form` versions 3 and 4 added a role="form" attribute to all forms. The W3C validator will raise a **warning** on forms with a role="form" attribute. `bootstrap_form` version 5 drops this attribute by default. Set this option to `{ role: "form" }` to make forms non-compliant with W3C, but generate the `role="form"` attribute like `bootstrap_form` versions 3 and 4. |
236+
| `turbo_submits_with` | {} | Add custom content to a submit button while the request is processing. Try `c.turbo_submits_with = :spinner` to check out the build-in spinner or pass a custom html string. Caveat: Only works on `form.primary` or `form.button` as `form.submit` renders a `input` file which only accepts a string and not a html element. Settings this also overrides `render_as_button` on `form.primary` |
236237

237238
Example:
238239

lib/bootstrap_form/configuration.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,25 @@ def default_form_attributes
2525
MESSAGE
2626
BootstrapForm.config.default_form_attributes
2727
end
28+
29+
def turbo_submits_with=(value)
30+
case value
31+
when nil
32+
@turbo_submits_with = nil
33+
when :spinner, "spinner"
34+
@turbo_submits_with = "<div class=\"spinner-border d-block mx-auto\" role=\"status\" style=\"--bs-spinner-width: 1lh; --bs-spinner-height: 1lh;\"><span class=\"visually-hidden\">Loading...</span></div>"
35+
when Symbol, String
36+
@turbo_submits_with = value.to_s
37+
else
38+
raise ArgumentError, "Unsupported turbo_submits_with #{value.inspect}"
39+
end
40+
end
41+
42+
def turbo_submits_with
43+
return @turbo_submits_with if defined? @turbo_submits_with
44+
45+
nil
46+
end
2847
end
2948

3049
mattr_accessor :config, default: ActiveSupport::OrderedOptions.new

lib/bootstrap_form/inputs/submit.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ module BootstrapForm
44
module Inputs
55
module Submit
66
def button(value=nil, options={}, &)
7+
if BootstrapForm.config.turbo_submits_with
8+
options.merge! data: { turbo_submits_with: BootstrapForm.config.turbo_submits_with }
9+
end
710
value = setup_css_class "btn btn-secondary", value, options
811
super
912
end
@@ -16,7 +19,7 @@ def submit(value=nil, options={})
1619
def primary(value=nil, options={}, &block)
1720
value = setup_css_class "btn btn-primary", value, options
1821

19-
if options[:render_as_button] || block
22+
if options[:render_as_button] || block || BootstrapForm.config.turbo_submits_with
2023
options.except! :render_as_button
2124
button(value, options, &block)
2225
else

0 commit comments

Comments
 (0)