@macrostrat/data-sheet 3.0.4 → 3.1.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.
- package/CHANGELOG.md +15 -1
- package/dist/cell-renderer.cjs +162 -0
- package/dist/cell-renderer.cjs.map +1 -0
- package/dist/cell-renderer.d.ts +8 -0
- package/dist/cell-renderer.js +162 -0
- package/dist/cell-renderer.js.map +1 -0
- package/dist/core.cjs +32 -159
- package/dist/core.cjs.map +1 -1
- package/dist/core.d.ts +4 -9
- package/dist/core.js +33 -158
- package/dist/core.js.map +1 -1
- package/dist/data-sheet.css +9 -9
- package/dist/index.cjs +13 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +13 -7
- package/dist/index.js.map +1 -1
- package/dist/postgrest-table/index.cjs.map +1 -1
- package/dist/postgrest-table/index.d.ts +1 -1
- package/dist/postgrest-table/index.js.map +1 -1
- package/dist/provider.cjs +59 -339
- package/dist/provider.cjs.map +1 -1
- package/dist/provider.d.ts +11 -65
- package/dist/provider.js +58 -339
- package/dist/provider.js.map +1 -1
- package/dist/types.d.ts +54 -0
- package/dist/utils/key-handling.cjs +26 -1
- package/dist/utils/key-handling.cjs.map +1 -1
- package/dist/utils/key-handling.d.ts +3 -2
- package/dist/utils/key-handling.js +27 -2
- package/dist/utils/key-handling.js.map +1 -1
- package/dist/zustand-store.cjs +320 -0
- package/dist/zustand-store.cjs.map +1 -0
- package/dist/zustand-store.d.ts +6 -0
- package/dist/zustand-store.js +318 -0
- package/dist/zustand-store.js.map +1 -0
- package/package.json +5 -4
- package/src/cell-renderer.ts +217 -0
- package/src/core.ts +52 -212
- package/src/index.ts +3 -0
- package/src/postgrest-table/index.ts +1 -1
- package/src/provider.ts +66 -486
- package/src/types.ts +66 -0
- package/src/utils/key-handling.ts +31 -5
- package/src/zustand-store.ts +377 -0
package/src/core.ts
CHANGED
|
@@ -5,22 +5,30 @@ import {
|
|
|
5
5
|
InputGroup,
|
|
6
6
|
Intent,
|
|
7
7
|
} from "@blueprintjs/core";
|
|
8
|
-
import {
|
|
8
|
+
import {
|
|
9
|
+
Column,
|
|
10
|
+
Region,
|
|
11
|
+
RegionCardinality,
|
|
12
|
+
RowHeaderCell,
|
|
13
|
+
Table,
|
|
14
|
+
TableProps,
|
|
15
|
+
} from "@blueprintjs/table";
|
|
9
16
|
import "@blueprintjs/table/lib/css/table.css";
|
|
10
17
|
import update from "immutability-helper";
|
|
11
18
|
import { ReactNode, useCallback, useEffect, useMemo, useState } from "react";
|
|
12
|
-
import {
|
|
19
|
+
import { DataSheetAction } from "./components";
|
|
13
20
|
import h from "./main.module.sass";
|
|
14
21
|
import {
|
|
15
22
|
DataSheetProvider,
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
singleFocusedCell,
|
|
23
|
+
storeAtom,
|
|
24
|
+
useAtomValue,
|
|
19
25
|
useSelector,
|
|
20
26
|
useStoreAPI,
|
|
21
|
-
|
|
27
|
+
atom,
|
|
22
28
|
} from "./provider";
|
|
23
|
-
import {
|
|
29
|
+
import { DataSheetProviderProps, VisibleCells } from "./types.ts";
|
|
30
|
+
import { basicCellRenderer } from "./cell-renderer.ts";
|
|
31
|
+
import { tableKeyHandlerAtom } from "./utils";
|
|
24
32
|
|
|
25
33
|
// More on component templates: https://storybook.js.org/docs/react/writing-stories/introduction#using-args
|
|
26
34
|
|
|
@@ -33,7 +41,7 @@ export enum DataSheetDensity {
|
|
|
33
41
|
LOW = "low",
|
|
34
42
|
}
|
|
35
43
|
|
|
36
|
-
interface DataSheetInternalProps<T> {
|
|
44
|
+
interface DataSheetInternalProps<T> extends TableProps {
|
|
37
45
|
onVisibleCellsChange?: (visibleCells: VisibleCells) => void;
|
|
38
46
|
onSaveData?: (updatedData: any[], data: T[]) => void;
|
|
39
47
|
onUpdateData?: (updatedData: any[], data: T[]) => void;
|
|
@@ -68,7 +76,7 @@ export function DataSheet<T>(props: DataSheetProps<T>) {
|
|
|
68
76
|
{
|
|
69
77
|
data,
|
|
70
78
|
columnSpec,
|
|
71
|
-
columnSpecOptions
|
|
79
|
+
columnSpecOptions,
|
|
72
80
|
enableColumnReordering,
|
|
73
81
|
defaultColumnWidth,
|
|
74
82
|
editable,
|
|
@@ -91,7 +99,6 @@ const deletedRowHeaderStyle = {
|
|
|
91
99
|
|
|
92
100
|
function _DataSheet<T>({
|
|
93
101
|
onVisibleCellsChange,
|
|
94
|
-
enableColumnReordering,
|
|
95
102
|
onSaveData,
|
|
96
103
|
onUpdateData,
|
|
97
104
|
onDeleteRows,
|
|
@@ -100,6 +107,8 @@ function _DataSheet<T>({
|
|
|
100
107
|
enableFocusedCell,
|
|
101
108
|
autoFocusEditor = true,
|
|
102
109
|
density = DataSheetDensity.HIGH,
|
|
110
|
+
selectionModes,
|
|
111
|
+
...rest
|
|
103
112
|
}: DataSheetInternalProps<T>) {
|
|
104
113
|
/**
|
|
105
114
|
* @param data: The data to be displayed in the table
|
|
@@ -183,7 +192,7 @@ function _DataSheet<T>({
|
|
|
183
192
|
[selectedRegions],
|
|
184
193
|
);
|
|
185
194
|
|
|
186
|
-
const columnWidths =
|
|
195
|
+
const columnWidths = useAtomValue(columnWidthsAtom);
|
|
187
196
|
|
|
188
197
|
const numRows = Math.max(updatedData.length, data.length);
|
|
189
198
|
|
|
@@ -247,7 +256,17 @@ function _DataSheet<T>({
|
|
|
247
256
|
[deletedRows],
|
|
248
257
|
);
|
|
249
258
|
|
|
250
|
-
const onKeyDown =
|
|
259
|
+
const onKeyDown = useAtomValue(tableKeyHandlerAtom);
|
|
260
|
+
|
|
261
|
+
let _selectionModes = selectionModes;
|
|
262
|
+
if (
|
|
263
|
+
editable &&
|
|
264
|
+
_selectionModes != null &&
|
|
265
|
+
!_selectionModes.includes(RegionCardinality.CELLS)
|
|
266
|
+
) {
|
|
267
|
+
_selectionModes = [..._selectionModes, RegionCardinality.CELLS];
|
|
268
|
+
// Ensure selection mode includes "cells"
|
|
269
|
+
}
|
|
251
270
|
|
|
252
271
|
return h("div.data-sheet-container", { className, style }, [
|
|
253
272
|
h.if(editable)(DataSheetEditToolbar, {
|
|
@@ -265,7 +284,6 @@ function _DataSheet<T>({
|
|
|
265
284
|
numRows,
|
|
266
285
|
className: "data-sheet",
|
|
267
286
|
enableFocusedCell,
|
|
268
|
-
enableColumnReordering,
|
|
269
287
|
onColumnsReordered,
|
|
270
288
|
focusedCell,
|
|
271
289
|
selectedRegions,
|
|
@@ -274,7 +292,6 @@ function _DataSheet<T>({
|
|
|
274
292
|
columnWidths,
|
|
275
293
|
onColumnWidthChanged,
|
|
276
294
|
onSelection,
|
|
277
|
-
renderMode: "batch",
|
|
278
295
|
enableRowReordering: false,
|
|
279
296
|
enableRowResizing: false,
|
|
280
297
|
// The cell renderer is memoized internally based on these data dependencies
|
|
@@ -287,6 +304,8 @@ function _DataSheet<T>({
|
|
|
287
304
|
],
|
|
288
305
|
onVisibleCellsChange: _onVisibleCellsChange,
|
|
289
306
|
rowHeaderCellRenderer,
|
|
307
|
+
selectionModes: _selectionModes,
|
|
308
|
+
...rest,
|
|
290
309
|
},
|
|
291
310
|
children,
|
|
292
311
|
),
|
|
@@ -294,200 +313,34 @@ function _DataSheet<T>({
|
|
|
294
313
|
]);
|
|
295
314
|
}
|
|
296
315
|
|
|
297
|
-
|
|
298
|
-
rowIndex: number,
|
|
299
|
-
colIndex: number,
|
|
300
|
-
columnSpec: ColumnSpec,
|
|
301
|
-
state: DataSheetStore<T>,
|
|
302
|
-
autoFocusEditor = true,
|
|
303
|
-
): any {
|
|
304
|
-
const data = state.data;
|
|
305
|
-
const updatedData = state.updatedData;
|
|
306
|
-
|
|
307
|
-
const isDeleted = state.deletedRows.has(rowIndex);
|
|
308
|
-
|
|
309
|
-
const row = data[rowIndex] ?? updatedData[rowIndex];
|
|
310
|
-
const loading = row == null;
|
|
311
|
-
const col = columnSpec;
|
|
312
|
-
|
|
313
|
-
const _topLeftCell = state.topLeftCell;
|
|
314
|
-
const onCellEdited = state.onCellEdited;
|
|
315
|
-
|
|
316
|
-
const value = updatedData[rowIndex]?.[col.key] ?? data[rowIndex]?.[col.key];
|
|
317
|
-
const _renderedValue = col.valueRenderer?.(value) ?? value;
|
|
318
|
-
|
|
319
|
-
let style = col.style ?? {};
|
|
320
|
-
if (isDeleted) {
|
|
321
|
-
style = {
|
|
322
|
-
...style,
|
|
323
|
-
opacity: 0.5,
|
|
324
|
-
textDecoration: "line-through",
|
|
325
|
-
};
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
//const focused =
|
|
329
|
-
// focusedCell?.col === colIndex && focusedCell?.row === rowIndex;
|
|
330
|
-
|
|
331
|
-
const editable = (col.editable ?? state.editable) && !isDeleted;
|
|
332
|
-
|
|
333
|
-
// Top left cell of a ranged selection
|
|
334
|
-
const topLeft =
|
|
335
|
-
_topLeftCell?.col === colIndex && _topLeftCell?.row === rowIndex;
|
|
336
|
-
|
|
337
|
-
if (!editable && state.editable) {
|
|
338
|
-
// If the cell is not editable but the sheet is editable, we want to differentiate
|
|
339
|
-
// between editable and non-editable cells.
|
|
340
|
-
style.color = "var(--secondary-color)";
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
const edited = updatedData[rowIndex]?.[col.key] != null;
|
|
344
|
-
let intent = edited ? "success" : undefined;
|
|
345
|
-
if (isDeleted) {
|
|
346
|
-
intent = "danger";
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
const _Cell = col.cellComponent ?? BaseCell;
|
|
350
|
-
|
|
351
|
-
let inlineEditor = editable ? (col.inlineEditor ?? true) : false;
|
|
352
|
-
|
|
353
|
-
if (!topLeft) {
|
|
354
|
-
// This should be the case for every cell except the focused one
|
|
355
|
-
return h(
|
|
356
|
-
_Cell,
|
|
357
|
-
{
|
|
358
|
-
intent,
|
|
359
|
-
loading,
|
|
360
|
-
value,
|
|
361
|
-
style,
|
|
362
|
-
isDeleted,
|
|
363
|
-
},
|
|
364
|
-
_renderedValue,
|
|
365
|
-
);
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
// The rest is for the top-left cell of a selection or the focused cell
|
|
369
|
-
|
|
370
|
-
// Hidden input to capture key events
|
|
371
|
-
let hiddenInput = h("input.hidden-input", {
|
|
372
|
-
autoFocus: true,
|
|
373
|
-
onKeyDown: state.editorKeyHandler,
|
|
374
|
-
});
|
|
375
|
-
|
|
376
|
-
let cellContents: ReactNode = _renderedValue;
|
|
377
|
-
|
|
378
|
-
let _dataEditor = null;
|
|
379
|
-
let className = null;
|
|
380
|
-
|
|
381
|
-
if (col.dataEditor != null) {
|
|
382
|
-
_dataEditor = h(
|
|
383
|
-
EditorPopup,
|
|
384
|
-
{
|
|
385
|
-
autoFocus: autoFocusEditor,
|
|
386
|
-
valueViewer: _renderedValue,
|
|
387
|
-
},
|
|
388
|
-
[
|
|
389
|
-
h(col.dataEditor, {
|
|
390
|
-
value,
|
|
391
|
-
editable,
|
|
392
|
-
onChange(value) {
|
|
393
|
-
if (!editable) return;
|
|
394
|
-
state.onSelectionEdited(value);
|
|
395
|
-
},
|
|
396
|
-
}),
|
|
397
|
-
],
|
|
398
|
-
);
|
|
399
|
-
}
|
|
400
|
-
|
|
401
|
-
if (!editable) {
|
|
402
|
-
// Most cells are not focused and don't need to be editable.
|
|
403
|
-
// This will be the rendering logic for almost all cells
|
|
404
|
-
|
|
405
|
-
if (_dataEditor != null) {
|
|
406
|
-
cellContents = _dataEditor;
|
|
407
|
-
}
|
|
408
|
-
|
|
409
|
-
return h(
|
|
410
|
-
_Cell,
|
|
411
|
-
{
|
|
412
|
-
intent,
|
|
413
|
-
value,
|
|
414
|
-
style,
|
|
415
|
-
},
|
|
416
|
-
cellContents,
|
|
417
|
-
);
|
|
418
|
-
// Could probably put the hidden input elsewhere,
|
|
419
|
-
}
|
|
420
|
-
|
|
421
|
-
/* The remaining logic covers cells that are focused and editable */
|
|
316
|
+
/** Atoms for efficient sub-selection of state */
|
|
422
317
|
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
if (value === e.target.value) return;
|
|
426
|
-
onCellEdited(rowIndex, col.key, e.target.value);
|
|
427
|
-
};
|
|
318
|
+
const deletedRowsAtom = atom((get) => get(storeAtom).deletedRows);
|
|
319
|
+
const updatedDataAtom = atom((get) => get(storeAtom).updatedData);
|
|
428
320
|
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
typeof _renderedValue === "string" ||
|
|
436
|
-
typeof _renderedValue === "number" ||
|
|
437
|
-
_renderedValue == null
|
|
438
|
-
) {
|
|
439
|
-
_value = _renderedValue;
|
|
440
|
-
}
|
|
441
|
-
_inlineEditor = h("input.main-editor", {
|
|
442
|
-
value: _value ?? "",
|
|
443
|
-
autoFocus: autoFocusEditor,
|
|
444
|
-
onChange,
|
|
445
|
-
onKeyDown: state.editorKeyHandler,
|
|
446
|
-
});
|
|
447
|
-
} else {
|
|
448
|
-
// If inlineEditor is a ReactNode, we use it directly
|
|
449
|
-
_inlineEditor = inlineEditor as ReactNode;
|
|
450
|
-
}
|
|
321
|
+
const hasUpdatesAtom = atom((get) => {
|
|
322
|
+
// Readable atom to indicate whether there are any updates in the updatedData array
|
|
323
|
+
const deletedRows = get(deletedRowsAtom);
|
|
324
|
+
const updatedData = get(updatedDataAtom);
|
|
325
|
+
return updatedData.length > 0 || deletedRows.size > 0;
|
|
326
|
+
});
|
|
451
327
|
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
hiddenInput = null;
|
|
456
|
-
} else if (_inlineEditor != null) {
|
|
457
|
-
cellContents = _inlineEditor;
|
|
458
|
-
className = "input-cell";
|
|
459
|
-
hiddenInput = null;
|
|
460
|
-
}
|
|
328
|
+
const columnSpecAtom = atom((get) => get(storeAtom).columnSpec);
|
|
329
|
+
const columnWidthsIndexAtom = atom((get) => get(storeAtom).columnWidthsIndex);
|
|
330
|
+
const defaultColumnWidthAtom = atom((get) => get(storeAtom).defaultColumnWidth);
|
|
461
331
|
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
intent,
|
|
467
|
-
value,
|
|
468
|
-
className,
|
|
469
|
-
style,
|
|
470
|
-
//truncated: false,
|
|
471
|
-
},
|
|
472
|
-
[
|
|
473
|
-
cellContents,
|
|
474
|
-
h.if(editable && isSingleCellSelection)(DragHandle),
|
|
475
|
-
hiddenInput,
|
|
476
|
-
],
|
|
332
|
+
const columnWidthsAtom = atom((get) => {
|
|
333
|
+
const ix = get(columnWidthsIndexAtom);
|
|
334
|
+
return get(columnSpecAtom).map(
|
|
335
|
+
(col) => ix.get(col.key) ?? col.width ?? get(defaultColumnWidthAtom),
|
|
477
336
|
);
|
|
478
|
-
}
|
|
479
|
-
|
|
480
|
-
function DragHandle() {
|
|
481
|
-
// TODO: we might want to drag multiple columns in some cases
|
|
482
|
-
// This should be on the last cell of a selection
|
|
483
|
-
const onMouseDown = useSelector((state) => state.onDragValue);
|
|
484
|
-
return h("div.corner-drag-handle", { onMouseDown });
|
|
485
|
-
}
|
|
337
|
+
});
|
|
486
338
|
|
|
487
339
|
function DataSheetEditToolbar({ onSaveData, onDeleteRows }) {
|
|
488
340
|
const selection = useSelector((state) => state.selection);
|
|
489
341
|
const resetChanges = useSelector((state) => state.resetChanges);
|
|
490
|
-
|
|
342
|
+
|
|
343
|
+
const hasUpdates = useAtomValue(hasUpdatesAtom);
|
|
491
344
|
|
|
492
345
|
return h("div.data-sheet-toolbar", [
|
|
493
346
|
h(ButtonGroup, { minimal: true }, [
|
|
@@ -568,19 +421,6 @@ function AddRowButton() {
|
|
|
568
421
|
);
|
|
569
422
|
}
|
|
570
423
|
|
|
571
|
-
export function BaseCell({ children, value, ...rest }) {
|
|
572
|
-
return h(
|
|
573
|
-
Cell,
|
|
574
|
-
{
|
|
575
|
-
interactive: true,
|
|
576
|
-
...rest,
|
|
577
|
-
},
|
|
578
|
-
children,
|
|
579
|
-
);
|
|
580
|
-
}
|
|
581
|
-
|
|
582
|
-
export { Cell };
|
|
583
|
-
|
|
584
424
|
export function getRowsToDelete(selection) {
|
|
585
425
|
let rowIndices: number[] = [];
|
|
586
426
|
for (const sel of selection) {
|
package/src/index.ts
CHANGED
|
@@ -16,7 +16,6 @@ import {
|
|
|
16
16
|
useToaster,
|
|
17
17
|
} from "@macrostrat/ui-components";
|
|
18
18
|
import { Spec } from "immutability-helper";
|
|
19
|
-
import { DataSheetProviderProps } from "../provider";
|
|
20
19
|
import { PostgrestFilterBuilder } from "@supabase/postgrest-js";
|
|
21
20
|
import type {
|
|
22
21
|
GenericFunction,
|
|
@@ -24,6 +23,7 @@ import type {
|
|
|
24
23
|
GenericView,
|
|
25
24
|
} from "@supabase/postgrest-js/dist/cjs/index";
|
|
26
25
|
import { ColorCell } from "../components";
|
|
26
|
+
import { DataSheetProviderProps } from "../types.ts";
|
|
27
27
|
|
|
28
28
|
const h = hyper.styled(styles);
|
|
29
29
|
|