Skip to content

Commit

Permalink
wrapped column logic
Browse files Browse the repository at this point in the history
  • Loading branch information
nidaqg committed Dec 23, 2024
1 parent 5cb4a34 commit 242ffd2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
<% 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") %>
<%= pb_rails("flex", props:{ align: "center", justify: cell_index.zero? ? "start" : "end", 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();">
Expand All @@ -17,7 +15,6 @@
<%= cell[:label] %>
<% end %>
<% end %>

<% end %>
<% end %>
<% end %>
Expand Down
33 changes: 28 additions & 5 deletions playbook/app/pb_kits/playbook/pb_advanced_table/table_header.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ def th_classname
end

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

rows = []
max_depth = compute_max_depth(wrapped_columns)
max_depth.times { rows << [] }

process_columns(column_definitions, rows, 0, max_depth)

process_columns(wrapped_columns, rows, 0, max_depth)
rows
end

Expand Down Expand Up @@ -63,6 +62,30 @@ def compute_leaf_columns(columns)
col[:columns] ? sum + compute_leaf_columns(col[:columns]) : sum + 1
end
end

def wrap_leaf_columns(column_definitions)
has_grouped_headers = column_definitions.any? { |col| col.key?(:columns) }

if has_grouped_headers
column_definitions.map do |col|
if col.key?(:columns)
{
label: col[:label],
columns: wrap_leaf_columns(col[:columns]),
}
elsif col.key?(:accessor)
{
label: "",
columns: [col],
}
else
col
end
end
else
column_definitions
end
end
end
end
end

0 comments on commit 242ffd2

Please sign in to comment.