Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Bootstrap 5 floating labels #27

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
6 changes: 5 additions & 1 deletion lib/comfy_bootstrap_form/bootstrap_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ class BootstrapOptions
# form.text_field :foo, bootstrap: {error: "Error Message"}
#
attr_accessor :error

# Use Bootstrap 5 floating labels
attr_accessor :floating

def initialize(options = {})
set_defaults
Expand Down Expand Up @@ -132,7 +135,8 @@ def set_defaults
@help = nil
@error = nil
@check_inline = false
@custom_control = true
@custom_control = false
@floating = false
end

end
Expand Down
30 changes: 19 additions & 11 deletions lib/comfy_bootstrap_form/form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def select(method, choices = nil, options = {}, html_options = {}, &block)

add_css_class!(html_options, "custom-select") if bootstrap.custom_control

draw_form_group(bootstrap, method, html_options) do
draw_form_group(bootstrap, method, html_options, true) do
super(method, choices, options, html_options, &block)
end
end
Expand All @@ -107,7 +107,7 @@ def collection_select(method, collection, value_method, text_method, options = {

add_css_class!(html_options, "custom-select") if bootstrap.custom_control

draw_form_group(bootstrap, method, html_options) do
draw_form_group(bootstrap, method, html_options, true) do
super(method, collection, value_method, text_method, options, html_options)
end
end
Expand All @@ -122,7 +122,8 @@ def file_field(method, options = {})

draw_form_group(bootstrap, method, options) do
if bootstrap.custom_control
content_tag(:div, class: "custom-file") do
content_tag(:div, class: "custom-file") do form_group_class += " form-floating" if bootstrap.floating?

add_css_class!(options, "custom-file-input")
remove_css_class!(options, "form-control")
label_text = options.delete(:placeholder)
Expand Down Expand Up @@ -370,21 +371,27 @@ def form_group(options = {})
private

# form group wrapper for input fields
def draw_form_group(bootstrap, method, options)
def draw_form_group(bootstrap, method, options, select = false)
label = draw_label(bootstrap, method, for_attr: options[:id])
errors = draw_errors(bootstrap, method)

control = draw_control(bootstrap, errors, method, options) do
control = draw_control(bootstrap, errors, method, options, select) do
yield
end

form_group_class = "form-group"
form_group_class += " row" if bootstrap.horizontal?
form_group_class += " mr-sm-2" if bootstrap.inline?
form_group_class += " row" if bootstrap.horizontal?
form_group_class += " mr-sm-2" if bootstrap.inline?
form_group_class += " form-floating" if bootstrap.floating

content_tag(:div, class: form_group_class) do
concat label
concat control
if bootstrap.floating
concat control
concat label
else
concat label
concat control
end
end
end

Expand Down Expand Up @@ -445,8 +452,9 @@ def draw_label(bootstrap, method, for_attr: nil)
end

# Renders control for a given field
def draw_control(bootstrap, errors, _method, options)
add_css_class!(options, "form-control")
def draw_control(bootstrap, errors, _method, options, select)
add_css_class!(options, "form-control") unless select
add_css_class!(options, "form-select") if select
add_css_class!(options, "is-invalid") if errors.present?

draw_control_column(bootstrap, offset: bootstrap.label[:hide]) do
Expand Down
2 changes: 1 addition & 1 deletion lib/comfy_bootstrap_form/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module ComfyBootstrapForm

VERSION = "4.0.9"
VERSION = "4.0.14"

end