Skip to content

Commit

Permalink
Added logic to account for columns in column_definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
nidaqg committed Dec 23, 2024
1 parent b8f1353 commit 5cb4a34
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<% column_definitions = [
{
accessor: "year",
label: "Year",
cellAccessors: ["quarter", "month", "day"],
},
{
label: "Enrollment Data",
columns: [
{
accessor: "newEnrollments",
label: "New Enrollments",
},
{
accessor: "scheduledMeetings",
label: "Scheduled Meetings",
},
],
},
{
label: "Performance Data",
columns: [
{
accessor: "attendanceRate",
label: "Attendance Rate",
},
{
accessor: "completedClasses",
label: "Completed Classes",
},
{
accessor: "classCompletionRate",
label: "Class Completion Rate",
},
{
accessor: "graduatedStudents",
label: "Graduated Students",
},
],
},
] %>

<%= pb_rails("advanced_table", props: { id: "beta_table_with_headers", 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_beta_sort: Enable Sorting
- advanced_table_custom_cell_rails: Custom Components for Cells
- advanced_table_column_headers: Multi-Header Columns

react:
- advanced_table_default: Default (Required Props)
Expand Down
21 changes: 20 additions & 1 deletion playbook/app/pb_kits/playbook/pb_advanced_table/table_body.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,32 @@ class TableBody < Playbook::KitBase
prop :collapsible_trail, type: Playbook::Props::Boolean,
default: true

def flatten_columns(columns)
columns.flat_map do |col|
if col[:columns]
flatten_columns(col[:columns])
elsif col[:accessor].present?
col
end
end.compact
end

def render_row_and_children(row, column_definitions, current_depth, first_parent_child)
leaf_columns = flatten_columns(column_definitions)

output = ActiveSupport::SafeBuffer.new
is_first_child_of_subrow = current_depth.positive? && first_parent_child && subrow_headers[current_depth - 1].present?

output << pb_rails("advanced_table/table_subrow_header", props: { row: row, column_definitions: column_definitions, depth: current_depth, subrow_header: subrow_headers[current_depth - 1], collapsible_trail: collapsible_trail }) if is_first_child_of_subrow && enable_toggle_expansion == "all"

output << pb_rails("advanced_table/table_row", props: { id: id, row: row, column_definitions: column_definitions, depth: current_depth, collapsible_trail: collapsible_trail })
# Pass only leaf_columns to table_row to account for multiple nested columns
output << pb_rails("advanced_table/table_row", props: {
id: id,
row: row,
column_definitions: leaf_columns,
depth: current_depth,
collapsible_trail: collapsible_trail,
})

if row[:children].present?
output << content_tag(:div, class: "toggle-content", data: { advanced_table_content: row.object_id.to_s + id }) do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
<%= pb_content_tag do %>
<%= pb_rails("table/table_row", props: { tag: "div" }) do %>
<% object.column_definitions.each_with_index do |item, index| %>
<%= pb_rails("table/table_header", props: { tag: "div", id: item[:accessor], classname: object.th_classname, sort_menu: item[:sort_menu] }) do %>
<%= pb_rails("flex", props:{ align: "center", justify: index.zero? ? "start" : "end", text_align: "end" }) do %>
<% if index.zero? && (object.enable_toggle_expansion == "header" || object.enable_toggle_expansion == "all") %>
<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 %>
<%= item[:label] %>
<% object.header_rows.each_with_index do |header_row, row_index| %>
<%= pb_rails("table/table_row", props: { tag: "div" }) 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: { tag: "div", id: header_id, classname: object.th_classname, sort_menu: cell[:accessor] ? cell[:sort_menu] : nil }) do %>
<%= pb_rails("flex", props:{ align: "center", justify: row_index.zero? ? "start" : "end", text_align: "end" }) do %>
<% if cell_index.zero? && (object.enable_toggle_expansion == "header" || object.enable_toggle_expansion == "all") %>
<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 %>
<% end %>
<% end %>
<% end %>
47 changes: 47 additions & 0 deletions playbook/app/pb_kits/playbook/pb_advanced_table/table_header.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,53 @@ def classname
def th_classname
generate_classname("table-header-cells", separator: " ")
end

def header_rows
rows = []
max_depth = compute_max_depth(column_definitions)

max_depth.times { rows << [] }

process_columns(column_definitions, rows, 0, max_depth)

rows
end

private

def compute_max_depth(columns)
columns.map do |col|
col[:columns] ? 1 + compute_max_depth(col[:columns]) : 1
end.max || 1
end

def process_columns(columns, rows, current_depth, max_depth)
columns.each do |col|
if col[:columns]
colspan = compute_leaf_columns(col[:columns])
rowspan = 1
rows[current_depth] << { label: col[:label], colspan: colspan, rowspan: rowspan }

process_columns(col[:columns], rows, current_depth + 1, max_depth)
else
colspan = 1
rowspan = max_depth - current_depth
rows[current_depth] << {
label: col[:label],
colspan: colspan,
rowspan: rowspan,
accessor: col[:accessor],
sort_menu: col[:sort_menu],
}
end
end
end

def compute_leaf_columns(columns)
columns.reduce(0) do |sum, col|
col[:columns] ? sum + compute_leaf_columns(col[:columns]) : sum + 1
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<%= pb_content_tag do %>
<% object.column_definitions.each_with_index do |column, index| %>
<% next unless column[:accessor].present? %>
<%= pb_rails("table/table_cell", props: { tag:"div", classname:object.td_classname}) do %>
<%= pb_rails("flex", props:{ align: "center", justify: index.zero? ? "start" : "end" }) do %>
<% if collapsible_trail && index.zero? %>
Expand Down
3 changes: 3 additions & 0 deletions playbook/app/pb_kits/playbook/pb_advanced_table/table_row.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ def depth_accessors
private

def custom_renderer_value(column, index)
return nil unless column[:accessor].present?

if index.zero?
if depth.zero?
row[column[:accessor].to_sym]
Expand All @@ -37,6 +39,7 @@ def custom_renderer_value(column, index)
key = item.to_sym
return row[key] if depth - 1 == accessor_index
end
nil
end
else
row[column[:accessor].to_sym]
Expand Down

0 comments on commit 5cb4a34

Please sign in to comment.