Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Render the Activity Breakdown tables with DS::Table (#4683)

Dov Benyomin Sohacheski1d52dc19a485
diff --git a/app/components/DS/table.rb b/app/components/DS/table.rb
index 49f84c5e9c..47695befec 100644
--- a/app/components/DS/table.rb
+++ b/app/components/DS/table.rb
@@ -15,7 +15,7 @@ class DS::Table < DesignSystemComponent
# markup or return a String: Rails' `capture` drops any other return value,
# so a bare Integer renders as an empty cell (call `to_s` on it).
renders_many :columns, ->(header, **options, &cell) do
- DS::Table::Column.new(header, cell: cell, sticky: sticky_header, **options)
+ DS::Table::Column.new(header, cell: cell, sticky: sticky_header, inset: inset, **options)
end
attr_reader :rows, :inset, :sticky_header, :label, :row_class, :row_opts, :opts
@@ -24,6 +24,8 @@ class DS::Table < DesignSystemComponent
# @param inset [Boolean] frame the table in container-inset, for a table that
# sits inside a card the way a list group does. A table on the page
# background is a card of its own.
+ # Its header row takes the frame's container-inset, so the two read as one
+ # surface.
# @param sticky_header [Boolean] cap the height and keep the header row in view
# while the body scrolls under it, for long tables such as a loan schedule
# @param label [String, nil] accessible name for the scroll area. Makes it a
diff --git a/app/components/DS/table/column.rb b/app/components/DS/table/column.rb
index 10b7719f61..02d66302b2 100644
--- a/app/components/DS/table/column.rb
+++ b/app/components/DS/table/column.rb
@@ -8,6 +8,7 @@ class DS::Table::Column < DesignSystemComponent
# @param header [String] the column heading
# @param cell [Proc] renders the cell for a row, given the row and its index
# @param sticky [Boolean] set by the table when its header row is sticky
+ # @param inset [Boolean] set by the table when it sits in an inset frame
# @param align [Symbol] :left (default) or :right, for the header and the cells
# @param numeric [Boolean] a column of figures: right-aligned, tabular digits,
# never wrapped mid-number. Implies align: :right.
@@ -15,10 +16,11 @@ class DS::Table::Column < DesignSystemComponent
# column that names each row
# @param class [String, nil] extra classes for the body cells, e.g. "text-secondary"
# or "privacy-sensitive"
- def initialize(header, cell:, sticky: false, align: :left, numeric: false, row_header: false, class: nil)
+ def initialize(header, cell:, sticky: false, inset: false, align: :left, numeric: false, row_header: false, class: nil)
@header = header
@cell = cell
@sticky = sticky
+ @inset = inset
@numeric = numeric
# to_s first so a nil or unrecognized value falls back instead of raising.
candidate = align.to_s.to_sym
@@ -36,7 +38,8 @@ class DS::Table::Column < DesignSystemComponent
alignment_classes,
# z-10 so a privacy-blurred amount, which paints in a layer of its own,
# doesn't show through the header as it scrolls under it.
- ("sticky top-0 z-10 bg-container" if @sticky)
+ ("sticky top-0 z-10" if @sticky),
+ @inset ? "bg-container-inset" : ("bg-container" if @sticky)
)
end
diff --git a/app/helpers/reports_helper.rb b/app/helpers/reports_helper.rb
index 15be9f2248..3aab7df44d 100644
--- a/app/helpers/reports_helper.rb
+++ b/app/helpers/reports_helper.rb
@@ -1,4 +1,13 @@
module ReportsHelper
+ # Where a breakdown row links: its category's transactions in the period.
+ # /transactions only lists Transaction rows, so trade-only rows (e.g. Other
+ # Investments) link nowhere rather than to an empty list.
+ def breakdown_transactions_path(item, start_date, end_date)
+ return unless item[:has_transactions] && start_date.present? && end_date.present?
+
+ transactions_path(q: { categories: [ item[:category_filter_value] ], start_date: start_date, end_date: end_date })
+ end
+
# Returns the DS::Pill tone for a given tax treatment. Mirrors the
# mapping used by `accounts/_tax_treatment_badge.html.erb` so the
# report and the per-account badge stay visually aligned.
diff --git a/app/javascript/controllers/hotkey_controller.js b/app/javascript/controllers/hotkey_controller.js
index 19c08cd2a5..c1edd278ee 100644
--- a/app/javascript/controllers/hotkey_controller.js
+++ b/app/javascript/controllers/hotkey_controller.js
@@ -24,6 +24,8 @@ export default class extends Controller {
// couldn't close the dialog.
// - A held key's auto-repeats, for a link. Each one would start the same
// Turbo visit over again.
+ // - Arrow keys in a focused scroll area (a labelled DS::Table). They scroll
+ // it, and the library would take them for the period hotkeys.
// Added in connect(), not as a data-action, so no hotkey can miss it, and in
// the capture phase so it runs before the library's own keydown listener.
syncInstall = (event) => {
@@ -31,7 +33,11 @@ export default class extends Controller {
event?.target.closest?.("dialog:modal") ??
document.querySelector("dialog:modal");
const heldLink = event?.repeat && this.element.matches("a[href]");
- const active = (!modal || modal.contains(this.element)) && !heldLink;
+ const scrolling =
+ event?.key?.startsWith("Arrow") &&
+ event.target.closest?.("[role='region'][tabindex]");
+ const active =
+ (!modal || modal.contains(this.element)) && !heldLink && !scrolling;
if (active === this.installed) return;
if (active) install(this.element);
diff --git a/app/views/reports/_breakdown_table.html.erb b/app/views/reports/_breakdown_table.html.erb
index 24d2d84721..bb283ec7f8 100644
--- a/app/views/reports/_breakdown_table.html.erb
+++ b/app/views/reports/_breakdown_table.html.erb
@@ -14,51 +14,32 @@
<span class="font-medium text-secondary <%= color_class %> privacy-sensitive"> <%= Money.new(total, Current.family.currency).format %></span>
</div>
- <div class="bg-container-inset rounded-xl p-1 overflow-x-auto">
- <div class="w-max sm:w-full">
- <div class="rounded-lg shadow-border-xs bg-container overflow-hidden">
- <table class="w-full">
- <thead class="bg-container-inset">
- <tr class="uppercase text-xs font-medium text-secondary">
- <th class="px-4 py-2 text-left font-medium" colspan="2"><%= t("reports.transactions_breakdown.table.category") %></th>
- <th class="px-4 py-2 text-right font-medium">
- <%= link_to reports_path(amount_sort_params), class: "inline-flex items-center gap-1 hover:text-primary" do %>
- <%= t("reports.transactions_breakdown.table.amount") %>
- <% if current_sort_by == "amount" %>
- <%= icon(current_sort_direction == "desc" ? "chevron-down" : "chevron-up", class: "w-3 h-3") %>
- <% end %>
- <% end %>
- </th>
- <th class="px-4 py-2 text-right font-medium"><%= t("reports.transactions_breakdown.table.percentage") %></th>
- </tr>
- </thead>
- <tbody>
- <% groups.each_with_index do |group, idx| %>
- <%= render "reports/category_row",
- item: group,
- total: total,
- color_class: color_class,
- level: :category,
- start_date: start_date,
- end_date: end_date,
- show_border: idx < groups.size - 1 || group[:subcategories].present? %>
- <%# Render subcategories if present %>
- <% if group[:subcategories].present? && group[:subcategories].any? %>
- <% group[:subcategories].each_with_index do |subcategory, sub_idx| %>
- <%= render "reports/category_row",
- item: subcategory,
- total: total,
- color_class: color_class,
- level: :subcategory,
- start_date: start_date,
- end_date: end_date,
- show_border: sub_idx < group[:subcategories].size - 1 %>
- <% end %>
- <% end %>
- <% end %>
- </tbody>
- </table>
- </div>
- </div>
- </div>
+ <%
+ rows = groups.flat_map do |group|
+ [ group.merge(level: :category), *Array(group[:subcategories]).map { |subcategory| subcategory.merge(level: :subcategory) } ]
+ end.map { |item| item.merge(href: breakdown_transactions_path(item, start_date, end_date)) }
+ amount_header = link_to reports_path(amount_sort_params), class: "inline-flex items-center gap-1 hover:text-primary" do
+ concat t("reports.transactions_breakdown.table.amount")
+ concat icon(current_sort_direction == "desc" ? "chevron-down" : "chevron-up", class: "w-3 h-3") if current_sort_by == "amount"
+ end
+ link_options = { class: "block", data: { turbo_frame: "_top", turbo_prefetch: false } }
+ %>
+
+ <%= render DS::Table.new(
+ rows: rows,
+ inset: true,
+ label: t(title_key),
+ row_class: ->(_item) { "text-secondary group/category-row" },
+ row_opts: ->(item) { { data: { category: "category-#{item[:category_id]}" } } }
+ ) do |table| %>
+ <% table.with_column(t("reports.transactions_breakdown.table.category")) do |item| %>
+ <%= render "reports/category_name", item: item %>
+ <% end %>
+ <% table.with_column(amount_header, numeric: true) do |item| %>
+ <%= link_to_if item[:href], tag.span(Money.new(item[:total], Current.family.currency).format, class: "#{color_class} privacy-sensitive"), item[:href], link_options %>
+ <% end %>
+ <% table.with_column(t("reports.transactions_breakdown.table.percentage"), numeric: true) do |item| %>
+ <%= link_to_if item[:href], "#{total.zero? ? 0 : (item[:total].to_f / total * 100).round(1)}%", item[:href], link_options %>
+ <% end %>
+ <% end %>
</div>
diff --git a/app/views/reports/_category_name.html.erb b/app/views/reports/_category_name.html.erb
new file mode 100644
index 0000000000..e1642d9a4e
--- /dev/null
+++ b/app/views/reports/_category_name.html.erb
@@ -0,0 +1,35 @@
+<%
+ href = item[:href]
+ is_sub = item[:level] == :subcategory
+ content = capture do
+ concat tag.div(icon("corner-down-right"), class: "text-subdued") if is_sub
+ if item[:category_icon]
+ concat tag.div(
+ icon(item[:category_icon], color: "current", size: "sm"),
+ class: "h-7 w-7 flex-shrink-0 rounded-full flex justify-center items-center #{"group-hover/category-row:scale-105 transition-all duration-300" if href}",
+ style: "background-color: color-mix(in oklab, #{item[:category_color]} 10%, transparent); border-color: color-mix(in oklab, #{item[:category_color]} 10%, transparent); color: #{item[:category_color]};"
+ )
+ else
+ concat render(DS::FilledIcon.new(
+ variant: :text,
+ hex_color: item[:category_color],
+ text: item[:category_name],
+ size: "md",
+ rounded: true
+ ))
+ end
+ concat tag.span(item[:category_name], class: "font-medium text-primary #{"hover:underline" if href}")
+ concat tag.span("(#{t("reports.transactions_breakdown.table.entries", count: item[:count])})", class: "text-xs text-subdued whitespace-nowrap")
+ end
+ classes = class_names("flex items-center gap-2", "pl-3" => is_sub)
+%>
+
+<%# The link wraps the whole cluster (icon, name, count) in normal flex flow —
+ deliberately NOT the `position: relative`/`absolute` stretched-link trick,
+ which inside a <table> row is a known source of WebKit table-layout
+ scrollHeight miscalculation (see #3093 investigation). %>
+<% if href %>
+ <%= link_to content, href, class: classes, data: { turbo_frame: "_top", turbo_prefetch: false } %>
+<% else %>
+ <%= tag.div content, class: classes %>
+<% end %>
diff --git a/app/views/reports/_category_row.html.erb b/app/views/reports/_category_row.html.erb
deleted file mode 100644
index 3e6676bb16..0000000000
--- a/app/views/reports/_category_row.html.erb
+++ /dev/null
@@ -1,80 +0,0 @@
-<%
- percentage = total.zero? ? 0 : (item[:total].to_f / total * 100).round(1)
- is_sub = level == :subcategory
- show_border = local_assigns.fetch(:show_border, false)
- row_start_date = local_assigns[:start_date]
- row_end_date = local_assigns[:end_date]
- # /transactions only lists Transaction rows. Skip trade-only buckets (e.g. Other
- # Investments) so the destination is not empty relative to the report row.
- clickable = item[:has_transactions] && row_start_date.present? && row_end_date.present?
- transactions_href = clickable ? transactions_path(q: { categories: [ item[:category_filter_value] ], start_date: row_start_date, end_date: row_end_date }) : nil
-%>
-
-<tr data-category="category-<%= item[:category_id] %>"
- class="text-secondary text-sm group/category-row <%= show_border ? "table-divider" : "" %>">
- <td colspan="2" class="py-3 px-4 lg:px-6 <%= is_sub ? "pl-7 lg:pl-9" : "" %>">
- <%
- row_content = capture do
- if is_sub
- concat tag.div(icon("corner-down-right"), class: "text-subdued")
- end
- if item[:category_icon]
- concat tag.div(
- icon(item[:category_icon], color: "current", size: "sm"),
- class: "h-7 w-7 flex-shrink-0 rounded-full flex justify-center items-center #{"group-hover/category-row:scale-105 transition-all duration-300" if clickable}",
- style: "background-color: color-mix(in oklab, #{item[:category_color]} 10%, transparent); border-color: color-mix(in oklab, #{item[:category_color]} 10%, transparent); color: #{item[:category_color]};"
- )
- else
- concat render(DS::FilledIcon.new(
- variant: :text,
- hex_color: item[:category_color],
- text: item[:category_name],
- size: "md",
- rounded: true
- ))
- end
- concat tag.span(item[:category_name], class: "font-medium text-primary #{"hover:underline" if clickable}")
- concat tag.span("(#{t("reports.transactions_breakdown.table.entries", count: item[:count])})", class: "text-xs text-subdued whitespace-nowrap")
- end
- %>
- <%# The clickable variant wraps the whole cluster (icon, name, count) in a
- single <a> using normal flex flow — deliberately NOT the `position:
- relative`/`absolute` stretched-link trick, which inside a <table> row
- is a known source of WebKit table-layout scrollHeight miscalculation
- (see #3093 investigation). %>
- <% if clickable %>
- <%= link_to row_content, transactions_href,
- class: "flex items-center gap-2",
- data: { turbo_frame: "_top", turbo_prefetch: false } %>
- <% else %>
- <%= tag.div row_content, class: "flex items-center gap-2" %>
- <% end %>
- </td>
-
- <td class="py-3 px-4 lg:px-6 text-right">
- <%
- amount_content = tag.span(
- Money.new(item[:total], Current.family.currency).format,
- class: "text-sm #{color_class} whitespace-nowrap privacy-sensitive"
- )
- %>
- <% if clickable %>
- <%= link_to amount_content, transactions_href,
- class: "block", data: { turbo_frame: "_top", turbo_prefetch: false } %>
- <% else %>
- <%= amount_content %>
- <% end %>
- </td>
-
- <td class="py-3 px-4 lg:px-6 text-right">
- <%
- percentage_content = tag.span("#{percentage}%", class: "text-sm text-secondary")
- %>
- <% if clickable %>
- <%= link_to percentage_content, transactions_href,
- class: "block", data: { turbo_frame: "_top", turbo_prefetch: false } %>
- <% else %>
- <%= percentage_content %>
- <% end %>
- </td>
-</tr>
diff --git a/docs/llm-guides/design-system.md b/docs/llm-guides/design-system.md
index 67b519f838..23cad1bb7e 100644
--- a/docs/llm-guides/design-system.md
+++ b/docs/llm-guides/design-system.md
@@ -36,8 +36,9 @@ header and every cell with the standard padding, header style and row rules.
- `numeric: true` for figures (right-aligned, tabular digits, no wrapping);
`align: :right` for other right-hand columns such as actions.
-- `inset: true` when the table sits inside a card. On the page background the
- table is a card of its own.
+- `inset: true` when the table sits inside a card; its header row takes the
+ frame's container-inset. On the page background the table is a card of its
+ own.
- `sticky_header: true` for long tables.
- Pass `label:` whenever the table can scroll, including sideways on a narrow
screen, so keyboard users can reach it.
diff --git a/test/components/DS/table_test.rb b/test/components/DS/table_test.rb
index 415134ea2b..0980f5ccff 100644
--- a/test/components/DS/table_test.rb
+++ b/test/components/DS/table_test.rb
@@ -86,6 +86,18 @@ class DS::TableTest < ViewComponent::TestCase
assert_selector "div.rounded-xl.bg-container-inset.p-1 > div.table-scroll.rounded-lg"
end
+ test "gives an inset table's header the frame's background, sticky or not" do
+ render_table
+ assert_no_selector "thead th.bg-container-inset"
+
+ render_table(inset: true)
+ assert_selector "thead th.bg-container-inset", count: 2
+
+ render_table(inset: true, sticky_header: true)
+ assert_selector "thead th.sticky.bg-container-inset", count: 2
+ assert_no_selector "thead th.bg-container"
+ end
+
test "caps the height and makes the header cells sticky" do
render_table(sticky_header: true)
diff --git a/test/controllers/reports_controller_test.rb b/test/controllers/reports_controller_test.rb
index d053a2c73e..acd397e2e7 100644
--- a/test/controllers/reports_controller_test.rb
+++ b/test/controllers/reports_controller_test.rb
@@ -594,6 +594,14 @@ class ReportsControllerTest < ActionDispatch::IntegrationTest
assert_select "tr[data-category='category-#{subcategory_games.id}']", text: /^Games/
end
+ test "index lets keyboard users scroll the breakdown table" do
+ create_transaction(account: @family.accounts.first, name: "Lunch", amount: 15, category: categories(:food_and_drink))
+
+ get reports_path(period_type: :monthly)
+
+ assert_select "[role=region][tabindex='0'][aria-label=?] table", "Expenses"
+ end
+
test "index links income and expense categories to filtered transactions" do
start_date = Date.current.beginning_of_month
end_date = Date.current.end_of_month
diff --git a/test/system/reports_test.rb b/test/system/reports_test.rb
index 2888ef2369..9f49b65ea1 100644
--- a/test/system/reports_test.rb
+++ b/test/system/reports_test.rb
@@ -46,6 +46,14 @@ class ReportsTest < ApplicationSystemTestCase
assert_equal %w[ArrowLeft], clicked_hotkeys
end
+ test "the arrow keys scroll a focused breakdown table instead of changing the period" do
+ find("section[data-section-key='transactions_breakdown'] [role='region']", match: :first).send_keys(:arrow_left)
+ assert_empty clicked_hotkeys
+
+ find("body").send_keys(:arrow_left)
+ assert_equal %w[ArrowLeft], clicked_hotkeys
+ end
+
test "a held arrow key steps one period" do
page.execute_script(<<~JS)
for (const repeat of [false, true, true]) {