@pretable/core 0.0.3 → 0.0.6

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.mjs CHANGED
@@ -224,7 +224,86 @@ function evaluateFilter(cell, type, operator, value) {
224
224
  }
225
225
  }
226
226
 
227
- // ../grid-core/dist/derived-rows.js
227
+ // ../grid-core/dist/aggregators.js
228
+ function isAggregatableNumber(value) {
229
+ return typeof value === "number" && !Number.isNaN(value);
230
+ }
231
+ var sum = {
232
+ init: () => ({ sum: 0, count: 0 }),
233
+ accumulate(acc, value) {
234
+ if (!isAggregatableNumber(value))
235
+ return acc;
236
+ acc.sum += value;
237
+ acc.count += 1;
238
+ return acc;
239
+ },
240
+ merge: (a, b) => ({ sum: a.sum + b.sum, count: a.count + b.count }),
241
+ finalize: (acc) => acc.count === 0 ? null : acc.sum
242
+ };
243
+ var avg = {
244
+ init: () => ({ sum: 0, count: 0 }),
245
+ accumulate: sum.accumulate,
246
+ merge: sum.merge,
247
+ finalize: (acc) => acc.count === 0 ? null : acc.sum / acc.count
248
+ };
249
+ var min = {
250
+ init: () => ({ value: null }),
251
+ accumulate(acc, value) {
252
+ if (!isAggregatableNumber(value))
253
+ return acc;
254
+ if (acc.value === null || value < acc.value)
255
+ acc.value = value;
256
+ return acc;
257
+ },
258
+ merge: (a, b) => ({
259
+ value: a.value === null ? b.value : b.value === null ? a.value : Math.min(a.value, b.value)
260
+ }),
261
+ finalize: (acc) => acc.value
262
+ };
263
+ var max = {
264
+ init: () => ({ value: null }),
265
+ accumulate(acc, value) {
266
+ if (!isAggregatableNumber(value))
267
+ return acc;
268
+ if (acc.value === null || value > acc.value)
269
+ acc.value = value;
270
+ return acc;
271
+ },
272
+ merge: (a, b) => ({
273
+ value: a.value === null ? b.value : b.value === null ? a.value : Math.max(a.value, b.value)
274
+ }),
275
+ finalize: (acc) => acc.value
276
+ };
277
+ var count = {
278
+ init: () => ({ count: 0 }),
279
+ accumulate(acc) {
280
+ acc.count += 1;
281
+ return acc;
282
+ },
283
+ merge: (a, b) => ({ count: a.count + b.count }),
284
+ // `0` is arguably count's identity, unlike sum/min/max where "no numeric
285
+ // values seen" has no honest number. We still return `null`, for consistency
286
+ // with the other built-ins and because it is unreachable for a real group —
287
+ // a group only exists because at least one row landed in it.
288
+ finalize: (acc) => acc.count === 0 ? null : acc.count
289
+ };
290
+ var builtinAggregators = {
291
+ sum,
292
+ avg,
293
+ min,
294
+ max,
295
+ count
296
+ };
297
+ function resolveAggregator(spec) {
298
+ if (spec === void 0 || spec === null)
299
+ return null;
300
+ if (typeof spec === "string") {
301
+ return builtinAggregators[spec] ?? null;
302
+ }
303
+ return spec;
304
+ }
305
+
306
+ // ../grid-core/dist/row-utils.js
228
307
  function createSourceRows(options) {
229
308
  return options.rows.map((row, index) => ({
230
309
  id: options.getRowId?.(row, index) ?? String(index),
@@ -232,38 +311,6 @@ function createSourceRows(options) {
232
311
  sourceIndex: index
233
312
  }));
234
313
  }
235
- function deriveVisibleRows(input) {
236
- const resolvedFilters = resolveFilters(input.columns, input.filters);
237
- const filtered = input.rows.filter((entry) => matchesFilters(entry.row, resolvedFilters));
238
- const sorted = sortRows(filtered, input.columns, input.sort);
239
- return sorted.map(({ id, row, sourceIndex }) => ({
240
- id,
241
- row,
242
- sourceIndex
243
- }));
244
- }
245
- function resolveFilters(columns, filters) {
246
- const columnMap = new Map(columns.map((c) => [c.id, c]));
247
- const resolved = [];
248
- for (const [columnId, filter] of Object.entries(filters)) {
249
- if (!filter || !isFilterActive(filter))
250
- continue;
251
- const column = columnMap.get(columnId);
252
- if (!column || column.filterable === false)
253
- continue;
254
- resolved.push({ column, filter });
255
- }
256
- return resolved;
257
- }
258
- function matchesFilters(row, resolvedFilters) {
259
- for (const { column, filter } of resolvedFilters) {
260
- const cell = readCellValue(row, column);
261
- if (!evaluateFilter(cell, column.type ?? "text", filter.operator, filter.value)) {
262
- return false;
263
- }
264
- }
265
- return true;
266
- }
267
314
  var collator = new Intl.Collator(void 0, {
268
315
  numeric: true,
269
316
  sensitivity: "base"
@@ -301,12 +348,300 @@ function readCellValue(row, column) {
301
348
  return column.value ? column.value(row) : row[column.id];
302
349
  }
303
350
 
351
+ // ../grid-core/dist/group-id.js
352
+ var GROUP_ID_PREFIX = "__group__:";
353
+ var LEVEL_SEPARATOR = "/";
354
+ var KEY_SEPARATOR = "=";
355
+ function escapeGroupKey(raw) {
356
+ return raw.replace(/%/g, "%25").replace(/\//g, "%2F").replace(/=/g, "%3D");
357
+ }
358
+ function stringifyGroupValue(value) {
359
+ if (value === null || value === void 0)
360
+ return "~";
361
+ switch (typeof value) {
362
+ case "string":
363
+ return `s:${value}`;
364
+ case "number":
365
+ return `n:${value}`;
366
+ case "boolean":
367
+ return `b:${value}`;
368
+ case "bigint":
369
+ return `i:${value}`;
370
+ default:
371
+ return `o:${safeToString(value)}`;
372
+ }
373
+ }
374
+ function safeToString(value) {
375
+ try {
376
+ return String(value);
377
+ } catch {
378
+ return "[unstringifiable]";
379
+ }
380
+ }
381
+ function makeGroupId(path) {
382
+ let id = GROUP_ID_PREFIX;
383
+ for (let i = 0; i < path.length; i += 1) {
384
+ if (i > 0)
385
+ id += LEVEL_SEPARATOR;
386
+ id += escapeGroupKey(path[i].columnId);
387
+ id += KEY_SEPARATOR;
388
+ id += escapeGroupKey(stringifyGroupValue(path[i].value));
389
+ }
390
+ return id;
391
+ }
392
+
393
+ // ../grid-core/dist/group-rows.js
394
+ function buildGroupedRows(args) {
395
+ const { rows, allRows, columns, rowGroups, sort } = args;
396
+ const sorted = sortRows(rows, columns, sort);
397
+ const levels = resolveLevels(columns, rowGroups);
398
+ if (levels.length === 0) {
399
+ return sorted.map(({ id, row, sourceIndex }) => ({
400
+ kind: "data",
401
+ id,
402
+ row,
403
+ sourceIndex,
404
+ depth: 0
405
+ }));
406
+ }
407
+ const roots = buildTree(sorted, levels);
408
+ const aggregateColumns = resolveAggregateColumns(columns);
409
+ if (aggregateColumns.length > 0) {
410
+ accumulate(allRows ? sortRows(allRows, columns, sort) : sorted, levels, roots, aggregateColumns);
411
+ }
412
+ const out = [];
413
+ flatten(roots, 0, levels, aggregateColumns, args, out);
414
+ return out;
415
+ }
416
+ function resolveLevels(columns, rowGroups) {
417
+ if (rowGroups.length === 0)
418
+ return [];
419
+ const byId = new Map(columns.map((column) => [column.id, column]));
420
+ const levels = [];
421
+ for (const columnId of rowGroups) {
422
+ const column = byId.get(columnId);
423
+ if (column)
424
+ levels.push(column);
425
+ }
426
+ return levels;
427
+ }
428
+ function resolveAggregateColumns(columns) {
429
+ const resolved = [];
430
+ for (const column of columns) {
431
+ const aggregator = resolveAggregator(column.aggregate);
432
+ if (aggregator)
433
+ resolved.push({ column, aggregator });
434
+ }
435
+ return resolved;
436
+ }
437
+ function buildTree(sorted, levels) {
438
+ const roots = /* @__PURE__ */ new Map();
439
+ const lastLevel = levels.length - 1;
440
+ const path = [];
441
+ for (const entry of sorted) {
442
+ let siblings = roots;
443
+ let node;
444
+ path.length = 0;
445
+ for (let depth = 0; depth <= lastLevel; depth += 1) {
446
+ const column = levels[depth];
447
+ const value = readCellValue(entry.row, column);
448
+ const key = stringifyGroupValue(value);
449
+ path.push({ columnId: column.id, value });
450
+ node = siblings.get(key);
451
+ if (!node) {
452
+ const isLeafLevel = depth === lastLevel;
453
+ node = {
454
+ id: makeGroupId(path),
455
+ depth,
456
+ columnId: column.id,
457
+ value,
458
+ childCount: 0,
459
+ children: isLeafLevel ? null : /* @__PURE__ */ new Map(),
460
+ rows: isLeafLevel ? [] : null,
461
+ accumulators: /* @__PURE__ */ new Map()
462
+ };
463
+ siblings.set(key, node);
464
+ }
465
+ node.childCount += 1;
466
+ if (node.children)
467
+ siblings = node.children;
468
+ }
469
+ node?.rows?.push(entry);
470
+ }
471
+ return roots;
472
+ }
473
+ function accumulate(source, levels, roots, aggregateColumns) {
474
+ for (const entry of source) {
475
+ const values = aggregateColumns.map(({ column }) => readCellValue(entry.row, column));
476
+ let siblings = roots;
477
+ for (let depth = 0; depth < levels.length && siblings; depth += 1) {
478
+ const key = stringifyGroupValue(readCellValue(entry.row, levels[depth]));
479
+ const node = siblings.get(key);
480
+ if (!node)
481
+ break;
482
+ for (let i = 0; i < aggregateColumns.length; i += 1) {
483
+ const { column, aggregator } = aggregateColumns[i];
484
+ const previous = node.accumulators.get(column.id);
485
+ const acc = previous === void 0 ? aggregator.init() : previous;
486
+ node.accumulators.set(column.id, aggregator.accumulate(acc, values[i], entry.row));
487
+ }
488
+ siblings = node.children;
489
+ }
490
+ }
491
+ }
492
+ function flatten(siblings, depth, levels, aggregateColumns, args, out) {
493
+ const nodes = [...siblings.values()];
494
+ sortSiblings(nodes, levels[depth], args.sort);
495
+ for (const node of nodes) {
496
+ out.push({
497
+ kind: "group",
498
+ id: node.id,
499
+ depth: node.depth,
500
+ columnId: node.columnId,
501
+ value: node.value,
502
+ childCount: node.childCount,
503
+ aggregates: finalizeAggregates(node, aggregateColumns)
504
+ });
505
+ if (!isExpanded(node.id, args))
506
+ continue;
507
+ if (node.children) {
508
+ flatten(node.children, depth + 1, levels, aggregateColumns, args, out);
509
+ continue;
510
+ }
511
+ for (const entry of node.rows ?? []) {
512
+ out.push({
513
+ kind: "data",
514
+ id: entry.id,
515
+ row: entry.row,
516
+ sourceIndex: entry.sourceIndex,
517
+ depth: depth + 1
518
+ });
519
+ }
520
+ }
521
+ }
522
+ function finalizeAggregates(node, aggregateColumns) {
523
+ const aggregates = {};
524
+ for (const { column, aggregator } of aggregateColumns) {
525
+ const acc = node.accumulators.get(column.id);
526
+ aggregates[column.id] = aggregator.finalize(acc === void 0 ? aggregator.init() : acc);
527
+ }
528
+ return aggregates;
529
+ }
530
+ function isExpanded(id, args) {
531
+ return args.groupExpansionOverrides.has(id) ? !args.defaultExpanded : args.defaultExpanded;
532
+ }
533
+ function sortSiblings(nodes, column, sort) {
534
+ if (nodes.length < 2)
535
+ return;
536
+ const entry = column ? sort.find((candidate) => candidate.columnId === column.id) : void 0;
537
+ const multiplier = entry?.direction === "desc" ? -1 : 1;
538
+ const allNumeric = nodes.every((node) => typeof node.value === "number");
539
+ nodes.sort((a, b) => {
540
+ const cmp = allNumeric ? a.value - b.value : collator.compare(String(a.value ?? ""), String(b.value ?? ""));
541
+ return cmp * multiplier;
542
+ });
543
+ }
544
+
545
+ // ../grid-core/dist/derived-rows.js
546
+ var NO_OVERRIDES = /* @__PURE__ */ new Set();
547
+ function deriveVisibleRows(input) {
548
+ const resolvedFilters = resolveFilters(input.columns, input.filters);
549
+ const filtered = input.rows.filter((entry) => matchesFilters(entry.row, resolvedFilters));
550
+ return buildGroupedRows({
551
+ rows: filtered,
552
+ // Only worth carrying the pre-filter set when it can actually differ.
553
+ // Equal lengths mean the filter removed nothing, so `filtered` is a copy of
554
+ // `input.rows` in the same order and the two sort identically —
555
+ // `buildGroupedRows` sorts whichever it folds over, so skipping here does
556
+ // not change the fold order.
557
+ allRows: input.aggregateFilteredRows && filtered.length !== input.rows.length ? input.rows : void 0,
558
+ columns: input.columns,
559
+ rowGroups: input.rowGroups ?? [],
560
+ sort: input.sort,
561
+ groupExpansionOverrides: input.groupExpansionOverrides ?? NO_OVERRIDES,
562
+ defaultExpanded: input.groupsDefaultExpanded ?? true
563
+ });
564
+ }
565
+ function resolveFilters(columns, filters) {
566
+ const columnMap = new Map(columns.map((c) => [c.id, c]));
567
+ const resolved = [];
568
+ for (const [columnId, filter] of Object.entries(filters)) {
569
+ if (!filter || !isFilterActive(filter))
570
+ continue;
571
+ const column = columnMap.get(columnId);
572
+ if (!column || column.filterable === false)
573
+ continue;
574
+ resolved.push({ column, filter });
575
+ }
576
+ return resolved;
577
+ }
578
+ function matchesFilters(row, resolvedFilters) {
579
+ for (const { column, filter } of resolvedFilters) {
580
+ const cell = readCellValue(row, column);
581
+ if (!evaluateFilter(cell, column.type ?? "text", filter.operator, filter.value)) {
582
+ return false;
583
+ }
584
+ }
585
+ return true;
586
+ }
587
+
588
+ // ../grid-core/dist/group-expansion.js
589
+ var DEFAULT_GROUP_EXPANSION_OVERRIDE_LIMIT = 1e4;
590
+ function resolveGroupExpansionOverrideLimit(configured) {
591
+ if (configured === void 0)
592
+ return DEFAULT_GROUP_EXPANSION_OVERRIDE_LIMIT;
593
+ if (configured === Number.POSITIVE_INFINITY)
594
+ return configured;
595
+ if (!Number.isFinite(configured)) {
596
+ return DEFAULT_GROUP_EXPANSION_OVERRIDE_LIMIT;
597
+ }
598
+ return Math.max(1, Math.floor(configured));
599
+ }
600
+ function addGroupExpansionOverride(current, groupId, limit) {
601
+ const next = new Set(current);
602
+ next.delete(groupId);
603
+ next.add(groupId);
604
+ while (next.size > limit) {
605
+ const oldest = next.values().next().value;
606
+ next.delete(oldest);
607
+ }
608
+ return next;
609
+ }
610
+
304
611
  // ../grid-core/dist/create-grid-core.js
305
612
  var ROW_SELECT_COLUMN_ID = "__pretable_row_select__";
613
+ function isDataRow(entry) {
614
+ return entry.kind === "data";
615
+ }
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
+ }
306
641
  function clampColumnWidth(width, column) {
307
- const min = column.minWidthPx ?? 40;
308
- const max = column.maxWidthPx ?? Infinity;
309
- return Math.max(min, Math.min(max, width));
642
+ const min2 = column.minWidthPx ?? 40;
643
+ const max2 = column.maxWidthPx ?? Infinity;
644
+ return Math.max(min2, Math.min(max2, width));
310
645
  }
311
646
  function applyAutosize(options, autosizeOptions) {
312
647
  const result = autosizeColumns({
@@ -355,8 +690,17 @@ function createGridCore(inputOptions) {
355
690
  let cachedVisibleRows = null;
356
691
  let cachedDerivedSort = null;
357
692
  let cachedDerivedFilters = null;
693
+ let cachedDerivedRowGroups = null;
694
+ let cachedDerivedOverrides = null;
695
+ let cachedDerivedDefaultExpanded = null;
696
+ let cachedDerivedAggregateFiltered = null;
358
697
  let sort = [];
359
698
  let filters = {};
699
+ let rowGroups = sanitizeRowGroups(options.columns.filter((column) => column.rowGroup === true).map((c) => c.id), options.columns);
700
+ let groupExpansionOverrides = /* @__PURE__ */ new Set();
701
+ const groupExpansionOverrideLimit = resolveGroupExpansionOverrideLimit(options.groupExpansionOverrideLimit);
702
+ let groupsDefaultExpanded = options.groupsDefaultExpanded ?? true;
703
+ let previousAggregates = /* @__PURE__ */ new Map();
360
704
  let selection = { ranges: [], anchor: null };
361
705
  let focus = { rowId: null, columnId: null };
362
706
  let editing = null;
@@ -461,9 +805,9 @@ function createGridCore(inputOptions) {
461
805
  emit();
462
806
  },
463
807
  selectAll() {
464
- const snapshot = getSnapshot();
465
- const firstRow = snapshot.visibleRows[0];
466
- const lastRow = snapshot.visibleRows[snapshot.visibleRows.length - 1];
808
+ const dataRows = getSnapshot().visibleRows.filter(isDataRow);
809
+ const firstRow = dataRows[0];
810
+ const lastRow = dataRows[dataRows.length - 1];
467
811
  const firstColumn = options.columns[0];
468
812
  const lastColumn = options.columns[options.columns.length - 1];
469
813
  if (!firstRow || !lastRow || !firstColumn || !lastColumn) {
@@ -551,17 +895,17 @@ function createGridCore(inputOptions) {
551
895
  emit();
552
896
  },
553
897
  setSelectAllVisible(checked) {
554
- const snapshot = getSnapshot();
898
+ const dataRows = getSnapshot().visibleRows.filter(isDataRow);
555
899
  const firstColumn = options.columns[0];
556
900
  const lastColumn = options.columns[options.columns.length - 1];
557
901
  if (!firstColumn || !lastColumn) {
558
902
  return;
559
903
  }
560
- const visibleIds = new Set(snapshot.visibleRows.map((r) => r.id));
904
+ const visibleIds = new Set(dataRows.map((r) => r.id));
561
905
  const nonRowRanges = selection.ranges.filter((r) => !isFullRowRange(r, r.startRowId, firstColumn.id, lastColumn.id) || !visibleIds.has(r.startRowId));
562
906
  let next;
563
907
  if (checked) {
564
- const newRanges = snapshot.visibleRows.map((row) => ({
908
+ const newRanges = dataRows.map((row) => ({
565
909
  startRowId: row.id,
566
910
  endRowId: row.id,
567
911
  startColumnId: firstColumn.id,
@@ -569,7 +913,7 @@ function createGridCore(inputOptions) {
569
913
  }));
570
914
  next = {
571
915
  ranges: [...nonRowRanges, ...newRanges],
572
- anchor: snapshot.visibleRows[0] ? { rowId: snapshot.visibleRows[0].id, columnId: firstColumn.id } : selection.anchor
916
+ anchor: dataRows[0] ? { rowId: dataRows[0].id, columnId: firstColumn.id } : selection.anchor
573
917
  };
574
918
  } else {
575
919
  next = { ranges: nonRowRanges, anchor: selection.anchor };
@@ -591,14 +935,13 @@ function createGridCore(inputOptions) {
591
935
  },
592
936
  moveFocus(direction, moveOptions = {}) {
593
937
  const snapshot = getSnapshot();
594
- const visibleRows = snapshot.visibleRows;
938
+ const { count: dataRowCount, index: currentRowIndex } = scanDataRows(snapshot.visibleRows, focus.rowId);
595
939
  const columnList = options.columns;
596
- if (visibleRows.length === 0 || columnList.length === 0) {
940
+ if (dataRowCount === 0 || columnList.length === 0) {
597
941
  focus = { rowId: null, columnId: null };
598
942
  emit();
599
943
  return;
600
944
  }
601
- const currentRowIndex = focus.rowId ? visibleRows.findIndex((r) => r.id === focus.rowId) : -1;
602
945
  const currentColumnIndex = focus.columnId ? columnList.findIndex((c) => c.id === focus.columnId) : -1;
603
946
  const hasRowFocus = currentRowIndex !== -1;
604
947
  const hasColumnFocus = currentColumnIndex !== -1;
@@ -606,28 +949,28 @@ function createGridCore(inputOptions) {
606
949
  const baseColumnIndex = hasColumnFocus ? currentColumnIndex : 0;
607
950
  let nextRowIndex = baseRowIndex;
608
951
  let nextColumnIndex = baseColumnIndex;
609
- const pageStep = computePageStep(viewport, visibleRows);
952
+ const pageStep = computePageStep(viewport, dataRowCount);
610
953
  switch (direction) {
611
954
  case "up":
612
955
  if (moveOptions.jumpToEdge) {
613
956
  nextRowIndex = 0;
614
957
  } else if (!hasRowFocus) {
615
- nextRowIndex = visibleRows.length - 1;
958
+ nextRowIndex = dataRowCount - 1;
616
959
  } else if (moveOptions.byPage) {
617
- nextRowIndex = clamp(baseRowIndex - pageStep, 0, visibleRows.length - 1);
960
+ nextRowIndex = clamp(baseRowIndex - pageStep, 0, dataRowCount - 1);
618
961
  } else {
619
- nextRowIndex = clamp(baseRowIndex - 1, 0, visibleRows.length - 1);
962
+ nextRowIndex = clamp(baseRowIndex - 1, 0, dataRowCount - 1);
620
963
  }
621
964
  break;
622
965
  case "down":
623
966
  if (moveOptions.jumpToEdge) {
624
- nextRowIndex = visibleRows.length - 1;
967
+ nextRowIndex = dataRowCount - 1;
625
968
  } else if (!hasRowFocus) {
626
969
  nextRowIndex = 0;
627
970
  } else if (moveOptions.byPage) {
628
- nextRowIndex = clamp(baseRowIndex + pageStep, 0, visibleRows.length - 1);
971
+ nextRowIndex = clamp(baseRowIndex + pageStep, 0, dataRowCount - 1);
629
972
  } else {
630
- nextRowIndex = clamp(baseRowIndex + 1, 0, visibleRows.length - 1);
973
+ nextRowIndex = clamp(baseRowIndex + 1, 0, dataRowCount - 1);
631
974
  }
632
975
  break;
633
976
  case "left":
@@ -649,7 +992,7 @@ function createGridCore(inputOptions) {
649
992
  }
650
993
  break;
651
994
  }
652
- const nextRow = visibleRows[nextRowIndex];
995
+ const nextRow = dataRowAt(snapshot.visibleRows, nextRowIndex);
653
996
  const nextColumn = columnList[nextColumnIndex];
654
997
  if (!nextRow || !nextColumn) {
655
998
  return;
@@ -1020,6 +1363,37 @@ function createGridCore(inputOptions) {
1020
1363
  cachedVisibleRows = null;
1021
1364
  emit();
1022
1365
  },
1366
+ setRowGroups(columnIds) {
1367
+ const next = sanitizeRowGroups(columnIds, options.columns);
1368
+ if (stringListsEqual(rowGroups, next)) {
1369
+ return;
1370
+ }
1371
+ rowGroups = next;
1372
+ groupExpansionOverrides = /* @__PURE__ */ new Set();
1373
+ emit();
1374
+ },
1375
+ toggleGroup(groupId) {
1376
+ store.setGroupExpanded(groupId, !isGroupExpanded(groupId));
1377
+ },
1378
+ setGroupExpanded(groupId, expanded) {
1379
+ if (isGroupExpanded(groupId) === expanded) {
1380
+ return;
1381
+ }
1382
+ if (expanded === groupsDefaultExpanded) {
1383
+ const next = new Set(groupExpansionOverrides);
1384
+ next.delete(groupId);
1385
+ groupExpansionOverrides = next;
1386
+ } else {
1387
+ groupExpansionOverrides = addGroupExpansionOverride(groupExpansionOverrides, groupId, groupExpansionOverrideLimit);
1388
+ }
1389
+ emit();
1390
+ },
1391
+ expandAll() {
1392
+ setExpansionDefault(true);
1393
+ },
1394
+ collapseAll() {
1395
+ setExpansionDefault(false);
1396
+ },
1023
1397
  beginEdit(addr, opts) {
1024
1398
  editing = {
1025
1399
  rowId: addr.rowId,
@@ -1079,19 +1453,60 @@ function createGridCore(inputOptions) {
1079
1453
  }
1080
1454
  };
1081
1455
  return store;
1456
+ function isGroupExpanded(groupId) {
1457
+ return groupExpansionOverrides.has(groupId) ? !groupsDefaultExpanded : groupsDefaultExpanded;
1458
+ }
1459
+ function setExpansionDefault(expanded) {
1460
+ if (groupsDefaultExpanded === expanded && groupExpansionOverrides.size === 0) {
1461
+ return;
1462
+ }
1463
+ groupsDefaultExpanded = expanded;
1464
+ groupExpansionOverrides = /* @__PURE__ */ new Set();
1465
+ emit();
1466
+ }
1467
+ function preserveAggregateIdentity(rows) {
1468
+ const next = /* @__PURE__ */ new Map();
1469
+ let result = rows;
1470
+ for (let i = 0; i < rows.length; i += 1) {
1471
+ const entry = rows[i];
1472
+ if (entry.kind !== "group")
1473
+ continue;
1474
+ const previous = previousAggregates.get(entry.id);
1475
+ if (previous && recordsEqual(previous, entry.aggregates)) {
1476
+ if (result === rows)
1477
+ result = rows.slice();
1478
+ result[i] = { ...entry, aggregates: previous };
1479
+ next.set(entry.id, previous);
1480
+ } else {
1481
+ next.set(entry.id, entry.aggregates);
1482
+ }
1483
+ }
1484
+ previousAggregates = next;
1485
+ return result;
1486
+ }
1082
1487
  function getSnapshot() {
1083
1488
  if (cachedSnapshot) {
1084
1489
  return cachedSnapshot;
1085
1490
  }
1086
- const visibleRows = cachedVisibleRows !== null && cachedDerivedSort === sort && cachedDerivedFilters === filters ? cachedVisibleRows : deriveVisibleRows({
1491
+ const aggregateFilteredRows = options.aggregateFilteredRows ?? false;
1492
+ const derivedIsFresh = cachedVisibleRows !== null && cachedDerivedSort === sort && cachedDerivedFilters === filters && cachedDerivedRowGroups === rowGroups && cachedDerivedOverrides === groupExpansionOverrides && cachedDerivedDefaultExpanded === groupsDefaultExpanded && cachedDerivedAggregateFiltered === aggregateFilteredRows;
1493
+ const visibleRows = derivedIsFresh ? cachedVisibleRows : preserveAggregateIdentity(deriveVisibleRows({
1087
1494
  columns: options.columns,
1088
1495
  filters,
1089
1496
  rows: sourceRows,
1090
- sort
1091
- });
1497
+ sort,
1498
+ rowGroups,
1499
+ groupExpansionOverrides,
1500
+ groupsDefaultExpanded,
1501
+ aggregateFilteredRows
1502
+ }));
1092
1503
  cachedVisibleRows = visibleRows;
1093
1504
  cachedDerivedSort = sort;
1094
1505
  cachedDerivedFilters = filters;
1506
+ cachedDerivedRowGroups = rowGroups;
1507
+ cachedDerivedOverrides = groupExpansionOverrides;
1508
+ cachedDerivedDefaultExpanded = groupsDefaultExpanded;
1509
+ cachedDerivedAggregateFiltered = aggregateFilteredRows;
1095
1510
  cachedSnapshot = {
1096
1511
  viewport,
1097
1512
  sort: sort.map((entry) => ({ ...entry })),
@@ -1107,7 +1522,10 @@ function createGridCore(inputOptions) {
1107
1522
  start: 0,
1108
1523
  end: visibleRows.length
1109
1524
  },
1110
- editing: editing ? { ...editing } : null
1525
+ editing: editing ? { ...editing } : null,
1526
+ rowGroups: [...rowGroups],
1527
+ groupExpansionOverrides: new Set(groupExpansionOverrides),
1528
+ groupsDefaultExpanded
1111
1529
  };
1112
1530
  return cachedSnapshot;
1113
1531
  }
@@ -1118,8 +1536,8 @@ function createGridCore(inputOptions) {
1118
1536
  }
1119
1537
  }
1120
1538
  }
1121
- function clamp(value, min, max) {
1122
- return Math.max(min, Math.min(max, value));
1539
+ function clamp(value, min2, max2) {
1540
+ return Math.max(min2, Math.min(max2, value));
1123
1541
  }
1124
1542
  function sameColumnLayout(a, b) {
1125
1543
  if (a.length !== b.length) {
@@ -1130,12 +1548,12 @@ function sameColumnLayout(a, b) {
1130
1548
  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;
1131
1549
  });
1132
1550
  }
1133
- function computePageStep(viewport, visibleRows) {
1134
- if (viewport.height <= 0 || visibleRows.length === 0) {
1551
+ function computePageStep(viewport, dataRowCount) {
1552
+ if (viewport.height <= 0 || dataRowCount === 0) {
1135
1553
  return 1;
1136
1554
  }
1137
1555
  const estimatedRowsPerPage = Math.max(1, Math.floor(viewport.height * 0.8 / 32));
1138
- return Math.min(estimatedRowsPerPage, visibleRows.length);
1556
+ return Math.min(estimatedRowsPerPage, dataRowCount);
1139
1557
  }
1140
1558
  function isFullRowRange(range, rowId, firstColumnId, lastColumnId) {
1141
1559
  return range.startRowId === rowId && range.endRowId === rowId && range.startColumnId === firstColumnId && range.endColumnId === lastColumnId;
@@ -1165,6 +1583,28 @@ function sanitizeSortEntries(entries, columns) {
1165
1583
  return column !== void 0 && column.sortable !== false;
1166
1584
  });
1167
1585
  }
1586
+ function sanitizeRowGroups(columnIds, columns) {
1587
+ const known = new Set(columns.map((column) => column.id));
1588
+ const seen = /* @__PURE__ */ new Set();
1589
+ const next = [];
1590
+ for (const columnId of columnIds) {
1591
+ if (!known.has(columnId) || seen.has(columnId))
1592
+ continue;
1593
+ seen.add(columnId);
1594
+ next.push(columnId);
1595
+ }
1596
+ return next;
1597
+ }
1598
+ function stringListsEqual(a, b) {
1599
+ return a.length === b.length && a.every((value, i) => value === b[i]);
1600
+ }
1601
+ function recordsEqual(a, b) {
1602
+ const aKeys = Object.keys(a);
1603
+ if (aKeys.length !== Object.keys(b).length) {
1604
+ return false;
1605
+ }
1606
+ return aKeys.every((key) => Object.prototype.hasOwnProperty.call(b, key) && Object.is(a[key], b[key]));
1607
+ }
1168
1608
  function sortsEqual(a, b) {
1169
1609
  if (a.length !== b.length) {
1170
1610
  return false;
@@ -1230,6 +1670,11 @@ function createGrid(options) {
1230
1670
  setSelectAllVisible: engine.setSelectAllVisible,
1231
1671
  setFocus: engine.setFocus,
1232
1672
  moveFocus: engine.moveFocus,
1673
+ setRowGroups: engine.setRowGroups,
1674
+ toggleGroup: engine.toggleGroup,
1675
+ setGroupExpanded: engine.setGroupExpanded,
1676
+ expandAll: engine.expandAll,
1677
+ collapseAll: engine.collapseAll,
1233
1678
  setViewport: engine.setViewport,
1234
1679
  autosizeColumns: engine.autosizeColumns,
1235
1680
  setColumnWidth: engine.setColumnWidth,