Skip to content

Commit

Permalink
Flatten the character count and form group divs
Browse files Browse the repository at this point in the history
The update guidance says that the outer character count div should now
be merged with the form group, like this:

  HTML before:

  <div class="govuk-character-count" data-module="govuk-character-count" data-maxlength="100">
    <div class="govuk-form-group">
      <!-- // Label, hint, error and textarea -->
    </div>
    <!-- // Count message -->
  </div>

  HTML after:

  <div class="govuk-form-group govuk-character-count" data-module="govuk-character-count" data-maxlength="100">
    <!-- // Label, hint, error, textarea and count message -->
  </div>

This allows us to simplify the code considerably and get rid of the
character count container completely.
  • Loading branch information
peteryates committed Mar 27, 2024
1 parent 3a9cb3f commit 84c8358
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 55 deletions.
46 changes: 0 additions & 46 deletions lib/govuk_design_system_formbuilder/containers/character_count.rb

This file was deleted.

33 changes: 25 additions & 8 deletions lib/govuk_design_system_formbuilder/elements/text_area.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down

0 comments on commit 84c8358

Please sign in to comment.