gm-datagrid 0.1.11 → 0.1.13

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
@@ -508,6 +508,7 @@ import type {
508
508
  GMGridContainerRef,
509
509
  Row,
510
510
  CellRenderParams,
511
+ CellEditParams,
511
512
  FilterGroup,
512
513
  FilterCellValue,
513
514
  DateFilterValue,
@@ -525,6 +526,88 @@ import { ColumnDataTypesEnum, isRangeValue, isDateFilterValue } from 'gm-datagri
525
526
 
526
527
  ---
527
528
 
529
+ ## Editing
530
+
531
+ ### Feature flags
532
+
533
+ | Prop | Type | Default | Description |
534
+ |---|---|---|---|
535
+ | `editable` | `boolean` | `false` | Enable / disable inline editing globally |
536
+ | `editableColumns` | `string[]` | — | Only these fields are editable (overrides per-column `editable` flag) |
537
+
538
+ ### Per-column editability
539
+
540
+ Set `editable: true` on each `ColumnDef` you want users to edit. The global `editable` prop and `editableColumns` whitelist take precedence.
541
+
542
+ ### Editing events (MUI DataGrid compatible)
543
+
544
+ ```tsx
545
+ import type { CellEditParams } from 'gm-datagrid'
546
+
547
+ <GMDatagrid
548
+ title="Products"
549
+ columns={columns}
550
+ sourceApi={fetchProducts}
551
+ editable
552
+ editableColumns={['name', 'price', 'stock']}
553
+ processRowUpdate={async (newRow, oldRow) => {
554
+ await api.patch(`/products/${newRow.id}`, newRow)
555
+ return newRow
556
+ }}
557
+ onCellEditStart={(params: CellEditParams) => {
558
+ console.log('editing started', params.field, params.value)
559
+ }}
560
+ onCellEditStop={(params: CellEditParams) => {
561
+ console.log('editing stopped', params.field)
562
+ }}
563
+ isCellEditable={(params: CellEditParams) => {
564
+ // Only allow editing rows that are in DRAFT status
565
+ return params.row.status === 'DRAFT'
566
+ }}
567
+ />
568
+ ```
569
+
570
+ ### Programmatic editing via `ref`
571
+
572
+ ```tsx
573
+ import { useRef } from 'react'
574
+ import { GMDatagrid } from 'gm-datagrid'
575
+ import type { GMDatagridRef } from 'gm-datagrid'
576
+
577
+ const ref = useRef<GMDatagridRef>(null)
578
+
579
+ // Start editing a specific cell
580
+ ref.current?.startCellEditMode({ id: 'row-1', field: 'name' })
581
+
582
+ // Save and exit
583
+ ref.current?.stopCellEditMode({ id: 'row-1', field: 'name' })
584
+
585
+ // Discard changes and exit
586
+ ref.current?.stopCellEditMode({ id: 'row-1', field: 'name', ignoreModifications: true })
587
+ ```
588
+
589
+ ### `CellEditParams` interface
590
+
591
+ ```ts
592
+ interface CellEditParams {
593
+ id: string | number // row id
594
+ field: string // column field name
595
+ value: unknown // current cell value
596
+ row: Row // full row object
597
+ }
598
+ ```
599
+
600
+ ### Editability hierarchy
601
+
602
+ All four levels must pass for a cell to be editable:
603
+
604
+ 1. `editable` prop — global feature flag (`true` by default)
605
+ 2. `editableColumns` whitelist — if set, field must be in the list
606
+ 3. `col.editable` — per-column flag in `ColumnDef`
607
+ 4. `isCellEditable(params)` callback — per-row guard
608
+
609
+ ---
610
+
528
611
  ## Internationalisation (`locale`)
529
612
 
530
613
  All built-in toolbar and filter labels are translated automatically when you set the `locale` prop.
@@ -1,4 +1,4 @@
1
- import { CellCoord, ColumnDef, Row } from '../types';
1
+ import { CellCoord, CellEditParams, ColumnDef, Row } from '../types';
2
2
  interface UseInlineEditProps {
3
3
  visibleRows: Row[];
4
4
  columns: ColumnDef[];
@@ -6,6 +6,16 @@ interface UseInlineEditProps {
6
6
  engineReady: boolean;
7
7
  processRowUpdate?: (newRow: Row, oldRow: Row) => Row | Promise<Row>;
8
8
  onCellError: (rowId: string, message: string) => void;
9
+ /** Fired when a cell enters edit mode (MUI DataGrid compatible). */
10
+ onCellEditStart?: (params: CellEditParams) => void;
11
+ /** Fired when a cell exits edit mode (MUI DataGrid compatible). */
12
+ onCellEditStop?: (params: CellEditParams) => void;
13
+ /** Per-cell editability guard — return false to block editing. */
14
+ isCellEditable?: (params: CellEditParams) => boolean;
15
+ /** Global editable flag — when false, no cell can be edited. */
16
+ editableFlag?: boolean;
17
+ /** Whitelist of editable fields — overrides per-column editable flag when set. */
18
+ editableSet?: Set<string> | null;
9
19
  }
10
20
  interface InlineEditState {
11
21
  editingCell: CellCoord | null;
@@ -17,8 +27,19 @@ interface InlineEditState {
17
27
  }
18
28
  /**
19
29
  * Manages Excel-like inline cell editing.
30
+ *
31
+ * Fires MUI DataGrid-compatible events:
32
+ * onCellEditStart({ id, field, value, row }) — when a cell enters edit mode
33
+ * onCellEditStop({ id, field, value, row }) — when a cell exits edit mode
34
+ *
35
+ * Editability hierarchy (all must pass):
36
+ * 1. editableFlag (global feature flag, default false)
37
+ * 2. editableSet (column whitelist from editableColumns prop)
38
+ * 3. col.editable (per-column ColumnDef flag)
39
+ * 4. isCellEditable(params) (per-row/cell callback)
40
+ *
20
41
  * On save: calls processRowUpdate (async), reverts on rejection.
21
42
  * On escape: discards changes.
22
43
  */
23
- export declare function useInlineEdit({ visibleRows, columns, engine, engineReady, processRowUpdate, onCellError, }: UseInlineEditProps): InlineEditState;
44
+ export declare function useInlineEdit({ visibleRows, columns, engine, engineReady, processRowUpdate, onCellError, onCellEditStart, onCellEditStop, isCellEditable, editableFlag, editableSet, }: UseInlineEditProps): InlineEditState;
24
45
  export {};