Class: NitroKit::Tabs

Inherits:
Component
  • Object
show all
Defined in:
app/components/nitro_kit/tabs.rb

Defined Under Namespace

Classes: Tab

Constant Summary collapse

ORIENTATIONS =
%i[horizontal vertical].freeze
ACTIVATIONS =
%i[automatic manual].freeze

Constants inherited from Component

Component::ADDITIVE_DATA_ATTRIBUTES, Component::COMPONENT_OWNED_DATA_ATTRIBUTES, Component::FORBIDDEN_ATTRIBUTES, Component::RESERVED_DATA_ATTRIBUTES

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(default: nil, id:, label: I18n.t("nitro_kit.tabs.label"), orientation: :horizontal, activation: :automatic, html: {}, aria: {}, data: {}, desperately_need_a_class: nil) ⇒ Tabs

Returns a new instance of Tabs.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/components/nitro_kit/tabs.rb', line 9

def initialize(
  default: nil,
  id:,
  label: I18n.t("nitro_kit.tabs.label"),
  orientation: :horizontal,
  activation: :automatic,
  html: {},
  aria: {},
  data: {},
  desperately_need_a_class: nil
)
  @identifier = component_id(id)
  @default = default.nil? ? nil : normalize_identity(default, name: "default tab key")
  @label = validate_label!(label)
  @orientation = validate_choice!(:orientation, orientation, ORIENTATIONS)
  @activation = validate_choice!(:activation, activation, ACTIVATIONS)
  @tabs = []

  super(
    component: :tabs,
    attributes: {
      id: @identifier,
      data: {
        controller: "nk--tabs",
        orientation: @orientation,
        activation: @activation,
        nk__tabs_active_value: @default || "",
        nk__tabs_orientation_value: @orientation,
        nk__tabs_activation_value: @activation
      }
    },
    html:,
    aria:,
    data:,
    desperately_need_a_class:
  )
end

Instance Attribute Details

#activationObject (readonly)

Returns the value of attribute activation.



47
48
49
# File 'app/components/nitro_kit/tabs.rb', line 47

def activation
  @activation
end

#defaultObject (readonly)

Returns the value of attribute default.



47
48
49
# File 'app/components/nitro_kit/tabs.rb', line 47

def default
  @default
end

#identifierObject (readonly)

Returns the value of attribute identifier.



47
48
49
# File 'app/components/nitro_kit/tabs.rb', line 47

def identifier
  @identifier
end

#labelObject (readonly)

Returns the value of attribute label.



47
48
49
# File 'app/components/nitro_kit/tabs.rb', line 47

def label
  @label
end

#orientationObject (readonly)

Returns the value of attribute orientation.



47
48
49
# File 'app/components/nitro_kit/tabs.rb', line 47

def orientation
  @orientation
end

Instance Method Details

#tab(key, label, disabled: false, &content) ⇒ Object

Raises:

  • (ArgumentError)


59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/components/nitro_kit/tabs.rb', line 59

def tab(key, label, disabled: false, &content)
  ensure_collecting!

  key = normalize_identity(key, name: "tab key")
  label = validate_label!(label)
  disabled = validate_boolean!(:disabled, disabled)
  raise ArgumentError, "Tab #{key.inspect} requires panel content" unless content
  raise ArgumentError, "Duplicate tab key #{key.inspect}" if find_tab(key)

  @tabs << Tab.new(key:, label:, disabled:, content:)
  nil
end

#view_template(&block) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'app/components/nitro_kit/tabs.rb', line 49

def view_template(&block)
  collect_tabs(&block)
  active_key = resolve_active_key

  div(**root_attributes_for(active_key)) do
    render_tablist(active_key)
    @tabs.each { |tab| render_panel(tab, active_key:) }
  end
end