Sha256: 18c04f2aa837cbd37b7e818348ba76c49a18dc5041676a550688a25615991138

Contents?: true

Size: 956 Bytes

Versions: 4

Compression:

Stored size: 956 Bytes

Contents

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

      def html
        return yield unless limit?

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

    private

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

      def threshold
        { threshold: @threshold }
      end

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
govuk_design_system_formbuilder-0.7.6 lib/govuk_design_system_formbuilder/containers/character_count.rb
govuk_design_system_formbuilder-0.7.5 lib/govuk_design_system_formbuilder/containers/character_count.rb
govuk_design_system_formbuilder-0.7.4 lib/govuk_design_system_formbuilder/containers/character_count.rb
govuk_design_system_formbuilder-0.7.3 lib/govuk_design_system_formbuilder/containers/character_count.rb