Sha256: e573642e1ff2c19112d2caf7acd23e23a63c314ab98c337070bc53cf439dc7f9

Contents?: true

Size: 825 Bytes

Versions: 3

Compression:

Stored size: 825 Bytes

Contents

module GOVUKDesignSystemFormBuilder
  module Containers
    class CharacterCount < Base
      def initialize(builder, max_words:, max_chars:)
        @builder = builder
        fail ArgumentError, 'limit can be words or chars' if max_words && max_chars
        @max_words = max_words
        @max_chars = max_chars
      end

      def html
        return yield unless limit?

        @builder.content_tag(
          'div',
          class: 'govuk-character-count',
          data: { module: 'character-count' }.merge(limit)
        ) do
          yield
        end
      end

    private

      def limit
        if @max_words
          { maxwords: @max_words }
        elsif @max_chars
          { maxlength: @max_chars }
        end
      end

      def limit?
        @max_words || @max_chars
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
govuk_design_system_formbuilder-0.7.2 lib/govuk_design_system_formbuilder/containers/character_count.rb
govuk_design_system_formbuilder-0.7.1 lib/govuk_design_system_formbuilder/containers/character_count.rb
govuk_design_system_formbuilder-0.7.0 lib/govuk_design_system_formbuilder/containers/character_count.rb