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` form
  • Loading branch information
shivam091 committed May 22, 2023
1 parent 64f1dcc commit 3b572c6
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 15 deletions.
2 changes: 1 addition & 1 deletion demo/app/views/users/_horizontal_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<%= form.radio_button :terms, bootstrap_form: {switch: false}, required: true %>
<%= form.range_field :excellence %>
<%= form.url_field :blog_url %>
<%= form.collection_radio_buttons :fruit_id, ::Fruit.all, :id, :name, {bootstrap_form: {inline: true}, checked: form.object.fruit_id} %>
<%= form.collection_radio_buttons :fruit_id, ::Fruit.all, :id, :name, {bootstrap_form: {inline: false}, checked: form.object.fruit_id} %>
<%= form.color_field :favorite_color %>
<%= form.collection_check_boxes :skill_ids, ::Skill.all, :id, :name, {bootstrap_form: {inline: true}} %>
<%= form.fields_for :address, include_id: false, bootstrap_form: {layout: :horizontal} do |address_form| %>
Expand Down
11 changes: 9 additions & 2 deletions lib/rails_bootstrap_form/components/check_box.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,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" if bootstrap_options.layout_vertical?
classes << "mb-3" if (!bootstrap_options.inline? && !bootstrap_options.layout_horizontal?)
classes.flatten.compact
end

private :check_box_label, :check_box_classes, :check_box_label_class, :check_box_wrapper_class
def check_box_container_classes(bootstrap_options)
classes = [bootstrap_options.field_col_wrapper_class]
classes << bootstrap_options.field_offset_class unless bootstrap_options.inline?
classes.flatten.compact
end

private :check_box_label, :check_box_classes, :check_box_label_class,
:check_box_wrapper_class, :check_box_container_classes
end
end
end
Expand Down
11 changes: 9 additions & 2 deletions lib/rails_bootstrap_form/components/radio_button.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,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" if bootstrap_options.layout_vertical?
classes << "mb-3" if (!bootstrap_options.inline? && !bootstrap_options.layout_horizontal?)
classes.flatten.compact
end

private :radio_button_label, :radio_button_classes, :radio_button_label_class, :radio_button_wrapper_class
def radio_button_container_classes(bootstrap_options)
classes = [bootstrap_options.field_col_wrapper_class]
classes << bootstrap_options.field_offset_class unless bootstrap_options.inline?
classes.flatten.compact
end

private :radio_button_label, :radio_button_classes, :radio_button_label_class,
:radio_button_wrapper_class, :radio_button_container_classes
end
end
end
Expand Down
14 changes: 4 additions & 10 deletions lib/rails_bootstrap_form/inputs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,18 +140,15 @@ def check_box(attribute, options = {}, checked_value = "1", unchecked_value = "0
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?
concat(generate_error(attribute)) if (is_invalid?(attribute) && !bootstrap_options.inline?)
concat(check_box_help_text) unless bootstrap_options.inline?
end

if (bootstrap_options.inline? || bootstrap_options.layout_vertical?)
check_box_html
else
check_box_container_classes = [bootstrap_options.field_col_wrapper_class]
check_box_container_classes << bootstrap_options.field_offset_class unless bootstrap_options.inline?

tag.div(class: field_wrapper_classes(bootstrap_options)) do
tag.div(class: check_box_container_classes) do
tag.div(class: check_box_container_classes(bootstrap_options)) do
check_box_html
end
end
Expand All @@ -171,18 +168,15 @@ def radio_button(attribute, value, options = {})
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?
concat(generate_error(attribute)) if (is_invalid?(attribute) && !bootstrap_options.inline?)
concat(radio_button_help_text) unless bootstrap_options.inline?
end

if (bootstrap_options.inline? || bootstrap_options.layout_vertical?)
radio_button_html
else
radio_button_container_classes = [bootstrap_options.field_col_wrapper_class]
radio_button_container_classes << bootstrap_options.field_offset_class unless bootstrap_options.inline?

tag.div(class: field_wrapper_classes(bootstrap_options)) do
tag.div(class: radio_button_container_classes) do
tag.div(class: radio_button_container_classes(bootstrap_options)) do
radio_button_html
end
end
Expand Down

0 comments on commit 3b572c6

Please sign in to comment.