From 2245c10057576c3a37720790572550772239bf41 Mon Sep 17 00:00:00 2001 From: Harshal LADHE Date: Mon, 15 May 2023 13:25:16 +0530 Subject: [PATCH 1/2] Added support to display `labels` along with the field --- .../bootstrap_form_options.rb | 28 +++++++++++++++ lib/rails_bootstrap_form/components.rb | 2 ++ lib/rails_bootstrap_form/components/labels.rb | 34 +++++++++++++++++++ .../field_wrapper_builder.rb | 2 ++ 4 files changed, 66 insertions(+) create mode 100644 lib/rails_bootstrap_form/components/labels.rb diff --git a/lib/rails_bootstrap_form/bootstrap_form_options.rb b/lib/rails_bootstrap_form/bootstrap_form_options.rb index f07b894..b16d17a 100644 --- a/lib/rails_bootstrap_form/bootstrap_form_options.rb +++ b/lib/rails_bootstrap_form/bootstrap_form_options.rb @@ -34,6 +34,27 @@ class BootstrapFormOptions # Default is nil. attr_accessor :help_text + # An option to override automatically generated label text. + attr_accessor :label_text + + # An option to custmize whether the label is to be displayed or not. + attr_accessor :skip_label + + # An option to customize whether the label is only visible to screen readers. + attr_accessor :hide_label + + # The CSS class that will be used when the label is only accessible by screen + # readers. Default is `visually-hidden` + attr_accessor :hide_class + + # Default CSS class that will be applied to all label tags. + # Default is `form-label`. + attr_accessor :label_class + + # An additional CSS class that will be added along with the existing + # `label_class` of the label. Default is nil. + attr_accessor :additional_label_class + def initialize(options = {}) set_defaults set_bootstrap_form_options(options) @@ -83,6 +104,13 @@ def set_defaults @additional_field_class = nil @help_text = nil + + @label_text = "" + @skip_label = false + @hide_label = false + @hide_class = "visually-hidden" + @label_class = "form-label" + @additional_label_class = nil end private :set_defaults diff --git a/lib/rails_bootstrap_form/components.rb b/lib/rails_bootstrap_form/components.rb index 0e71461..cf71908 100644 --- a/lib/rails_bootstrap_form/components.rb +++ b/lib/rails_bootstrap_form/components.rb @@ -7,7 +7,9 @@ module Components extend ActiveSupport::Autoload autoload :HelpText + autoload :Labels include HelpText + include Labels end end diff --git a/lib/rails_bootstrap_form/components/labels.rb b/lib/rails_bootstrap_form/components/labels.rb new file mode 100644 index 0000000..2333e69 --- /dev/null +++ b/lib/rails_bootstrap_form/components/labels.rb @@ -0,0 +1,34 @@ +# -*- encoding: utf-8 -*- +# -*- frozen_string_literal: true -*- +# -*- warn_indent: true -*- + +module RailsBootstrapForm + module Components + module Labels + extend ActiveSupport::Concern + + def self.included(base_class) + def label(attribute, bootstrap_options) + unless bootstrap_options.skip_label + label_class = label_classes(attribute, bootstrap_options) + label_text = label_text(attribute, bootstrap_options) + + super(attribute, label_text, class: label_class) + end + end + + def label_classes(attribute, bootstrap_options) + classes = [bootstrap_options.label_class, bootstrap_options.additional_label_class] + classes << bootstrap_options.hide_class if bootstrap_options.hide_label + classes.flatten.compact + end + + def label_text(attribute, bootstrap_options) + bootstrap_options.label_text || object&.class.try(:human_attribute_name, attribute) + end + + private :label, :label_classes, :label_text + end + end + end +end diff --git a/lib/rails_bootstrap_form/field_wrapper_builder.rb b/lib/rails_bootstrap_form/field_wrapper_builder.rb index 027311d..c0c5fb1 100644 --- a/lib/rails_bootstrap_form/field_wrapper_builder.rb +++ b/lib/rails_bootstrap_form/field_wrapper_builder.rb @@ -13,9 +13,11 @@ def field_wrapper_builder(attribute, options, html_options = nil, &block) end def field_wrapper(attribute, bootstrap_options, options, &block) + label = label(attribute, bootstrap_options) help_text = help_text(attribute, bootstrap_options) tag.div(class: field_wrapper_classes) do + concat(label) concat(capture(&block)) concat(help_text) end From 7651bc0132733d8f0d2727675ec4f57ebccaf850 Mon Sep 17 00:00:00 2001 From: Harshal LADHE Date: Mon, 15 May 2023 13:26:02 +0530 Subject: [PATCH 2/2] Bump version to `0.2.2` --- Gemfile.lock | 2 +- lib/rails_bootstrap_form/version.rb | 2 +- spec/rails_bootstrap_form_spec.rb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 0174178..6cc69fe 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - rails_bootstrap_form (0.2.1) + rails_bootstrap_form (0.2.2) GEM remote: http://rubygems.org/ diff --git a/lib/rails_bootstrap_form/version.rb b/lib/rails_bootstrap_form/version.rb index a4b2f6b..3e9206b 100644 --- a/lib/rails_bootstrap_form/version.rb +++ b/lib/rails_bootstrap_form/version.rb @@ -3,6 +3,6 @@ # -*- warn_indent: true -*- module RailsBootstrapForm - VERSION = "0.2.1".freeze + VERSION = "0.2.2".freeze REQUIRED_RAILS_VERSION = "~> 7.0".freeze end diff --git a/spec/rails_bootstrap_form_spec.rb b/spec/rails_bootstrap_form_spec.rb index 10e7444..e27a6b3 100644 --- a/spec/rails_bootstrap_form_spec.rb +++ b/spec/rails_bootstrap_form_spec.rb @@ -6,7 +6,7 @@ RSpec.describe RailsBootstrapForm do it "has a valid version number" do - expect(RailsBootstrapForm::VERSION).to eq("0.2.1") + expect(RailsBootstrapForm::VERSION).to eq("0.2.2") end it "has a valid rails version number" do