Sha256: 3fbb7c179e4adc17ac81fd401de65d8dcace9ea859b55c0cd1817a3c67fcada9

Contents?: true

Size: 963 Bytes

Versions: 7

Compression:

Stored size: 963 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?

        content_tag(
          'div',
          class: %(#{brand}-character-count),
          data: { module: %(#{brand}-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

7 entries across 7 versions & 1 rubygems

Version Path
govuk_design_system_formbuilder-1.2.3 lib/govuk_design_system_formbuilder/containers/character_count.rb
govuk_design_system_formbuilder-1.2.2 lib/govuk_design_system_formbuilder/containers/character_count.rb
govuk_design_system_formbuilder-1.2.1 lib/govuk_design_system_formbuilder/containers/character_count.rb
govuk_design_system_formbuilder-1.2.0 lib/govuk_design_system_formbuilder/containers/character_count.rb
govuk_design_system_formbuilder-1.2.0b4 lib/govuk_design_system_formbuilder/containers/character_count.rb
govuk_design_system_formbuilder-1.2.0b2 lib/govuk_design_system_formbuilder/containers/character_count.rb
govuk_design_system_formbuilder-1.2.0b1 lib/govuk_design_system_formbuilder/containers/character_count.rb