Class: NitroKit::Table

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

Defined Under Namespace

Classes: Caption, Section

Constant Summary collapse

ALIGNMENTS =
%i[left center right].freeze
DEFAULT_ALIGNMENT =
:left
SCOPES =
%i[col row].freeze
DIRECTIONS =
%i[asc desc].freeze
SORT_ICONS =
{ asc: "arrow-up", desc: "arrow-down", none: "chevrons-up-down" }.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(sort: nil, direction: nil, id: nil, html: {}, aria: {}, data: {}, table_html: {}, table_aria: {}, table_data: {}, desperately_need_a_class: nil) ⇒ Table

Table renders in two phases. caption, thead, and tbody only collect declarations, so the component owns caption → head → body order regardless of caller order. tr, th, and td run inside a collected block and render immediately, which is why their validation raises before the cell it describes is emitted rather than after the table is complete.



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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/components/nitro_kit/table.rb', line 18

def initialize(
  sort: nil,
  direction: nil,
  id: nil,
  html: {},
  aria: {},
  data: {},
  table_html: {},
  table_aria: {},
  table_data: {},
  desperately_need_a_class: nil
)
  @sort = sort.nil? ? nil : normalize_sort_key(sort, name: "sort")
  unless direction.nil? || direction.is_a?(Symbol) || direction.is_a?(String)
    raise ArgumentError, "Table direction must be a Symbol or String"
  end
  @direction = direction.nil? ? nil : validate_choice!(:direction, direction.to_sym, DIRECTIONS)
  unless @sort.nil? == @direction.nil?
    raise ArgumentError, "Table sort: and direction: must both be set or both be nil"
  end

  super(
    component: :table,
    attributes: { id:, data: { sort: @sort, direction: @direction }.compact }.compact,
    html:,
    aria:,
    data:,
    desperately_need_a_class:
  )

  @table_attributes = slot_attributes(
    :element,
    html: table_html,
    aria: table_aria,
    data: table_data
  )
  @caption = nil
  @caption_id = "#{id || "nk-table-#{SecureRandom.hex(4)}"}-caption"
  @head = nil
  @bodies = []
  @sort_keys = []
end

Instance Attribute Details

#directionObject (readonly)

Returns the value of attribute direction.



61
62
63
# File 'app/components/nitro_kit/table.rb', line 61

def direction
  @direction
end

#sortObject (readonly)

Returns the value of attribute sort.



61
62
63
# File 'app/components/nitro_kit/table.rb', line 61

def sort
  @sort
end

Instance Method Details

#caption(text = nil, html: {}, aria: {}, data: {}, desperately_need_a_class: nil, &block) ⇒ Object

Raises:

  • (ArgumentError)


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/components/nitro_kit/table.rb', line 86

def caption(text = nil, html: {}, aria: {}, data: {}, desperately_need_a_class: nil, &block)
  ensure_collecting_sections!(:caption)
  raise ArgumentError, "Table accepts at most one caption" if @caption
  raise ArgumentError, "Table caption accepts text or a block, not both" if !text.nil? && block
  validate_content_text!("Table caption", text) unless block

  @caption = Caption.new(
    text:,
    content: block,
    html:,
    aria:,
    data:,
    css_class: desperately_need_a_class
  )
  nil
end

#html_captionObject



79
# File 'app/components/nitro_kit/table.rb', line 79

alias :html_caption :caption

#html_tbodyObject



81
# File 'app/components/nitro_kit/table.rb', line 81

alias :html_tbody :tbody

#html_tdObject



84
# File 'app/components/nitro_kit/table.rb', line 84

alias :html_td :td

#html_thObject



83
# File 'app/components/nitro_kit/table.rb', line 83

alias :html_th :th

#html_theadObject



80
# File 'app/components/nitro_kit/table.rb', line 80

alias :html_thead :thead

#html_trObject



82
# File 'app/components/nitro_kit/table.rb', line 82

alias :html_tr :tr

#tbody(html: {}, aria: {}, data: {}, desperately_need_a_class: nil, &block) ⇒ Object

Raises:

  • (ArgumentError)


112
113
114
115
116
117
118
# File 'app/components/nitro_kit/table.rb', line 112

def tbody(html: {}, aria: {}, data: {}, desperately_need_a_class: nil, &block)
  ensure_collecting_sections!(:tbody)
  raise ArgumentError, "Table tbody requires a block" unless block

  @bodies << Section.new(content: block, html:, aria:, data:, css_class: desperately_need_a_class)
  nil
end

#td(text = nil, align: :left, html: {}, aria: {}, data: {}, desperately_need_a_class: nil, &block) ⇒ Object

Raises:

  • (ArgumentError)


175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'app/components/nitro_kit/table.rb', line 175

def td(
  text = nil,
  align: :left,
  html: {},
  aria: {},
  data: {},
  desperately_need_a_class: nil,
  &block
)
  ensure_rendering_row!(:td)
  raise ArgumentError, "Table td accepts text or a block, not both" if !text.nil? && block
  alignment = validate_choice!(:align, align, ALIGNMENTS)
  html_td(
    **slot_attributes(
      :cell,
      attributes: { data: { align: alignment_value(alignment) }.compact },
      html:,
      aria:,
      data:,
      desperately_need_a_class:
    )
  ) { text_or_block(text, &block) }
end

#th(text = nil, align: :left, scope: :col, sort: nil, href: nil, sort_data: {}, html: {}, aria: {}, data: {}, desperately_need_a_class: nil, &block) ⇒ Object

Raises:

  • (ArgumentError)


132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'app/components/nitro_kit/table.rb', line 132

def th(
  text = nil,
  align: :left,
  scope: :col,
  sort: nil,
  href: nil,
  sort_data: {},
  html: {},
  aria: {},
  data: {},
  desperately_need_a_class: nil,
  &block
)
  ensure_rendering_row!(:th)
  raise ArgumentError, "Table th accepts text or a block, not both" if !text.nil? && block
  raise ArgumentError, "Table th href: requires sort:" if sort.nil? && !href.nil?
  raise ArgumentError, "Table th sort: must be declared inside thead" if !sort.nil? && @rendering_section != :head
  alignment = validate_choice!(:align, align, ALIGNMENTS)
  scope = validate_choice!(:scope, scope, SCOPES)
  key = sort.nil? ? nil : declare_sort_header!(sort, href:)

  html_th(
    **slot_attributes(
      :header,
      attributes: {
        scope:,
        aria: key ? { sort: aria_sort(key) } : {},
        data: { align: alignment_value(alignment), sort_key: key }.compact
      },
      html:,
      aria:,
      data:,
      desperately_need_a_class:
    )
  ) do
    if key
      render_sort_link(key, text, href:, data: sort_data, &block)
    else
      text_or_block(text, &block)
    end
  end
end

#thead(html: {}, aria: {}, data: {}, desperately_need_a_class: nil, &block) ⇒ Object

Raises:

  • (ArgumentError)


103
104
105
106
107
108
109
110
# File 'app/components/nitro_kit/table.rb', line 103

def thead(html: {}, aria: {}, data: {}, desperately_need_a_class: nil, &block)
  ensure_collecting_sections!(:thead)
  raise ArgumentError, "Table accepts at most one thead" if @head
  raise ArgumentError, "Table thead requires a block" unless block

  @head = Section.new(content: block, html:, aria:, data:, css_class: desperately_need_a_class)
  nil
end

#tr(html: {}, aria: {}, data: {}, desperately_need_a_class: nil, &block) ⇒ Object

Raises:

  • (ArgumentError)


120
121
122
123
124
125
126
127
128
129
130
# File 'app/components/nitro_kit/table.rb', line 120

def tr(html: {}, aria: {}, data: {}, desperately_need_a_class: nil, &block)
  raise ArgumentError, "Table rows must be declared inside thead or tbody" unless @rendering_section
  raise ArgumentError, "Table tr requires a block" unless block

  html_tr(**slot_attributes(:row, html:, aria:, data:, desperately_need_a_class:)) do
    @rendering_row = true
    render(block)
  ensure
    @rendering_row = false
  end
end

#view_templateObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/components/nitro_kit/table.rb', line 63

def view_template
  @collecting_sections = true
  yield self if block_given?
  @collecting_sections = false

  div(**scroll_region_attributes) do
    table(**@table_attributes) do
      render_caption if @caption
      render_section(:head, @head, :html_thead) if @head
      @bodies.each { |body| render_section(:body, body, :html_tbody) }
    end
  end
ensure
  @collecting_sections = false
end