Class: GOVUKDesignSystemFormBuilder::Elements::Label

Inherits:
Base
  • Object
show all
Includes:
Traits::Caption, Traits::Localisation
Defined in:
lib/govuk_design_system_formbuilder/elements/label.rb

Instance Method Summary collapse

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.



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

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)


40
41
42
# File 'lib/govuk_design_system_formbuilder/elements/label.rb', line 40

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

#htmlObject



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

def html
  return unless active?

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