Class: GovukComponent::TabComponent::Tab

Inherits:
Base
  • Object
show all
Defined in:
app/components/govuk_component/tab_component.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#html_attributes

Instance Method Summary collapse

Methods inherited from Base

#brand, #class_prefix

Constructor Details

#initialize(label:, text: nil, id: nil, classes: [], html_attributes: {}) ⇒ Tab

Returns a new instance of Tab.



24
25
26
27
28
29
30
# File 'app/components/govuk_component/tab_component.rb', line 24

def initialize(label:, text: nil, id: nil, classes: [], html_attributes: {})
  @label = label
  @text  = h(text)
  @id    = id || label.parameterize

  super(classes:, html_attributes:)
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



22
23
24
# File 'app/components/govuk_component/tab_component.rb', line 22

def id
  @id
end

#labelObject (readonly)

Returns the value of attribute label.



22
23
24
# File 'app/components/govuk_component/tab_component.rb', line 22

def label
  @label
end

#textObject (readonly)

Returns the value of attribute text.



22
23
24
# File 'app/components/govuk_component/tab_component.rb', line 22

def text
  @text
end

Instance Method Details

#callObject



54
55
56
# File 'app/components/govuk_component/tab_component.rb', line 54

def call
  content || text || fail(ArgumentError, "no text or content")
end

#combined_attributes(i) ⇒ Object



50
51
52
# File 'app/components/govuk_component/tab_component.rb', line 50

def combined_attributes(i)
  html_attributes.deep_merge_html_attributes({ class: hidden_class(i) }).deep_tidy_html_attributes
end

#default_attributesObject



46
47
48
# File 'app/components/govuk_component/tab_component.rb', line 46

def default_attributes
  { id:, class: "#{brand}-tabs__panel" }
end

#hidden_class(i = nil) ⇒ Object



32
33
34
35
36
# File 'app/components/govuk_component/tab_component.rb', line 32

def hidden_class(i = nil)
  return [] if i&.zero?

  ["#{brand}-tabs__panel--hidden"]
end

#li_classes(i = nil) ⇒ Object



38
39
40
# File 'app/components/govuk_component/tab_component.rb', line 38

def li_classes(i = nil)
  class_names("#{brand}-tabs__list-item", "#{brand}-tabs__list-item--selected" => i&.zero?).split
end


42
43
44
# File 'app/components/govuk_component/tab_component.rb', line 42

def li_link
  link_to(label, "#" + id, class: "#{brand}-tabs__tab")
end