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

Constructor Details

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

Returns a new instance of Tab.



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

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

  super(classes:, html_attributes:)
end

Instance Attribute Details

#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



57
58
59
# File 'app/components/govuk_component/tab_component.rb', line 57

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

#combined_attributes(i) ⇒ Object



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

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

#default_attributesObject



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

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

#hidden_class(i = nil) ⇒ Object



35
36
37
38
39
# File 'app/components/govuk_component/tab_component.rb', line 35

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

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

#id(prefix: nil) ⇒ Object



31
32
33
# File 'app/components/govuk_component/tab_component.rb', line 31

def id(prefix: nil)
  [prefix, label.parameterize].join
end

#li_classes(i = nil) ⇒ Object



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

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


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

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