@lenso/primitives 0.5.0 → 0.7.0

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.
@@ -0,0 +1,117 @@
1
+ import { type ColumnDef, type CellSelectionState, type RowData, type ReactTable, type SortingState, type TableOptions } from "@tanstack/react-table";
2
+ export type DataGridValue = string | number | boolean | null;
3
+ export type DataGridEditReason = "edit" | "paste" | "fill" | "programmatic";
4
+ export interface DataGridCellChange<TRow> {
5
+ rowId: string;
6
+ rowIndex: number;
7
+ columnId: string;
8
+ previousValue: DataGridValue;
9
+ value: DataGridValue;
10
+ previousRow: TRow;
11
+ row: TRow;
12
+ reason: DataGridEditReason;
13
+ }
14
+ export interface DataGridRowChange<TRow> {
15
+ rowId: string;
16
+ rowIndex: number;
17
+ previousRow: TRow;
18
+ row: TRow;
19
+ changes: readonly DataGridCellChange<TRow>[];
20
+ reason: DataGridEditReason;
21
+ }
22
+ export interface DataGridColumn<TRow extends RowData, TValidationData = unknown> {
23
+ id: string;
24
+ header: string;
25
+ getValue: (row: TRow) => DataGridValue;
26
+ setValue?: (row: TRow, value: DataGridValue) => TRow;
27
+ parse?: (input: string, row: TRow) => DataGridValue;
28
+ validate?: (value: DataGridValue, row: TRow, context: DataGridValidationContext<TRow, TValidationData>) => string | null;
29
+ editable?: boolean | ((row: TRow) => boolean);
30
+ sortable?: boolean;
31
+ width?: number;
32
+ minWidth?: number;
33
+ maxWidth?: number;
34
+ pinned?: boolean;
35
+ onCellEditComplete?: (change: DataGridCellChange<TRow>) => void;
36
+ tableColumn?: Omit<Partial<ColumnDef<typeof dataGridFeatures, TRow, unknown>>, "id" | "header" | "accessorFn">;
37
+ }
38
+ export interface DataGridEdit {
39
+ rowId: string;
40
+ columnId: string;
41
+ value: DataGridValue;
42
+ }
43
+ export interface DataGridCellEvent<TRow> {
44
+ rowId: string;
45
+ columnId: string;
46
+ onEditComplete: (change: DataGridCellChange<TRow>) => void;
47
+ }
48
+ export type DataGridCommitResult<TRow> = {
49
+ ok: true;
50
+ changes: readonly DataGridCellChange<TRow>[];
51
+ } | {
52
+ ok: false;
53
+ error: string;
54
+ edit: DataGridEdit;
55
+ };
56
+ export interface UseDataGridOptions<TRow extends RowData, TValidationData = unknown> {
57
+ rows: readonly TRow[];
58
+ columns: readonly DataGridColumn<TRow, TValidationData>[];
59
+ getRowId: (row: TRow) => string;
60
+ readOnly?: boolean;
61
+ rowSelection?: boolean;
62
+ cellSelection?: boolean;
63
+ sorting?: boolean;
64
+ onRowsChange?: (rows: TRow[], changes: readonly DataGridCellChange<TRow>[]) => void;
65
+ onCellEditComplete?: (change: DataGridCellChange<TRow>) => void;
66
+ cellEvents?: readonly DataGridCellEvent<TRow>[];
67
+ onRowEditComplete?: (change: DataGridRowChange<TRow>) => void;
68
+ onRowSelectionChange?: (rowIds: ReadonlySet<string>) => void;
69
+ onCellSelectionChange?: (ranges: CellSelectionState) => void;
70
+ onSortingChange?: (sorting: SortingState) => void;
71
+ validationData?: TValidationData;
72
+ tableOptions?: DataGridTableOptions<TRow>;
73
+ }
74
+ export declare const dataGridFeatures: {
75
+ cellSelectionFeature: import("@tanstack/react-table").TableFeature;
76
+ columnSizingFeature: import("@tanstack/react-table").TableFeature;
77
+ columnResizingFeature: import("@tanstack/react-table").TableFeature;
78
+ rowSelectionFeature: import("@tanstack/react-table").TableFeature;
79
+ rowSortingFeature: import("@tanstack/react-table").TableFeature;
80
+ sortedRowModel: (table: import("@tanstack/react-table").Table<any, any>) => () => import("@tanstack/react-table").RowModel<any, any>;
81
+ sortFns: {
82
+ alphanumeric: import("@tanstack/react-table").CreatedSortFn<any, any>;
83
+ basic: import("@tanstack/react-table").CreatedSortFn<any, any>;
84
+ datetime: import("@tanstack/react-table").CreatedSortFn<any, any>;
85
+ text: import("@tanstack/react-table").CreatedSortFn<any, any>;
86
+ };
87
+ };
88
+ export type DataGridTable<TRow extends RowData> = ReactTable<typeof dataGridFeatures, TRow>;
89
+ export type DataGridTableOptions<TRow extends RowData> = Partial<Omit<TableOptions<typeof dataGridFeatures, TRow>, "features" | "columns" | "data" | "getRowId">>;
90
+ export interface DataGridValidationContext<TRow extends RowData, TValidationData = unknown> {
91
+ rowId: string;
92
+ rowIndex: number;
93
+ columnId: string;
94
+ rows: readonly TRow[];
95
+ originalRows: readonly TRow[];
96
+ changes: readonly DataGridCellChange<TRow>[];
97
+ reason: DataGridEditReason;
98
+ data: TValidationData | undefined;
99
+ table: DataGridTable<TRow>;
100
+ }
101
+ export declare function useDataGrid<TRow extends RowData, TValidationData = unknown>({ rows, columns, getRowId, readOnly, rowSelection, cellSelection, sorting, onRowsChange, onCellEditComplete, cellEvents, onRowEditComplete, onRowSelectionChange, onCellSelectionChange, onSortingChange, validationData, tableOptions, }: UseDataGridOptions<TRow, TValidationData>): {
102
+ table: ReactTable<{
103
+ cellSelectionFeature: import("@tanstack/react-table").TableFeature;
104
+ columnSizingFeature: import("@tanstack/react-table").TableFeature;
105
+ columnResizingFeature: import("@tanstack/react-table").TableFeature;
106
+ rowSelectionFeature: import("@tanstack/react-table").TableFeature;
107
+ rowSortingFeature: import("@tanstack/react-table").TableFeature;
108
+ sortedRowModel: (table: import("@tanstack/react-table").Table<any, any>) => () => import("@tanstack/react-table").RowModel<any, any>;
109
+ sortFns: {
110
+ alphanumeric: import("@tanstack/react-table").CreatedSortFn<any, any>;
111
+ basic: import("@tanstack/react-table").CreatedSortFn<any, any>;
112
+ datetime: import("@tanstack/react-table").CreatedSortFn<any, any>;
113
+ text: import("@tanstack/react-table").CreatedSortFn<any, any>;
114
+ };
115
+ }, TRow, import("@tanstack/react-table").TableState_CellSelection & import("@tanstack/react-table").TableState_ColumnResizing & import("@tanstack/react-table").TableState_ColumnSizing & import("@tanstack/react-table").TableState_RowSelection & import("@tanstack/react-table").TableState_RowSorting>;
116
+ commitCells: (edits: readonly DataGridEdit[], reason?: DataGridEditReason) => DataGridCommitResult<TRow>;
117
+ };
@@ -0,0 +1,179 @@
1
+ "use client";
2
+ import * as React from "react";
3
+ import { cellSelectionFeature, columnResizingFeature, columnSizingFeature, createSortedRowModel, functionalUpdate, makeStateUpdater, rowSelectionFeature, rowSortingFeature, sortFn_alphanumeric, sortFn_basic, sortFn_datetime, sortFn_text, tableFeatures, useTable } from "@tanstack/react-table";
4
+ //#region src/data-grid/index.tsx
5
+ const dataGridFeatures = tableFeatures({
6
+ cellSelectionFeature,
7
+ columnSizingFeature,
8
+ columnResizingFeature,
9
+ rowSelectionFeature,
10
+ rowSortingFeature,
11
+ sortedRowModel: createSortedRowModel(),
12
+ sortFns: {
13
+ alphanumeric: sortFn_alphanumeric,
14
+ basic: sortFn_basic,
15
+ datetime: sortFn_datetime,
16
+ text: sortFn_text
17
+ }
18
+ });
19
+ function useDataGrid({ rows, columns, getRowId, readOnly = false, rowSelection = true, cellSelection = true, sorting = true, onRowsChange, onCellEditComplete, cellEvents, onRowEditComplete, onRowSelectionChange, onCellSelectionChange, onSortingChange, validationData, tableOptions }) {
20
+ const tableColumns = React.useMemo(() => columns.map((column) => ({
21
+ id: column.id,
22
+ header: column.header,
23
+ accessorFn: column.getValue,
24
+ enableSorting: sorting && column.sortable !== false,
25
+ enableCellSelection: cellSelection,
26
+ size: column.width ?? 180,
27
+ minSize: column.minWidth ?? 80,
28
+ maxSize: column.maxWidth ?? 1200,
29
+ ...column.tableColumn
30
+ })), [
31
+ cellSelection,
32
+ columns,
33
+ sorting
34
+ ]);
35
+ const table = useTable({
36
+ features: dataGridFeatures,
37
+ columns: tableColumns,
38
+ data: rows,
39
+ getRowId,
40
+ enableRowSelection: rowSelection,
41
+ enableCellSelection: cellSelection,
42
+ enableCellSelectionDrag: cellSelection,
43
+ autoResetCellSelection: false,
44
+ enableSorting: sorting,
45
+ ...tableOptions,
46
+ onRowSelectionChange: (updater) => {
47
+ const next = functionalUpdate(updater, table.state.rowSelection);
48
+ (tableOptions?.onRowSelectionChange ?? makeStateUpdater("rowSelection", table))(updater);
49
+ onRowSelectionChange?.(new Set(Object.keys(next).filter((id) => next[id])));
50
+ },
51
+ onCellSelectionChange: (updater) => {
52
+ const next = functionalUpdate(updater, table.state.cellSelection);
53
+ (tableOptions?.onCellSelectionChange ?? makeStateUpdater("cellSelection", table))(updater);
54
+ onCellSelectionChange?.(next);
55
+ },
56
+ onSortingChange: (updater) => {
57
+ const next = functionalUpdate(updater, table.state.sorting);
58
+ (tableOptions?.onSortingChange ?? makeStateUpdater("sorting", table))(updater);
59
+ onSortingChange?.(next);
60
+ }
61
+ });
62
+ return {
63
+ table,
64
+ commitCells: React.useCallback((edits, reason = "edit") => {
65
+ if (edits.length === 0) return {
66
+ ok: true,
67
+ changes: []
68
+ };
69
+ if (readOnly || !onRowsChange) return {
70
+ ok: false,
71
+ error: "This grid is read-only",
72
+ edit: edits[0]
73
+ };
74
+ const nextRows = [...rows];
75
+ const rowIndexById = new Map(rows.map((row, index) => [getRowId(row), index]));
76
+ const columnById = new Map(columns.map((column) => [column.id, column]));
77
+ const changes = [];
78
+ const uniqueEdits = /* @__PURE__ */ new Map();
79
+ for (const edit of edits) uniqueEdits.set(JSON.stringify([edit.rowId, edit.columnId]), edit);
80
+ for (const edit of uniqueEdits.values()) {
81
+ const rowIndex = rowIndexById.get(edit.rowId);
82
+ const column = columnById.get(edit.columnId);
83
+ if (rowIndex === void 0 || !column) return {
84
+ ok: false,
85
+ error: "Unknown row or column",
86
+ edit
87
+ };
88
+ const row = nextRows[rowIndex];
89
+ if (!(column.setValue && (typeof column.editable === "function" ? column.editable(row) : column.editable !== false))) return {
90
+ ok: false,
91
+ error: "This cell is read-only",
92
+ edit
93
+ };
94
+ const previousValue = column.getValue(row);
95
+ const value = edit.value;
96
+ if (Object.is(previousValue, value)) continue;
97
+ const updatedRow = column.setValue(row, value);
98
+ nextRows[rowIndex] = updatedRow;
99
+ changes.push({
100
+ rowId: edit.rowId,
101
+ rowIndex,
102
+ columnId: column.id,
103
+ previousValue,
104
+ value,
105
+ previousRow: row,
106
+ row: updatedRow,
107
+ reason
108
+ });
109
+ }
110
+ if (changes.length === 0) return {
111
+ ok: true,
112
+ changes
113
+ };
114
+ for (const change of changes) {
115
+ const validationError = columnById.get(change.columnId).validate?.(change.value, nextRows[change.rowIndex], {
116
+ rowId: change.rowId,
117
+ rowIndex: change.rowIndex,
118
+ columnId: change.columnId,
119
+ rows: nextRows,
120
+ originalRows: rows,
121
+ changes,
122
+ reason,
123
+ data: validationData,
124
+ table
125
+ });
126
+ if (validationError) return {
127
+ ok: false,
128
+ error: validationError,
129
+ edit: {
130
+ rowId: change.rowId,
131
+ columnId: change.columnId,
132
+ value: change.value
133
+ }
134
+ };
135
+ }
136
+ onRowsChange(nextRows, changes);
137
+ for (const change of changes) {
138
+ onCellEditComplete?.(change);
139
+ columnById.get(change.columnId)?.onCellEditComplete?.(change);
140
+ for (const event of cellEvents ?? []) if (event.rowId === change.rowId && event.columnId === change.columnId) event.onEditComplete(change);
141
+ }
142
+ const changedRows = /* @__PURE__ */ new Map();
143
+ for (const change of changes) {
144
+ const rowChanges = changedRows.get(change.rowId) ?? [];
145
+ rowChanges.push(change);
146
+ changedRows.set(change.rowId, rowChanges);
147
+ }
148
+ for (const [rowId, rowChanges] of changedRows) {
149
+ const first = rowChanges[0];
150
+ const last = rowChanges[rowChanges.length - 1];
151
+ onRowEditComplete?.({
152
+ rowId,
153
+ rowIndex: first.rowIndex,
154
+ previousRow: first.previousRow,
155
+ row: last.row,
156
+ changes: rowChanges,
157
+ reason
158
+ });
159
+ }
160
+ return {
161
+ ok: true,
162
+ changes
163
+ };
164
+ }, [
165
+ cellEvents,
166
+ columns,
167
+ getRowId,
168
+ onCellEditComplete,
169
+ onRowEditComplete,
170
+ onRowsChange,
171
+ readOnly,
172
+ rows,
173
+ table,
174
+ validationData
175
+ ])
176
+ };
177
+ }
178
+ //#endregion
179
+ export { dataGridFeatures, useDataGrid };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lenso/primitives",
3
- "version": "0.5.0",
3
+ "version": "0.7.0",
4
4
  "description": "Headless product primitives for Lenso UI.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -18,6 +18,10 @@
18
18
  "types": "./dist/resize-handle/index.d.ts",
19
19
  "import": "./dist/resize-handle/index.js"
20
20
  },
21
+ "./data-grid": {
22
+ "types": "./dist/data-grid/index.d.ts",
23
+ "import": "./dist/data-grid/index.js"
24
+ },
21
25
  "./sidebar": {
22
26
  "types": "./dist/sidebar/index.d.ts",
23
27
  "import": "./dist/sidebar/index.js"
@@ -26,6 +30,9 @@
26
30
  "publishConfig": {
27
31
  "access": "public"
28
32
  },
33
+ "dependencies": {
34
+ "@tanstack/react-table": "9.2.4"
35
+ },
29
36
  "devDependencies": {
30
37
  "@base-ui/react": "1.7.0",
31
38
  "@stylexjs/stylex": "0.19.0",