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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ The current configuration options are:
| Option | Default value | Description |
|---------------------------|------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `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. |
| `turbo_submits_with` | {} | Add custom content to a submit button while the request is processing. Try `c.turbo_submits_with = :spinner` to use the built-in spinner, or pass a custom HTML string. Caveat: Only works on `form.primary` or `form.button` as `form.submit` renders a `input` field which only accepts a string and not an HTML element. Settings this also forces `render_as_button` on `form.primary` |

Example:

Expand Down
19 changes: 19 additions & 0 deletions lib/bootstrap_form/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,25 @@ def default_form_attributes
MESSAGE
BootstrapForm.config.default_form_attributes
end

def turbo_submits_with=(value)
case value
when nil
@turbo_submits_with = nil
when :spinner, "spinner"
@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>"
when Symbol, String
@turbo_submits_with = value.to_s
else
raise ArgumentError, "Unsupported turbo_submits_with #{value.inspect}"
end
end

def turbo_submits_with
return @turbo_submits_with if defined? @turbo_submits_with

nil
end
end

mattr_accessor :config, default: ActiveSupport::OrderedOptions.new
Expand Down
5 changes: 4 additions & 1 deletion lib/bootstrap_form/inputs/submit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ module BootstrapForm
module Inputs
module Submit
def button(value=nil, options={}, &)
if BootstrapForm.config.turbo_submits_with
options.merge! data: { turbo_submits_with: BootstrapForm.config.turbo_submits_with }
end
value = setup_css_class "btn btn-secondary", value, options
super
end
Expand All @@ -16,7 +19,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] || block || BootstrapForm.config.turbo_submits_with
options.except! :render_as_button
button(value, options, &block)
else
Expand Down