Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PBNTR-743] Advanced Table - Add Responsiveness to Rails kit #4151

Merged
merged 7 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
<%= pb_content_tag do %>
<%= pb_rails("table", props: { size: "sm", data_table: true, number_spacing:"tabular", responsive:"none", dark: dark }.merge(object.table_props)) do %>
<% if content.present? %>
<% content.presence %>
<% else %>
<%= pb_rails("advanced_table/table_header", props: { column_definitions: object.column_definitions, enable_toggle_expansion: object.enable_toggle_expansion }) %>
<%= pb_rails("advanced_table/table_body", props: { id: object.id, table_data: object.table_data, column_definitions: object.column_definitions }) %>
<% end %>
<% end %>
<%= pb_rails("table", props: { size: "sm", data_table: true, number_spacing:"tabular", responsive: object.responsive, dark: dark }.merge(object.table_props)) do %>
<% if content.present? %>
<% content.presence %>
<% else %>
<%= pb_rails("advanced_table/table_header", props: { column_definitions: object.column_definitions,
enable_toggle_expansion: object.enable_toggle_expansion,
responsive: object.responsive,
is_pinned_left: object.responsive == "scroll" }) %>
<%= pb_rails("advanced_table/table_body", props: { id: object.id,
table_data: object.table_data,
column_definitions: object.column_definitions,
responsive: object.responsive,
is_pinned_left: object.responsive == "scroll" }) %>
<% end %>
<% end %>
<% end %>
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class AdvancedTable < Playbook::KitBase
default: "header"
prop :responsive, type: Playbook::Props::Enum,
values: %w[none scroll],
default: "none"
default: "scroll"
prop :table_props, type: Playbook::Props::HashProp,
default: {}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

<% column_definitions = [
{
accessor: "year",
label: "Year",
cellAccessors: ["quarter", "month", "day"],
},
{
accessor: "newEnrollments",
label: "New Enrollments",
},
{
accessor: "scheduledMeetings",
label: "Scheduled Meetings",
},
{
accessor: "attendanceRate",
label: "Attendance Rate",
},
{
accessor: "completedClasses",
label: "Completed Classes",
},
{
accessor: "classCompletionRate",
label: "Class Completion Rate",
},
{
accessor: "graduatedStudents",
label: "Graduated Students",
},
] %>

<%= pb_rails("title", props: { size: 4 }) do %> Not Responsive <% end %>
<%= pb_rails("advanced_table", props: { id: "table_props_table_non_responsive", table_data: @table_data, column_definitions: column_definitions, responsive: "none" }) %>

<%= pb_rails("title", props: { padding_top: "sm", size: 4 }) do %> Responsive as Default <% end %>
<%= pb_rails("advanced_table", props: { id: "table_props_table_responsive", table_data: @table_data, column_definitions: column_definitions }) %>
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ examples:
- advanced_table_collapsible_trail_rails: Collapsible Trail
- advanced_table_table_props: Table Props
- advanced_table_beta_sort: Enable Sorting
- advanced_table_responsive: Responsive Tables
- advanced_table_custom_cell_rails: Custom Components for Cells
- advanced_table_column_headers: Multi-Header Columns
- advanced_table_column_headers_multiple: Multi-Header Columns (Multiple Levels)
Expand Down
21 changes: 18 additions & 3 deletions playbook/app/pb_kits/playbook/pb_advanced_table/table_body.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ class TableBody < Playbook::KitBase
default: []
prop :collapsible_trail, type: Playbook::Props::Boolean,
default: true
prop :responsive, type: Playbook::Props::Enum,
values: %w[none scroll],
default: "scroll"
prop :is_pinned_left, type: Playbook::Props::Boolean,
nidaqg marked this conversation as resolved.
Show resolved Hide resolved
default: false

def flatten_columns(columns)
columns.flat_map do |col|
Expand Down Expand Up @@ -46,12 +51,12 @@ def render_row_and_children(row, column_definitions, current_depth, first_parent
row_parent: "#{id}_#{ancestor_ids.last}",
}
# Subrow header if applicable
output << pb_rails("advanced_table/table_subrow_header", props: { row: row, column_definitions: leaf_columns, depth: current_depth, subrow_header: subrow_headers[current_depth - 1], collapsible_trail: collapsible_trail, classname: "toggle-content", subrow_data_attributes: subrow_data_attributes }) if is_first_child_of_subrow && enable_toggle_expansion == "all"
output << pb_rails("advanced_table/table_subrow_header", props: { row: row, column_definitions: leaf_columns, depth: current_depth, subrow_header: subrow_headers[current_depth - 1], collapsible_trail: collapsible_trail, classname: "toggle-content", responsive: responsive, is_pinned_left: is_pinned_left, subrow_data_attributes: subrow_data_attributes }) if is_first_child_of_subrow && enable_toggle_expansion == "all"

current_data_attributes = current_depth.zero? ? { row_depth: 0 } : table_data_attributes

# Additional class and data attributes needed for toggle logic
output << pb_rails("advanced_table/table_row", props: { id: id, row: row, column_definitions: leaf_columns, depth: current_depth, collapsible_trail: collapsible_trail, classname: additional_classes, table_data_attributes: current_data_attributes })
output << pb_rails("advanced_table/table_row", props: { id: id, row: row, column_definitions: leaf_columns, depth: current_depth, collapsible_trail: collapsible_trail, classname: additional_classes, table_data_attributes: current_data_attributes, responsive: responsive, is_pinned_left: is_pinned_left })

if row[:children].present?
row[:children].each do |child_row|
Expand All @@ -74,7 +79,17 @@ def render_row_and_children(row, column_definitions, current_depth, first_parent
end

def classname
generate_classname("pb_advanced_table_body", separator: " ")
additional_classes = []
additional_classes << "advanced-table-responsive-#{responsive}" if responsive == "scroll"
additional_classes << "pinned-left" if is_pinned_left && responsive == "scroll"

generate_classname("pb_advanced_table_body", *additional_classes, separator: " ")
end

def pinned_cell_class(index)
return "pinned-left" if index.zero? && is_pinned_left && responsive == "scroll"

""
end

private
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
<%= pb_rails("table/table_row") do %>
<% header_row.each_with_index do |cell, cell_index| %>
<% header_id = cell[:accessor].present? ? cell[:accessor] : "header_#{row_index}_#{cell_index}" %>
<%= pb_rails("table/table_header", props: { id: header_id, colspan: cell[:colspan], classname: [object.th_classname, ('last-header-cell' if cell[:is_last_in_group] && cell_index != 0)].compact.join(' '), sort_menu: cell[:accessor] ? cell[:sort_menu] : nil }) do %>
<%= pb_rails("flex", props:{ align: "center", justify: cell_index.zero? ? "start" : row_index === header_rows.size - 1 ? "end" : "center", text_align: "end" }) do %>
<% if cell_index.zero? && (object.enable_toggle_expansion == "header" || object.enable_toggle_expansion == "all") && row_index === header_rows.size - 1 %>
<button
class="gray-icon toggle-all-icon"
onclick="expandAllRows(this); event.preventDefault();">
<%= pb_rails("icon", props: { icon: "arrows-from-line", cursor: "pointer", fixed_width: true, padding_right: "xs" }) %>
</button>
<% end %>
<%= cell[:label] %>
<% end %>
<%= pb_rails("table/table_header", props: { id: header_id, colspan: cell[:colspan], classname: [object.th_classname(is_first_column: cell_index.zero?), ('last-header-cell' if cell[:is_last_in_group] && cell_index != 0)].compact.join(' '), sort_menu: cell[:accessor] ? cell[:sort_menu] : nil }) do %>
<%= pb_rails("flex", props: { align: "center", justify: cell_index.zero? ? "start" : row_index === header_rows.size - 1 ? "end" : "center", text_align: "end" }) do %>
<% if cell_index.zero? && (object.enable_toggle_expansion == "header" || object.enable_toggle_expansion == "all") && row_index === header_rows.size - 1 %>
<button
class="gray-icon toggle-all-icon"
onclick="expandAllRows(this); event.preventDefault();">
<%= pb_rails("icon", props: { icon: "arrows-from-line", cursor: "pointer", fixed_width: true, padding_right: "xs" }) %>
</button>
<% end %>
<%= cell[:label] %>
<% end %>
<% end %>
<% end %>
<% end %>
Expand Down
18 changes: 15 additions & 3 deletions playbook/app/pb_kits/playbook/pb_advanced_table/table_header.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,25 @@ class TableHeader < Playbook::KitBase
prop :enable_toggle_expansion, type: Playbook::Props::Enum,
values: %w[all header none],
default: "header"
prop :responsive, type: Playbook::Props::Enum,
values: %w[none scroll],
default: "scroll"
prop :is_pinned_left, type: Playbook::Props::Boolean,
default: false

def classname
generate_classname("pb_advanced_table_header", "pb_table_thead", separator: " ")
additional_classes = []
additional_classes << "advanced-table-responsive-#{responsive}" if responsive == "scroll"
additional_classes << "pinned-left" if is_pinned_left && responsive == "scroll"

generate_classname("pb_advanced_table_header", "pb_table_thead", *additional_classes, separator: " ")
end

def th_classname
generate_classname("table-header-cells", separator: " ")
def th_classname(is_first_column: false)
additional_classes = []
additional_classes << "pinned-left" if is_first_column && responsive == "scroll" && is_pinned_left

generate_classname("table-header-cells", *additional_classes, separator: " ")
end

def header_rows
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<%= pb_content_tag(:tr) do %>
<% object.column_definitions.each_with_index do |column, index| %>
<% next unless column[:accessor].present? %>
<%= pb_rails("table/table_cell", props: { classname:object.td_classname(column)}) do %>
<%= pb_rails("table/table_cell", props: { classname:object.td_classname(column, index)}) do %>
<%= pb_rails("flex", props:{ align: "center", justify: index.zero? ? "start" : "end" }) do %>
<% if collapsible_trail && index.zero? %>
<% (1..depth).each do |i| %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ class TableRow < Playbook::KitBase
default: true
prop :table_data_attributes, type: Playbook::Props::HashProp,
default: {}
prop :responsive, type: Playbook::Props::Enum,
values: %w[none scroll],
default: "scroll"
prop :is_pinned_left, type: Playbook::Props::Boolean,
default: false

def data
Hash(prop(:data)).merge(table_data_attributes)
Expand All @@ -22,9 +27,10 @@ def classname
generate_classname("pb_table_tr", "bg-white", subrow_depth_classname, separator: " ")
end

def td_classname(column)
def td_classname(column, index)
classes = %w[id-cell chrome-styles]
classes << "last-cell" if column[:is_last_in_group]
classes << "pinned-left" if index.zero? && is_pinned_left && responsive == "scroll"
classes.join(" ")
end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<%= pb_content_tag(:tr) do %>
<% object.column_definitions.each_with_index do |column, index| %>
<%= pb_rails("table/table_cell", props: { classname: "id-cell chrome-styles"}) do %>
<%= pb_rails("table/table_cell", props: { classname: object.td_classname(index) }) do %>
<%= pb_rails("flex", props:{ align: "center", justify: "start" }) do %>
<% if collapsible_trail && index.zero? %>
<% (1..depth).each do |i| %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ class TableSubrowHeader < Playbook::KitBase
default: true
prop :subrow_data_attributes, type: Playbook::Props::HashProp,
default: {}
prop :responsive, type: Playbook::Props::Enum,
values: %w[none scroll],
default: "scroll"
prop :is_pinned_left, type: Playbook::Props::Boolean,
default: false

def data
Hash(prop(:data)).merge(subrow_data_attributes)
Expand All @@ -25,6 +30,12 @@ def classname
generate_classname("pb_table_tr", "bg-silver", "pb_subrow_header", subrow_depth_classname, separator: " ")
end

def td_classname(index)
classes = %w[id-cell chrome-styles]
classes << "pinned-left" if index.zero? && is_pinned_left && responsive == "scroll"
classes.join(" ")
end

private

def subrow_depth_classname
Expand Down
Loading