Skip to content

Commit

Permalink
Added option to add custom options to the field's wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
shivam091 committed May 21, 2023
1 parent e789a55 commit 11c2bd0
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 12 deletions.
2 changes: 1 addition & 1 deletion demo/app/views/users/_vertical_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
</div>
<div class="card-body">
<%= 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 %>
Expand Down
10 changes: 6 additions & 4 deletions lib/rails_bootstrap_form/bootstrap_form_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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:
Expand Down Expand Up @@ -152,6 +152,8 @@ def set_defaults
@static_field_class = "form-control-plaintext"

@switch = false

@wrapper_options = {}
end

private :set_defaults
Expand Down
6 changes: 6 additions & 0 deletions lib/rails_bootstrap_form/components/check_box.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions lib/rails_bootstrap_form/components/radio_button.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 8 additions & 5 deletions lib/rails_bootstrap_form/field_wrapper_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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)
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/rails_bootstrap_form/inputs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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?
Expand Down

0 comments on commit 11c2bd0

Please sign in to comment.