gm-datagrid 0.1.17 → 0.1.19

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
@@ -7,6 +7,7 @@ Enterprise-grade, Excel-like data grid for React — powered by a **Rust/WebAsse
7
7
  - 📋 Excel-compatible clipboard (Ctrl+C / Ctrl+V TSV)
8
8
  - ✏️ Inline cell editing with async validation and automatic revert on error
9
9
  - 📊 Column subtotals updated live as filters change
10
+ - 🔢 **Aggregation footer row** — `sum`, `avg`, `min`, `max`, `size` per column; MUI DataGrid Premium compatible `aggregationModel` API
10
11
  - 📅 Date filter operators — is / is not / is after / is before / is empty / is not empty
11
12
  - 📥 **Multi-format export** — toolbar picker for `xlsx` (styled, freeze pane, zebra striping via ExcelJS), `csv`, `txt`, `json`
12
13
  - 🌐 Built-in **i18n** — `locale="es"` / `locale="en"` with full label overrides
@@ -400,9 +401,10 @@ async function saveRow(newRow, oldRow) {
400
401
  | `Enter` | Save and move to the next row |
401
402
  | `Tab` / `Shift+Tab` | Save and move right / left |
402
403
  | `Escape` | Discard and exit edit mode |
403
- | Arrow keys | Move focused cell (outside edit mode) |
404
+ | Arrow keys | Move focused cell (view mode only) |
405
+ | `Ctrl+Arrow` / `Meta+Arrow` | Passed through to the browser — never hijacked |
404
406
 
405
- If `processRowUpdate` throws, the cell reverts automatically to its original value.
407
+ > **Focus-scoped keyboard handling** — all grid keyboard shortcuts are active **only when focus is inside the grid**. Arrow keys, `Shift+Arrow`, `Ctrl+Arrow`, and clipboard shortcuts (`Ctrl+C` / `Ctrl+V`) in other inputs or text areas on the same page are never intercepted by the grid.
406
408
 
407
409
  ---
408
410
 
@@ -532,6 +534,77 @@ Set `calculateSubTotal: true` on any numeric column to display a live running su
532
534
 
533
535
  ---
534
536
 
537
+ ## Aggregation
538
+
539
+ Show a sticky footer row with computed aggregate values per column. Compatible with **MUI DataGrid Premium**'s `aggregationModel` API.
540
+
541
+ > **Feature flag** — disabled by default. Set `aggregable` to opt in.
542
+
543
+ ### Basic usage
544
+
545
+ ```tsx
546
+ <GMDatagrid
547
+ aggregable // enable the feature
548
+ aggregationModel={{ total: 'sum', price: 'avg', id: 'size' }}
549
+ columns={columns}
550
+ sourceApi={fetchData}
551
+ />
552
+ ```
553
+
554
+ ### Controlled model with change callback
555
+
556
+ ```tsx
557
+ const [aggModel, setAggModel] = useState({ total: 'sum' });
558
+
559
+ <GMDatagrid
560
+ aggregable
561
+ aggregationModel={aggModel}
562
+ onAggregationModelChange={setAggModel}
563
+ ...
564
+ />
565
+ ```
566
+
567
+ ### Uncontrolled initial state (MUI-compatible)
568
+
569
+ ```tsx
570
+ <GMDatagrid
571
+ aggregable
572
+ initialState={{ aggregation: { model: { total: 'sum', price: 'avg' } } }}
573
+ ...
574
+ />
575
+ ```
576
+
577
+ ### Per-column control
578
+
579
+ ```tsx
580
+ // Prevent a column from being aggregated
581
+ { field: 'code', type: ColumnDataTypesEnum.CODE, aggregable: false }
582
+
583
+ // Restrict which functions are offered in the column menu (future picker)
584
+ { field: 'price', type: ColumnDataTypesEnum.PRICE, availableAggregationFunctions: ['avg', 'min', 'max'] }
585
+ ```
586
+
587
+ ### Built-in aggregation functions
588
+
589
+ | Function | Symbol | Description |
590
+ |---|---|---|
591
+ | `sum` | Σ | Sum of all non-null values |
592
+ | `avg` | x̄ | Arithmetic mean of non-null values |
593
+ | `min` | min | Smallest value |
594
+ | `max` | max | Largest value |
595
+ | `size` | # | Count of non-null cells |
596
+
597
+ ### Aggregation props
598
+
599
+ | Prop | Type | Default | Description |
600
+ |---|---|---|---|
601
+ | `aggregable` | `boolean` | `false` | Enable the footer row |
602
+ | `aggregationModel` | `AggregationModel` | — | Controlled `{ field: fn }` map |
603
+ | `onAggregationModelChange` | `(model) => void` | — | Change callback |
604
+ | `initialState.aggregation.model` | `AggregationModel` | — | Uncontrolled seed |
605
+
606
+ ---
607
+
535
608
  ## TypeScript
536
609
 
537
610
  All types are exported from the package root:
@@ -0,0 +1,23 @@
1
+ import { default as React } from 'react';
2
+ import { AggregationFunction, AggregationModel, AggregationResults, ColumnDef } from '../types';
3
+ interface UseAggregationProps {
4
+ /** Grid root engine ref (same one used by useExport). */
5
+ engineRef: React.RefObject<unknown>;
6
+ columns: ColumnDef[];
7
+ /** { field: 'sum' | 'avg' | ... } — only entries here produce footer cells. */
8
+ aggregationModel: AggregationModel;
9
+ /** When false the hook is a no-op. */
10
+ enabled: boolean;
11
+ }
12
+ /**
13
+ * Computes aggregated footer values from the full filtered+sorted dataset.
14
+ * Runs entirely in TypeScript using `engine.get_all_rows()` — no Rust changes needed.
15
+ *
16
+ * Compatible with MUI DataGrid Premium aggregation model:
17
+ * aggregationModel = { gross: 'sum', year: 'max' }
18
+ * → returns { gross: 50212864155, year: 2021 }
19
+ */
20
+ export declare function useAggregation({ engineRef, columns, aggregationModel, enabled, }: UseAggregationProps): AggregationResults;
21
+ /** Human-readable label for an aggregation function (used in footer cells). */
22
+ export declare function aggFnLabel(fn: AggregationFunction): string;
23
+ export {};
@@ -1,3 +1,4 @@
1
+ import { default as React } from 'react';
1
2
  import { CellRange, ColumnDef, Row } from '../types';
2
3
  interface UseClipboardProps {
3
4
  range: CellRange | null;
@@ -6,7 +7,9 @@ interface UseClipboardProps {
6
7
  engine: any;
7
8
  engineReady: boolean;
8
9
  onPasted: () => void;
10
+ /** Grid root element — clipboard shortcuts only fire when focus is inside. */
11
+ gridRootRef: React.RefObject<HTMLDivElement | null>;
9
12
  }
10
13
  /** Serializes selected range to TSV for Ctrl+C; parses TSV from Ctrl+V. */
11
- export declare function useClipboard({ range, visibleRows, columns, engine, engineReady, onPasted, }: UseClipboardProps): void;
14
+ export declare function useClipboard({ range, visibleRows, columns, engine, engineReady, onPasted, gridRootRef, }: UseClipboardProps): void;
12
15
  export {};
@@ -7,7 +7,9 @@ interface UseKeyboardNavProps {
7
7
  stopEditing: (save: boolean) => Promise<void>;
8
8
  totalRows: number;
9
9
  columns: ColumnDef[];
10
- containerRef: React.RefObject<HTMLDivElement>;
10
+ /** Root element of the grid — the listener is scoped to this element so it
11
+ * never intercepts keystrokes when focus is outside the grid. */
12
+ gridRootRef: React.RefObject<HTMLDivElement | null>;
11
13
  }
12
14
  /**
13
15
  * Keyboard navigation contract (Excel-like):
@@ -18,6 +20,14 @@ interface UseKeyboardNavProps {
18
20
  * - Tab: save, move focus one column right
19
21
  * - Shift+Tab: save, move focus one column left
20
22
  * - Escape: discard, exit edit mode
23
+ *
24
+ * IMPORTANT: The listener attaches to the grid root element, NOT document.
25
+ * This means:
26
+ * - Arrow / Tab / Enter keys only fire grid navigation when focus is inside
27
+ * the grid — they never steal keystrokes from inputs, text selections, or
28
+ * other page widgets outside the grid.
29
+ * - Ctrl+Arrow, Shift+Arrow, and any browser-native text-selection shortcuts
30
+ * are passed through untouched when the event originates outside the grid.
21
31
  */
22
- export declare function useKeyboardNav({ focusedCell, setFocusedCell, editingCell, startEditing, stopEditing, totalRows, columns, }: UseKeyboardNavProps): void;
32
+ export declare function useKeyboardNav({ focusedCell, setFocusedCell, editingCell, startEditing, stopEditing, totalRows, columns, gridRootRef, }: UseKeyboardNavProps): void;
23
33
  export {};