Sha256: bee1b14a23cb6339d5663872af337b8bb8977d268ab65398f548871ebf9f9822

Contents?: true

Size: 970 Bytes

Versions: 15

Compression:

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

        tag.div(**options) { yield }
      end

    private

      def options
        {
          class: %(#{brand}-character-count),
          data: { module: %(#{brand}-character-count) }.merge(**limit, **threshold).compact
        }
      end

      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

15 entries across 15 versions & 1 rubygems

Version Path
govuk_design_system_formbuilder-2.1.7b1 lib/govuk_design_system_formbuilder/containers/character_count.rb
govuk_design_system_formbuilder-2.1.6 lib/govuk_design_system_formbuilder/containers/character_count.rb
govuk_design_system_formbuilder-2.1.5 lib/govuk_design_system_formbuilder/containers/character_count.rb
govuk_design_system_formbuilder-2.1.4 lib/govuk_design_system_formbuilder/containers/character_count.rb
govuk_design_system_formbuilder-2.1.3 lib/govuk_design_system_formbuilder/containers/character_count.rb
govuk_design_system_formbuilder-2.1.2 lib/govuk_design_system_formbuilder/containers/character_count.rb
govuk_design_system_formbuilder-2.1.1 lib/govuk_design_system_formbuilder/containers/character_count.rb
govuk_design_system_formbuilder-2.1.0 lib/govuk_design_system_formbuilder/containers/character_count.rb
govuk_design_system_formbuilder-2.0.0 lib/govuk_design_system_formbuilder/containers/character_count.rb
govuk_design_system_formbuilder-2.0.0b4 lib/govuk_design_system_formbuilder/containers/character_count.rb
govuk_design_system_formbuilder-1.2.9 lib/govuk_design_system_formbuilder/containers/character_count.rb
govuk_design_system_formbuilder-2.0.0b3 lib/govuk_design_system_formbuilder/containers/character_count.rb
govuk_design_system_formbuilder-2.0.0b2 lib/govuk_design_system_formbuilder/containers/character_count.rb
govuk_design_system_formbuilder-2.0.0b1 lib/govuk_design_system_formbuilder/containers/character_count.rb
govuk_design_system_formbuilder-1.2.7 lib/govuk_design_system_formbuilder/containers/character_count.rb