Class: CCS::Components::GovUK::Field::Input::CharacterCount::CountMessage

Inherits:
Object
  • Object
show all
Includes:
ActionView::Context, ActionView::Helpers
Defined in:
lib/ccs/components/govuk/field/input/character_count/count_message.rb

Overview

GOV.UK Character count message

This is used to generate the character count message

Instance Method Summary collapse

Constructor Details

#initialize(character_count_attribute:, context:, character_count_options:, after_input: nil) ⇒ CountMessage

Returns a new instance of CountMessage.

Parameters:

  • character_count_attribute (String)

    the name of the field as it will appear in the textarea

  • context (ActionView::Base)

    the view context

  • after_input (String) (defaults to: nil)

    Text or HTML that goes after the input



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ccs/components/govuk/field/input/character_count/count_message.rb', line 33

def initialize(character_count_attribute:, context:, character_count_options:, after_input: nil)
  count_message = character_count_options[:textarea_description] || {}

  count_message_length = character_count_options[:maxwords] || character_count_options[:maxlength]
  count_message_default = "You can enter up to %<count>s #{character_count_options[:maxwords] ? 'words' : 'characters'}"

  text = count_message_length ? format(count_message[:count_message] || count_message_default, count: count_message_length) : ''
  classes = "govuk-character-count__message #{count_message[:classes]}".rstrip

  @count_message = Hint.new(text: text, classes: classes, attributes: { id: "#{character_count_attribute}-info" }, context: context)
  @after_input = after_input
end

Instance Method Details

#renderActiveSupport::SafeBuffer

Generates the HTML for the GOV.UK Character count message

Returns:

  • (ActiveSupport::SafeBuffer)


50
51
52
53
54
55
# File 'lib/ccs/components/govuk/field/input/character_count/count_message.rb', line 50

def render
  capture do
    concat(count_message.render)
    concat(after_input) if after_input
  end
end