Skip to content

Commit 42f71ab

Browse files
feat(table): update rails examples,
refactor JS & css
1 parent 3263a9a commit 42f71ab

File tree

4 files changed

+85
-22
lines changed

4 files changed

+85
-22
lines changed

docs/app/views/examples/components/table/_preview.html.erb

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,51 @@ sample_table = {
110110
}
111111
]
112112
}
113+
114+
table_dropdown_menu_items = [
115+
{
116+
attributes: { "href": "#" },
117+
icon: "pen",
118+
value: "Edit",
119+
},
120+
{
121+
attributes: {
122+
"href": "https://kajabi.com",
123+
"data-js-tooltip": "Tooltip",
124+
"data-js-tooltip-position": "right",
125+
},
126+
icon: "pen",
127+
modifiers: ["disabled"],
128+
value: "Disabled link w/ tooltip",
129+
},
130+
{
131+
attributes: { "href": "#" },
132+
icon: "add",
133+
style: "primary",
134+
value: "New",
135+
},
136+
{
137+
attributes: { "href": "#" },
138+
icon: "url",
139+
modifiers: ["border-before"],
140+
value: "Share Element",
141+
},
142+
{
143+
attributes: { "href": "#" },
144+
icon: "remove-circle",
145+
style: "danger",
146+
value: "Take A Dangerous Action",
147+
},
148+
{
149+
attributes: {
150+
"data-js-tooltip": "Tooltip",
151+
"data-js-tooltip-position": "right",
152+
},
153+
icon: "users",
154+
modifiers: ["disabled"],
155+
value: "Disabled w/ Tooltip",
156+
}
157+
]
113158
%>
114159

115160
<%= sage_component SageTable, {
@@ -435,7 +480,12 @@ This involves calling `sage_table_for` with a collection, and then using `t.colu
435480
**NOTE:** This is an MVP implementation based on a helper written earlier for `kajabi-products` and may not be fully aligned with SageTable. It is safe to use, but should be tracked against future updates.
436481
") %>
437482

438-
<%= sage_table_for people_data, selectable: true, sortable: true, responsive: true, caption: "Caption positioned above using `caption_side`".html_safe, caption_side: "top" do |t| %>
483+
<%= sage_table_for people_data,
484+
selectable: true,
485+
sortable: true,
486+
responsive: true,
487+
responsive_stack: true,
488+
caption: "Caption positioned above using `caption_side`".html_safe, caption_side: "top" do |t| %>
439489
<% t.column :initials, label: "", data_type: "checkbox" do |c| %>
440490
<%= sage_component SageCheckbox, {
441491
label_text: 'Select row',
@@ -467,15 +517,15 @@ This involves calling `sage_table_for` with a collection, and then using `t.colu
467517
</span>
468518
<% end %>
469519

470-
<% t.column :price, label: "Price", style: "width: 140px", hide: { md: true } do |c| %>
520+
<% t.column :price, label: "Price", hide: { md: true } do |c| %>
471521
<%= c[:price] %>
472522
<% end %>
473523

474-
<% t.column :amount, label: "Amount", style: "width: 100px", hide: { md: true } do |c| %>
524+
<% t.column :amount, label: "Amount", hide: { md: true } do |c| %>
475525
<%= c[:amount] %>
476526
<% end %>
477527

478-
<% t.column :labels, label: "Labels", style: "width: 150px", hide: { sm: true } do |c| %>
528+
<% t.column :labels, label: "Labels", hide: { sm: true } do |c| %>
479529
<% c[:labels].each do |label| %>
480530
<%= sage_component SageBadge, {
481531
value: label,
@@ -484,14 +534,14 @@ This involves calling `sage_table_for` with a collection, and then using `t.colu
484534
<% end %>
485535
<% end %>
486536

487-
<% t.column :status, label: "Status", style: "width: 100px", hide: { sm: true } do |c| %>
537+
<% t.column :status, label: "Status", hide: { sm: true } do |c| %>
488538
<%= sage_component SageBadge, {
489539
value: c[:status].titlecase,
490540
color: c[:status],
491541
} %>
492542
<% end %>
493543

494-
<% t.column :actions, label: "Actions", style: "width: 100px" do |c| %>
544+
<% t.column :actions, label: "Actions" do |c| %>
495545
<%= sage_component SageButtonGroup, { gap: :xs } do %>
496546
<%= sage_component SageButton, {
497547
subtle: true,

docs/lib/sage_rails/app/helpers/sage_table_helper.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def sortable_title
8080
end
8181

8282
class SageTableFor
83-
attr_reader :caption, :caption_side, :columns, :condensed, :template, :id, :class_name, :collection, :has_borders, :row_proc, :sortable, :responsive, :skip_headers, :reset_above, :reset_below
83+
attr_reader :caption, :caption_side, :columns, :condensed, :template, :id, :class_name, :collection, :has_borders, :row_proc, :sortable, :responsive, :responsive_stack, :skip_headers, :reset_above, :reset_below
8484
delegate :content_tag, :tag, to: :template
8585

8686
def initialize(template, collection, opts={})
@@ -94,6 +94,7 @@ def initialize(template, collection, opts={})
9494
@reset_above = opts[:reset_above]
9595
@reset_below = opts[:reset_below]
9696
@responsive = opts[:responsive]
97+
@responsive_stack = opts[:responsive_stack]
9798
@skip_headers = opts[:skip_headers]
9899
@sortable = opts[:sortable]
99100
@id = opts[:id]
@@ -132,6 +133,7 @@ def table_contents
132133
table_classes = "sage-table"
133134
table_classes << " sage-table--condensed" if condensed
134135
table_classes << " sage-table--sortable" if sortable
136+
table_classes << " sage-table--stack" if responsive_stack
135137
table_classes << " #{class_name}" if class_name
136138

137139
content_tag "table", id: id, class: table_classes do

packages/sage-assets/lib/stylesheets/components/_table.scss

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ $-table-overflow-indicator-gradient: linear-gradient(
3535
100%
3636
);
3737

38+
// Responsive stacked table
39+
$-table-stack-row-padding-block: rem(18px);
40+
$-table-stack-row-padding-inline: rem(20px);
41+
$-table-stack-cell-max-children: 4;
42+
3843
// Other
3944
$-table-row-color-hover: sage-color(grey, 100);
4045
$-table-cell-focus: sage-color(charcoal, 300);
@@ -367,15 +372,15 @@ $-table-avatar-width: rem(32px);
367372
}
368373

369374
.sage-table-cell__heading--responsive {
370-
display: inline-block;
375+
display: inline-flex;
371376
align-items: center;
372377
}
373378

374379
&.sage-table--has-menu-options {
375380
td:last-of-type {
376381
position: absolute;
377-
top: 1rem;
378-
right: 1rem;
382+
top: #{$-table-stack-row-padding-block};
383+
right: #{$-table-stack-row-padding-inline};
379384
}
380385
}
381386

@@ -394,27 +399,27 @@ $-table-avatar-width: rem(32px);
394399
}
395400

396401
tr {
397-
padding: rem(16px) rem(18px);
402+
padding: #{$-table-stack-row-padding-block} #{$-table-stack-row-padding-inline};
398403
border: sage-border(default);
399404
border-radius: sage-border(radius);
400405

401406
& + & {
402-
margin-top: sage-spacing(sm);
407+
margin-top: #{$-table-stack-row-padding-block};
403408
}
404409
}
405410

406411
td {
407412
display: grid;
408-
grid-template-columns: auto auto;
409-
grid-gap: 1em 0.5em;
410-
justify-content: space-between;
413+
grid-template-columns: 1fr repeat(#{$-table-stack-cell-max-children}, auto);
411414
}
412415

413-
// td:nth-of-type(4)::before,
414-
// td:nth-of-type(5)::before {
415-
// text-align: left;
416-
// }
416+
// table cell overrides
417+
.sage-table-cell--truncate,
418+
.sage-table-cell__truncated-content {
419+
max-width: none;
420+
}
417421

422+
.sage-table-cell--avatar,
418423
.sage-table--has-menu-options td:last-child::before {
419424
display: none;
420425
}

packages/sage-system/lib/table.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,19 +70,25 @@ Sage.table = (function() {
7070
cell.prepend(newHeader);
7171
});
7272

73-
// look for cells with positioned content
73+
// find cells with special cases (positioned content, multiple children)
7474
cells.forEach((cell) => {
7575
const checkboxes = cell.querySelectorAll(".sage-checkbox");
76+
const badges = cell.querySelectorAll(".sage-badge");
77+
const containers = cell.querySelectorAll("div");
7678

7779
if (checkboxes.length) {
7880
cell.classList.add('sage-table-cell--checkbox');
7981
}
82+
83+
if (badges.length || containers.length) {
84+
cell.classList.add('sage-table-cell--group');
85+
}
8086
})
8187
})
8288
})
8389
}
8490

85-
function applyAriaRoles(args) {
91+
function setAriaRole(args) {
8692
// expects an object of the form { items: "<selector(s)>", role: "<role to represent>" }
8793
// based on:
8894
// https://adrianroselli.com/2018/02/tables-css-display-properties-and-aria.html
@@ -104,7 +110,7 @@ Sage.table = (function() {
104110
{ items: 'th[scope=row]', role: 'rowheader' },
105111
];
106112

107-
tableItems.map((item) => applyAriaRoles(item));
113+
tableItems.map((item) => setAriaRole(item));
108114
}
109115

110116
// reset classes on elements

0 commit comments

Comments
 (0)