Skip to content

feat(core): add useTableGroupedRows plugin - #3763

Merged
humbertovirtudes merged 2 commits into
meta:mainfrom
humbertovirtudes:navi/feat/table-grouped-rows
Jul 14, 2026
Merged

humbertovirtudes merged 2 commits into
meta:mainfrom
humbertovirtudes:navi/feat/table-grouped-rows

Conversation

@humbertovirtudes

Copy link
Copy Markdown
Contributor

What

Adds useTableGroupedRows — groups a flat data array into collapsible section rows.

const [collapsed, setCollapsed] = useState<Set<string>>(new Set());
const grouped = useTableGroupedRows({
  data: rows,
  groupBy: r => r.team,
  collapsedGroups: collapsed,
  onToggleGroup: key => setCollapsed(prev => { const n = new Set(prev); n.has(key) ? n.delete(key) : n.add(key); return n; }),
  getRowKey: r => r.id,
});
<Table data={grouped.data} columns={columns} idKey={grouped.getRowKey} plugins={{grouped: grouped.plugin}} />;

Design

  • Each distinct groupBy value becomes a full-width section-header row with a chevron toggle, the group label, and a member count. Collapsing hides that group's data rows while keeping the header visible.
  • Mirrors useTableRowExpansionState: the consumer owns the collapsedGroups set; the hook returns {data, plugin, getRowKey} — pass to <Table> data / plugins / idKey respectively.
  • Synthetic header rows are Proxy-wrapped so arbitrary field access from user cell renderers (item.name.toUpperCase()) resolves to '' instead of throwing — BaseTable evaluates renderCell(item) on every row before transformBodyRow can replace the header's cells with a single full-width cell.
  • renderGroupHeader(groupKey, count, collapsed) for custom header content; groupOrder to pin specific groups first.

Includes

  • Storybook (Core/TableGroupedRows): Default, InitiallyCollapsed, CustomOrderAndHeader.
  • Docsite: useTableGroupedRows hook page + a "Collapsible Groups" example block.
  • Table Lab: added as a toggle (groups by team; overrides data + idKey when active).
  • Unit tests (5): header per group with label+count, members shown when expanded, hidden when collapsed, chevron toggle, groupOrder.

Validation

  • Build ✅, lint ✅, sandbox typecheck ✅
  • 145 tests pass (groupedRows + full Table suite, no regressions); changeset included.

Groups a flat data array into collapsible section rows. Each distinct
groupBy value becomes a full-width section-header row with a chevron
toggle, group label, and member count; collapsing hides that group's rows
while keeping the header visible.

- Mirrors useTableRowExpansionState: consumer owns the collapsedGroups set;
  the hook returns {data, plugin, getRowKey} — pass to Table data/plugins/idKey.
- Supports renderGroupHeader (custom header content) and groupOrder.
- Synthetic header rows are Proxy-wrapped so user cell renderers can't throw
  on them before transformBodyRow replaces the row with a full-width cell.
- Storybook stories (default, initially-collapsed, custom order/header),
  docsite hook doc + example block, unit tests, and a Table Lab toggle.
@vercel

vercel Bot commented Jul 10, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
astryx Ready Ready Preview, Comment Jul 12, 2026 5:03am

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Jul 10, 2026
@ernestt

ernestt commented Jul 10, 2026

Copy link
Copy Markdown
Contributor
CleanShot 2026-07-10 at 14 23 51@2x It generally is looking really good. Just a few small details: 1. Can we use the chevron icon standalone and not in a button so the icon aligns with the start of the table 2. Add a divider line below the grouped row. 3. Grouped row label should use emphasized body and not a larger text size.

A reference for visual can be found here: https://astryx.atmeta.com/templates?preview=table-grouped

collapsedGroups,
onToggleGroup,
renderGroupHeader,
getRowKey: getRowKeyProp,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been trying avoid needing to create too many callbacks so the table accepts an idKey which can optionally be a getter. Think we could match the shape here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Matched the shape — the config still takes getRowKey (a getter), and the result now exposes it as idKey so it lines up with <Table idKey={...}> (see the sibling comment). Kept it a single getter rather than string-or-getter since the plugin injects synthetic header rows that need custom keying, so a plain property key wouldn't cover them.

return out;
}, [data, groupBy, collapsedGroups, groupOrder]);

const getRowKey = useCallback(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For parallelism with Table we could name this idKey in the output:

  const groupedRows = useTableGroupedRows(...);

<Table idKey={groupedRows.idKey} .../>

// vs

<Table idKey={groupedRows.getRowKey} .../>


Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done — renamed the result field getRowKey → idKey, so it now reads <Table idKey={grouped.idKey} … />. Updated the stories, block template, Table Lab, and docs to match.

@cixzhang cixzhang added area:core needs:code-review High-risk change (new package/component/API) — needs human code review before merge and removed area:core labels Jul 11, 2026
@github-actions github-actions Bot added needs:design-review Affects visuals — Design should review and removed needs:code-review High-risk change (new package/component/API) — needs human code review before merge labels Jul 12, 2026
Address review feedback (cixzhang, ernestt) and self-review findings:

- Rename result field getRowKey -> idKey for parallelism with the Table prop
  (cixzhang): <Table idKey={grouped.idKey} />. Config input stays `getRowKey`.
- Fix O(n^2) row keying (self-review): the positional fallback used
  flattened.indexOf(item) per row. Prebuild an item->index Map once per
  flattened array so key lookup is O(1) — O(n^2) -> O(n) for the whole table.
- Chevron alignment (ernestt meta#1): the chevron is a standalone, chrome-less
  button flush with the table's leading edge (no heavy button background),
  still keyboard-operable; the whole header row remains a click convenience.
- Divider (ernestt meta#2): add a bottom border beneath each group header row.
- Typography (ernestt meta#3): the group label uses emphasized body (semibold at
  body size), not a larger text size; count is regular weight.
- Tests (10, all green): add renderGroupHeader coverage, empty-data,
  collapse-persistence-across-data-change (count updates, stays collapsed),
  toggle-and-back, aria-expanded on the header row, and a named keyboard-
  operable toggle button.

Tags: ai-generated

This diff was generated with AI assistance (Navi). The implementation and
tests were produced by an AI agent; the human author reviewed and approved
the changes.
@humbertovirtudes

Copy link
Copy Markdown
Contributor Author

Thanks Ernest — all three addressed:

  1. The chevron is now a standalone, chrome-less button flush with the table's leading edge (no button background), so it aligns with the start of the row.
  2. Added a divider line beneath each group header row.
  3. The group label now uses emphasized body (semibold at body size) instead of a larger text size; the count is regular weight.

Matched against the reference template. Kept in draft for another look.

@github-actions

Copy link
Copy Markdown
Contributor

PR Analysis Report

No new or modified components detected.

Bundle Size Summary

Package Size (ESM) Size (CJS) Gzipped
@astryxdesign/core N/A 4.6KB 0B

Accessibility Audit

Status: No accessibility violations detected.


Generated by PR Enrichment workflow | View full report

@humbertovirtudes
humbertovirtudes marked this pull request as ready for review July 14, 2026 08:19
@humbertovirtudes
humbertovirtudes merged commit 7a36764 into meta:main Jul 14, 2026
24 checks passed
AKnassa added a commit to AKnassa/astryx that referenced this pull request Jul 16, 2026
Resolves the transforms/v0.1.5/index.mjs add/add collision with the
Switch labelSpacing codemod (meta#3881). Core 0.1.5 and 0.1.6 have since
released, so the tableProps codemod moves to transforms/v0.1.7/
(registry entry, registry test, and doc comments renamed to match) and
v0.1.5/ is kept exactly as it shipped on main.

Also updates the new useTableGroupedRows plugin (meta#3763, added on main
after this branch) to this PR's renamed row-transform contract
(styles -> xstyle), fixing its 8 tests against the merged tree.

This branch was successfully deployed

1 active deployment
Preview — faafa7b5 Deployed Jul 12, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot. needs:design-review Affects visuals — Design should review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants