@macrostrat/data-sheet 2.2.1 → 2.2.3

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.
Files changed (96) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/dist/esm/{data-sheet.e09e2546.js → data-sheet.00353511.js} +9 -8
  3. package/dist/esm/data-sheet.00353511.js.map +1 -0
  4. package/dist/esm/data-sheet.186dcac8.js +86 -0
  5. package/dist/esm/data-sheet.186dcac8.js.map +1 -0
  6. package/dist/esm/data-sheet.410c5122.js.map +1 -1
  7. package/dist/esm/{data-sheet.1d6ac6c6.js → data-sheet.6776e250.js} +11 -23
  8. package/dist/esm/data-sheet.6776e250.js.map +1 -0
  9. package/dist/esm/data-sheet.8dbfccef.js +16 -0
  10. package/dist/esm/data-sheet.8dbfccef.js.map +1 -0
  11. package/dist/esm/{data-sheet.b5946d6b.js → data-sheet.96446cae.js} +5 -26
  12. package/dist/esm/data-sheet.96446cae.js.map +1 -0
  13. package/dist/esm/{data-sheet.ce2a3218.js → data-sheet.9b593da4.js} +4 -4
  14. package/dist/esm/{data-sheet.ce2a3218.js.map → data-sheet.9b593da4.js.map} +1 -1
  15. package/dist/esm/{data-sheet.2521a9d7.js → data-sheet.ba9a1241.js} +101 -125
  16. package/dist/esm/data-sheet.ba9a1241.js.map +1 -0
  17. package/dist/esm/{data-sheet.b4098761.js → data-sheet.c9b6b6f2.js} +66 -8
  18. package/dist/esm/data-sheet.c9b6b6f2.js.map +1 -0
  19. package/dist/esm/{data-sheet.aa2b4902.js → data-sheet.d2160988.js} +2 -1
  20. package/dist/esm/data-sheet.d2160988.js.map +1 -0
  21. package/dist/esm/index.d.ts +67 -40
  22. package/dist/esm/index.d.ts.map +1 -1
  23. package/dist/esm/index.js +7 -5
  24. package/dist/esm/index.js.map +1 -1
  25. package/dist/node/{data-sheet.22cde343.js → data-sheet.2a613259.js} +2 -2
  26. package/dist/node/data-sheet.2a613259.js.map +1 -0
  27. package/dist/node/{data-sheet.846d5f8f.js → data-sheet.648db7af.js} +2 -2
  28. package/dist/node/data-sheet.648db7af.js.map +1 -0
  29. package/dist/node/data-sheet.6a663031.js +2 -0
  30. package/dist/node/data-sheet.6a663031.js.map +1 -0
  31. package/dist/node/data-sheet.809cdb29.js +2 -0
  32. package/dist/node/data-sheet.809cdb29.js.map +1 -0
  33. package/dist/node/data-sheet.97ac1b50.js +2 -0
  34. package/dist/node/data-sheet.97ac1b50.js.map +1 -0
  35. package/dist/node/data-sheet.ae543670.js +2 -0
  36. package/dist/node/data-sheet.ae543670.js.map +1 -0
  37. package/dist/node/data-sheet.bb45401c.js +2 -0
  38. package/dist/node/data-sheet.bb45401c.js.map +1 -0
  39. package/dist/node/data-sheet.c350336f.js +2 -0
  40. package/dist/node/data-sheet.c350336f.js.map +1 -0
  41. package/dist/node/data-sheet.d971095a.js +2 -0
  42. package/dist/node/data-sheet.d971095a.js.map +1 -0
  43. package/dist/node/data-sheet.dffdda71.js +2 -0
  44. package/dist/node/data-sheet.dffdda71.js.map +1 -0
  45. package/dist/node/index.js +1 -1
  46. package/dist/node/index.js.map +1 -1
  47. package/package.json +2 -2
  48. package/src/components/editor-popup.ts +11 -24
  49. package/src/components/text-areas.ts +2 -43
  50. package/src/core.ts +132 -148
  51. package/src/index.ts +1 -0
  52. package/src/postgrest-table/data-loaders.ts +1 -0
  53. package/src/postgrest-table/index.ts +16 -13
  54. package/src/provider.ts +87 -35
  55. package/src/utils/column-spec.ts +25 -1
  56. package/src/utils/index.ts +2 -1
  57. package/src/utils/key-handling.ts +132 -0
  58. package/dist/esm/data-sheet.0adfe599.js +0 -14
  59. package/dist/esm/data-sheet.0adfe599.js.map +0 -1
  60. package/dist/esm/data-sheet.1d6ac6c6.js.map +0 -1
  61. package/dist/esm/data-sheet.2521a9d7.js.map +0 -1
  62. package/dist/esm/data-sheet.aa2b4902.js.map +0 -1
  63. package/dist/esm/data-sheet.b4098761.js.map +0 -1
  64. package/dist/esm/data-sheet.b5946d6b.js.map +0 -1
  65. package/dist/esm/data-sheet.e09e2546.js.map +0 -1
  66. package/dist/node/data-sheet.1a487c3c.js +0 -2
  67. package/dist/node/data-sheet.1a487c3c.js.map +0 -1
  68. package/dist/node/data-sheet.22cde343.js.map +0 -1
  69. package/dist/node/data-sheet.50d0742f.js +0 -2
  70. package/dist/node/data-sheet.50d0742f.js.map +0 -1
  71. package/dist/node/data-sheet.52753d1d.js +0 -2
  72. package/dist/node/data-sheet.52753d1d.js.map +0 -1
  73. package/dist/node/data-sheet.7bbad640.js +0 -2
  74. package/dist/node/data-sheet.7bbad640.js.map +0 -1
  75. package/dist/node/data-sheet.820e5bc3.js +0 -2
  76. package/dist/node/data-sheet.820e5bc3.js.map +0 -1
  77. package/dist/node/data-sheet.846d5f8f.js.map +0 -1
  78. package/dist/node/data-sheet.86550c41.js +0 -2
  79. package/dist/node/data-sheet.86550c41.js.map +0 -1
  80. package/dist/node/data-sheet.a65463ef.js +0 -2
  81. package/dist/node/data-sheet.a65463ef.js.map +0 -1
  82. package/src/__archive/base.ts +0 -56
  83. package/src/__archive/components/column-utils.ts +0 -126
  84. package/src/__archive/components/index.ts +0 -115
  85. package/src/__archive/components/toolbar.ts +0 -79
  86. package/src/__archive/editors/index.ts +0 -78
  87. package/src/__archive/editors/styles.module.scss +0 -10
  88. package/src/__archive/enhancers.ts +0 -36
  89. package/src/__archive/index.ts +0 -8
  90. package/src/__archive/main.ts +0 -180
  91. package/src/__archive/provider.ts +0 -179
  92. package/src/__archive/styles.module.scss +0 -56
  93. package/src/__archive/test-sheet/attitude-sheet.old.styl +0 -53
  94. package/src/__archive/test-sheet/attitude-sheet.scss +0 -60
  95. package/src/__archive/test-sheet/index.ts +0 -210
  96. package/src/__archive/virtualized.ts +0 -82
@@ -1,56 +0,0 @@
1
- import ReactDataSheet from "react-datasheet";
2
- import "react-datasheet/lib/react-datasheet.css";
3
- import h from "@macrostrat/hyper";
4
- import { forwardRef } from "react";
5
- import { useDataSheet } from "./provider";
6
- import { Row, Sheet } from "./components";
7
- import React from "react";
8
-
9
- interface GridElement extends ReactDataSheet.Cell<GridElement, number> {
10
- value: number | null;
11
- }
12
-
13
- interface SheetContainerProps<
14
- T = any,
15
- > extends ReactDataSheet.DataSheetProps<T> {
16
- height?: number;
17
- width?: number;
18
- children?: React.ReactNode;
19
- className?: string;
20
- scrollable?: boolean;
21
- }
22
-
23
- export const BaseSheet = forwardRef((props: SheetContainerProps, ref) => {
24
- const {
25
- scrollable = true,
26
- height,
27
- width,
28
- children,
29
- className,
30
- ...rest
31
- } = props;
32
-
33
- const { dispatch } = useDataSheet();
34
-
35
- return h(
36
- "div.data-sheet-container",
37
- { style: { height, width }, className, ref },
38
- [
39
- h("div.ui", { style: { height, width } }, [
40
- h(ReactDataSheet, {
41
- width,
42
- height,
43
- sheetRenderer: Sheet,
44
- rowRenderer: Row,
45
- onSelect(sel) {
46
- dispatch({ type: "set-selection", value: sel });
47
- },
48
- ...rest,
49
- }),
50
- ]),
51
- children,
52
- ],
53
- );
54
- });
55
-
56
- export { GridElement };
@@ -1,126 +0,0 @@
1
- import { sum } from "d3-array";
2
- import { useDataSheet } from "../provider";
3
- import { useDrop, DropTargetMonitor } from "react-dnd";
4
-
5
- interface ColumnData {
6
- name: string;
7
- key: string;
8
- width?: number;
9
- idealWidth?: number;
10
- editable?: boolean;
11
- }
12
-
13
- interface ColumnWidthInfo {
14
- key: string;
15
- offset: number;
16
- width: number;
17
- }
18
-
19
- type DragItem = {
20
- type: "column";
21
- id: string;
22
- index: number;
23
- };
24
-
25
- /* Column width functionality */
26
- function apportionWidths(
27
- columns: ColumnData[],
28
- desiredWidths: { [key: string]: number },
29
- containerWidth: number,
30
- ): ColumnData[] {
31
- if (columns == null || desiredWidths == null || containerWidth == null)
32
- return columns;
33
-
34
- const totalSize = sum(Object.values(desiredWidths));
35
-
36
- //console.log(maxContentWidth);
37
-
38
- return columns.map((col) => {
39
- return {
40
- ...col,
41
- width: (desiredWidths[col.key] / totalSize) * (containerWidth - 50),
42
- };
43
- });
44
- }
45
-
46
- function useColumnDropTarget(ref, index) {
47
- const { dispatch } = useDataSheet();
48
- return useDrop({
49
- accept: "column",
50
- hover(item: DragItem, monitor: DropTargetMonitor) {
51
- if (!ref.current) {
52
- return;
53
- }
54
- const dragIndex = item.index;
55
- const hoverIndex = index;
56
-
57
- // Don't replace items with themselves
58
- if (dragIndex === hoverIndex) {
59
- return;
60
- }
61
-
62
- // Determine rectangle on screen
63
- const hoverBoundingRect = ref.current?.getBoundingClientRect();
64
-
65
- // Get horizontal middle
66
- const hoverMiddleX =
67
- (hoverBoundingRect.right - hoverBoundingRect.left) / 2;
68
-
69
- // Determine mouse position
70
- const clientOffset = monitor.getClientOffset();
71
-
72
- // Get pixels to the left
73
- const hoverClientX = clientOffset.x - hoverBoundingRect.left;
74
-
75
- // Only perform the move when the mouse has crossed half of the items height
76
- // When dragging downwards, only move when the cursor is below 50%
77
- // When dragging upwards, only move when the cursor is above 50%
78
- // Dragging downwards
79
- if (dragIndex < hoverIndex && hoverClientX < hoverMiddleX) {
80
- return;
81
- }
82
-
83
- // Dragging upwards
84
- if (dragIndex > hoverIndex && hoverClientX > hoverMiddleX) {
85
- return;
86
- }
87
-
88
- // Time to actually perform the action
89
- dispatch({ type: "move-column", dragIndex, hoverIndex });
90
- //moveCard(dragIndex, hoverIndex);
91
-
92
- // Note: we're mutating the monitor item here!
93
- // Generally it's better to avoid mutations,
94
- // but it's good here for the sake of performance
95
- // to avoid expensive index searches.
96
- item.index = hoverIndex;
97
- },
98
- });
99
- }
100
-
101
- function useColumnWidths(): ColumnWidthInfo[] {
102
- const { columns, columnWidths, containerWidth } = useDataSheet();
103
-
104
- let widthArray = [];
105
- let offset = 50; // for index, we should probably not hard-code this...
106
- for (const [i, col] of columns.entries()) {
107
- let width =
108
- columnWidths[col.name] ??
109
- col.width ??
110
- (containerWidth - 50) / columns.length;
111
- if (i == columns.length - 1) {
112
- // special case for last column to fill container
113
- width = containerWidth - offset;
114
- }
115
-
116
- widthArray.push({
117
- key: col.name,
118
- offset: offset,
119
- width,
120
- });
121
- offset += width;
122
- }
123
- return widthArray;
124
- }
125
-
126
- export { ColumnData, apportionWidths, useColumnWidths, useColumnDropTarget };
@@ -1,115 +0,0 @@
1
- import { useRef, memo } from "react";
2
- import { hyperStyled } from "@macrostrat/hyper";
3
- import { ColumnInfo, useDataSheet } from "../provider";
4
- import { useColumnDropTarget, useColumnWidths } from "./column-utils";
5
- import { useDrag } from "react-dnd";
6
- import Draggable from "react-draggable";
7
- import styles from "../styles.module.scss";
8
-
9
- const h = hyperStyled(styles);
10
-
11
- function Row({ row, children, className }) {
12
- const { rowHeight, rowOffset } = useDataSheet();
13
- const style = { height: rowHeight };
14
- return h("tr", { style, className }, [
15
- h("td.index-cell.cell.read-only.index", row + rowOffset + 1),
16
- children,
17
- ]);
18
- }
19
-
20
- function Columns() {
21
- const columns = useColumnWidths();
22
- return h("colgroup", [
23
- h("col.index-column", { key: "index", style: { width: 50 } }),
24
- columns.map(({ width, key }) => {
25
- return h("col", {
26
- key,
27
- style: { width },
28
- });
29
- }),
30
- ]);
31
- }
32
-
33
- function ColumnResizeHandles() {
34
- const { dispatch } = useDataSheet();
35
- const columnWidths = useColumnWidths();
36
- return h(
37
- "td.column-resizers",
38
- columnWidths.map((d, i) => {
39
- if (i == columnWidths.length - 1) return null;
40
- return h(
41
- Draggable,
42
- {
43
- axis: "x",
44
- position: { x: d.offset + d.width, y: 0 },
45
- onStop(event, { x }) {
46
- dispatch({
47
- type: "update-column-width",
48
- key: d.key,
49
- value: x - d.offset,
50
- });
51
- },
52
- },
53
- h("span.cell-drag-handle", {
54
- onMouseDown(e) {
55
- e.preventDefault();
56
- e.stopPropagation();
57
- },
58
- }),
59
- );
60
- }),
61
- );
62
- }
63
-
64
- function HeaderCell({ col, index }: { col: ColumnInfo; index: number }) {
65
- const ref = useRef();
66
-
67
- const [, drop] = useColumnDropTarget(ref, index);
68
-
69
- const [{ isDragging }, drag] = useDrag({
70
- item: { id: col.name, index },
71
- type: "column",
72
- collect: (monitor: any) => ({
73
- isDragging: monitor.isDragging(),
74
- }),
75
- });
76
-
77
- const opacity = isDragging ? 0 : 1;
78
-
79
- drag(drop(ref));
80
- return h(
81
- "td.cell.header.read-only.header-cell",
82
- { style: { opacity }, key: col.name },
83
- [h("div.cell-content", { ref }, col.name)],
84
- );
85
- }
86
-
87
- function Header_({ width }) {
88
- const style = { width };
89
- const { columns } = useDataSheet();
90
-
91
- return h("thead", { style }, [
92
- h("tr.header", { style }, [
93
- h("td.index-column.cell.read-only", ""),
94
- columns.map((col, index) => {
95
- return h(HeaderCell, { key: col.name, col, index });
96
- }),
97
- h(ColumnResizeHandles),
98
- ]),
99
- ]);
100
- }
101
-
102
- const Header = memo(Header_);
103
-
104
- function Sheet({ className, children }) {
105
- const { containerWidth: width } = useDataSheet();
106
- return h("table", { className, style: { width } }, [
107
- h(Columns),
108
- h(Header, { width }),
109
- h("tbody", children),
110
- ]);
111
- }
112
-
113
- export * from "./column-utils";
114
- export * from "./toolbar";
115
- export { Sheet, Row };
@@ -1,79 +0,0 @@
1
- import h from "@macrostrat/hyper";
2
- import { useState } from "react";
3
- import { Dialog, Classes, Button, ButtonGroup } from "@blueprintjs/core";
4
- import { SaveButton, CancelButton } from "@macrostrat/ui-components";
5
-
6
- function SubmitDialog({
7
- onClick,
8
- content,
9
- className = null,
10
- disabled,
11
- ...rest
12
- }) {
13
- const [open, setOpen] = useState(false);
14
- return h("div", null, [
15
- h(
16
- SaveButton,
17
- {
18
- disabled: disabled,
19
- ...rest,
20
- onClick: () => setOpen(true),
21
- },
22
- "Save Changes",
23
- ),
24
- h(Dialog, { isOpen: open }, [
25
- h("div", { className: Classes.DIALOG_HEADER }, h("h3", null, "WARNING")),
26
- h("div", { className: Classes.DIALOG_BODY }, h("p", null, content)),
27
- h("div", { className: Classes.DIALOG_FOOTER }, [
28
- h(
29
- SaveButton,
30
- {
31
- className: "save-btn",
32
- small: true,
33
- onClick: () => {
34
- onClick();
35
- setOpen(false);
36
- },
37
- },
38
- "Save changes",
39
- ),
40
- h(
41
- CancelButton,
42
- {
43
- small: true,
44
- onClick: () => setOpen(false),
45
- },
46
- "Cancel",
47
- ),
48
- ]),
49
- ]),
50
- ]);
51
- }
52
-
53
- export function SheetToolbar(props) {
54
- const { onSubmit, onUndo, hasChanges, children } = props;
55
- const buttonProps = { disabled: !hasChanges, small: true };
56
- var constant =
57
- "Are you sure you want to save your edits? All changes will be final. If you do not want to submit, click Cancel.";
58
- return h("div.sheet-header", [
59
- h("h3.sheet-title", "Sample metadata"),
60
- h(ButtonGroup, { className: "sheet-actions" }, [
61
- h(SubmitDialog, {
62
- className: "save-btn",
63
- onClick: onSubmit,
64
- content: constant,
65
- ...buttonProps,
66
- }),
67
- h(
68
- Button,
69
- {
70
- onClick: onUndo,
71
- ...buttonProps,
72
- },
73
- "Reset changes",
74
- ),
75
- ]),
76
- h("div.spacer"),
77
- children,
78
- ]);
79
- }
@@ -1,78 +0,0 @@
1
- import h from "@macrostrat/hyper";
2
- import { ErrorBoundary, useInDarkMode } from "@macrostrat/ui-components";
3
- import { ChromePicker } from "react-color";
4
- import { Popover } from "@blueprintjs/core";
5
- import chroma from "chroma-js";
6
- import "./styles.module.scss";
7
-
8
- export function BasePopupEditor(props) {
9
- const { children, valueRenderer, value, ...rest } = props;
10
-
11
- return h(
12
- Popover,
13
- {
14
- content: h(
15
- "div.interaction-barrier",
16
- {
17
- onMouseDown(evt) {
18
- //evt.nativeEvent.stopImmediatePropagation();
19
- },
20
- onKeyDown(evt) {
21
- console.log(evt);
22
- },
23
- },
24
- h(ErrorBoundary, null, children),
25
- ),
26
- enforceFocus: false,
27
- autoFocus: false,
28
- minimal: true,
29
- modifiers: {
30
- //offset: { enabled: true, options: { offset: [0, 8] } },
31
- },
32
- interactionKind: "hover-target",
33
- isOpen: true,
34
- onClose(evt) {
35
- props.onKeyDown(evt);
36
- },
37
- usePortal: false,
38
- },
39
- children,
40
- );
41
- }
42
-
43
- export function ColorPicker({ value, onChange }) {
44
- const darkMode = useInDarkMode();
45
- const background = darkMode ? "#383e47;" : "#ffffff";
46
- let color = "#aaaaaa";
47
- try {
48
- color = chroma(value).hex();
49
- } catch {}
50
- return h(ChromePicker, {
51
- disableAlpha: true,
52
- color,
53
- styles: {
54
- default: {
55
- picker: {
56
- background,
57
- },
58
- },
59
- },
60
- onChange(color, evt) {
61
- let c = "";
62
- try {
63
- c = chroma(color.hex);
64
- } finally {
65
- onChange(c);
66
- evt.stopPropagation();
67
- }
68
- },
69
- onKeyDown(evt) {
70
- console.log(evt);
71
- },
72
- });
73
- }
74
-
75
- export function ColorEditor(props) {
76
- const { value, onChange, ...rest } = props;
77
- return h(BasePopupEditor, rest, h(ColorPicker, { value, onChange }));
78
- }
@@ -1,10 +0,0 @@
1
- .popover-container span.popover-target,
2
- .popover-container span.bp5-popover-target {
3
- display: inline !important;
4
- pointer-events: none;
5
- width: 10px !important;
6
- height: 10px !important;
7
- }
8
- .interaction-barrier {
9
- pointer-events: all;
10
- }
@@ -1,36 +0,0 @@
1
- import ReactDataSheet from "react-datasheet";
2
- //import classNames from "classnames";
3
- interface GridElement extends ReactDataSheet.Cell<GridElement, number> {
4
- value: number | null;
5
- }
6
-
7
- type SheetContent = GridElement[][];
8
-
9
- export interface Field<Key> {
10
- name: string;
11
- key: Key;
12
- required?: boolean;
13
- isValid?(k: any): boolean;
14
- transform?(k: any): any;
15
- valueViewer?: (k: any) => any;
16
- dataEditor?: ReactDataSheet.DataEditor<ReactDataSheet.Cell<any, any>>;
17
- }
18
-
19
- export function getFieldData<K>(field: Field<K>): Field<K> {
20
- const {
21
- transform = (d) => parseFloat(d),
22
- isValid = (d) => !isNaN(d),
23
- required = true,
24
- ...rest
25
- } = field;
26
- return { ...rest, transform, isValid, required };
27
- }
28
-
29
- export function enhanceData(row: GridElement[], fields): any[] {
30
- if (row == null) return [];
31
- return row.map((cellData, i) => {
32
- const { dataEditor, valueViewer, key } = getFieldData(fields[i]);
33
-
34
- return { dataEditor, valueViewer, ...cellData };
35
- });
36
- }
@@ -1,8 +0,0 @@
1
- export * from "./provider";
2
- export * from "./components";
3
- export * from "./virtualized";
4
- export * from "./editors";
5
- import { DataSheet } from "./main";
6
- export { DataSheet };
7
-
8
- export default DataSheet;
@@ -1,180 +0,0 @@
1
- import { DataSheetProvider } from "./provider";
2
- import { VirtualizedSheet } from "./virtualized";
3
- import { BaseSheet } from "./base";
4
- import { ErrorBoundary, useElementSize } from "@macrostrat/ui-components";
5
- import { enhanceData } from "./enhancers";
6
- import { GridElement } from "./base";
7
- import h from "@macrostrat/hyper";
8
- import { useState, useEffect, useRef } from "react";
9
- import { SheetToolbar } from "./components";
10
- import classNames from "classnames";
11
- import update from "immutability-helper";
12
-
13
- export function DataSheet(props) {
14
- const { data: baseData, columns, onSubmit, ...rest } = props;
15
- const [data, setData] = useState<any[]>(baseData);
16
- //const [edits, setEdits] = useState([]);
17
- /**
18
- * For edits, what I could do is create an array of indexes based on the row number.
19
- * And at the end I can grab the whole row and send it to the backend.
20
- */
21
-
22
- useEffect(() => {
23
- // Set data to start with the value of the initial data
24
- if (baseData == null) return;
25
- setData(baseData);
26
- }, [baseData]);
27
-
28
- const ref = useRef();
29
- const size = useElementSize(ref);
30
-
31
- const onCancel = () => {
32
- setData(baseData);
33
- };
34
-
35
- //const [columns, setDesiredWidth] = useState(columnSpec);
36
-
37
- //const desiredWidths =
38
- // initialData != null ? calculateWidths(initialData, columns) : {};
39
-
40
- // Change management
41
- // const handleUndo = () => {
42
- // setData(initialData);
43
- // setEdits([]);
44
- // };
45
- // const handleSubmit = () => {
46
- // //push method for sending data back to api
47
- // if (data.length === 0) {
48
- // return null;
49
- // }
50
- // setData(data);
51
- // const post_data = BuildEdits();
52
- // console.log(post_data);
53
- // postData(post_data);
54
- // setEdits([]);
55
- // };
56
-
57
- // function BuildEdits() {
58
- // /** Grabs the edits from the data for POST.
59
- // *
60
- // * How can I group them by rows so I don't have to send a item per cell
61
- // */
62
-
63
- // const editsList = addNecesaryFields(edits, data);
64
- // const finalEdits = combineLikeIds(editsList);
65
- // return finalEdits;
66
- // }
67
-
68
- function onCellsChanged(changes) {
69
- /** Cell change function that uses immutability-helper */
70
- const spec = {};
71
- // const edits = []; // something like what's happening with spec
72
- console.log(changes);
73
- changes.forEach(({ cell, row, col, value }) => {
74
- // Get the key that should be used to assign the value
75
- const { key } = columns[col];
76
- // Substitute empty strings for nulls
77
- const $set = value == "" ? null : value;
78
- // sets edits to the state index and the column name
79
- //setEdits((prevEdits) => [{ row, key }, ...prevEdits]);
80
- spec[row] = {
81
- ...(spec[row] || {}),
82
- [key]: { $set },
83
- };
84
- });
85
- setData(update(data, spec));
86
- }
87
-
88
- /** Builds the properties for the cell */
89
- const finalizeCellProps = (
90
- value: any,
91
- row: number,
92
- key: string,
93
- col: number,
94
- ) => {
95
- /** Need to turn off submit on click away event for cell */
96
-
97
- // Check if values is the same as the initial data key
98
- //console.log(arguments);
99
-
100
- const isChanged = value != baseData[row][key];
101
- const readOnly = !(columns[col].editable ?? true);
102
- const className = classNames(
103
- { edited: isChanged, "read-only": readOnly },
104
- "cell",
105
- "nowrap",
106
- "clip",
107
- );
108
- return {
109
- value,
110
- className,
111
- readOnly,
112
- };
113
- };
114
-
115
- /** We now build cell properties in the render function, rather than storing
116
- them using a precomputed useEffect hook. We decide what cells have changed using
117
- a direct comparison with the equivalent value of initialData. (That's the
118
- power of immutable data.)
119
-
120
- This is where the Components should be attached.
121
- */
122
-
123
- const sheetHeader = h(
124
- SheetToolbar,
125
- {
126
- onSubmit,
127
- onCancel,
128
- hasChanges: baseData != data,
129
- },
130
- //h("div.right-actions", null, h(SettingsPopup))
131
- );
132
-
133
- return h(DataSheetUI, {
134
- sheetHeader,
135
- columns,
136
- onCellsChanged,
137
- finalizeCellProps,
138
- data,
139
- ...rest,
140
- });
141
- }
142
-
143
- function DataSheetUI({
144
- columns,
145
- data,
146
- virtualized = false,
147
- sheetHeader = null,
148
- finalizeCellProps,
149
- ...rest
150
- }) {
151
- const component = virtualized ? VirtualizedSheet : BaseSheet;
152
- let newData = data.map((d) => transformData(columns, d));
153
-
154
- if (finalizeCellProps != null) {
155
- newData = newData.map((obj, row) => {
156
- return columns.map(({ key }, col) =>
157
- finalizeCellProps(obj[col].value, row, key, col),
158
- );
159
- });
160
- }
161
- return h(
162
- ErrorBoundary,
163
- null,
164
- h(
165
- DataSheetProvider,
166
- { columns },
167
- h("div.data-sheet", [
168
- sheetHeader,
169
- h(component, { ...rest, data: newData }),
170
- ]),
171
- ),
172
- );
173
- }
174
-
175
- function transformData(columns, data: object): GridElement[] {
176
- const row1 = columns.map((d) => {
177
- return { value: data[d.key] ?? null, className: "test" };
178
- });
179
- return enhanceData(row1, columns);
180
- }