Sha256: 9d0e4abba25693d6b80ee72c7ed95f27452676fd745452dca35b20c7051a5eb7

Contents?: true

Size: 1.45 KB

Versions: 17

Compression:

Stored size: 1.45 KB

Contents

module GOVUKDesignSystemFormBuilder
  module Elements
    class Hint < Base
      using PrefixableArray

      include Traits::Localisation

      def initialize(builder, object_name, attribute_name, value: nil, text: nil, content: nil, radio: false, checkbox: false, **kwargs)
        super(builder, object_name, attribute_name)

        @value           = value
        @radio           = radio
        @checkbox        = checkbox
        @html_attributes = kwargs

        if content
          @raw = capture { content.call }
        else
          @text = retrieve_text(text)
        end
      end

      def active?
        [@text, @raw].any?(&:present?)
      end

      def html
        return unless active?

        content_tag(hint_tag, **hint_options, **@html_attributes) { hint_body }
      end

      def hint_id
        return unless active?

        build_id('hint')
      end

    private

      def hint_options
        { class: classes, id: hint_id }
      end

      def hint_tag
        @raw.presence ? 'div' : 'span'
      end

      def hint_body
        @raw || @text
      end

      def retrieve_text(supplied)
        supplied.presence || localised_text(:hint)
      end

      def classes
        %w(hint).prefix(brand).push(radio_class, checkbox_class).compact
      end

      def radio_class
        @radio ? %(#{brand}-radios__hint) : nil
      end

      def checkbox_class
        @checkbox ? %(#{brand}-checkboxes__hint) : nil
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
govuk_design_system_formbuilder-2.8.0 lib/govuk_design_system_formbuilder/elements/hint.rb
govuk_design_system_formbuilder-2.7.6 lib/govuk_design_system_formbuilder/elements/hint.rb
govuk_design_system_formbuilder-2.7.5 lib/govuk_design_system_formbuilder/elements/hint.rb
govuk_design_system_formbuilder-2.7.4 lib/govuk_design_system_formbuilder/elements/hint.rb
govuk_design_system_formbuilder-2.7.3 lib/govuk_design_system_formbuilder/elements/hint.rb
govuk_design_system_formbuilder-2.7.2 lib/govuk_design_system_formbuilder/elements/hint.rb
govuk_design_system_formbuilder-2.7.1 lib/govuk_design_system_formbuilder/elements/hint.rb
govuk_design_system_formbuilder-2.7.0 lib/govuk_design_system_formbuilder/elements/hint.rb
govuk_design_system_formbuilder-2.6.0 lib/govuk_design_system_formbuilder/elements/hint.rb
govuk_design_system_formbuilder-2.6.0b1 lib/govuk_design_system_formbuilder/elements/hint.rb
govuk_design_system_formbuilder-2.5.3 lib/govuk_design_system_formbuilder/elements/hint.rb
govuk_design_system_formbuilder-2.5.2 lib/govuk_design_system_formbuilder/elements/hint.rb
govuk_design_system_formbuilder-2.5.1 lib/govuk_design_system_formbuilder/elements/hint.rb
govuk_design_system_formbuilder-2.5.1b1 lib/govuk_design_system_formbuilder/elements/hint.rb
govuk_design_system_formbuilder-2.5.0 lib/govuk_design_system_formbuilder/elements/hint.rb
govuk_design_system_formbuilder-2.4.0 lib/govuk_design_system_formbuilder/elements/hint.rb
govuk_design_system_formbuilder-2.3.0 lib/govuk_design_system_formbuilder/elements/hint.rb