Class: CCS::Components::GovUK::Tabs::Tab

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

Overview

GOV.UK Tabs tab

The individual tab

Constant Summary

Constants inherited from Base

Base::DEFAULT_ATTRIBUTES

Instance Method Summary collapse

Constructor Details

#initialize(index:, id_prefix:, label:, panel:, **options) ⇒ Tab

Returns a new instance of Tab.

Parameters:

  • index (Integer)

    the index of the tab

  • id_prefix (Integer)

    prefix used for the id of a panel if no id is specified

  • label (String)

    the text for the tab

  • panel (Hash)

    used to find the id of the panel. See Panel#initialize for more details

  • options (Hash)

    options that will be used in customising the HTML

Options Hash (**options):

  • :attributes (Hash)

    any additional attributes that will added as part of the HTML



33
34
35
36
37
38
39
40
41
# File 'lib/ccs/components/govuk/tabs/tab.rb', line 33

def initialize(index:, id_prefix:, label:, panel:, **options)
  super(**options)

  @options[:attributes][:class] = 'govuk-tabs__tab'

  @index = index
  @label = label
  @href = "##{panel.dig(:attributes, :id) || "#{id_prefix}-#{index}"}"
end

Instance Method Details

#renderActiveSupport::SafeBuffer

Generates the HTML for the GOV.UK Tabs tab

Returns:

  • (ActiveSupport::SafeBuffer)


47
48
49
50
51
# File 'lib/ccs/components/govuk/tabs/tab.rb', line 47

def render
  tag.li(class: "govuk-tabs__list-item #{'govuk-tabs__list-item--selected' if index == 1}".rstrip) do
    link_to(label, href, **options[:attributes])
  end
end