@xeplr/ui-table 1.0.7 → 1.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -63,6 +63,7 @@ The package ships its `src/` as ES modules with JSX and imports its own styleshe
63
63
  | `headerStyle` | — | `({ columnKey, isGroupHeader, depth }) => style` |
64
64
  | `cellZoom` | `false` | double-click a cell to show it enlarged. Ignored when the schema has children shown as `'popup'`, where double-click opens the record |
65
65
  | `columnSizing` | fill | `'content'` sizes columns to their text |
66
+ | `columnWindowing` | `'auto'` | wide tables draw only the columns in view — see Column windowing. `'on'` / `'off'` force it |
66
67
  | `columnText`, `sizingSampleRows` (200), `minColumnWidth` (40), `maxColumnWidth` (420), `columnWidths`, `sizingKey`, `onColumnSizing` | | content sizing — see below |
67
68
  | `className` | — | added to the container |
68
69
 
@@ -210,6 +211,18 @@ Numeric strings compare as numbers. A host `cellStyle` prop result is merged ove
210
211
 
211
212
  Each renderer is `(config) => (ctx) => node`. The `renderers` object is the registry the table reads, so `renderers.myType = (config) => (ctx) => …` adds one.
212
213
 
214
+ ## Column windowing
215
+
216
+ Rows are paged; columns are not. So a wide table (a pivot of a date by day over a year: 1,095 columns) built every cell of the page at once, and that froze the browser. With `columnWindowing: 'auto'` (the default), a table with **more than 60 leaf columns** draws only:
217
+ - the columns in view, plus 6 on each side
218
+ - one spacer cell at each edge, holding the width of the rest
219
+
220
+ The scrollbar, the scroll position and the layout are the full table's. Scrolling sideways redraws the window, once per frame. A grouped header (a date over its measures) is cut to the part in view.
221
+
222
+ - **Widths:** the measured ones under `columnSizing: 'content'` (pinned `columnWidths` included); otherwise 140px, or `minColumnWidth` if larger. A windowed table is always fixed-layout at exactly those widths.
223
+ - **Rule:** the arithmetic is in `src/columnWindow.js` (`columnWindow`, `windowHeaders`, `shouldWindowColumns`), pure and tested on its own.
224
+ - **Limitation:** columns off screen are not in the DOM, so the browser's find-in-page does not see them.
225
+
213
226
  ## Content sizing
214
227
 
215
228
  `columnSizing="content"` measures each column's text (canvas, at the table's font) over the first `sizingSampleRows` rows of `data` — not the current page, so columns do not jump when paging.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xeplr/ui-table",
3
- "version": "1.0.7",
3
+ "version": "1.0.8",
4
4
  "description": "Controlled TanStack Table wrapper with auto-detected column types and smart filters",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -3,6 +3,7 @@ import { flexRender } from '@tanstack/react-table';
3
3
  import useTableController from './useTableController.js';
4
4
  import useActionsController from './useActionsController.js';
5
5
  import useColumnWidths from './useColumnWidths.js';
6
+ import { shouldWindowColumns, columnWindow, windowHeaders, DEFAULT_WINDOW_COLUMN_WIDTH } from './columnWindow.js';
6
7
  import CellZoom from './CellZoom.jsx';
7
8
  import { TYPES } from './detectTypes.js';
8
9
  import StringFilter from './filters/StringFilter.jsx';
@@ -68,6 +69,12 @@ var CHILD_DISPLAY = { POPUP: 'popup', INNER: 'inner' };
68
69
  * the container, as a table always has. 'content' instead sizes each column
69
70
  * to its text at the current font, and leaves any width it doesn't need
70
71
  * blank rather than stretching to fill. See columnWidths.js for the rule.
72
+ * @param {string} [props.columnWindowing] - 'auto' (default): a table
73
+ * with more than 60 columns draws only the ones in view, plus a margin, and
74
+ * holds the rest of the width in a spacer each side — a 1,000-column pivot
75
+ * costs what a 30-column one does. 'on' always, 'off' never. Widths are the
76
+ * measured ones under 'content' sizing (and `columnWidths`), otherwise 140px
77
+ * (or `minColumnWidth`, if larger). See columnWindow.js.
71
78
  * @param {Function} [props.columnText] - (row, column) => string.
72
79
  * Only used by 'content' sizing, and only worth passing if cells are
73
80
  * formatted: measuring 36260.8 when the screen shows ₹36,260.80 sizes the
@@ -228,6 +235,63 @@ export default function XeplrTable(props) {
228
235
  });
229
236
  var sizing = widths.sizing;
230
237
 
238
+ // ── Column windowing (columnWindow.js) ──
239
+ // Rows are paged; columns were all drawn, and a pivot by day over a year
240
+ // (1,095 columns × 100 rows) froze the page. Above the threshold only the
241
+ // columns in view are drawn. Hooks run unconditionally; the work is skipped
242
+ // when the table is narrow.
243
+ var leafColumns = table.getVisibleLeafColumns();
244
+ var windowing = shouldWindowColumns(leafColumns.length, props.columnWindowing);
245
+ var fallbackWidth = Math.max(DEFAULT_WINDOW_COLUMN_WIDTH, props.minColumnWidth || 0);
246
+ var leafWidths = windowing
247
+ ? leafColumns.map(function(c, i) {
248
+ var measured = sizing && sizing.widths ? sizing.widths[i] : null;
249
+ return measured > 0 ? measured : fallbackWidth;
250
+ })
251
+ : null;
252
+ var [scrollView, setScrollView] = useState({ left: 0, width: 0 });
253
+ useLayoutEffect(function() {
254
+ if (!windowing) return undefined;
255
+ var el = widths.scrollRef.current;
256
+ if (!el) return undefined;
257
+ var frame = 0;
258
+ function read() {
259
+ frame = 0;
260
+ var next = { left: el.scrollLeft, width: el.clientWidth };
261
+ setScrollView(function(prev) { return prev.left === next.left && prev.width === next.width ? prev : next; });
262
+ }
263
+ // One read per frame however fast the scroll events come.
264
+ function onScroll() { if (!frame) frame = requestAnimationFrame(read); }
265
+ read();
266
+ el.addEventListener('scroll', onScroll, { passive: true });
267
+ var observer = typeof ResizeObserver === 'undefined' ? null : new ResizeObserver(onScroll);
268
+ if (observer) observer.observe(el);
269
+ return function() {
270
+ el.removeEventListener('scroll', onScroll);
271
+ if (observer) observer.disconnect();
272
+ if (frame) cancelAnimationFrame(frame);
273
+ };
274
+ }, [windowing, widths.scrollRef]);
275
+ var extraLeading = (showExpandCol ? ((sizing && sizing.extraWidths.expand) || 36) : 0) +
276
+ (actions.hasDelete ? ((sizing && sizing.extraWidths.checkbox) || 40) : 0);
277
+ var extraTrailing = hasRowActions ? ((sizing && sizing.extraWidths.actions) || 80) : 0;
278
+ var win = windowing
279
+ ? columnWindow({ widths: leafWidths, scrollLeft: scrollView.left, viewportWidth: scrollView.width, leadingWidth: extraLeading })
280
+ : null;
281
+ var windowedTableWidth = windowing
282
+ ? extraLeading + extraTrailing + leafWidths.reduce(function(sum, w) { return sum + w; }, 0)
283
+ : 0;
284
+ // Each header row cut to the window — or the row as it is.
285
+ function headersOf(headerGroup) {
286
+ return win
287
+ ? windowHeaders(headerGroup.headers, win.start, win.end)
288
+ : headerGroup.headers.map(function(h) { return { header: h, colSpan: h.colSpan }; });
289
+ }
290
+ function cellsOf(row) {
291
+ var cells = row.getVisibleCells();
292
+ return win ? cells.slice(win.start, win.end) : cells;
293
+ }
294
+
231
295
  // PER-COLUMN CLASS, resolved by leaf index — the same index `columnWidths`
232
296
  // uses, so a host that pins column 3 highlights column 3 without a second
233
297
  // way of naming it. Header and body share one answer: a selected column is
@@ -258,7 +322,9 @@ export default function XeplrTable(props) {
258
322
  enableFiltering: enableFiltering
259
323
  };
260
324
 
261
- var totalColumns = headerGroups[0]?.headers.length || 1;
325
+ var totalColumns = win
326
+ ? (win.end - win.start) + (win.before > 0 ? 1 : 0) + (win.after > 0 ? 1 : 0) || 1
327
+ : headerGroups[0]?.headers.length || 1;
262
328
  if (showExpandCol) totalColumns++;
263
329
  if (actions.hasDelete) totalColumns++;
264
330
  if (hasRowActions) totalColumns++;
@@ -359,10 +425,22 @@ export default function XeplrTable(props) {
359
425
  table edge instead of being stretched across empty space. */}
360
426
  <table
361
427
  ref={widths.tableRef}
362
- className={'xeplr-table' + (sizing ? ' xeplr-table-sized' : '') + (props.cellStyle ? ' xeplr-table-styled' : '')}
363
- style={sizing ? { width: sizing.tableWidth + 'px' } : undefined}
428
+ className={'xeplr-table' + (sizing || win ? ' xeplr-table-sized' : '') + (win ? ' xeplr-table-windowed' : '') + (props.cellStyle ? ' xeplr-table-styled' : '')}
429
+ style={win ? { width: windowedTableWidth + 'px' } : (sizing ? { width: sizing.tableWidth + 'px' } : undefined)}
364
430
  >
365
- {sizing && (
431
+ {win ? (
432
+ <colgroup>
433
+ {showExpandCol && <col style={{ width: ((sizing && sizing.extraWidths.expand) || 36) + 'px' }} />}
434
+ {actions.hasDelete && <col style={{ width: ((sizing && sizing.extraWidths.checkbox) || 40) + 'px' }} />}
435
+ {win.before > 0 && <col style={{ width: win.before + 'px' }} />}
436
+ {leafWidths.slice(win.start, win.end).map(function(width, i) {
437
+ var leaf = leafColumns[win.start + i];
438
+ return <col key={leaf ? leaf.id : win.start + i} style={{ width: width + 'px' }} />;
439
+ })}
440
+ {win.after > 0 && <col style={{ width: win.after + 'px' }} />}
441
+ {hasRowActions && <col style={{ width: extraTrailing + 'px' }} />}
442
+ </colgroup>
443
+ ) : sizing && (
366
444
  <colgroup>
367
445
  {showExpandCol && <col style={{ width: (sizing.extraWidths.expand || 36) + 'px' }} />}
368
446
  {actions.hasDelete && <col style={{ width: (sizing.extraWidths.checkbox || 40) + 'px' }} />}
@@ -391,7 +469,9 @@ export default function XeplrTable(props) {
391
469
  onChange={function() { actions.toggleSelectAll(visibleIds); }} />
392
470
  </th>
393
471
  )}
394
- {headerGroup.headers.map(function(header) {
472
+ {win && win.before > 0 && <th className="xeplr-table-th xeplr-table-col-spacer" aria-hidden="true" style={{ top: stickyTop }} />}
473
+ {headersOf(headerGroup).map(function(item) {
474
+ var header = item.header;
395
475
  var canSort = header.column.getCanSort();
396
476
  var sorted = header.column.getIsSorted();
397
477
  var dataType = header.column.columnDef.meta?.dataType || TYPES.STRING;
@@ -430,7 +510,7 @@ export default function XeplrTable(props) {
430
510
  // before. It only widens for a GROUP header (a pivoted
431
511
  // date spanning the measures under it), which is what
432
512
  // TanStack computed it for.
433
- <th key={header.id} colSpan={header.colSpan} className={thClass}
513
+ <th key={header.id} colSpan={item.colSpan} className={thClass}
434
514
  // The host's header style sits UNDER `top`, so a
435
515
  // sticky header stays stuck whatever it is styled with.
436
516
  style={Object.assign(
@@ -462,6 +542,7 @@ export default function XeplrTable(props) {
462
542
  </th>
463
543
  );
464
544
  })}
545
+ {win && win.after > 0 && <th className="xeplr-table-th xeplr-table-col-spacer" aria-hidden="true" style={{ top: stickyTop }} />}
465
546
  {hasRowActions && (
466
547
  <th className="xeplr-table-th xeplr-table-th-actions" style={{ top: stickyTop }}>
467
548
  <div className="xeplr-table-header-cell">
@@ -518,7 +599,8 @@ export default function XeplrTable(props) {
518
599
  onChange={function() { actions.toggleSelect(rowId); }} />
519
600
  </td>
520
601
  )}
521
- {row.getVisibleCells().map(function(cell) {
602
+ {win && win.before > 0 && <td className="xeplr-table-td xeplr-table-col-spacer" aria-hidden="true" />}
603
+ {cellsOf(row).map(function(cell) {
522
604
  var meta = cell.column.columnDef.meta;
523
605
  var cellStyleDef = meta?.cellStyle;
524
606
  var condStyle = cellStyleDef
@@ -568,6 +650,7 @@ export default function XeplrTable(props) {
568
650
  </td>
569
651
  );
570
652
  })}
653
+ {win && win.after > 0 && <td className="xeplr-table-td xeplr-table-col-spacer" aria-hidden="true" />}
571
654
  {hasRowActions && (
572
655
  <td className="xeplr-table-td xeplr-table-td-actions">
573
656
  <ActionsCell row={original} hasSave={actions.hasSave} hasDelete={actions.hasDelete}
@@ -0,0 +1,97 @@
1
+ // COLUMN WINDOWING — which columns of a wide table are worth drawing.
2
+ //
3
+ // Rows are already bounded: the table pages them. Columns were not: a pivot
4
+ // of a date by day over a year is 1,095 columns, and 100 rows of it is over a
5
+ // hundred thousand cells, built all at once, which froze the page. Only the
6
+ // columns in view (plus a margin) are drawn; one spacer cell on each side
7
+ // holds the width of everything left out, so the scrollbar, the scroll
8
+ // position and the layout are exactly what the full table would have.
9
+ //
10
+ // Pure functions — no React, no DOM — so the arithmetic is tested on its own.
11
+
12
+ /** Above this many leaf columns windowing switches on (with `columnWindowing: 'auto'`). */
13
+ export var COLUMN_WINDOW_THRESHOLD = 60;
14
+
15
+ /** Width of a column the table has not measured (no content sizing). */
16
+ export var DEFAULT_WINDOW_COLUMN_WIDTH = 140;
17
+
18
+ /** Columns drawn beyond each edge of the view, so a scroll never shows a blank. */
19
+ export var COLUMN_WINDOW_OVERSCAN = 6;
20
+
21
+ /**
22
+ * Whether a table with `leafCount` columns draws only its visible ones.
23
+ * @param {number} leafCount
24
+ * @param {'auto'|'on'|'off'} [mode='auto']
25
+ */
26
+ export function shouldWindowColumns(leafCount, mode) {
27
+ if (mode === 'off') return false;
28
+ if (mode === 'on') return leafCount > 0;
29
+ return leafCount > COLUMN_WINDOW_THRESHOLD;
30
+ }
31
+
32
+ /**
33
+ * The run of columns to draw.
34
+ *
35
+ * @param {object} o
36
+ * @param {number[]} o.widths - every leaf column's width, in order (px)
37
+ * @param {number} o.scrollLeft - the scroll port's scrollLeft
38
+ * @param {number} o.viewportWidth - the scroll port's clientWidth
39
+ * @param {number} [o.leadingWidth=0] - fixed columns before the data ones (expand, checkbox)
40
+ * @param {number} [o.overscan] - columns past each edge
41
+ * @returns {{ start: number, end: number, before: number, after: number }}
42
+ * `end` is exclusive; `before`/`after` are the widths the spacers hold.
43
+ */
44
+ export function columnWindow(o) {
45
+ var widths = o.widths || [];
46
+ var n = widths.length;
47
+ if (!n) return { start: 0, end: 0, before: 0, after: 0 };
48
+ var overscan = o.overscan === undefined ? COLUMN_WINDOW_OVERSCAN : o.overscan;
49
+ // Before the first measure there is no viewport yet: draw a screenful at
50
+ // the left rather than nothing.
51
+ var viewport = o.viewportWidth > 0 ? o.viewportWidth : 1600;
52
+ var left = Math.max(0, (o.scrollLeft || 0) - (o.leadingWidth || 0));
53
+ var right = left + viewport;
54
+
55
+ var x = 0;
56
+ var first = n - 1;
57
+ var last = n - 1;
58
+ var foundFirst = false;
59
+ for (var i = 0; i < n; i++) {
60
+ var w = widths[i] || 0;
61
+ if (!foundFirst && x + w > left) { first = i; foundFirst = true; }
62
+ if (x >= right) { last = i - 1; break; }
63
+ x += w;
64
+ }
65
+ if (!foundFirst) first = n - 1;
66
+ if (last < first) last = first;
67
+
68
+ var start = Math.max(0, first - overscan);
69
+ var end = Math.min(n, last + 1 + overscan);
70
+ var before = 0;
71
+ var after = 0;
72
+ for (var b = 0; b < start; b++) before += widths[b] || 0;
73
+ for (var a = end; a < n; a++) after += widths[a] || 0;
74
+ return { start: start, end: end, before: before, after: after };
75
+ }
76
+
77
+ /**
78
+ * A header row cut to the window: each header that reaches into [start, end)
79
+ * with its colSpan reduced to the part inside. Headers of one row cover the
80
+ * leaf columns contiguously, in order (TanStack pads every row to full width),
81
+ * which is what lets the leaf range of each be found by adding up spans.
82
+ *
83
+ * @param {Array<{ colSpan: number }>} headers
84
+ * @returns {Array<{ header: object, colSpan: number }>}
85
+ */
86
+ export function windowHeaders(headers, start, end) {
87
+ var out = [];
88
+ var at = 0;
89
+ for (var i = 0; i < headers.length; i++) {
90
+ var span = headers[i].colSpan || 1;
91
+ var from = Math.max(at, start);
92
+ var to = Math.min(at + span, end);
93
+ if (to > from) out.push({ header: headers[i], colSpan: to - from });
94
+ at += span;
95
+ }
96
+ return out;
97
+ }
@@ -1057,3 +1057,14 @@
1057
1057
  cursor: pointer;
1058
1058
  user-select: none;
1059
1059
  }
1060
+
1061
+ /* ── Column windowing (columnWindow.js) ──
1062
+ The spacers hold the width of the columns not drawn, so the scrollbar and
1063
+ the layout are the full table's. They must hold width and nothing else. */
1064
+ .xeplr-table-col-spacer,
1065
+ .xeplr-table-th.xeplr-table-col-spacer,
1066
+ .xeplr-table-td.xeplr-table-col-spacer {
1067
+ padding: 0;
1068
+ border-left: none;
1069
+ border-right: none;
1070
+ }