From 29e9a022ce74ee51eaf34769c8de7cad4313091c Mon Sep 17 00:00:00 2001 From: Peter Yates Date: Wed, 27 Mar 2024 16:38:01 +0000 Subject: [PATCH] Flatten the character count and form group divs The update guidance says that the outer character count div should now be merged with the form group, like this: HTML before:
HTML after:
This allows us to simplify the code considerably and get rid of the character count container completely. --- .../containers/character_count.rb | 46 ------------------- .../elements/text_area.rb | 33 +++++++++---- .../builder/text_area_spec.rb | 2 +- 3 files changed, 26 insertions(+), 55 deletions(-) delete mode 100644 lib/govuk_design_system_formbuilder/containers/character_count.rb diff --git a/lib/govuk_design_system_formbuilder/containers/character_count.rb b/lib/govuk_design_system_formbuilder/containers/character_count.rb deleted file mode 100644 index 9a1d231d..00000000 --- a/lib/govuk_design_system_formbuilder/containers/character_count.rb +++ /dev/null @@ -1,46 +0,0 @@ -module GOVUKDesignSystemFormBuilder - module Containers - class CharacterCount < Base - def initialize(builder, max_words:, max_chars:, threshold:) - super(builder, nil, nil) - - fail ArgumentError, 'limit can be words or chars' if max_words && max_chars - - @max_words = max_words - @max_chars = max_chars - @threshold = threshold - end - - def html(&block) - return yield unless limit? - - tag.div(**options, &block) - end - - private - - def options - { - class: %(#{brand}-character-count), - data: { module: %(#{brand}-character-count) }.merge(**limit, **threshold).compact - } - end - - def limit - if @max_words - { maxwords: @max_words } - elsif @max_chars - { maxlength: @max_chars } - end - end - - def threshold - { threshold: @threshold } - end - - def limit? - @max_words || @max_chars - end - end - end -end diff --git a/lib/govuk_design_system_formbuilder/elements/text_area.rb b/lib/govuk_design_system_formbuilder/elements/text_area.rb index 03a1dce9..6592b881 100644 --- a/lib/govuk_design_system_formbuilder/elements/text_area.rb +++ b/lib/govuk_design_system_formbuilder/elements/text_area.rb @@ -13,6 +13,8 @@ class TextArea < Base def initialize(builder, object_name, attribute_name, hint:, label:, caption:, rows:, max_words:, max_chars:, threshold:, form_group:, **kwargs, &block) super(builder, object_name, attribute_name, &block) + fail ArgumentError, 'limit can be words or chars' if max_words && max_chars + @label = label @caption = caption @hint = hint @@ -25,19 +27,13 @@ def initialize(builder, object_name, attribute_name, hint:, label:, caption:, ro end def html - Containers::CharacterCount.new(@builder, **character_count_options).html do - Containers::FormGroup.new(*bound, **@form_group).html do - safe_join([label_element, supplemental_content, hint_element, error_element, text_area, limit_description]) - end + Containers::FormGroup.new(*bound, **@form_group.merge(limit_form_group_options)).html do + safe_join([label_element, supplemental_content, hint_element, error_element, text_area, limit_description]) end end private - def character_count_options - { max_words: @max_words, max_chars: @max_chars, threshold: @threshold } - end - def text_area @builder.text_area(@attribute_name, **attributes(@html_attributes)) end @@ -92,6 +88,27 @@ def limit_description_id limit_id end + + def limit_form_group_options + return {} unless limit? + + { + class: %(#{brand}-character-count), + data: { module: %(#{brand}-character-count) }.merge(**limit_max_options, **limit_threshold_options).compact + } + end + + def limit_max_options + if @max_words + { maxwords: @max_words } + elsif @max_chars + { maxlength: @max_chars } + end + end + + def limit_threshold_options + { threshold: @threshold } + end end end end diff --git a/spec/govuk_design_system_formbuilder/builder/text_area_spec.rb b/spec/govuk_design_system_formbuilder/builder/text_area_spec.rb index 86aed204..1bb43ab5 100644 --- a/spec/govuk_design_system_formbuilder/builder/text_area_spec.rb +++ b/spec/govuk_design_system_formbuilder/builder/text_area_spec.rb @@ -93,7 +93,7 @@ let(:max_words) { 20 } subject { builder.send(*args, max_words:) } - specify 'should wrap the form group inside a character count tag' do + specify 'adds the character count class and data attributes to the form group' do expect(subject).to have_tag( 'div', with: {