Class: CCS::Components::GovUK::Label

Inherits:
Base
  • Object
show all
Defined in:
lib/ccs/components/govuk/label.rb

Overview

GOV.UK Label

This is used to generate the label component from the Government Design Systems

Constant Summary collapse

DEFAULT_ATTRIBUTES =

The default attributes for the label

{ class: 'govuk-label' }.freeze

Instance Method Summary collapse

Constructor Details

#initialize(attribute:, text: nil, form: nil, **options) ⇒ Label

Returns a new instance of Label.

Parameters:

  • attribute (String, Symbol)

    the attribute of the input that requires a label

  • text (String) (defaults to: nil)

    (nil) the text for the label If nil, then a block will be rendered

  • form (ActionView::Helpers::FormBuilder) (defaults to: nil)

    (nil) optional form builder used to create the label

  • options (Hash)

    options that will be used in customising the HTML

Options Hash (**options):

  • :classes (String)

    additional CSS classes for the label HTML

  • :is_page_heading (Boolean) — default: false

    if the label is also the heading it will rendered in a h1

  • :attributes (Hash) — default: {}

    any additional attributes that will added as part of the HTML



34
35
36
37
38
39
40
# File 'lib/ccs/components/govuk/label.rb', line 34

def initialize(attribute:, text: nil, form: nil, **options)
  super(**options)

  @attribute = attribute
  @text = text
  @form = form
end

Instance Method Details

#render { ... } ⇒ ActiveSupport::SafeBuffer

Generates the HTML for the GOV.UK label component

Yields:

  • HTML that will be generated for the label

Returns:

  • (ActiveSupport::SafeBuffer)


48
49
50
51
52
53
54
55
56
# File 'lib/ccs/components/govuk/label.rb', line 48

def render(&block)
  if options[:is_page_heading]
    tag.h1(class: 'govuk-label-wrapper') do
      label_html(&block)
    end
  else
    label_html(&block)
  end
end