Class: CCS::Components::GovUK::Fieldset::Legend

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

Overview

GOV.UK Fieldset Legend

The legend for the fieldset

Constant Summary collapse

DEFAULT_ATTRIBUTES =

The default attributes for the fieldset legend

{ class: 'govuk-fieldset__legend' }.freeze

Instance Method Summary collapse

Constructor Details

#initialize(text:, is_page_heading: nil, caption: nil, **options) ⇒ Legend

Returns a new instance of Legend.

Parameters:

  • text (String)

    the text for the fieldset legend

  • is_page_heading (Boolean) (defaults to: nil)

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

  • caption (String) (defaults to: nil)

    options for the caption to be rendered above the heading if :is_page_heading is true

  • options (Hash)

    options that will be used in customising the HTML

Options Hash (caption:):

  • :text (String)

    text for the caption

  • :classes (String)

    additional CSS classes for the caption HTML

Options Hash (**options):

  • :classes (String)

    additional CSS classes for the legend HTML



35
36
37
38
39
40
41
# File 'lib/ccs/components/govuk/fieldset/legend.rb', line 35

def initialize(text:, is_page_heading: nil, caption: nil, **options)
  super(**options)

  @text = text
  @is_page_heading = is_page_heading
  @caption = caption
end

Instance Method Details

#renderActiveSupport::SafeBuffer

Generates the HTML for the GOV.UK Fieldset legend

Returns:

  • (ActiveSupport::SafeBuffer)


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

def render
  tag.legend(class: options[:attributes][:class]) do
    if is_page_heading
      concat(tag.span(caption[:text], class: caption[:classes])) if caption
      concat(tag.h1(text, class: 'govuk-fieldset__heading'))
    else
      text
    end
  end
end