Skip to content
12 changes: 12 additions & 0 deletions .changeset/35742-overview-body-table-order.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
'@object-ui/plugin-detail': patch
---

fix(plugin-detail): the record page Overview draws the record's own field groups first and its master-detail tables under them, at the list's dense row height, never with a half-cut column

Clause-②: no
Metadata-protocol: none

- **Order.** On the default record page's Overview, a master-detail (body) table now follows the field sections the page draws open, instead of leading them: stage bar and key fields (the strip across the top), then the record's field groups, then its body tables. An object with no stage bar and no key fields opens on its own fields. A field group whose `relatedList` names a body table still places it at that group's position. Sections drawn closed (a field group with `collapse: 'collapsed'`, or a `collapsible` section that is `defaultCollapsed`) that trail the last open one follow the tables; a closed section between open ones keeps its declared place. With every section closed, the tables follow them all.
- **Density.** A body table's data rows are 33px (a 32px cell over the row line) under a 32px header, the measures of a list's compact rows (`BODY_TABLE_DENSITY`).
- **Width.** A body table draws the leading columns, in their order, that fit whole in the details column, and offers the rest behind its labelled ⤢ "All columns" button; it no longer scrolls sideways with a column cut at the right edge. Only a table whose first column alone is wider than the column still scrolls, with that column pinned.
121 changes: 114 additions & 7 deletions ui/packages/plugin-detail/src/RelatedList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,59 @@ const BodyTableTotalsLine: React.FC<{ totals: readonly BodyTableTotal[] }> = ({
);
};

/**
* A body table's density (objectstack#35742): the list's dense rows — a 32px
* cell over the row's 1px line, 33px a row — under a 32px header at 13px, the
* measures `object-grid` draws a list's `compact` rows with
* (objectstack#35548). On the table's root, so it reaches every header and
* body cell, the row-actions cell included, over the table primitive's
* roomier defaults.
*/
export const BODY_TABLE_DENSITY = [
'[&_thead_th]:h-8 [&_thead_th]:px-3 [&_thead_th]:py-0 [&_thead_th]:text-[13px]',
'[&_tbody_td]:h-8 [&_tbody_td]:px-3 [&_tbody_td]:py-0 [&_tbody_td]:text-[13px] [&_tbody_td]:leading-tight',
].join(' ');

/**
* The width a body table's drawn column asks for: the inline width the table
* gives its header cell (a user's resize, the declared width or the estimate
* from what its cells draw), which does not grow when the table stretches
* its columns to fill the row; else the width already `known` for it, else
* the drawn width; `undefined` while it has none.
*/
function bodyTableColumnWidth(th: HTMLElement, known: number | undefined): number | undefined {
const declared = th.style.minWidth;
if (/^\d+(\.\d+)?(px)?$/.test(declared)) return parseFloat(declared);
// A width the table spells otherwise (a fit-content `1%`, an authored CSS
// length) is read off the drawn cell once, and only when it has one: a
// header drawn before the table has sized it asks for nothing yet.
if (known !== undefined) return known;
const drawn = th.getBoundingClientRect().width;
return drawn > 0 ? drawn : undefined;
}

/**
* How many of a body table's columns, in their priority order, fit whole in
* `available` px (objectstack#35742): the leading run whose widths sum to no
* more than it, never fewer than one. `null` when every column fits, or when
* a column's width is not known yet (it has not been drawn), so the table
* draws them all and is measured again.
*/
export function bodyTableColumnsThatFit(
keys: readonly string[],
widths: ReadonlyMap<string, number>,
available: number,
): number | null {
let used = 0;
for (let index = 0; index < keys.length; index += 1) {
const width = widths.get(keys[index]);
if (width === undefined) return null;
used += width;
if (used > available) return Math.max(1, index);
}
return null;
}

/** The add config of {@link RelatedListProps.add}, as `derivedAddOf` derives one. */
type RelatedListAdd = NonNullable<RelatedListProps['add']>;

Expand Down Expand Up @@ -3266,6 +3319,25 @@ export const RelatedList: React.FC<RelatedListProps> = ({
// table's scroll box by the effect below the empty answer.
const bodyTableHostRef = React.useRef<HTMLDivElement | null>(null);
const [bodyTableOverflows, setBodyTableOverflows] = React.useState(false);
// objectstack#35742 — never a half-cut column: a body table draws the
// leading run of its columns, in their priority order, that fits whole in
// its column (`bodyTableColumnsThatFit`); the rest wait behind ⤢ 「全部列」.
// `null` draws every column. The width each column asks for is kept by its
// key, so a column left out is still counted when the box grows.
const [bodyTableColumnCap, setBodyTableColumnCap] = React.useState<number | null>(null);
const bodyTableColumnWidths = React.useRef(new Map<string, number>());
const bodyTableColumnKeysKey = React.useMemo(
() => JSON.stringify((tableColumns ?? []).map((col: any) => String(col?.accessorKey ?? ''))),
[tableColumns],
);
// A different column set is measured afresh, every column drawn.
React.useLayoutEffect(() => {
bodyTableColumnWidths.current.clear();
setBodyTableColumnCap(null);
}, [bodyTableColumnKeysKey]);
const bodyTableColumnsOmitted = bodyTable && bodyTableColumnCap !== null;
// ⤢ in words, and the overflow cue: the table scrolls, or leaves columns out.
const bodyTableNarrow = bodyTableOverflows || bodyTableColumnsOmitted;

// The row click hands the host the rows on show beside the clicked one
// (objectstack#26679), so its drawer can step through them.
Expand Down Expand Up @@ -3391,7 +3463,11 @@ export const RelatedList: React.FC<RelatedListProps> = ({
return {
type: 'data-table',
data: paginatedData,
columns: compact ? tableColumns.slice(0, 1) : tableColumns,
columns: compact
? tableColumns.slice(0, 1)
: bodyTableColumnsOmitted ? tableColumns.slice(0, bodyTableColumnCap!) : tableColumns,
// The list's dense rows and header (objectstack#35742).
...(bodyTable ? { className: BODY_TABLE_DENSITY } : {}),
// A body table wider than its column keeps its first column, the
// row's identity, in view while it scrolls (objectstack#29945).
...(bodyTable && bodyTableOverflows ? { frozenColumns: 1 } : {}),
Expand Down Expand Up @@ -3433,7 +3509,7 @@ export const RelatedList: React.FC<RelatedListProps> = ({
default:
return { type: 'div', children: 'No view configured' };
}
}, [type, paginatedData, tableColumns, effectiveColumns, schema, effectivePageSize, hasRowActions, hasCustomRowActions, rowActions, onRowAction, onRowEdit, onRowDelete, handleDeleteRow, handleRowClick, isMobile, compact, enlargedFace, api, objectSchema, activeSort, handleTableSort, bodyTable, bodyTableOverflows, bodySummaryRow, ownRelationshipId, storedListPreference?.columnState]);
}, [type, paginatedData, tableColumns, effectiveColumns, schema, effectivePageSize, hasRowActions, hasCustomRowActions, rowActions, onRowAction, onRowEdit, onRowDelete, handleDeleteRow, handleRowClick, isMobile, compact, enlargedFace, api, objectSchema, activeSort, handleTableSort, bodyTable, bodyTableOverflows, bodyTableColumnsOmitted, bodyTableColumnCap, bodySummaryRow, ownRelationshipId, storedListPreference?.columnState]);

const enlargedGrid = enlargedFace && !schema && (type === 'grid' || type === 'table');

Expand Down Expand Up @@ -3568,10 +3644,36 @@ export const RelatedList: React.FC<RelatedListProps> = ({
}
let resizes: ResizeObserver | null = null;
let mounts: MutationObserver | null = null;
let cells: MutationObserver | null = null;
const findBox = () => host.querySelector<HTMLElement>('[data-table-scroll-box]');
const keys = readColumnKeys(bodyTableColumnKeysKey);
const watch = (box: HTMLElement) => {
const read = () => setBodyTableOverflows(box.scrollWidth - box.clientWidth > 1);
const read = () => {
// objectstack#35742 — the columns that fit whole: the box's width
// less the cells that are no column (the row actions), against the
// width each column asks for.
let besideColumns = 0;
for (const th of Array.from(box.querySelectorAll<HTMLElement>('thead th'))) {
const key = th.getAttribute('data-column-key');
if (key === null) besideColumns += th.getBoundingClientRect().width;
else {
const width = bodyTableColumnWidth(th, bodyTableColumnWidths.current.get(key));
if (width !== undefined) bodyTableColumnWidths.current.set(key, width);
}
}
// A box with no width has not been laid out: it leaves nothing out.
setBodyTableColumnCap(box.clientWidth > 0
? bodyTableColumnsThatFit(keys, bodyTableColumnWidths.current, box.clientWidth - besideColumns)
: null);
setBodyTableOverflows(box.scrollWidth - box.clientWidth > 1);
};
read();
// The table settles its columns a render after it is handed fewer:
// read again when its cells change.
if (typeof MutationObserver !== 'undefined') {
cells = new MutationObserver(read);
cells.observe(box, { childList: true, subtree: true });
}
if (typeof ResizeObserver === 'undefined') return;
resizes = new ResizeObserver(read);
resizes.observe(box);
Expand All @@ -3595,9 +3697,12 @@ export const RelatedList: React.FC<RelatedListProps> = ({
}
return () => {
mounts?.disconnect();
cells?.disconnect();
resizes?.disconnect();
};
}, [bodyTableDrawn, isMobile]);
// Re-read once the table draws the columns that fit: what scrolls now is
// the table with those columns.
}, [bodyTableDrawn, isMobile, bodyTableColumnKeysKey, bodyTableColumnCap]);
// When the consumer explicitly enables `filterable`, always render the
// filter input — they're opting in. (data-table's own auto-search is
// suppressed via the viewSchema below to avoid a duplicate input.)
Expand Down Expand Up @@ -4105,9 +4210,10 @@ export const RelatedList: React.FC<RelatedListProps> = ({
{add.label || t('detail.add', { defaultValue: 'Add' })}
</Button>
)}
{bodyTable && enlargeRelated && ownRelationshipId && (bodyTableOverflows ? (
{bodyTable && enlargeRelated && ownRelationshipId && (bodyTableNarrow ? (
// A table wider than its column offers ⤢ in words: the enlarged
// tab is where every column fits (objectstack#29945).
// tab is where every column fits (objectstack#29945), the ones
// the table left out included (objectstack#35742).
<Button
variant="outline"
size="sm"
Expand Down Expand Up @@ -4296,7 +4402,8 @@ export const RelatedList: React.FC<RelatedListProps> = ({
<div
ref={bodyTableHostRef}
className={cn(loading && 'opacity-60 pointer-events-none transition-opacity')}
data-body-table-overflow={bodyTable ? (bodyTableOverflows ? 'true' : 'false') : undefined}
data-body-table-overflow={bodyTable ? (bodyTableNarrow ? 'true' : 'false') : undefined}
data-body-table-columns-omitted={bodyTableColumnsOmitted ? String(tableColumns.length - bodyTableColumnCap!) : undefined}
>
<SchemaRenderer schema={viewSchema} />
</div>
Expand Down
Loading
Loading