diff --git a/README.md b/README.md index b233c2d3..3f001b9d 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/lib/bootstrap_form/configuration.rb b/lib/bootstrap_form/configuration.rb index fdd2e0d2..d5a73689 100644 --- a/lib/bootstrap_form/configuration.rb +++ b/lib/bootstrap_form/configuration.rb @@ -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 = "
Loading...
" + 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 diff --git a/lib/bootstrap_form/inputs/submit.rb b/lib/bootstrap_form/inputs/submit.rb index 24c196d2..71ec812b 100644 --- a/lib/bootstrap_form/inputs/submit.rb +++ b/lib/bootstrap_form/inputs/submit.rb @@ -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 @@ -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