Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | 124x 89x 67x 124x 69x 124x 89x 25x 89x 99x 100x 99x 76x 99x 108x 76x 124x 91x 9x 9x 124x 124x 29x 58x 58x 29x 124x 124x 124x 92x 1x 1x 1x 1x 1x 1x 1x 124x 1x 1x | /**
* Which rows a table shows, and in what order.
*
* Three sources meet here and only one of them applies: a detail table holds its rows
* in the form value, a list table receives the page the server sent, and a pivot holds
* a fixed set of slots that the stored rows are overlaid on. What follows is the same
* in every mode — the parent's cascade, the column filters and the view sort — and it
* stays on the client so a filter can hide a row without the form value losing it.
*/
import {useMemo, useState} from 'react';
import {KEY, type Row} from './model.js';
export interface ITableRowsOptions {
/** The form value, for a detail table. */
value: unknown;
/** The page the server sent, for a list table. */
listRows: Row[];
isListMode: boolean;
pivotCfg?: {
examples?: Row[];
dropdown?: string;
join?: Record<string, string>;
defaults?: Row;
};
/** The slots a pivot overlays stored rows onto, when they are known. */
pivotBaseRows?: Row[];
/** The field a cascaded table is narrowed by, and the keys it maps to the parent. */
parentFieldName?: string;
masterMapping?: Record<string, string>;
parentSelection?: {row: Record<string, unknown>; index: number} | null;
/** The columns that carry a filter, and the ones whose filter compares a key. */
filterFields: string[];
pickFields: Set<string>;
/** What the filter inputs currently hold. */
committedFilters: Record<string, string>;
}
export function useTableRows({
value,
listRows,
isListMode,
pivotCfg,
pivotBaseRows,
parentFieldName,
masterMapping,
parentSelection,
filterFields,
pickFields,
committedFilters,
}: ITableRowsOptions) {
const rows: Row[] = useMemo(
() =>
isListMode
? listRows
: (Array.isArray(value) ? (value as Row[]) : []).map((row, i) => ({
...row,
[KEY]: (row as Row)[KEY] ?? i,
})),
[isListMode, listRows, value],
);
const pivotJoinDataFields = useMemo(
() => new Set<string>(Object.values(pivotCfg?.join ?? {})),
[pivotCfg],
);
// A pivot's slot for a stored row is found by the `join` fields, and a slot with
// no stored row still shows — seeded with `defaults` and the join values, so the
// reader can fill it in.
const baseRows: Row[] = useMemo(() => {
if (!pivotBaseRows) return rows;
const joinEntries = Object.entries(pivotCfg?.join ?? {});
return pivotBaseRows.map((pivotRow, i) => {
const found = rows.find(row =>
joinEntries.every(([pivotKey, rowKey]) => pivotRow[pivotKey] === row[rowKey]),
);
if (found) return {...pivotCfg?.defaults, ...found, [KEY]: `pivot-${i}`};
const seeded: Row = {...pivotCfg?.defaults, [KEY]: `pivot-${i}`};
for (const [pivotKey, rowKey] of joinEntries) {
seeded[rowKey] = pivotRow[pivotKey];
}
return seeded;
});
}, [pivotBaseRows, pivotCfg, rows]);
// The cascade is client-side here; a list table sends the same mapping to the
// server instead, so it never reaches this filter.
const filteredRows = useMemo(
() =>
!isListMode && parentFieldName && masterMapping
? parentSelection
? baseRows.filter(row =>
Object.entries(masterMapping).every(
([ownKey, parentKey]) =>
row[ownKey] === parentSelection.row[parentKey],
),
)
: []
: baseRows,
[isListMode, parentFieldName, masterMapping, parentSelection, baseRows],
);
// Display-only: a filter hides a row, it never removes it from the form value.
const displayRows = useMemo(() => {
if (isListMode || filterFields.length === 0) return filteredRows;
const active = filterFields
.map(field => [field, committedFilters[field] ?? ''] as const)
.filter(([, text]) => text !== '');
Eif (active.length === 0) return filteredRows;
return filteredRows.filter(row =>
active.every(([field, text]) =>
pickFields.has(field)
? String(row[field] ?? '') === text
: String(row[field] ?? '')
.toLocaleLowerCase()
.includes(text.toLocaleLowerCase()),
),
);
}, [isListMode, filteredRows, filterFields, pickFields, committedFilters]);
/**
* Sorting a form table reorders the view, not the value: a detail list keeps the
* order the record was saved in, and the row key still identifies the row, so a
* cell edit lands on the right one. The list mode sorts through the server.
* @@D-615
*/
const [sortField, setSortField] = useState<string | null>(null);
const [sortOrder, setSortOrder] = useState<1 | -1>(1);
const sortedRows = useMemo(() => {
if (isListMode || !sortField) return displayRows;
const direction = sortOrder;
return [...displayRows].sort((left, right) => {
const a = left[sortField];
const b = right[sortField];
// Empties last in either direction: a missing value says nothing about
// its order among the values that exist.
Iif (a == null || a === '') return b == null || b === '' ? 0 : 1;
Iif (b == null || b === '') return -1;
Eif (typeof a === 'number' && typeof b === 'number') return (a - b) * direction;
return String(a).localeCompare(String(b)) * direction;
});
}, [isListMode, displayRows, sortField, sortOrder]);
return {
/** The rows as the form holds them, each carrying the key an edit lands on. */
rows,
/** The rows the reader can see, before the view sort. */
visibleRows: displayRows,
/** What the table renders: narrowed by the cascade and the filters, then sorted. */
sortedRows,
sortField,
sortOrder,
/** Change the view's order; the stored order is untouched. */
onSortChange: (field: string | null, order: 1 | -1) => {
setSortField(field);
setSortOrder(order);
},
/** The pivot's data fields, which a cell must not edit directly. */
pivotJoinDataFields,
};
}
|