Skip to content

Commit

Permalink
Merge pull request #488 from x-govuk/flatten-govuk-textarea
Browse files Browse the repository at this point in the history
Update the text area container when character counting is enabled
  • Loading branch information
peteryates authored Mar 28, 2024
2 parents 0581bc3 + 29e9a02 commit 20b70aa
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 20b70aa

Please sign in to comment.