diff --git a/lib/rails_bootstrap_form/components/check_box.rb b/lib/rails_bootstrap_form/components/check_box.rb index 83f5a65..bede92d 100644 --- a/lib/rails_bootstrap_form/components/check_box.rb +++ b/lib/rails_bootstrap_form/components/check_box.rb @@ -7,6 +7,8 @@ module Components module CheckBox extend ActiveSupport::Concern + include RailsBootstrapForm::Inputs::Base + def self.included(base_class) def check_box_label(attribute, checked_value, options, bootstrap_options, &block) unless bootstrap_options.skip_label @@ -62,11 +64,18 @@ def check_box_wrapper_class(bootstrap_options) classes = Array("form-check") classes << "form-switch" if bootstrap_options.switch classes << "form-check-inline" if bootstrap_options.inline? - classes << "mb-3" + classes << "mb-3" unless (bootstrap_options.layout_horizontal? || bootstrap_options.inline?) + classes.flatten.compact + end + + def check_box_container_classes(bootstrap_options) + classes = [bootstrap_options.field_col_wrapper_class] + classes << field_offset_class(bootstrap_options.label_col_wrapper_class) classes.flatten.compact end - private :check_box_label, :check_box_classes, :check_box_label_class, :check_box_wrapper_class + private :check_box_label, :check_box_classes, :check_box_label_class, + :check_box_wrapper_class, :check_box_container_classes end end end diff --git a/lib/rails_bootstrap_form/components/radio_button.rb b/lib/rails_bootstrap_form/components/radio_button.rb index 7f2fec7..8bec382 100644 --- a/lib/rails_bootstrap_form/components/radio_button.rb +++ b/lib/rails_bootstrap_form/components/radio_button.rb @@ -7,6 +7,8 @@ module Components module RadioButton extend ActiveSupport::Concern + include RailsBootstrapForm::Inputs::Base + def self.included(base_class) def radio_button_label(attribute, value, options, bootstrap_options) unless bootstrap_options.skip_label @@ -58,11 +60,18 @@ def radio_button_label_class(attribute, bootstrap_options, options) def radio_button_wrapper_class(bootstrap_options) classes = Array("form-check") classes << "form-check-inline" if bootstrap_options.inline? - classes << "mb-3" + classes << "mb-3" unless (bootstrap_options.layout_horizontal? || bootstrap_options.inline?) + classes.flatten.compact + end + + def radio_button_container_classes(bootstrap_options) + classes = [bootstrap_options.field_col_wrapper_class] + classes << field_offset_class(bootstrap_options.label_col_wrapper_class) classes.flatten.compact end - private :radio_button_label, :radio_button_classes, :radio_button_label_class, :radio_button_wrapper_class + private :radio_button_label, :radio_button_classes, :radio_button_label_class, + :radio_button_wrapper_class, :radio_button_container_classes end end end diff --git a/lib/rails_bootstrap_form/inputs.rb b/lib/rails_bootstrap_form/inputs.rb index a030a94..6009708 100644 --- a/lib/rails_bootstrap_form/inputs.rb +++ b/lib/rails_bootstrap_form/inputs.rb @@ -141,10 +141,22 @@ def check_box(attribute, options = {}, checked_value = "1", unchecked_value = "0 concat(check_box_field) concat(check_box_label) concat(check_box_help_text) unless bootstrap_options.inline? - concat(generate_error(attribute)) if is_invalid?(attribute) && !bootstrap_options.inline? + concat(generate_error(attribute)) if (is_invalid?(attribute) && !bootstrap_options.inline?) end - check_box_html + if bootstrap_options.inline? + check_box_html + else + if bootstrap_options.layout_horizontal? + tag.div(class: field_wrapper_classes(bootstrap_options)) do + tag.div(class: check_box_container_classes(bootstrap_options)) do + check_box_html + end + end + else + check_box_html + end + end end def radio_button(attribute, value, options = {}) @@ -161,10 +173,22 @@ def radio_button(attribute, value, options = {}) concat(radio_button_field) concat(radio_button_label) concat(radio_button_help_text) unless bootstrap_options.inline? - concat(generate_error(attribute)) if is_invalid?(attribute) && !bootstrap_options.inline? + concat(generate_error(attribute)) if (is_invalid?(attribute) && !bootstrap_options.inline?) end - radio_button_html + if bootstrap_options.inline? + radio_button_html + else + if bootstrap_options.layout_horizontal? + tag.div(class: field_wrapper_classes(bootstrap_options)) do + tag.div(class: radio_button_container_classes(bootstrap_options)) do + radio_button_html + end + end + else + radio_button_html + end + end end def collection_check_boxes(attribute, collection, value_method, text_method, options = {}, html_options = {}, &block) @@ -176,7 +200,8 @@ def collection_check_boxes(attribute, collection, value_method, text_method, opt input_value = value_method.respond_to?(:call) ? value_method.call(object) : object.send(value_method) input_options = { bootstrap_form: { - label_text: text_method.respond_to?(:call) ? text_method.call(object) : object.send(text_method) + label_text: text_method.respond_to?(:call) ? text_method.call(object) : object.send(text_method), + inline: true } }.deep_merge!(options) @@ -184,7 +209,7 @@ def collection_check_boxes(attribute, collection, value_method, text_method, opt end if options.delete(:include_hidden) { true } - inputs.prepend hidden_field(attribute, value: "", multiple: true) + inputs.prepend(hidden_field(attribute, value: "", multiple: options[:multiple])) end field_wrapper_builder(attribute, options, html_options) do @@ -201,7 +226,8 @@ def collection_radio_buttons(attribute, collection, value_method, text_method, o input_value = value_method.respond_to?(:call) ? value_method.call(object) : object.send(value_method) input_options = { bootstrap_form: { - label_text: text_method.respond_to?(:call) ? text_method.call(object) : object.send(text_method) + label_text: text_method.respond_to?(:call) ? text_method.call(object) : object.send(text_method), + inline: true } }.deep_merge!(options) diff --git a/lib/rails_bootstrap_form/inputs/base.rb b/lib/rails_bootstrap_form/inputs/base.rb index 4b27014..6813cad 100644 --- a/lib/rails_bootstrap_form/inputs/base.rb +++ b/lib/rails_bootstrap_form/inputs/base.rb @@ -21,6 +21,10 @@ def is_size_valid?(bootstrap_options) bootstrap_options.size && %i(sm lg).include?(bootstrap_options.size) end + def field_offset_class(label_col_wrapper_class) + label_col_wrapper_class.gsub(/\bcol-(\w+)-(\d)\b/, 'offset-\1-\2') + end + private :collection_input_checked?, :control_specific_class, :is_size_valid? end end