Class: GOVUKDesignSystemFormBuilder::Elements::Hint

Inherits:
Base
  • Object
show all
Includes:
Traits::HTMLAttributes, Traits::HTMLClasses, Traits::Localisation
Defined in:
lib/govuk_design_system_formbuilder/elements/hint.rb

Constant Summary

Constants included from Traits::Localisation

Traits::Localisation::BASE_NAME_REGEXP

Instance Method Summary collapse

Methods included from Traits::HTMLClasses

#build_classes

Methods included from Traits::HTMLAttributes

#attributes

Methods inherited from Base

#field_id, #to_s

Constructor Details

#initialize(builder, object_name, attribute_name, value: nil, text: nil, content: nil, radio: false, checkbox: false, **kwargs) ⇒ Hint

Returns a new instance of Hint.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/govuk_design_system_formbuilder/elements/hint.rb', line 10

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

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/govuk_design_system_formbuilder/elements/hint.rb', line 25

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

#hint_idObject



35
36
37
38
39
# File 'lib/govuk_design_system_formbuilder/elements/hint.rb', line 35

def hint_id
  return unless active?

  build_id('hint')
end

#htmlObject



29
30
31
32
33
# File 'lib/govuk_design_system_formbuilder/elements/hint.rb', line 29

def html
  return unless active?

  tag.div(**attributes(@html_attributes)) { hint_body }
end