@pretable/core 0.0.6 → 0.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/dist/index.cjs CHANGED
@@ -587,6 +587,32 @@ function matchesFilters(row, resolvedFilters) {
587
587
  return true;
588
588
  }
589
589
 
590
+ // ../grid-core/dist/group-column.js
591
+ var GROUP_COLUMN_ID = "__pretable_group__";
592
+ var DEFAULT_GROUP_COLUMN_WIDTH_PX = 200;
593
+ function resolveEffectiveColumns(input) {
594
+ const { columns, rowGroups, groupColumn, hideGroupedColumns } = input;
595
+ if (rowGroups.length === 0) {
596
+ return columns;
597
+ }
598
+ const grouped = new Set(rowGroups);
599
+ const kept = hideGroupedColumns === false ? columns : columns.filter((column) => !grouped.has(column.id));
600
+ return [makeGroupColumn(columns, rowGroups, groupColumn), ...kept];
601
+ }
602
+ function makeGroupColumn(columns, rowGroups, groupColumn) {
603
+ const firstLevel = columns.find((column2) => column2.id === rowGroups[0]);
604
+ const column = {
605
+ id: GROUP_COLUMN_ID,
606
+ header: groupColumn?.header ?? firstLevel?.header ?? "",
607
+ widthPx: groupColumn?.widthPx ?? DEFAULT_GROUP_COLUMN_WIDTH_PX,
608
+ // The group column shows a tree, not a value: sorting or filtering it has
609
+ // no meaning, and both would index into a column that has no `value`.
610
+ sortable: false,
611
+ filterable: false
612
+ };
613
+ return groupColumn?.pinned === "left" ? { ...column, pinned: "left" } : column;
614
+ }
615
+
590
616
  // ../grid-core/dist/group-expansion.js
591
617
  var DEFAULT_GROUP_EXPANSION_OVERRIDE_LIMIT = 1e4;
592
618
  function resolveGroupExpansionOverrideLimit(configured) {
@@ -615,31 +641,6 @@ var ROW_SELECT_COLUMN_ID = "__pretable_row_select__";
615
641
  function isDataRow(entry) {
616
642
  return entry.kind === "data";
617
643
  }
618
- function scanDataRows(rows, rowId) {
619
- let count2 = 0;
620
- let index = -1;
621
- for (const entry of rows) {
622
- if (!isDataRow(entry))
623
- continue;
624
- if (rowId !== null && entry.id === rowId)
625
- index = count2;
626
- count2 += 1;
627
- }
628
- return { count: count2, index };
629
- }
630
- function dataRowAt(rows, ordinal) {
631
- if (ordinal < 0)
632
- return void 0;
633
- let seen = 0;
634
- for (const entry of rows) {
635
- if (!isDataRow(entry))
636
- continue;
637
- if (seen === ordinal)
638
- return entry;
639
- seen += 1;
640
- }
641
- return void 0;
642
- }
643
644
  function clampColumnWidth(width, column) {
644
645
  const min2 = column.minWidthPx ?? 40;
645
646
  const max2 = column.maxWidthPx ?? Infinity;
@@ -669,7 +670,7 @@ function groupColumnsByPin(columns) {
669
670
  const right = [];
670
671
  for (const column of columns) {
671
672
  const region = column.pinned === "left" ? left : column.pinned === "right" ? right : unpinned;
672
- if (column.id === ROW_SELECT_COLUMN_ID) {
673
+ if (column.id === ROW_SELECT_COLUMN_ID || column.id === GROUP_COLUMN_ID) {
673
674
  region.unshift(column);
674
675
  } else {
675
676
  region.push(column);
@@ -696,6 +697,9 @@ function createGridCore(inputOptions) {
696
697
  let cachedDerivedOverrides = null;
697
698
  let cachedDerivedDefaultExpanded = null;
698
699
  let cachedDerivedAggregateFiltered = null;
700
+ let cachedEffectiveColumns = null;
701
+ let cachedEffectiveColumnsSource = null;
702
+ let cachedEffectiveColumnsRowGroups = null;
699
703
  let sort = [];
700
704
  let filters = {};
701
705
  let rowGroups = sanitizeRowGroups(options.columns.filter((column) => column.rowGroup === true).map((c) => c.id), options.columns);
@@ -723,6 +727,7 @@ function createGridCore(inputOptions) {
723
727
  };
724
728
  },
725
729
  getSnapshot,
730
+ getColumns,
726
731
  setSort(columnId, direction) {
727
732
  const candidate = columnId && direction ? [{ columnId, direction }] : [];
728
733
  const next = sanitizeSortEntries(candidate, options.columns);
@@ -937,13 +942,15 @@ function createGridCore(inputOptions) {
937
942
  },
938
943
  moveFocus(direction, moveOptions = {}) {
939
944
  const snapshot = getSnapshot();
940
- const { count: dataRowCount, index: currentRowIndex } = scanDataRows(snapshot.visibleRows, focus.rowId);
941
- const columnList = options.columns;
942
- if (dataRowCount === 0 || columnList.length === 0) {
945
+ const rowList = snapshot.visibleRows;
946
+ const rowCount = rowList.length;
947
+ const columnList = getColumns();
948
+ if (rowCount === 0 || columnList.length === 0) {
943
949
  focus = { rowId: null, columnId: null };
944
950
  emit();
945
951
  return;
946
952
  }
953
+ const currentRowIndex = focus.rowId === null ? -1 : rowList.findIndex((entry) => entry.id === focus.rowId);
947
954
  const currentColumnIndex = focus.columnId ? columnList.findIndex((c) => c.id === focus.columnId) : -1;
948
955
  const hasRowFocus = currentRowIndex !== -1;
949
956
  const hasColumnFocus = currentColumnIndex !== -1;
@@ -951,28 +958,28 @@ function createGridCore(inputOptions) {
951
958
  const baseColumnIndex = hasColumnFocus ? currentColumnIndex : 0;
952
959
  let nextRowIndex = baseRowIndex;
953
960
  let nextColumnIndex = baseColumnIndex;
954
- const pageStep = computePageStep(viewport, dataRowCount);
961
+ const pageStep = computePageStep(viewport, rowCount);
955
962
  switch (direction) {
956
963
  case "up":
957
964
  if (moveOptions.jumpToEdge) {
958
965
  nextRowIndex = 0;
959
966
  } else if (!hasRowFocus) {
960
- nextRowIndex = dataRowCount - 1;
967
+ nextRowIndex = rowCount - 1;
961
968
  } else if (moveOptions.byPage) {
962
- nextRowIndex = clamp(baseRowIndex - pageStep, 0, dataRowCount - 1);
969
+ nextRowIndex = clamp(baseRowIndex - pageStep, 0, rowCount - 1);
963
970
  } else {
964
- nextRowIndex = clamp(baseRowIndex - 1, 0, dataRowCount - 1);
971
+ nextRowIndex = clamp(baseRowIndex - 1, 0, rowCount - 1);
965
972
  }
966
973
  break;
967
974
  case "down":
968
975
  if (moveOptions.jumpToEdge) {
969
- nextRowIndex = dataRowCount - 1;
976
+ nextRowIndex = rowCount - 1;
970
977
  } else if (!hasRowFocus) {
971
978
  nextRowIndex = 0;
972
979
  } else if (moveOptions.byPage) {
973
- nextRowIndex = clamp(baseRowIndex + pageStep, 0, dataRowCount - 1);
980
+ nextRowIndex = clamp(baseRowIndex + pageStep, 0, rowCount - 1);
974
981
  } else {
975
- nextRowIndex = clamp(baseRowIndex + 1, 0, dataRowCount - 1);
982
+ nextRowIndex = clamp(baseRowIndex + 1, 0, rowCount - 1);
976
983
  }
977
984
  break;
978
985
  case "left":
@@ -994,7 +1001,7 @@ function createGridCore(inputOptions) {
994
1001
  }
995
1002
  break;
996
1003
  }
997
- const nextRow = dataRowAt(snapshot.visibleRows, nextRowIndex);
1004
+ const nextRow = rowList[nextRowIndex];
998
1005
  const nextColumn = columnList[nextColumnIndex];
999
1006
  if (!nextRow || !nextColumn) {
1000
1007
  return;
@@ -1381,6 +1388,7 @@ function createGridCore(inputOptions) {
1381
1388
  if (isGroupExpanded(groupId) === expanded) {
1382
1389
  return;
1383
1390
  }
1391
+ const before = getSnapshot().visibleRows;
1384
1392
  if (expanded === groupsDefaultExpanded) {
1385
1393
  const next = new Set(groupExpansionOverrides);
1386
1394
  next.delete(groupId);
@@ -1388,6 +1396,7 @@ function createGridCore(inputOptions) {
1388
1396
  } else {
1389
1397
  groupExpansionOverrides = addGroupExpansionOverride(groupExpansionOverrides, groupId, groupExpansionOverrideLimit);
1390
1398
  }
1399
+ reanchorFocusAfterCollapse(before);
1391
1400
  emit();
1392
1401
  },
1393
1402
  expandAll() {
@@ -1462,10 +1471,34 @@ function createGridCore(inputOptions) {
1462
1471
  if (groupsDefaultExpanded === expanded && groupExpansionOverrides.size === 0) {
1463
1472
  return;
1464
1473
  }
1474
+ const before = getSnapshot().visibleRows;
1465
1475
  groupsDefaultExpanded = expanded;
1466
1476
  groupExpansionOverrides = /* @__PURE__ */ new Set();
1477
+ reanchorFocusAfterCollapse(before);
1467
1478
  emit();
1468
1479
  }
1480
+ function reanchorFocusAfterCollapse(before) {
1481
+ if (focus.rowId === null)
1482
+ return;
1483
+ const fromIndex = before.findIndex((entry) => entry.id === focus.rowId);
1484
+ if (fromIndex === -1)
1485
+ return;
1486
+ cachedSnapshot = null;
1487
+ const after = getSnapshot().visibleRows;
1488
+ if (after.some((entry) => entry.id === focus.rowId))
1489
+ return;
1490
+ const surviving = new Set(after.map((entry) => entry.id));
1491
+ let nextRowId = null;
1492
+ for (let i = fromIndex - 1; i >= 0; i -= 1) {
1493
+ const candidate = before[i];
1494
+ if (candidate.kind === "group" && surviving.has(candidate.id)) {
1495
+ nextRowId = candidate.id;
1496
+ break;
1497
+ }
1498
+ }
1499
+ focus = nextRowId === null ? { rowId: null, columnId: null } : { rowId: nextRowId, columnId: focus.columnId };
1500
+ cachedSnapshot = null;
1501
+ }
1469
1502
  function preserveAggregateIdentity(rows) {
1470
1503
  const next = /* @__PURE__ */ new Map();
1471
1504
  let result = rows;
@@ -1486,6 +1519,22 @@ function createGridCore(inputOptions) {
1486
1519
  previousAggregates = next;
1487
1520
  return result;
1488
1521
  }
1522
+ function getColumns() {
1523
+ if (cachedEffectiveColumns !== null && cachedEffectiveColumnsSource === options.columns && cachedEffectiveColumnsRowGroups === rowGroups) {
1524
+ return cachedEffectiveColumns;
1525
+ }
1526
+ const resolved = resolveEffectiveColumns({
1527
+ columns: options.columns,
1528
+ rowGroups,
1529
+ groupColumn: options.groupColumn,
1530
+ hideGroupedColumns: options.hideGroupedColumns
1531
+ });
1532
+ const effective = resolved === options.columns ? resolved : groupColumnsByPin(resolved);
1533
+ cachedEffectiveColumns = effective;
1534
+ cachedEffectiveColumnsSource = options.columns;
1535
+ cachedEffectiveColumnsRowGroups = rowGroups;
1536
+ return effective;
1537
+ }
1489
1538
  function getSnapshot() {
1490
1539
  if (cachedSnapshot) {
1491
1540
  return cachedSnapshot;
@@ -1550,12 +1599,12 @@ function sameColumnLayout(a, b) {
1550
1599
  return left.id === right.id && left.header === right.header && left.widthPx === right.widthPx && left.minWidthPx === right.minWidthPx && left.maxWidthPx === right.maxWidthPx && left.pinned === right.pinned && left.sortable === right.sortable && left.filterable === right.filterable && left.resizable === right.resizable && left.reorderable === right.reorderable && left.wrap === right.wrap;
1551
1600
  });
1552
1601
  }
1553
- function computePageStep(viewport, dataRowCount) {
1554
- if (viewport.height <= 0 || dataRowCount === 0) {
1602
+ function computePageStep(viewport, rowCount) {
1603
+ if (viewport.height <= 0 || rowCount === 0) {
1555
1604
  return 1;
1556
1605
  }
1557
1606
  const estimatedRowsPerPage = Math.max(1, Math.floor(viewport.height * 0.8 / 32));
1558
- return Math.min(estimatedRowsPerPage, dataRowCount);
1607
+ return Math.min(estimatedRowsPerPage, rowCount);
1559
1608
  }
1560
1609
  function isFullRowRange(range, rowId, firstColumnId, lastColumnId) {
1561
1610
  return range.startRowId === rowId && range.endRowId === rowId && range.startColumnId === firstColumnId && range.endColumnId === lastColumnId;
@@ -1657,6 +1706,7 @@ function createGrid(options) {
1657
1706
  },
1658
1707
  subscribe: engine.subscribe,
1659
1708
  getSnapshot: engine.getSnapshot,
1709
+ getColumns: engine.getColumns,
1660
1710
  setSort: engine.setSort,
1661
1711
  replaceSort: engine.replaceSort,
1662
1712
  setColumnFilter: engine.setColumnFilter,
@@ -1700,4 +1750,5 @@ function createGrid(options) {
1700
1750
  };
1701
1751
  }
1702
1752
 
1753
+ exports.GROUP_COLUMN_ID = GROUP_COLUMN_ID;
1703
1754
  exports.createGrid = createGrid;
package/dist/index.d.cts CHANGED
@@ -20,6 +20,37 @@ interface AutosizeOptions {
20
20
  cellPaddingPx?: number;
21
21
  }
22
22
 
23
+ /**
24
+ * Id of the synthetic column that carries the group label, twisty and child
25
+ * count for every grouping level.
26
+ *
27
+ * It is *derived*, never stored: `options.columns` remains the consumer's
28
+ * truth, and {@link resolveEffectiveColumns} rebuilds this column on read
29
+ * whenever `rowGroups` is non-empty. The alternative — pushing it into
30
+ * `options.columns` — cannot work, because `mergeColumnsFromProps` rebuilds
31
+ * that array by mapping over the consumer's own array and would delete any
32
+ * column props never mentioned.
33
+ *
34
+ * @public
35
+ */
36
+ declare const GROUP_COLUMN_ID = "__pretable_group__";
37
+ /**
38
+ * Configuration for the derived group column.
39
+ *
40
+ * @public
41
+ */
42
+ interface PretableGroupColumnOptions {
43
+ /** Header text. Defaults to the header of the first grouped column. */
44
+ header?: string;
45
+ /** Column width. Defaults to 200. */
46
+ widthPx?: number;
47
+ /**
48
+ * Pin the group column to the left. Unpinned by default — pinning it would
49
+ * silently consume the pinned-left region a consumer may already be using.
50
+ */
51
+ pinned?: "left";
52
+ }
53
+
23
54
  /**
24
55
  * Base row constraint — every row is at minimum a string-keyed record.
25
56
  *
@@ -100,6 +131,15 @@ interface PretableColumn<TRow extends PretableRow = PretableRow> {
100
131
  options?: ColumnOption[];
101
132
  value?: (row: TRow) => unknown;
102
133
  format?: (input: PretableFormatInput<TRow>) => string;
134
+ /**
135
+ * Render this column's aggregate on a group row.
136
+ *
137
+ * Deliberately not `format`: `PretableFormatInput.row` is non-optional, so
138
+ * every consumer formatter is entitled to dereference it, and a group row has
139
+ * no row. Columns without `formatAggregate` fall back to the same default
140
+ * stringification a plain cell uses.
141
+ */
142
+ formatAggregate?: (input: PretableAggregateFormatInput<TRow>) => string;
103
143
  minWidthPx?: number;
104
144
  maxWidthPx?: number;
105
145
  /**
@@ -132,6 +172,17 @@ interface PretableFormatInput<TRow extends PretableRow = PretableRow> {
132
172
  row: TRow;
133
173
  column: PretableColumn<TRow>;
134
174
  }
175
+ /**
176
+ * Input passed to a column's `formatAggregate` function. Carries the group row
177
+ * in place of a data row — an aggregate has no row behind it.
178
+ *
179
+ * @public
180
+ */
181
+ interface PretableAggregateFormatInput<TRow extends PretableRow = PretableRow> {
182
+ value: unknown;
183
+ column: PretableColumn<TRow>;
184
+ group: PretableGroupRow;
185
+ }
135
186
  /**
136
187
  * Options accepted by `createGrid`.
137
188
  *
@@ -172,6 +223,18 @@ interface PretableGridOptions<TRow extends PretableRow = PretableRow> {
172
223
  * matter how many groups exist.
173
224
  */
174
225
  groupExpansionOverrideLimit?: number;
226
+ /**
227
+ * Configure the derived group column — the synthetic column that carries the
228
+ * label, twisty and child count for every grouping level. Present in
229
+ * `getColumns()` exactly while `rowGroups` is non-empty.
230
+ */
231
+ groupColumn?: PretableGroupColumnOptions;
232
+ /**
233
+ * Drop the grouped columns from the data area while grouping is active.
234
+ * Default `true`, uniformly — whether the grouping came from `rowGroup: true`
235
+ * on a column or from a later `setRowGroups` call.
236
+ */
237
+ hideGroupedColumns?: boolean;
175
238
  }
176
239
  /**
177
240
  * One entry in the ordered sort list; index in the list = priority.
@@ -376,6 +439,13 @@ interface PretableGrid<TRow extends PretableRow = PretableRow> {
376
439
  subscribe(listener: () => void): () => void;
377
440
  /** Read the current snapshot. Stable reference until the next mutation. */
378
441
  getSnapshot(): PretableGridSnapshot<TRow>;
442
+ /**
443
+ * The columns to draw: `options.columns` plus the derived group column, minus
444
+ * the grouped columns, while grouping is active. Identical to
445
+ * `options.columns` — by identity — when ungrouped. Stable reference until
446
+ * the columns or the grouping levels change.
447
+ */
448
+ getColumns(): readonly PretableColumn<TRow>[];
379
449
  setSort(columnId: string | null, direction: PretableSortDirection): void;
380
450
  /** Atomically replace the ordered sort list. Unknown and `sortable: false` columns are dropped. */
381
451
  replaceSort(entries: PretableSortEntry[]): void;
@@ -463,4 +533,4 @@ interface PretableGrid<TRow extends PretableRow = PretableRow> {
463
533
  */
464
534
  declare function createGrid<TRow extends PretableRow = PretableRow>(options: PretableGridOptions<TRow>): PretableGrid<TRow>;
465
535
 
466
- export { type AutosizeOptions, type ColumnFilter, type ColumnOption, type ColumnType, type FilterOperator, type FilterValue, type PretableAggregateSpec, type PretableAggregator, type PretableCellAddress, type PretableCellRange, type PretableColumn, type PretableDataRow, type PretableEditInput, type PretableEditState, type PretableEditStatus, type PretableFocusDirection, type PretableFocusState, type PretableFormatInput, type PretableGrid, type PretableGridOptions, type PretableGridSnapshot, type PretableGroupRow, type PretableMoveFocusOptions, type PretableRow, type PretableRowRange, type PretableRowSelectionTriState, type PretableSelectionState, type PretableSortDirection, type PretableSortEntry, type PretableTransaction, type PretableViewportState, type PretableVisibleRow, createGrid };
536
+ export { type AutosizeOptions, type ColumnFilter, type ColumnOption, type ColumnType, type FilterOperator, type FilterValue, GROUP_COLUMN_ID, type PretableAggregateFormatInput, type PretableAggregateSpec, type PretableAggregator, type PretableCellAddress, type PretableCellRange, type PretableColumn, type PretableDataRow, type PretableEditInput, type PretableEditState, type PretableEditStatus, type PretableFocusDirection, type PretableFocusState, type PretableFormatInput, type PretableGrid, type PretableGridOptions, type PretableGridSnapshot, type PretableGroupColumnOptions, type PretableGroupRow, type PretableMoveFocusOptions, type PretableRow, type PretableRowRange, type PretableRowSelectionTriState, type PretableSelectionState, type PretableSortDirection, type PretableSortEntry, type PretableTransaction, type PretableViewportState, type PretableVisibleRow, createGrid };
package/dist/index.d.ts CHANGED
@@ -20,6 +20,37 @@ interface AutosizeOptions {
20
20
  cellPaddingPx?: number;
21
21
  }
22
22
 
23
+ /**
24
+ * Id of the synthetic column that carries the group label, twisty and child
25
+ * count for every grouping level.
26
+ *
27
+ * It is *derived*, never stored: `options.columns` remains the consumer's
28
+ * truth, and {@link resolveEffectiveColumns} rebuilds this column on read
29
+ * whenever `rowGroups` is non-empty. The alternative — pushing it into
30
+ * `options.columns` — cannot work, because `mergeColumnsFromProps` rebuilds
31
+ * that array by mapping over the consumer's own array and would delete any
32
+ * column props never mentioned.
33
+ *
34
+ * @public
35
+ */
36
+ declare const GROUP_COLUMN_ID = "__pretable_group__";
37
+ /**
38
+ * Configuration for the derived group column.
39
+ *
40
+ * @public
41
+ */
42
+ interface PretableGroupColumnOptions {
43
+ /** Header text. Defaults to the header of the first grouped column. */
44
+ header?: string;
45
+ /** Column width. Defaults to 200. */
46
+ widthPx?: number;
47
+ /**
48
+ * Pin the group column to the left. Unpinned by default — pinning it would
49
+ * silently consume the pinned-left region a consumer may already be using.
50
+ */
51
+ pinned?: "left";
52
+ }
53
+
23
54
  /**
24
55
  * Base row constraint — every row is at minimum a string-keyed record.
25
56
  *
@@ -100,6 +131,15 @@ interface PretableColumn<TRow extends PretableRow = PretableRow> {
100
131
  options?: ColumnOption[];
101
132
  value?: (row: TRow) => unknown;
102
133
  format?: (input: PretableFormatInput<TRow>) => string;
134
+ /**
135
+ * Render this column's aggregate on a group row.
136
+ *
137
+ * Deliberately not `format`: `PretableFormatInput.row` is non-optional, so
138
+ * every consumer formatter is entitled to dereference it, and a group row has
139
+ * no row. Columns without `formatAggregate` fall back to the same default
140
+ * stringification a plain cell uses.
141
+ */
142
+ formatAggregate?: (input: PretableAggregateFormatInput<TRow>) => string;
103
143
  minWidthPx?: number;
104
144
  maxWidthPx?: number;
105
145
  /**
@@ -132,6 +172,17 @@ interface PretableFormatInput<TRow extends PretableRow = PretableRow> {
132
172
  row: TRow;
133
173
  column: PretableColumn<TRow>;
134
174
  }
175
+ /**
176
+ * Input passed to a column's `formatAggregate` function. Carries the group row
177
+ * in place of a data row — an aggregate has no row behind it.
178
+ *
179
+ * @public
180
+ */
181
+ interface PretableAggregateFormatInput<TRow extends PretableRow = PretableRow> {
182
+ value: unknown;
183
+ column: PretableColumn<TRow>;
184
+ group: PretableGroupRow;
185
+ }
135
186
  /**
136
187
  * Options accepted by `createGrid`.
137
188
  *
@@ -172,6 +223,18 @@ interface PretableGridOptions<TRow extends PretableRow = PretableRow> {
172
223
  * matter how many groups exist.
173
224
  */
174
225
  groupExpansionOverrideLimit?: number;
226
+ /**
227
+ * Configure the derived group column — the synthetic column that carries the
228
+ * label, twisty and child count for every grouping level. Present in
229
+ * `getColumns()` exactly while `rowGroups` is non-empty.
230
+ */
231
+ groupColumn?: PretableGroupColumnOptions;
232
+ /**
233
+ * Drop the grouped columns from the data area while grouping is active.
234
+ * Default `true`, uniformly — whether the grouping came from `rowGroup: true`
235
+ * on a column or from a later `setRowGroups` call.
236
+ */
237
+ hideGroupedColumns?: boolean;
175
238
  }
176
239
  /**
177
240
  * One entry in the ordered sort list; index in the list = priority.
@@ -376,6 +439,13 @@ interface PretableGrid<TRow extends PretableRow = PretableRow> {
376
439
  subscribe(listener: () => void): () => void;
377
440
  /** Read the current snapshot. Stable reference until the next mutation. */
378
441
  getSnapshot(): PretableGridSnapshot<TRow>;
442
+ /**
443
+ * The columns to draw: `options.columns` plus the derived group column, minus
444
+ * the grouped columns, while grouping is active. Identical to
445
+ * `options.columns` — by identity — when ungrouped. Stable reference until
446
+ * the columns or the grouping levels change.
447
+ */
448
+ getColumns(): readonly PretableColumn<TRow>[];
379
449
  setSort(columnId: string | null, direction: PretableSortDirection): void;
380
450
  /** Atomically replace the ordered sort list. Unknown and `sortable: false` columns are dropped. */
381
451
  replaceSort(entries: PretableSortEntry[]): void;
@@ -463,4 +533,4 @@ interface PretableGrid<TRow extends PretableRow = PretableRow> {
463
533
  */
464
534
  declare function createGrid<TRow extends PretableRow = PretableRow>(options: PretableGridOptions<TRow>): PretableGrid<TRow>;
465
535
 
466
- export { type AutosizeOptions, type ColumnFilter, type ColumnOption, type ColumnType, type FilterOperator, type FilterValue, type PretableAggregateSpec, type PretableAggregator, type PretableCellAddress, type PretableCellRange, type PretableColumn, type PretableDataRow, type PretableEditInput, type PretableEditState, type PretableEditStatus, type PretableFocusDirection, type PretableFocusState, type PretableFormatInput, type PretableGrid, type PretableGridOptions, type PretableGridSnapshot, type PretableGroupRow, type PretableMoveFocusOptions, type PretableRow, type PretableRowRange, type PretableRowSelectionTriState, type PretableSelectionState, type PretableSortDirection, type PretableSortEntry, type PretableTransaction, type PretableViewportState, type PretableVisibleRow, createGrid };
536
+ export { type AutosizeOptions, type ColumnFilter, type ColumnOption, type ColumnType, type FilterOperator, type FilterValue, GROUP_COLUMN_ID, type PretableAggregateFormatInput, type PretableAggregateSpec, type PretableAggregator, type PretableCellAddress, type PretableCellRange, type PretableColumn, type PretableDataRow, type PretableEditInput, type PretableEditState, type PretableEditStatus, type PretableFocusDirection, type PretableFocusState, type PretableFormatInput, type PretableGrid, type PretableGridOptions, type PretableGridSnapshot, type PretableGroupColumnOptions, type PretableGroupRow, type PretableMoveFocusOptions, type PretableRow, type PretableRowRange, type PretableRowSelectionTriState, type PretableSelectionState, type PretableSortDirection, type PretableSortEntry, type PretableTransaction, type PretableViewportState, type PretableVisibleRow, createGrid };
package/dist/index.mjs CHANGED
@@ -585,6 +585,32 @@ function matchesFilters(row, resolvedFilters) {
585
585
  return true;
586
586
  }
587
587
 
588
+ // ../grid-core/dist/group-column.js
589
+ var GROUP_COLUMN_ID = "__pretable_group__";
590
+ var DEFAULT_GROUP_COLUMN_WIDTH_PX = 200;
591
+ function resolveEffectiveColumns(input) {
592
+ const { columns, rowGroups, groupColumn, hideGroupedColumns } = input;
593
+ if (rowGroups.length === 0) {
594
+ return columns;
595
+ }
596
+ const grouped = new Set(rowGroups);
597
+ const kept = hideGroupedColumns === false ? columns : columns.filter((column) => !grouped.has(column.id));
598
+ return [makeGroupColumn(columns, rowGroups, groupColumn), ...kept];
599
+ }
600
+ function makeGroupColumn(columns, rowGroups, groupColumn) {
601
+ const firstLevel = columns.find((column2) => column2.id === rowGroups[0]);
602
+ const column = {
603
+ id: GROUP_COLUMN_ID,
604
+ header: groupColumn?.header ?? firstLevel?.header ?? "",
605
+ widthPx: groupColumn?.widthPx ?? DEFAULT_GROUP_COLUMN_WIDTH_PX,
606
+ // The group column shows a tree, not a value: sorting or filtering it has
607
+ // no meaning, and both would index into a column that has no `value`.
608
+ sortable: false,
609
+ filterable: false
610
+ };
611
+ return groupColumn?.pinned === "left" ? { ...column, pinned: "left" } : column;
612
+ }
613
+
588
614
  // ../grid-core/dist/group-expansion.js
589
615
  var DEFAULT_GROUP_EXPANSION_OVERRIDE_LIMIT = 1e4;
590
616
  function resolveGroupExpansionOverrideLimit(configured) {
@@ -613,31 +639,6 @@ var ROW_SELECT_COLUMN_ID = "__pretable_row_select__";
613
639
  function isDataRow(entry) {
614
640
  return entry.kind === "data";
615
641
  }
616
- function scanDataRows(rows, rowId) {
617
- let count2 = 0;
618
- let index = -1;
619
- for (const entry of rows) {
620
- if (!isDataRow(entry))
621
- continue;
622
- if (rowId !== null && entry.id === rowId)
623
- index = count2;
624
- count2 += 1;
625
- }
626
- return { count: count2, index };
627
- }
628
- function dataRowAt(rows, ordinal) {
629
- if (ordinal < 0)
630
- return void 0;
631
- let seen = 0;
632
- for (const entry of rows) {
633
- if (!isDataRow(entry))
634
- continue;
635
- if (seen === ordinal)
636
- return entry;
637
- seen += 1;
638
- }
639
- return void 0;
640
- }
641
642
  function clampColumnWidth(width, column) {
642
643
  const min2 = column.minWidthPx ?? 40;
643
644
  const max2 = column.maxWidthPx ?? Infinity;
@@ -667,7 +668,7 @@ function groupColumnsByPin(columns) {
667
668
  const right = [];
668
669
  for (const column of columns) {
669
670
  const region = column.pinned === "left" ? left : column.pinned === "right" ? right : unpinned;
670
- if (column.id === ROW_SELECT_COLUMN_ID) {
671
+ if (column.id === ROW_SELECT_COLUMN_ID || column.id === GROUP_COLUMN_ID) {
671
672
  region.unshift(column);
672
673
  } else {
673
674
  region.push(column);
@@ -694,6 +695,9 @@ function createGridCore(inputOptions) {
694
695
  let cachedDerivedOverrides = null;
695
696
  let cachedDerivedDefaultExpanded = null;
696
697
  let cachedDerivedAggregateFiltered = null;
698
+ let cachedEffectiveColumns = null;
699
+ let cachedEffectiveColumnsSource = null;
700
+ let cachedEffectiveColumnsRowGroups = null;
697
701
  let sort = [];
698
702
  let filters = {};
699
703
  let rowGroups = sanitizeRowGroups(options.columns.filter((column) => column.rowGroup === true).map((c) => c.id), options.columns);
@@ -721,6 +725,7 @@ function createGridCore(inputOptions) {
721
725
  };
722
726
  },
723
727
  getSnapshot,
728
+ getColumns,
724
729
  setSort(columnId, direction) {
725
730
  const candidate = columnId && direction ? [{ columnId, direction }] : [];
726
731
  const next = sanitizeSortEntries(candidate, options.columns);
@@ -935,13 +940,15 @@ function createGridCore(inputOptions) {
935
940
  },
936
941
  moveFocus(direction, moveOptions = {}) {
937
942
  const snapshot = getSnapshot();
938
- const { count: dataRowCount, index: currentRowIndex } = scanDataRows(snapshot.visibleRows, focus.rowId);
939
- const columnList = options.columns;
940
- if (dataRowCount === 0 || columnList.length === 0) {
943
+ const rowList = snapshot.visibleRows;
944
+ const rowCount = rowList.length;
945
+ const columnList = getColumns();
946
+ if (rowCount === 0 || columnList.length === 0) {
941
947
  focus = { rowId: null, columnId: null };
942
948
  emit();
943
949
  return;
944
950
  }
951
+ const currentRowIndex = focus.rowId === null ? -1 : rowList.findIndex((entry) => entry.id === focus.rowId);
945
952
  const currentColumnIndex = focus.columnId ? columnList.findIndex((c) => c.id === focus.columnId) : -1;
946
953
  const hasRowFocus = currentRowIndex !== -1;
947
954
  const hasColumnFocus = currentColumnIndex !== -1;
@@ -949,28 +956,28 @@ function createGridCore(inputOptions) {
949
956
  const baseColumnIndex = hasColumnFocus ? currentColumnIndex : 0;
950
957
  let nextRowIndex = baseRowIndex;
951
958
  let nextColumnIndex = baseColumnIndex;
952
- const pageStep = computePageStep(viewport, dataRowCount);
959
+ const pageStep = computePageStep(viewport, rowCount);
953
960
  switch (direction) {
954
961
  case "up":
955
962
  if (moveOptions.jumpToEdge) {
956
963
  nextRowIndex = 0;
957
964
  } else if (!hasRowFocus) {
958
- nextRowIndex = dataRowCount - 1;
965
+ nextRowIndex = rowCount - 1;
959
966
  } else if (moveOptions.byPage) {
960
- nextRowIndex = clamp(baseRowIndex - pageStep, 0, dataRowCount - 1);
967
+ nextRowIndex = clamp(baseRowIndex - pageStep, 0, rowCount - 1);
961
968
  } else {
962
- nextRowIndex = clamp(baseRowIndex - 1, 0, dataRowCount - 1);
969
+ nextRowIndex = clamp(baseRowIndex - 1, 0, rowCount - 1);
963
970
  }
964
971
  break;
965
972
  case "down":
966
973
  if (moveOptions.jumpToEdge) {
967
- nextRowIndex = dataRowCount - 1;
974
+ nextRowIndex = rowCount - 1;
968
975
  } else if (!hasRowFocus) {
969
976
  nextRowIndex = 0;
970
977
  } else if (moveOptions.byPage) {
971
- nextRowIndex = clamp(baseRowIndex + pageStep, 0, dataRowCount - 1);
978
+ nextRowIndex = clamp(baseRowIndex + pageStep, 0, rowCount - 1);
972
979
  } else {
973
- nextRowIndex = clamp(baseRowIndex + 1, 0, dataRowCount - 1);
980
+ nextRowIndex = clamp(baseRowIndex + 1, 0, rowCount - 1);
974
981
  }
975
982
  break;
976
983
  case "left":
@@ -992,7 +999,7 @@ function createGridCore(inputOptions) {
992
999
  }
993
1000
  break;
994
1001
  }
995
- const nextRow = dataRowAt(snapshot.visibleRows, nextRowIndex);
1002
+ const nextRow = rowList[nextRowIndex];
996
1003
  const nextColumn = columnList[nextColumnIndex];
997
1004
  if (!nextRow || !nextColumn) {
998
1005
  return;
@@ -1379,6 +1386,7 @@ function createGridCore(inputOptions) {
1379
1386
  if (isGroupExpanded(groupId) === expanded) {
1380
1387
  return;
1381
1388
  }
1389
+ const before = getSnapshot().visibleRows;
1382
1390
  if (expanded === groupsDefaultExpanded) {
1383
1391
  const next = new Set(groupExpansionOverrides);
1384
1392
  next.delete(groupId);
@@ -1386,6 +1394,7 @@ function createGridCore(inputOptions) {
1386
1394
  } else {
1387
1395
  groupExpansionOverrides = addGroupExpansionOverride(groupExpansionOverrides, groupId, groupExpansionOverrideLimit);
1388
1396
  }
1397
+ reanchorFocusAfterCollapse(before);
1389
1398
  emit();
1390
1399
  },
1391
1400
  expandAll() {
@@ -1460,10 +1469,34 @@ function createGridCore(inputOptions) {
1460
1469
  if (groupsDefaultExpanded === expanded && groupExpansionOverrides.size === 0) {
1461
1470
  return;
1462
1471
  }
1472
+ const before = getSnapshot().visibleRows;
1463
1473
  groupsDefaultExpanded = expanded;
1464
1474
  groupExpansionOverrides = /* @__PURE__ */ new Set();
1475
+ reanchorFocusAfterCollapse(before);
1465
1476
  emit();
1466
1477
  }
1478
+ function reanchorFocusAfterCollapse(before) {
1479
+ if (focus.rowId === null)
1480
+ return;
1481
+ const fromIndex = before.findIndex((entry) => entry.id === focus.rowId);
1482
+ if (fromIndex === -1)
1483
+ return;
1484
+ cachedSnapshot = null;
1485
+ const after = getSnapshot().visibleRows;
1486
+ if (after.some((entry) => entry.id === focus.rowId))
1487
+ return;
1488
+ const surviving = new Set(after.map((entry) => entry.id));
1489
+ let nextRowId = null;
1490
+ for (let i = fromIndex - 1; i >= 0; i -= 1) {
1491
+ const candidate = before[i];
1492
+ if (candidate.kind === "group" && surviving.has(candidate.id)) {
1493
+ nextRowId = candidate.id;
1494
+ break;
1495
+ }
1496
+ }
1497
+ focus = nextRowId === null ? { rowId: null, columnId: null } : { rowId: nextRowId, columnId: focus.columnId };
1498
+ cachedSnapshot = null;
1499
+ }
1467
1500
  function preserveAggregateIdentity(rows) {
1468
1501
  const next = /* @__PURE__ */ new Map();
1469
1502
  let result = rows;
@@ -1484,6 +1517,22 @@ function createGridCore(inputOptions) {
1484
1517
  previousAggregates = next;
1485
1518
  return result;
1486
1519
  }
1520
+ function getColumns() {
1521
+ if (cachedEffectiveColumns !== null && cachedEffectiveColumnsSource === options.columns && cachedEffectiveColumnsRowGroups === rowGroups) {
1522
+ return cachedEffectiveColumns;
1523
+ }
1524
+ const resolved = resolveEffectiveColumns({
1525
+ columns: options.columns,
1526
+ rowGroups,
1527
+ groupColumn: options.groupColumn,
1528
+ hideGroupedColumns: options.hideGroupedColumns
1529
+ });
1530
+ const effective = resolved === options.columns ? resolved : groupColumnsByPin(resolved);
1531
+ cachedEffectiveColumns = effective;
1532
+ cachedEffectiveColumnsSource = options.columns;
1533
+ cachedEffectiveColumnsRowGroups = rowGroups;
1534
+ return effective;
1535
+ }
1487
1536
  function getSnapshot() {
1488
1537
  if (cachedSnapshot) {
1489
1538
  return cachedSnapshot;
@@ -1548,12 +1597,12 @@ function sameColumnLayout(a, b) {
1548
1597
  return left.id === right.id && left.header === right.header && left.widthPx === right.widthPx && left.minWidthPx === right.minWidthPx && left.maxWidthPx === right.maxWidthPx && left.pinned === right.pinned && left.sortable === right.sortable && left.filterable === right.filterable && left.resizable === right.resizable && left.reorderable === right.reorderable && left.wrap === right.wrap;
1549
1598
  });
1550
1599
  }
1551
- function computePageStep(viewport, dataRowCount) {
1552
- if (viewport.height <= 0 || dataRowCount === 0) {
1600
+ function computePageStep(viewport, rowCount) {
1601
+ if (viewport.height <= 0 || rowCount === 0) {
1553
1602
  return 1;
1554
1603
  }
1555
1604
  const estimatedRowsPerPage = Math.max(1, Math.floor(viewport.height * 0.8 / 32));
1556
- return Math.min(estimatedRowsPerPage, dataRowCount);
1605
+ return Math.min(estimatedRowsPerPage, rowCount);
1557
1606
  }
1558
1607
  function isFullRowRange(range, rowId, firstColumnId, lastColumnId) {
1559
1608
  return range.startRowId === rowId && range.endRowId === rowId && range.startColumnId === firstColumnId && range.endColumnId === lastColumnId;
@@ -1655,6 +1704,7 @@ function createGrid(options) {
1655
1704
  },
1656
1705
  subscribe: engine.subscribe,
1657
1706
  getSnapshot: engine.getSnapshot,
1707
+ getColumns: engine.getColumns,
1658
1708
  setSort: engine.setSort,
1659
1709
  replaceSort: engine.replaceSort,
1660
1710
  setColumnFilter: engine.setColumnFilter,
@@ -1698,4 +1748,4 @@ function createGrid(options) {
1698
1748
  };
1699
1749
  }
1700
1750
 
1701
- export { createGrid };
1751
+ export { GROUP_COLUMN_ID, createGrid };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pretable/core",
3
- "version": "0.0.6",
3
+ "version": "0.0.8",
4
4
  "description": "Framework-agnostic grid state primitives for Pretable.",
5
5
  "license": "MIT",
6
6
  "repository": {