diff --git a/README.md b/README.md index 5d868003..db114bd7 100644 --- a/README.md +++ b/README.md @@ -236,6 +236,7 @@ The current configuration options are: |---------------------------|------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | `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. | | `group_around_collections` | false | Historically, `bootstrap_form` generated a wrapper around `collection_checkboxes` and `collection_radio_buttons` using the same `form_group` as individual controls used. This markup caused accessibility problems. Setting `group_around_collections = true` will generate collections of checkboxes and radio buttons wrapper in a `
` with the text as a `` (https://www.w3.org/WAI/tutorials/forms/grouping/). This _will_ make visible changes to pages that use the collection methods.

The default for this option will be changed to `true` in a future version. | +| `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/gemfiles/8.0.gemfile b/gemfiles/8.0.gemfile index adbc130f..74dd2398 100644 --- a/gemfiles/8.0.gemfile +++ b/gemfiles/8.0.gemfile @@ -3,9 +3,9 @@ eval File.read(gems), binding, gems # rubocop: disable Security/Eval gem "bigdecimal" if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("3.4.0") gem "drb" if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("3.4.0") +gem "json", "~> 2.21" gem "minitest", "~> 5.0" gem "mutex_m" if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("3.4.0") gem "propshaft" gem "rails", "~> 8.0.1" gem "sqlite3" -gem "json", "~> 2.21" diff --git a/lib/bootstrap_form/configuration.rb b/lib/bootstrap_form/configuration.rb index fdd2e0d2..e5695ed2 100644 --- a/lib/bootstrap_form/configuration.rb +++ b/lib/bootstrap_form/configuration.rb @@ -25,6 +25,27 @@ 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..bfade1f7 100644 --- a/lib/bootstrap_form/inputs/submit.rb +++ b/lib/bootstrap_form/inputs/submit.rb @@ -4,6 +4,8 @@ module BootstrapForm module Inputs module Submit def button(value=nil, options={}, &) + turbo_submits_with = BootstrapForm.config.turbo_submits_with + options[:data] = { turbo_submits_with: turbo_submits_with } if turbo_submits_with value = setup_css_class "btn btn-secondary", value, options super end @@ -16,7 +18,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