Sha256: efad3f954e94e453e3b74a8162944646b8b26074ccb169b29a66ad31ec3043c3

Contents?: true

Size: 958 Bytes

Versions: 5

Compression:

Stored size: 958 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

5 entries across 5 versions & 1 rubygems

Version Path
govuk_design_system_formbuilder-0.9.0 lib/govuk_design_system_formbuilder/containers/character_count.rb
govuk_design_system_formbuilder-0.7.10 lib/govuk_design_system_formbuilder/containers/character_count.rb
govuk_design_system_formbuilder-0.7.9 lib/govuk_design_system_formbuilder/containers/character_count.rb
govuk_design_system_formbuilder-0.7.8 lib/govuk_design_system_formbuilder/containers/character_count.rb
govuk_design_system_formbuilder-0.7.7 lib/govuk_design_system_formbuilder/containers/character_count.rb