Class: CCS::Components::GovUK::Field::Input::CharacterCount
- Inherits:
-
Object
- Object
- CCS::Components::GovUK::Field::Input::CharacterCount
- Defined in:
- lib/ccs/components/govuk/field/input/character_count.rb,
lib/ccs/components/govuk/field/input/character_count/count_message.rb
Overview
GOV.UK Character count
This is used for generating the character count component from the GDS - Components - Character count
This is a wrapper around a Textarea module and so makes use of the methods in Textarea
Defined Under Namespace
Classes: CountMessage
Instance Attribute Summary collapse
-
#character_count_html_options ⇒ Hash
readonly
HTML options for the character count.
Instance Method Summary collapse
-
#initialize(attribute:, context:, character_count_options: {}, **options) ⇒ CharacterCount
constructor
A new instance of CharacterCount.
Constructor Details
#initialize(attribute:, context:, character_count_options: {}, **options) ⇒ CharacterCount
Returns a new instance of CharacterCount.
38 39 40 41 42 |
# File 'lib/ccs/components/govuk/field/input/character_count.rb', line 38 def initialize(attribute:, context:, character_count_options: {}, **) character_count_attribute = [:form] ? "#{[:form].object_name}_#{attribute}" : attribute initialise_textarea(attribute, character_count_attribute, , context, ) end |
Instance Attribute Details
#character_count_html_options ⇒ Hash (readonly)
Returns HTML options for the character count.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/ccs/components/govuk/field/input/character_count.rb', line 23 class CharacterCount private attr_reader :textarea, :textarea_description public # @param (see CCS::Components::GovUK::Field::Input::Textarea#initialize) # @param character_count_options [Hash] options for the character count # @param context [ActionView::Base] the view context # # @option (see CCS::Components::GovUK::Field::Input::Textarea#initialize) # # @option (see initialise_character_count_html_options) def initialize(attribute:, context:, character_count_options: {}, **) character_count_attribute = [:form] ? "#{[:form].object_name}_#{attribute}" : attribute initialise_textarea(attribute, character_count_attribute, , context, ) end delegate :render, to: :textarea private # Initialises the Textarea for the character count # # @param attribute [Symbol] the attribute of the field # @param character_count_attribute [String] the name of the field as it will appear in the textarea # @param character_count_options [Hash] options for the character count # @param context [ActionView::Base] the view context # @param options [Hash] options that will be used for the textarea # # @option (see CCS::Components::GovUK::Field::Input::Textarea#initialize) def initialise_textarea(attribute, character_count_attribute, , context, ) (, ) (([:attributes] ||= {})[:aria] ||= {})[:describedby] = [.dig(:attributes, :aria, :describedby), "#{character_count_attribute}-info"].compact.join(' ') [:classes] = "govuk-js-character-count #{[:classes]}".rstrip = CountMessage.new(character_count_attribute: character_count_attribute, context: context, character_count_options: , after_input: .delete(:after_input)) @textarea = Textarea.new(attribute: attribute, context: context, after_input: .render, **) end # Sets the charcter count form group options # # @param character_count_options [Hash] options for the charcter count # # @option character_count_options [String] :maxlength the maximum number of characters. # If +maxwords+ is set, this is not required. # If +maxwords+ is provided, the +maxlength+ option will be ignored. # @option character_count_options [String] :maxwords the maximum number of words. # If maxlength is set, this is not required. # If +maxwords+ is provided, the +maxlength+ option will be ignored. # @option character_count_options [String] :threshold the percentage value of the limit at which point the count message is displayed. # If this attribute is set, the count message will be hidden by default. # @option character_count_options [Hash] :textarea_description ({}) additional parameters that will be used to create the hint containing the character count text: # - +:count_message+ replaced the default text for the count message. # If you want the count number to appear, put %<count>s in the string and it will be replaced with the number # - +classes+ additional CSS classes for the textarea description HTML # @option character_count_options [String] :characters_under_limit Message displayed when the number of characters is under the configured maximum, maxlength # @option character_count_options [String] :characters_at_limit_text Message displayed when the number of characters reaches the configured maximum, maxlength # @option character_count_options [String] :characters_over_limit Message displayed when the number of characters is over the configured maximum, maxlength # @option character_count_options [String] :words_under_limit Message displayed when the number of words is under the configured maximum, maxwords # @option character_count_options [String] :words_at_limit_text Message displayed when the number of words reaches the configured maximum, maxwords # @option character_count_options [String] :words_over_limit Message displayed when the number of words is over the configured maximum, maxwords def (, ) ([:form_group] ||= {})[:classes] = "govuk-character-count #{.dig(:form_group, :classes)}".rstrip (([:form_group][:attributes] ||= {})[:data] ||= {})[:module] = 'govuk-character-count' %i[maxlength threshold maxwords].each do |data_attribute| [:form_group][:attributes][:data][data_attribute] = [data_attribute].to_s if [data_attribute] end get_chacrter_count_translations() do |data_attribute, value| [:form_group][:attributes][:data][data_attribute] = value end end # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity # Generator for the translation options for character count # # @param (see initialise_character_count_html_options) # # @option (see initialise_character_count_html_options) # # @return [Hash] def get_chacrter_count_translations() %i[characters_at_limit words_at_limit].each do |data_attribute| data_attribute_name = :"#{data_attribute}_text" next unless [data_attribute_name] yield :"i18n.#{data_attribute.to_s.gsub('_', '-')}", [data_attribute_name] end %i[characters_under_limit characters_over_limit words_under_limit words_over_limit].each do |data_attribute| next unless [data_attribute] %i[other one].each do |plural_rule| next unless [data_attribute][plural_rule] yield :"i18n.#{data_attribute.to_s.gsub('_', '-')}.#{plural_rule}", [data_attribute][plural_rule] end end yield :'i18n.textarea-description.other', [:textarea_description][:count_message] if .dig(:textarea_description, :count_message) && !([:maxwords] || [:maxlength]) end # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity end |