"use client" /** Group divider rows, group selection and the pinned group clone. */ import * as React from "react" import { cn } from "../../lib/utils" import { Checkbox } from "../ui/checkbox" import { GROUP_BAR_CLASS, GROUP_BAR_WITH_SELECT_CLASS, GROUP_EMPTY_KEY, groupBarContent, groupBarTint, groupKindNode } from "./table-helpers" import type { ColumnDef } from "./types" import type { TableState } from "./table-state-types" // ───────────────────────────────────────────────────────────────────────────── // Group dividers // ───────────────────────────────────────────────────────────────────────────── export interface GroupDisplay { node: React.ReactNode label: string /** Declared-tone fill, or undefined for the shared grey wash. */ tint: string | undefined } type GroupValueOption = NonNullable>["filter"]>["options"]>[number] /** * What a divider shows for one group: the column's chip when it has one, so * the divider names the value the way the rows beneath it do, and the fill the * band carries. * * Fill rule. A column that declares a `tone` per value (status, pill) gives * each band that tone's fill, so a group reads as the same colour as the chips * in its rows: `neutral` included, which is a real tone and keeps its grey * chip. Every other column (owner, term, type, date, text) gets the one shared * grey wash on every band; a hue per value would invent a meaning the data * does not have. */ function resolveGroupDisplay( groupLabel: string, option: GroupValueOption | undefined, groupedColumnKind: ColumnDef>["cellKind"], ): GroupDisplay { const tint = option?.tone ? groupBarTint(option.tone) : undefined if (option?.node) return { node: option.node, label: option.label, tint } const empty = groupLabel === GROUP_EMPTY_KEY const label = option?.label ?? (empty ? "None" : groupLabel) // An empty group has no identity to rebuild — no avatar for a missing // person, no calendar for a missing date. return { node: empty ? label : groupKindNode(label, groupedColumnKind), label, tint } } /** * The grouped column's own vocabulary for its values, keyed by raw value — * grouping keys on `String(row[groupBy])`, so a status group arrives as * `approved`, not as the chip the column paints in its cells. * * Read off `ColumnDef.filter.options` rather than the resolved filter, because * only the column def carries `node` and `tone`; the resolved def is narrowed * to value and label for the filter menus. */ export function useGroupDisplay>( columns: ColumnDef[], groupBy: string | null | undefined, ) { const groupedColumn = groupBy ? columns.find(c => c.key === groupBy) : undefined const groupValueOptions = React.useMemo(() => { const options = groupedColumn?.filter?.options return options?.length ? new Map(options.map(o => [o.value, o])) : undefined }, [groupedColumn]) /** The kind of thing the grouped column shows, so the divider can speak it. */ const groupedColumnKind = groupedColumn?.cellKind /** Display for one group label; `undefined` for the ungrouped bucket. */ return (groupLabel: string | null): GroupDisplay | undefined => groupLabel ? resolveGroupDisplay(groupLabel, groupValueOptions?.get(groupLabel), groupedColumnKind) : undefined } /** * The select column's rendered box, which is the slot the group checkbox sits * in. Read from the live width so a consumer that ships a wider select column * keeps the two checkboxes on one axis. */ export function useSelectColumnWidth>( displayCols: ColumnDef[], colWidths: Record, ) { return React.useMemo(() => { const col = displayCols.find(c => c.key === "select") return col ? (colWidths[col.key] ?? col.width ?? col.minWidth ?? 40) : undefined }, [colWidths, displayCols]) } function groupSelectionState(ids: (string | number)[], selected: ReadonlySet) { let selectedCount = 0 for (const id of ids) if (selected.has(id)) selectedCount += 1 const all = selectedCount === ids.length return { all, checked: selectionCheckedValue(all, selectedCount > 0) } } function selectionCheckedValue(all: boolean, some: boolean): boolean | "indeterminate" { if (all) return true return some ? "indeterminate" : false } function pluralRecords(count: number): string { return `${count} record${count !== 1 ? "s" : ""}` } /** * A group's checkbox, shown in its divider bar. Row ids are derived with the * group-local index so they match the ids the body rows register under, and * the toggle adds or removes only this group's ids — a group is a subset of * the selection, not a replacement for it. * * `interactive: false` renders the mirror for the pinned clone: same box, no * pointer, no Tab stop, because the real bar is still in the table for the * keyboard and screen reader. * * Returns `null` (not an empty element) so `groupBarContent` can skip the slot. */ export function renderGroupSelect>({ groupLabel, groupRows, interactive = true, selectable, state, getRowIdProp, }: { groupLabel: string | null groupRows: TData[] interactive?: boolean selectable: boolean state: TableState getRowIdProp: ((row: TData, index: number) => string | number) | undefined }): React.ReactNode { if (!selectable || groupRows.length === 0) return null const ids = groupRows.map((row, i) => state.getRowId(row, i, getRowIdProp)) const { all, checked } = groupSelectionState(ids, state.selected) return ( state.setRowsSelected(ids, !all) : undefined} aria-label={`Select ${pluralRecords(ids.length)} in ${groupLabel ?? "group"}`} className={cn(!interactive && "pointer-events-none")} /> ) } /** Divider row for a group inside the table body. */ export function GroupBarRow({ colSpan, display, count, selectable, stickyPinsDisabled, select, selectColumnWidth, }: { colSpan: number display: GroupDisplay count: number selectable: boolean stickyPinsDisabled: boolean select: React.ReactNode selectColumnWidth: number | undefined }) { return ( {/* The cell carries the tint too: the bar inside it is only as wide as the scrollport, so on a horizontal scroll the cell is what shows past the bar's trailing edge. */}
{groupBarContent(display.node, count, select, selectColumnWidth)}
) } /** * The pinned group divider. The clip window is transparent and exactly one bar * tall, so an outgoing divider slides up behind the column header and the * incoming real row shows through the space it vacates. Nothing here is * interactive, and the real divider is still in the table for a screen reader * to read in order, so the clone stays out of the pointer and accessibility trees. */ export function PinnedGroupClone({ clipRef, slideRef, display, count, selectable, select, selectColumnWidth, }: { clipRef: React.Ref slideRef: React.Ref display: GroupDisplay | undefined count: number selectable: boolean select: React.ReactNode selectColumnWidth: number | undefined }) { return ( ) }