Skip to content

Commit

Permalink
Added support to render check boxes and radio buttons in `horizon…
Browse files Browse the repository at this point in the history
…tal` forms
  • Loading branch information
shivam091 committed May 22, 2023
1 parent f51d0b3 commit a0efc56
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 11 deletions.
13 changes: 11 additions & 2 deletions lib/rails_bootstrap_form/components/check_box.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
13 changes: 11 additions & 2 deletions lib/rails_bootstrap_form/components/radio_button.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
40 changes: 33 additions & 7 deletions lib/rails_bootstrap_form/inputs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {})
Expand All @@ -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)
Expand All @@ -176,15 +200,16 @@ 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)

inputs << check_box(attribute, input_options, input_value, nil)
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
Expand All @@ -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)

Expand Down
4 changes: 4 additions & 0 deletions lib/rails_bootstrap_form/inputs/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a0efc56

Please sign in to comment.