Class: GOVUKDesignSystemFormBuilder::Elements::Label

Inherits:
Base
  • Object
show all
Includes:
Traits::Caption, Traits::HTMLAttributes, Traits::HTMLClasses, Traits::Localisation
Defined in:
lib/govuk_design_system_formbuilder/elements/label.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, text: nil, value: nil, size: nil, hidden: false, radio: false, checkbox: false, tag: nil, link_errors: true, content: nil, caption: nil, **kwargs) ⇒ Label

Returns a new instance of Label.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/govuk_design_system_formbuilder/elements/label.rb', line 11

def initialize(builder, object_name, attribute_name, text: nil, value: nil, size: nil, hidden: false, radio: false, checkbox: false, tag: nil, link_errors: true, content: nil, caption: nil, **kwargs)
  super(builder, object_name, attribute_name)

  @value           = value # used by field_id
  @tag             = tag
  @radio           = radio
  @checkbox        = checkbox
  @link_errors     = link_errors
  @html_attributes = kwargs

  # content is passed in directly via a proc and overrides
  # the other display options
  if content
    @content = capture { content.call }
  else
    @text       = retrieve_text(text, hidden)
    @size_class = size_class(size)
    @caption    = caption
  end
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/govuk_design_system_formbuilder/elements/label.rb', line 42

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

#htmlObject



32
33
34
35
36
37
38
39
40
# File 'lib/govuk_design_system_formbuilder/elements/label.rb', line 32

def html
  return unless active?

  if @tag.present?
    (@tag, class: %(#{brand}-label-wrapper)) { label }
  else
    label
  end
end