Skip to content

Commit 865de56

Browse files
Merge pull request #125 from basemate/extra-table-components
Add extra table components to core
2 parents cf82a65 + 1ddf3f1 commit 865de56

File tree

8 files changed

+205
-30
lines changed

8 files changed

+205
-30
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
%tbody{@tag_attributes}
2+
- if block_given?
3+
= yield
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module Matestack::Ui::Core::Tbody
2+
class Tbody < Matestack::Ui::Core::Component::Static
3+
4+
end
5+
end
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
%tfoot{@tag_attributes}
2+
- if block_given?
3+
= yield
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module Matestack::Ui::Core::Tfoot
2+
class Tfoot < Matestack::Ui::Core::Component::Static
3+
4+
end
5+
end
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
%thead{@tag_attributes}
2+
- if block_given?
3+
= yield
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module Matestack::Ui::Core::Thead
2+
class Thead < Matestack::Ui::Core::Component::Static
3+
4+
end
5+
end

docs/components/table.md

Lines changed: 95 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
Show [specs](../../spec/usage/components/table_spec.rb)
44

5-
Use tables to implement `<table>`, `<tr>`, `<th>` and `<td>`-tags.
5+
Use tables to implement `<table>`, `<tr>`, `<th>`, `<td>`, `<thead>`, `<tbody>` and `<tfoot>` tags.
66

77
## Parameters
88

9-
`<table>`, `<tr>`, can take 2 optional configuration params.
10-
`<th>` and `<td>`-tags can also take a third param, text input.
9+
`<table>`, `<tr>`, `<thead>`, `<tbody>` and `<tfoot>` can take 2 optional configuration params.
10+
`<th>` and `<td>` tags can also take a third param, text input.
1111

1212
#### # id (optional)
1313
Expects a string with all ids the element should have.
@@ -23,20 +23,31 @@ Implementing a simple, hard coded table.
2323

2424
```ruby
2525
table class: "foo" do
26-
tr class: "bar" do
27-
th text: "First"
28-
th text: "Matestack"
29-
th text: "Table"
26+
thead do
27+
tr class: "bar" do
28+
th text: "First"
29+
th text: "Matestack"
30+
th text: "Table"
31+
end
3032
end
31-
tr do
32-
td text: "One"
33-
td text: "Two"
34-
td text: "Three"
33+
tbody do
34+
tr do
35+
td text: "One"
36+
td text: "Two"
37+
td text: "Three"
38+
end
39+
tr do
40+
td text: "Uno"
41+
td text: "Dos"
42+
td text: "Tres"
43+
end
3544
end
36-
tr do
37-
td text: "Uno"
38-
td text: "Dos"
39-
td text: "Tres"
45+
tfoot do
46+
tr do
47+
td text: "Eins"
48+
td text: "Zwei"
49+
td text: "Drei"
50+
end
4051
end
4152
end
4253
```
@@ -128,3 +139,72 @@ returns
128139
</tr>
129140
</table>
130141
```
142+
143+
## Example 3
144+
145+
`thead`, `tbody` and `tfoot` are optional containers for any number of `tr`s. If none are specified, `tbody` will be used to contain all `tr` components. `thead` is typically used for the head of the table, and `tfoot` for any table footer, where applicable, such as a sum or count.
146+
147+
```ruby
148+
table do
149+
thead do
150+
tr do
151+
th text: "Product"
152+
th text: "Price"
153+
end
154+
end
155+
# tbody is unnecessary, since it has no class or id and will be added automatically
156+
# tbody do
157+
tr do
158+
td text: "Apples"
159+
td text: "3.50"
160+
end
161+
tr do
162+
td text: "Oranges"
163+
td text: "2.75"
164+
end
165+
tr do
166+
td text: "Bananas"
167+
td text: "4.99"
168+
end
169+
# end
170+
tfoot do
171+
tr do
172+
td text: "Total:"
173+
td text: "11.24"
174+
end
175+
end
176+
end
177+
```
178+
179+
returns
180+
181+
```html
182+
<table>
183+
<thead>
184+
<tr>
185+
<th>Product</th>
186+
<th>Price</th>
187+
</tr>
188+
</thead>
189+
<tbody>
190+
<tr>
191+
<td>Apples</td>
192+
<td>3.50</td>
193+
</tr>
194+
<tr>
195+
<td>Oranges</td>
196+
<td>2.75</td>
197+
</tr>
198+
<tr>
199+
<td>Bananas</td>
200+
<td>4.99</td>
201+
</tr>
202+
</tbody>
203+
<tfoot>
204+
<tr>
205+
<td>Total:</td>
206+
<td>11.24</td>
207+
</tr>
208+
</tfoot>
209+
</table>
210+
```

spec/usage/components/table_th_tr_td_spec.rb renamed to spec/usage/components/table_spec.rb

Lines changed: 86 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require_relative '../../support/utils'
22
include Utils
33

4-
describe 'Table Components table, th, tr, td', type: :feature, js: true do
4+
describe 'Table Components table, th, tr, td, thead, tbody, tfoot', type: :feature, js: true do
55

66
it 'Example 1' do
77

@@ -10,20 +10,31 @@ class ExamplePage < Matestack::Ui::Page
1010
def response
1111
components {
1212
table class: 'foo' do
13-
tr class: 'bar' do
14-
th text: 'First'
15-
th text: 'Matestack'
16-
th text: 'Table'
13+
thead class: 'head' do
14+
tr class: 'bar' do
15+
th text: 'First'
16+
th text: 'Matestack'
17+
th text: 'Table'
18+
end
1719
end
18-
tr do
19-
td text: 'One'
20-
td text: 'Two'
21-
td text: 'Three'
20+
tbody class: 'body' do
21+
tr do
22+
td text: 'One'
23+
td text: 'Two'
24+
td text: 'Three'
25+
end
26+
tr do
27+
td text: 'Uno'
28+
td text: 'Dos'
29+
td text: 'Tres'
30+
end
2231
end
23-
tr do
24-
td text: 'Uno'
25-
td text: 'Dos'
26-
td text: 'Tres'
32+
tfoot class: 'foot' do
33+
tr do
34+
td text: 'Eins'
35+
td text: 'Zwei'
36+
td text: 'Drei'
37+
end
2738
end
2839
end
2940
}
@@ -37,12 +48,14 @@ def response
3748

3849
expected_static_output = <<~HTML
3950
<table class="foo">
40-
<tbody>
51+
<thead class="head">
4152
<tr class="bar">
4253
<th>First</th>
4354
<th>Matestack</th>
4455
<th>Table</th>
4556
</tr>
57+
</thead>
58+
<tbody class="body">
4659
<tr>
4760
<td>One</td>
4861
<td>Two</td>
@@ -54,13 +67,20 @@ def response
5467
<td>Tres</td>
5568
</tr>
5669
</tbody>
70+
<tfoot class="foot">
71+
<tr>
72+
<td>Eins</td>
73+
<td>Zwei</td>
74+
<td>Drei</td>
75+
</tr>
76+
</tfoot>
5777
</table>
5878
HTML
5979

6080
expect(stripped(static_output)).to include(stripped(expected_static_output))
6181
end
6282

63-
it 'Example 2' do
83+
it 'Example 2, when thead, tbody, tfoot are omitted, then tbody is implied' do
6484

6585
class ExamplePage < Matestack::Ui::Page
6686

@@ -137,4 +157,55 @@ def response
137157
expect(stripped(static_output)).to include(stripped(expected_static_output))
138158
end
139159

160+
it 'Example 2, with only thead, then tbody is implied for other rows' do
161+
162+
class ExamplePage < Matestack::Ui::Page
163+
164+
def response
165+
components {
166+
table do
167+
thead do
168+
tr do
169+
th text: 'First'
170+
th text: 'Matestack'
171+
th text: 'Table'
172+
end
173+
end
174+
tr do
175+
td text: 'One'
176+
td text: 'Two'
177+
td text: 'Three'
178+
end
179+
end
180+
}
181+
end
182+
183+
end
184+
185+
visit '/example'
186+
187+
static_output = page.html
188+
189+
expected_static_output = <<~HTML
190+
<table>
191+
<thead>
192+
<tr>
193+
<th>First</th>
194+
<th>Matestack</th>
195+
<th>Table</th>
196+
</tr>
197+
</thead>
198+
<tbody>
199+
<tr>
200+
<td>One</td>
201+
<td>Two</td>
202+
<td>Three</td>
203+
</tr>
204+
</tbody>
205+
</table>
206+
HTML
207+
208+
expect(stripped(static_output)).to include(stripped(expected_static_output))
209+
end
210+
140211
end

0 commit comments

Comments
 (0)