From 11c2bd0a5b1e2f88d02f3eb12d372ed8cbe7e7ec Mon Sep 17 00:00:00 2001 From: Harshal LADHE Date: Sun, 21 May 2023 12:23:13 +0530 Subject: [PATCH] Added option to add custom `options` to the field's `wrapper` --- demo/app/views/users/_vertical_form.html.erb | 2 +- lib/rails_bootstrap_form/bootstrap_form_options.rb | 10 ++++++---- lib/rails_bootstrap_form/components/check_box.rb | 6 ++++++ lib/rails_bootstrap_form/components/radio_button.rb | 6 ++++++ lib/rails_bootstrap_form/field_wrapper_builder.rb | 13 ++++++++----- lib/rails_bootstrap_form/inputs.rb | 4 ++-- 6 files changed, 29 insertions(+), 12 deletions(-) diff --git a/demo/app/views/users/_vertical_form.html.erb b/demo/app/views/users/_vertical_form.html.erb index aa51aae..f060233 100644 --- a/demo/app/views/users/_vertical_form.html.erb +++ b/demo/app/views/users/_vertical_form.html.erb @@ -4,7 +4,7 @@
<%= bootstrap_form_for @user, bootstrap_form: {} do |form| %> - <%= form.text_field :name, autocomplete: "new-name", bootstrap_form: {} %> + <%= form.text_field :name, autocomplete: "new-name", class: "dsds", bootstrap_form: {} %> <%= form.text_field :email, autocomplete: "new-email", bootstrap_form: {} %> <%= form.text_field :password, autocomplete: "new-password" %> <%= form.phone_field :mobile_number %> diff --git a/lib/rails_bootstrap_form/bootstrap_form_options.rb b/lib/rails_bootstrap_form/bootstrap_form_options.rb index e857c6a..a14069b 100644 --- a/lib/rails_bootstrap_form/bootstrap_form_options.rb +++ b/lib/rails_bootstrap_form/bootstrap_form_options.rb @@ -90,6 +90,10 @@ class BootstrapFormOptions # Default is `false`. attr_accessor :switch + # Controls the HTML attributes and options that will be added to the field wrapper. + # Default is `{}`. + attr_accessor :wrapper_options + def initialize(options = {}) set_defaults set_bootstrap_form_options(options) @@ -107,10 +111,6 @@ def vertical? @layout.to_s == "vertical" end - def floating? - @layout.to_s == "floating" - end - # This will return a copy of `BootstrapFormOptions` object with new options set # that don't affect original object. This way we can have options specific # to a given form field. For example, we can change grid just for one field: @@ -152,6 +152,8 @@ def set_defaults @static_field_class = "form-control-plaintext" @switch = false + + @wrapper_options = {} end private :set_defaults diff --git a/lib/rails_bootstrap_form/components/check_box.rb b/lib/rails_bootstrap_form/components/check_box.rb index 27b8675..8541125 100644 --- a/lib/rails_bootstrap_form/components/check_box.rb +++ b/lib/rails_bootstrap_form/components/check_box.rb @@ -27,6 +27,12 @@ def check_box_label(attribute, checked_value, options, bootstrap_options, &block end end + def check_box_wrapper_options(bootstrap_options) + {}.tap do |option| + option[:class] = check_box_wrapper_class(bootstrap_options) + end.merge(bootstrap_options.wrapper_options) + end + def check_box_label_text(attribute, options, bootstrap_options, &block) block ? capture(&block) : label_text(attribute, bootstrap_options) end diff --git a/lib/rails_bootstrap_form/components/radio_button.rb b/lib/rails_bootstrap_form/components/radio_button.rb index 6882b03..5abfe53 100644 --- a/lib/rails_bootstrap_form/components/radio_button.rb +++ b/lib/rails_bootstrap_form/components/radio_button.rb @@ -28,6 +28,12 @@ def radio_button_label(attribute, value, options, bootstrap_options) end end + def radio_button_wrapper_options(bootstrap_options) + {}.tap do |option| + option[:class] = radio_button_wrapper_class(bootstrap_options) + end.merge(bootstrap_options.wrapper_options) + end + def radio_button_value(attribute, value) # label's `for` attribute needs to match checkbox tag's id, # IE sanitized value, IE diff --git a/lib/rails_bootstrap_form/field_wrapper_builder.rb b/lib/rails_bootstrap_form/field_wrapper_builder.rb index cbdc7a0..15a2824 100644 --- a/lib/rails_bootstrap_form/field_wrapper_builder.rb +++ b/lib/rails_bootstrap_form/field_wrapper_builder.rb @@ -17,7 +17,7 @@ def field_wrapper(attribute, bootstrap_options, options, &block) help_text = help_text(attribute, bootstrap_options) if bootstrap_options.floating - tag.div(class: field_wrapper_classes) do + tag.div(**field_wrapper_options(bootstrap_options)) do concat(input_group_wrapper(attribute, bootstrap_options) do tag.div(class: floating_label_classes(attribute)) do concat(capture(&block)) @@ -27,7 +27,7 @@ def field_wrapper(attribute, bootstrap_options, options, &block) concat(help_text) end else - tag.div(class: field_wrapper_classes) do + tag.div(**field_wrapper_options(bootstrap_options)) do concat(label) concat(input_group_wrapper(attribute, bootstrap_options) do capture(&block) @@ -37,14 +37,17 @@ def field_wrapper(attribute, bootstrap_options, options, &block) end end + def field_wrapper_options(bootstrap_options) + {}.tap do |option| + option[:class] = field_wrapper_classes + end.merge(bootstrap_options.wrapper_options) + end + def field_wrapper_classes classes = [form_wrapper_default_class] classes.flatten.compact end - def field_wrapper_options - end - def form_wrapper_default_class "mb-3" end diff --git a/lib/rails_bootstrap_form/inputs.rb b/lib/rails_bootstrap_form/inputs.rb index 931e636..bc4e045 100644 --- a/lib/rails_bootstrap_form/inputs.rb +++ b/lib/rails_bootstrap_form/inputs.rb @@ -137,7 +137,7 @@ def check_box(attribute, options = {}, checked_value = "1", unchecked_value = "0 check_box_label = check_box_label(attribute, checked_value, options, bootstrap_options, &block) - check_box_html = tag.div(class: check_box_wrapper_class(bootstrap_options)) do + check_box_html = tag.div(**check_box_wrapper_options(bootstrap_options)) do concat(check_box_field) concat(check_box_label) concat(check_box_help_text) unless bootstrap_options.inline? @@ -157,7 +157,7 @@ def radio_button(attribute, value, options = {}) radio_button_label = radio_button_label(attribute, value, options, bootstrap_options) - radio_button_html = tag.div(class: radio_button_wrapper_class(bootstrap_options)) do + radio_button_html = tag.div(**radio_button_wrapper_options(bootstrap_options)) do concat(radio_button_field) concat(radio_button_label) concat(radio_button_help_text) unless bootstrap_options.inline?