Sha256: 60fb386a815414f73b1cf7f804391b22d2fdc2e8ee4db901ba2bc2e64ecfbb0a

Contents?: true

Size: 1.47 KB

Versions: 1

Compression:

Stored size: 1.47 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)

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

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

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

      def html
        return nil unless active?

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

      def hint_id
        return nil unless active?

        build_id('hint')
      end

    private

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

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

      def hint_body
        @content || @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

1 entries across 1 versions & 1 rubygems

Version Path
govuk_design_system_formbuilder-2.0.0b1 lib/govuk_design_system_formbuilder/elements/hint.rb