@macrostrat/data-sheet 3.0.4 → 4.0.0-dev1
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 +20 -1
- package/dist/actions/builtin.cjs +56 -0
- package/dist/actions/builtin.cjs.map +1 -0
- package/dist/actions/builtin.d.ts +15 -0
- package/dist/actions/builtin.js +56 -0
- package/dist/actions/builtin.js.map +1 -0
- package/dist/actions/clipboard.cjs +250 -0
- package/dist/actions/clipboard.cjs.map +1 -0
- package/dist/actions/clipboard.d.ts +42 -0
- package/dist/actions/clipboard.js +250 -0
- package/dist/actions/clipboard.js.map +1 -0
- package/dist/actions/filters.cjs +123 -0
- package/dist/actions/filters.cjs.map +1 -0
- package/dist/actions/filters.d.ts +6 -0
- package/dist/actions/filters.js +123 -0
- package/dist/actions/filters.js.map +1 -0
- package/dist/actions/index.d.ts +6 -0
- package/dist/actions/selection.cjs +194 -0
- package/dist/actions/selection.cjs.map +1 -0
- package/dist/actions/selection.d.ts +40 -0
- package/dist/actions/selection.js +192 -0
- package/dist/actions/selection.js.map +1 -0
- package/dist/actions/toolbar.cjs +171 -0
- package/dist/actions/toolbar.cjs.map +1 -0
- package/dist/actions/toolbar.d.ts +10 -0
- package/dist/actions/toolbar.js +171 -0
- package/dist/actions/toolbar.js.map +1 -0
- package/dist/actions/toolbar.module.sass.cjs +22 -0
- package/dist/actions/toolbar.module.sass.cjs.map +1 -0
- package/dist/actions/toolbar.module.sass.js +20 -0
- package/dist/actions/toolbar.module.sass.js.map +1 -0
- package/dist/actions/types.d.ts +143 -0
- package/dist/cell-renderer.cjs +165 -0
- package/dist/cell-renderer.cjs.map +1 -0
- package/dist/cell-renderer.d.ts +3 -0
- package/dist/cell-renderer.js +165 -0
- package/dist/cell-renderer.js.map +1 -0
- package/dist/components/actions.module.sass.cjs +3 -3
- package/dist/components/actions.module.sass.js +2 -2
- package/dist/components/colors.cjs +20 -16
- package/dist/components/colors.cjs.map +1 -1
- package/dist/components/colors.d.ts +4 -3
- package/dist/components/colors.js +20 -16
- package/dist/components/colors.js.map +1 -1
- package/dist/components/editor-popup.cjs +4 -3
- package/dist/components/editor-popup.cjs.map +1 -1
- package/dist/components/editor-popup.js +4 -3
- package/dist/components/editor-popup.js.map +1 -1
- package/dist/components/index.d.ts +1 -0
- package/dist/components/main.module.sass.cjs +12 -12
- package/dist/components/main.module.sass.js +11 -11
- package/dist/components/text-areas.cjs +23 -18
- package/dist/components/text-areas.cjs.map +1 -1
- package/dist/components/text-areas.d.ts +8 -4
- package/dist/components/text-areas.js +20 -13
- package/dist/components/text-areas.js.map +1 -1
- package/dist/components/view-info.cjs +18 -0
- package/dist/components/view-info.cjs.map +1 -0
- package/dist/components/view-info.d.ts +1 -0
- package/dist/components/view-info.js +18 -0
- package/dist/components/view-info.js.map +1 -0
- package/dist/core.cjs +201 -300
- package/dist/core.cjs.map +1 -1
- package/dist/core.d.ts +18 -10
- package/dist/core.js +204 -299
- package/dist/core.js.map +1 -1
- package/dist/data-sheet.css +190 -52
- package/dist/index.cjs +47 -8
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.js +48 -9
- package/dist/index.js.map +1 -1
- package/dist/main.module.sass.cjs +15 -14
- package/dist/main.module.sass.cjs.map +1 -1
- package/dist/main.module.sass.js +14 -13
- package/dist/main.module.sass.js.map +1 -1
- package/dist/notifications.cjs +19 -0
- package/dist/notifications.cjs.map +1 -0
- package/dist/notifications.d.ts +9 -0
- package/dist/notifications.js +19 -0
- package/dist/notifications.js.map +1 -0
- package/dist/postgrest-table/data-loaders.cjs +181 -104
- package/dist/postgrest-table/data-loaders.cjs.map +1 -1
- package/dist/postgrest-table/data-loaders.d.ts +52 -28
- package/dist/postgrest-table/data-loaders.js +181 -104
- package/dist/postgrest-table/data-loaders.js.map +1 -1
- package/dist/postgrest-table/index.cjs +205 -79
- package/dist/postgrest-table/index.cjs.map +1 -1
- package/dist/postgrest-table/index.d.ts +14 -17
- package/dist/postgrest-table/index.js +186 -58
- package/dist/postgrest-table/index.js.map +1 -1
- package/dist/postgrest-table/lazy-loader-table.cjs +18 -17
- package/dist/postgrest-table/lazy-loader-table.cjs.map +1 -1
- package/dist/postgrest-table/lazy-loader-table.d.ts +1 -1
- package/dist/postgrest-table/lazy-loader-table.js +17 -14
- package/dist/postgrest-table/lazy-loader-table.js.map +1 -1
- package/dist/postgrest-table/loading-utils.cjs +16 -0
- package/dist/postgrest-table/loading-utils.cjs.map +1 -0
- package/dist/postgrest-table/loading-utils.d.ts +13 -0
- package/dist/postgrest-table/loading-utils.js +16 -0
- package/dist/postgrest-table/loading-utils.js.map +1 -0
- package/dist/postgrest-table/main.module.sass.cjs +18 -13
- package/dist/postgrest-table/main.module.sass.cjs.map +1 -1
- package/dist/postgrest-table/main.module.sass.js +18 -13
- package/dist/postgrest-table/main.module.sass.js.map +1 -1
- package/dist/provider.cjs +96 -344
- package/dist/provider.cjs.map +1 -1
- package/dist/provider.js +96 -343
- package/dist/provider.js.map +1 -1
- package/dist/renderers/column-header-cell-postgrest.cjs +216 -0
- package/dist/renderers/column-header-cell-postgrest.cjs.map +1 -0
- package/dist/renderers/column-header-cell-postgrest.d.ts +13 -0
- package/dist/renderers/column-header-cell-postgrest.js +216 -0
- package/dist/renderers/column-header-cell-postgrest.js.map +1 -0
- package/dist/renderers/column-header-cell-postgrest.module.sass.cjs +23 -0
- package/dist/renderers/column-header-cell-postgrest.module.sass.cjs.map +1 -0
- package/dist/renderers/column-header-cell-postgrest.module.sass.js +22 -0
- package/dist/renderers/column-header-cell-postgrest.module.sass.js.map +1 -0
- package/dist/renderers/column-header-cell.cjs +103 -0
- package/dist/renderers/column-header-cell.cjs.map +1 -0
- package/dist/renderers/column-header-cell.d.ts +30 -0
- package/dist/renderers/column-header-cell.js +101 -0
- package/dist/renderers/column-header-cell.js.map +1 -0
- package/dist/renderers/index.d.ts +2 -0
- package/dist/table-updates/data-parameter.d.ts +14 -0
- package/dist/table-updates/filter.d.ts +16 -0
- package/dist/table-updates/index.d.ts +19 -0
- package/dist/types.cjs +9 -0
- package/dist/types.cjs.map +1 -0
- package/dist/types.d.ts +108 -0
- package/dist/types.js +9 -0
- package/dist/types.js.map +1 -0
- package/dist/utils/column-spec.cjs +9 -1
- package/dist/utils/column-spec.cjs.map +1 -1
- package/dist/utils/column-spec.d.ts +21 -0
- package/dist/utils/column-spec.js +9 -1
- package/dist/utils/column-spec.js.map +1 -1
- package/dist/utils/key-handling.cjs +161 -47
- package/dist/utils/key-handling.cjs.map +1 -1
- package/dist/utils/key-handling.d.ts +3 -3
- package/dist/utils/key-handling.js +161 -47
- package/dist/utils/key-handling.js.map +1 -1
- package/dist/zustand-store.cjs +485 -0
- package/dist/zustand-store.cjs.map +1 -0
- package/dist/zustand-store.d.ts +6 -0
- package/dist/zustand-store.js +483 -0
- package/dist/zustand-store.js.map +1 -0
- package/package.json +6 -5
- package/src/actions/builtin.ts +65 -0
- package/src/actions/clipboard.ts +350 -0
- package/src/actions/filters.ts +150 -0
- package/src/actions/index.ts +6 -0
- package/src/actions/selection.ts +271 -0
- package/src/actions/toolbar.module.sass +30 -0
- package/src/actions/toolbar.ts +212 -0
- package/src/actions/types.ts +159 -0
- package/src/cell-renderer.ts +221 -0
- package/src/components/colors.ts +22 -19
- package/src/components/editor-popup.ts +3 -2
- package/src/components/index.ts +1 -0
- package/src/components/main.module.sass +1 -0
- package/src/components/text-areas.ts +34 -19
- package/src/components/view-info.ts +17 -0
- package/src/core.ts +273 -360
- package/src/index.ts +4 -0
- package/src/main.module.sass +14 -2
- package/src/notifications.ts +22 -0
- package/src/postgrest-table/data-loaders.ts +300 -201
- package/src/postgrest-table/index.ts +260 -86
- package/src/postgrest-table/lazy-loader-table.ts +18 -17
- package/src/postgrest-table/loading-utils.ts +49 -0
- package/src/postgrest-table/main.module.sass +16 -0
- package/src/provider.ts +110 -487
- package/src/renderers/column-header-cell-postgrest.module.sass +71 -0
- package/src/renderers/column-header-cell-postgrest.ts +282 -0
- package/src/renderers/column-header-cell.ts +388 -0
- package/src/renderers/index.ts +2 -0
- package/src/table-updates/data-parameter.ts +44 -0
- package/src/table-updates/filter.ts +124 -0
- package/src/table-updates/index.ts +239 -0
- package/src/types.ts +121 -0
- package/src/utils/column-spec.ts +47 -0
- package/src/utils/key-handling.ts +201 -66
- package/src/zustand-store.ts +582 -0
- package/dist/provider.d.ts +0 -69
package/src/core.ts
CHANGED
|
@@ -1,26 +1,54 @@
|
|
|
1
1
|
import {
|
|
2
2
|
Button,
|
|
3
|
-
ButtonGroup,
|
|
4
3
|
HotkeysProvider,
|
|
5
4
|
InputGroup,
|
|
6
|
-
|
|
5
|
+
OverlaysProvider,
|
|
6
|
+
useHotkeys,
|
|
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
|
-
import
|
|
11
|
-
|
|
12
|
-
|
|
17
|
+
import {
|
|
18
|
+
ReactNode,
|
|
19
|
+
useCallback,
|
|
20
|
+
useEffect,
|
|
21
|
+
useMemo,
|
|
22
|
+
useRef,
|
|
23
|
+
useState,
|
|
24
|
+
} from "react";
|
|
25
|
+
import { DataSheetAction, InfoBar } from "./components";
|
|
26
|
+
import {
|
|
27
|
+
autoFilterId,
|
|
28
|
+
renderColumnHeaderCell,
|
|
29
|
+
SortFilterBar,
|
|
30
|
+
} from "./renderers";
|
|
13
31
|
import h from "./main.module.sass";
|
|
14
32
|
import {
|
|
33
|
+
columnSpecAtom,
|
|
15
34
|
DataSheetProvider,
|
|
16
|
-
|
|
17
|
-
DataSheetStore,
|
|
18
|
-
singleFocusedCell,
|
|
35
|
+
storeAtom,
|
|
19
36
|
useSelector,
|
|
20
37
|
useStoreAPI,
|
|
21
|
-
|
|
38
|
+
ctx,
|
|
39
|
+
tableActionsAtom,
|
|
22
40
|
} from "./provider";
|
|
23
|
-
import {
|
|
41
|
+
import { atom } from "jotai";
|
|
42
|
+
import {
|
|
43
|
+
DataSheetProviderProps,
|
|
44
|
+
TableElementStatus,
|
|
45
|
+
VisibleCells,
|
|
46
|
+
} from "./types.ts";
|
|
47
|
+
import { basicCellRenderer } from "./cell-renderer.ts";
|
|
48
|
+
import { tableHotkeysAtom } from "./utils";
|
|
49
|
+
import { clipboardActions, TableAction, TableFilter } from "./actions";
|
|
50
|
+
import { ActionsToolbar, FilterBar } from "./actions";
|
|
51
|
+
import { useScrollHandler } from "./postgrest-table";
|
|
24
52
|
|
|
25
53
|
// More on component templates: https://storybook.js.org/docs/react/writing-stories/introduction#using-args
|
|
26
54
|
|
|
@@ -33,22 +61,36 @@ export enum DataSheetDensity {
|
|
|
33
61
|
LOW = "low",
|
|
34
62
|
}
|
|
35
63
|
|
|
36
|
-
interface DataSheetInternalProps<T> {
|
|
64
|
+
interface DataSheetInternalProps<T> extends TableProps {
|
|
37
65
|
onVisibleCellsChange?: (visibleCells: VisibleCells) => void;
|
|
38
66
|
onSaveData?: (updatedData: any[], data: T[]) => void;
|
|
39
67
|
onUpdateData?: (updatedData: any[], data: T[]) => void;
|
|
40
68
|
onDeleteRows?: (selection: Region[]) => void;
|
|
41
69
|
verbose?: boolean;
|
|
42
70
|
enableColumnReordering?: boolean;
|
|
71
|
+
enableClipboard?: boolean;
|
|
43
72
|
enableFocusedCell?: boolean;
|
|
44
73
|
dataSheetActions?: ReactNode | null;
|
|
45
74
|
editable?: boolean;
|
|
46
75
|
autoFocusEditor?: boolean;
|
|
47
76
|
density?: DataSheetDensity;
|
|
77
|
+
/** Configurable table actions shown in a selection-aware toolbar.
|
|
78
|
+
* When provided, the actions toolbar renders alongside the existing
|
|
79
|
+
* edit toolbar. Actions are filtered by the current selection cardinality. */
|
|
80
|
+
actions?: TableAction<T>[];
|
|
81
|
+
/** Available column/table filters shown in a filter bar.
|
|
82
|
+
* Filters can also be defined per-column via `ColumnSpec.filters`. */
|
|
83
|
+
filters?: TableFilter<T>[];
|
|
84
|
+
/** Optional custom column header cell renderer, called for each column.
|
|
85
|
+
* Receives the ColumnSpec and column index; should return a React element
|
|
86
|
+
* (typically a Blueprint ColumnHeaderCell). */
|
|
87
|
+
columnHeaderCellRenderer?: (col: any, colIndex: number) => ReactNode;
|
|
48
88
|
}
|
|
49
89
|
|
|
50
90
|
type DataSheetProps<T> = DataSheetProviderProps<T> & DataSheetInternalProps<T>;
|
|
51
91
|
|
|
92
|
+
const emptyData: any[] = [];
|
|
93
|
+
|
|
52
94
|
export function DataSheet<T>(props: DataSheetProps<T>) {
|
|
53
95
|
const {
|
|
54
96
|
data,
|
|
@@ -58,29 +100,28 @@ export function DataSheet<T>(props: DataSheetProps<T>) {
|
|
|
58
100
|
enableColumnReordering = false,
|
|
59
101
|
enableFocusedCell = false,
|
|
60
102
|
defaultColumnWidth = 150,
|
|
103
|
+
children,
|
|
61
104
|
...rest
|
|
62
105
|
} = props;
|
|
63
106
|
|
|
64
107
|
return h(
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
}),
|
|
83
|
-
),
|
|
108
|
+
DataSheetProvider<T>,
|
|
109
|
+
{
|
|
110
|
+
data: data ?? emptyData,
|
|
111
|
+
columnSpec,
|
|
112
|
+
columnSpecOptions,
|
|
113
|
+
enableColumnReordering,
|
|
114
|
+
defaultColumnWidth,
|
|
115
|
+
editable,
|
|
116
|
+
...rest,
|
|
117
|
+
},
|
|
118
|
+
h(_DataSheet<any>, {
|
|
119
|
+
...rest,
|
|
120
|
+
children,
|
|
121
|
+
editable,
|
|
122
|
+
enableColumnReordering,
|
|
123
|
+
enableFocusedCell,
|
|
124
|
+
}),
|
|
84
125
|
);
|
|
85
126
|
}
|
|
86
127
|
|
|
@@ -91,7 +132,6 @@ const deletedRowHeaderStyle = {
|
|
|
91
132
|
|
|
92
133
|
function _DataSheet<T>({
|
|
93
134
|
onVisibleCellsChange,
|
|
94
|
-
enableColumnReordering,
|
|
95
135
|
onSaveData,
|
|
96
136
|
onUpdateData,
|
|
97
137
|
onDeleteRows,
|
|
@@ -99,7 +139,15 @@ function _DataSheet<T>({
|
|
|
99
139
|
dataSheetActions = null,
|
|
100
140
|
enableFocusedCell,
|
|
101
141
|
autoFocusEditor = true,
|
|
142
|
+
enableClipboard = true,
|
|
143
|
+
showInfoBar = false,
|
|
102
144
|
density = DataSheetDensity.HIGH,
|
|
145
|
+
selectionModes,
|
|
146
|
+
actions,
|
|
147
|
+
filters,
|
|
148
|
+
columnHeaderCellRenderer,
|
|
149
|
+
children,
|
|
150
|
+
...rest
|
|
103
151
|
}: DataSheetInternalProps<T>) {
|
|
104
152
|
/**
|
|
105
153
|
* @param data: The data to be displayed in the table
|
|
@@ -107,14 +155,34 @@ function _DataSheet<T>({
|
|
|
107
155
|
* @param columnSpecOptions: Options for generating a column spec from data
|
|
108
156
|
*/
|
|
109
157
|
|
|
158
|
+
// Turn on debug features
|
|
159
|
+
const debugMode = false;
|
|
160
|
+
|
|
161
|
+
// Sync table actions to atom
|
|
162
|
+
const _actions: TableAction[] = useMemo(() => {
|
|
163
|
+
const _actions = [];
|
|
164
|
+
if (actions != null) {
|
|
165
|
+
_actions.push(...actions);
|
|
166
|
+
}
|
|
167
|
+
if (enableClipboard) {
|
|
168
|
+
_actions.push(...clipboardActions);
|
|
169
|
+
}
|
|
170
|
+
return _actions;
|
|
171
|
+
}, [actions, enableClipboard]);
|
|
172
|
+
|
|
173
|
+
ctx.useSync(tableActionsAtom, _actions);
|
|
174
|
+
|
|
175
|
+
const hotkeysConfig = ctx.useValue(tableHotkeysAtom);
|
|
176
|
+
const { handleKeyDown, handleKeyUp } = useHotkeys(hotkeysConfig);
|
|
177
|
+
|
|
110
178
|
// For now, we only consider a single cell "focused" when we have one cell selected.
|
|
111
179
|
// Multi-cell selections have a different set of "bulk" actions.
|
|
112
|
-
const selectedRegions = useSelector<T>((state) => state.selection);
|
|
180
|
+
const selectedRegions = useSelector<T>((state) => state.selection ?? []);
|
|
113
181
|
|
|
114
182
|
const data = useSelector((state) => state.data);
|
|
115
183
|
const editable = useSelector((state) => state.editable);
|
|
116
184
|
|
|
117
|
-
const
|
|
185
|
+
const rowStatus = useSelector((state) => state.rowStatus);
|
|
118
186
|
|
|
119
187
|
const focusedCell = useSelector((state) => state.focusedCell);
|
|
120
188
|
|
|
@@ -125,45 +193,20 @@ function _DataSheet<T>({
|
|
|
125
193
|
// A sparse array to hold updates
|
|
126
194
|
// TODO: create a "changeset" concept to facilitate undo/redo
|
|
127
195
|
const updatedData = useSelector((state) => state.updatedData);
|
|
128
|
-
const setUpdatedData = useSelector((state) => state.setUpdatedData);
|
|
129
196
|
|
|
130
197
|
const onSelection = useSelector((state) => state.onSelection);
|
|
131
198
|
|
|
132
199
|
const storeAPI = useStoreAPI<T>();
|
|
133
200
|
|
|
134
|
-
const
|
|
135
|
-
if (onSaveData == null) return null;
|
|
136
|
-
return () => {
|
|
137
|
-
onSaveData(updatedData, data);
|
|
138
|
-
setUpdatedData([]);
|
|
139
|
-
};
|
|
140
|
-
}, [updatedData, data, onSaveData]);
|
|
141
|
-
|
|
142
|
-
const setVisibleCells = useSelector((state) => state.setVisibleCells);
|
|
143
|
-
|
|
201
|
+
const onScroll = useScrollHandler();
|
|
144
202
|
const _onVisibleCellsChange = useCallback(
|
|
145
203
|
(visibleCells: VisibleCells) => {
|
|
146
|
-
|
|
204
|
+
onScroll(visibleCells);
|
|
147
205
|
onVisibleCellsChange?.(visibleCells);
|
|
148
206
|
},
|
|
149
|
-
[onVisibleCellsChange,
|
|
207
|
+
[onVisibleCellsChange, onScroll],
|
|
150
208
|
);
|
|
151
209
|
|
|
152
|
-
const onAddRow = useCallback(() => {
|
|
153
|
-
setUpdatedData((updatedData: any[]): any[] => {
|
|
154
|
-
const ix = Math.max(updatedData.length, data.length);
|
|
155
|
-
const addRowSpec = { [ix]: { $set: {} } };
|
|
156
|
-
const newUpdatedData = update(updatedData, addRowSpec);
|
|
157
|
-
return newUpdatedData;
|
|
158
|
-
});
|
|
159
|
-
}, [setUpdatedData]);
|
|
160
|
-
|
|
161
|
-
const deleteSelectedRows = useSelector((state) => state.deleteSelectedRows);
|
|
162
|
-
const _onDeleteRows = useCallback(() => {
|
|
163
|
-
deleteSelectedRows();
|
|
164
|
-
onDeleteRows?.(selectedRegions);
|
|
165
|
-
}, [onDeleteRows, selectedRegions, deleteSelectedRows]);
|
|
166
|
-
|
|
167
210
|
useEffect(() => {
|
|
168
211
|
if (!verbose) return;
|
|
169
212
|
console.log("Updated data", updatedData);
|
|
@@ -178,43 +221,68 @@ function _DataSheet<T>({
|
|
|
178
221
|
console.log("Selected regions", selectedRegions);
|
|
179
222
|
}, [selectedRegions]);
|
|
180
223
|
|
|
181
|
-
const
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
224
|
+
const tableElementRef = useRef<HTMLElement>(null);
|
|
225
|
+
useEffect(() => {
|
|
226
|
+
// Return the focus to the table
|
|
227
|
+
tableElementRef.current?.focus();
|
|
228
|
+
}, [focusedCell]);
|
|
185
229
|
|
|
186
|
-
const columnWidths =
|
|
230
|
+
const columnWidths = ctx.useValue(columnWidthsAtom);
|
|
187
231
|
|
|
188
|
-
|
|
232
|
+
// When filters are active, only show matching rows
|
|
233
|
+
const filteredRowIndices = useSelector((state) => state.filteredRowIndices);
|
|
234
|
+
const totalRows = Math.max(updatedData.length, data.length);
|
|
235
|
+
const numRows =
|
|
236
|
+
filteredRowIndices != null ? filteredRowIndices.length : totalRows;
|
|
189
237
|
|
|
190
238
|
let className = `${density}-density`;
|
|
191
239
|
|
|
192
|
-
|
|
193
|
-
let defaultColumnWidth = 150;
|
|
194
|
-
let style = {
|
|
195
|
-
"--data-sheet-row-height": "20px",
|
|
196
|
-
"--data-sheet-font-size": "12px",
|
|
197
|
-
};
|
|
198
|
-
if (density === DataSheetDensity.MEDIUM) {
|
|
199
|
-
rowHeight = 24;
|
|
200
|
-
style = {
|
|
201
|
-
"--data-sheet-row-height": "24px",
|
|
202
|
-
"--data-sheet-font-size": "14px",
|
|
203
|
-
};
|
|
204
|
-
} else if (density === DataSheetDensity.LOW) {
|
|
205
|
-
rowHeight = 30;
|
|
206
|
-
style = {
|
|
207
|
-
"--data-sheet-row-height": "30px",
|
|
208
|
-
"--data-sheet-font-size": "18px",
|
|
209
|
-
};
|
|
210
|
-
}
|
|
240
|
+
const { rowHeight, style } = styleParamsForDensity(density);
|
|
211
241
|
|
|
212
242
|
const onColumnsReordered = useSelector((state) => state.onColumnsReordered);
|
|
213
243
|
|
|
214
|
-
|
|
244
|
+
// Auto-detect if any columns have sortable/filterable set.
|
|
245
|
+
// If so and no explicit columnHeaderCellRenderer was provided,
|
|
246
|
+
// use the built-in client-side sort/filter header.
|
|
247
|
+
const hasSortableOrFilterable = useMemo(
|
|
248
|
+
() => columnSpec.some((col) => col.sortable || col.filterable),
|
|
249
|
+
[columnSpec],
|
|
250
|
+
);
|
|
251
|
+
|
|
252
|
+
const columnSorts = useSelector((state) => state.columnSorts);
|
|
253
|
+
const activeFilters = useSelector((state) => state.activeFilters);
|
|
254
|
+
|
|
255
|
+
const realizedColumns = useMemo(() => {
|
|
215
256
|
return columnSpec.map((col, colIndex) => {
|
|
257
|
+
let fn =
|
|
258
|
+
col.headerCellRenderer ??
|
|
259
|
+
columnHeaderCellRenderer ??
|
|
260
|
+
renderColumnHeaderCell;
|
|
261
|
+
let activeSort = null;
|
|
262
|
+
let activeFilter = null;
|
|
263
|
+
if (col.sortable) {
|
|
264
|
+
activeSort = columnSorts?.find((s) => s.key === col.key);
|
|
265
|
+
}
|
|
266
|
+
if (col.filterable) {
|
|
267
|
+
// The autoFilterID implementation is too complicated
|
|
268
|
+
activeFilter =
|
|
269
|
+
activeFilters?.get(col.key) ??
|
|
270
|
+
activeFilters?.get(autoFilterId(col.key));
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
const _columnHeaderCellRenderer = (colIndex) => {
|
|
274
|
+
return fn({
|
|
275
|
+
col,
|
|
276
|
+
colIndex,
|
|
277
|
+
activeSort,
|
|
278
|
+
activeFilter,
|
|
279
|
+
});
|
|
280
|
+
};
|
|
281
|
+
|
|
216
282
|
return h(Column, {
|
|
283
|
+
id: col.key,
|
|
217
284
|
name: col.name,
|
|
285
|
+
columnHeaderCellRenderer: _columnHeaderCellRenderer,
|
|
218
286
|
cellRenderer: (rowIndex) => {
|
|
219
287
|
const state = storeAPI.getState();
|
|
220
288
|
return basicCellRenderer<T>(
|
|
@@ -223,11 +291,20 @@ function _DataSheet<T>({
|
|
|
223
291
|
col,
|
|
224
292
|
state,
|
|
225
293
|
autoFocusEditor,
|
|
294
|
+
filteredRowIndices,
|
|
226
295
|
);
|
|
227
296
|
},
|
|
228
297
|
});
|
|
229
298
|
});
|
|
230
|
-
}, [
|
|
299
|
+
}, [
|
|
300
|
+
columnSpec,
|
|
301
|
+
columnSorts,
|
|
302
|
+
activeFilters,
|
|
303
|
+
storeAPI,
|
|
304
|
+
autoFocusEditor,
|
|
305
|
+
filteredRowIndices,
|
|
306
|
+
columnHeaderCellRenderer,
|
|
307
|
+
]);
|
|
231
308
|
|
|
232
309
|
const onColumnWidthChanged = useSelector(
|
|
233
310
|
(state) => state.onColumnWidthChanged,
|
|
@@ -235,29 +312,57 @@ function _DataSheet<T>({
|
|
|
235
312
|
|
|
236
313
|
const rowHeaderCellRenderer = useCallback(
|
|
237
314
|
(rowIndex: number) => {
|
|
238
|
-
const
|
|
315
|
+
const dataRowIndex =
|
|
316
|
+
filteredRowIndices != null
|
|
317
|
+
? (filteredRowIndices[rowIndex] ?? rowIndex)
|
|
318
|
+
: rowIndex;
|
|
319
|
+
const style =
|
|
320
|
+
rowStatus[dataRowIndex] == TableElementStatus.DELETED
|
|
321
|
+
? deletedRowHeaderStyle
|
|
322
|
+
: null;
|
|
239
323
|
|
|
240
324
|
return h(RowHeaderCell, {
|
|
241
325
|
enableRowReordering: false,
|
|
242
326
|
index: rowIndex,
|
|
243
|
-
name: `${
|
|
327
|
+
name: `${dataRowIndex + 1}`,
|
|
244
328
|
style,
|
|
245
329
|
});
|
|
246
330
|
},
|
|
247
|
-
[
|
|
331
|
+
[rowStatus, filteredRowIndices],
|
|
248
332
|
);
|
|
249
333
|
|
|
250
|
-
|
|
334
|
+
let _selectionModes = selectionModes;
|
|
335
|
+
if (
|
|
336
|
+
editable &&
|
|
337
|
+
_selectionModes != null &&
|
|
338
|
+
!_selectionModes.includes(RegionCardinality.CELLS)
|
|
339
|
+
) {
|
|
340
|
+
_selectionModes = [..._selectionModes, RegionCardinality.CELLS];
|
|
341
|
+
// Ensure selection mode includes "cells"
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
const cellRendererDependencies = [
|
|
345
|
+
data,
|
|
346
|
+
updatedData,
|
|
347
|
+
focusedCell,
|
|
348
|
+
rowStatus,
|
|
349
|
+
filteredRowIndices,
|
|
350
|
+
];
|
|
251
351
|
|
|
252
352
|
return h("div.data-sheet-container", { className, style }, [
|
|
253
|
-
h.if(
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
}),
|
|
353
|
+
h.if(actions != null)(ActionsToolbar, { actions }),
|
|
354
|
+
h.if(filters != null && filters.length > 0)(FilterBar, { filters }),
|
|
355
|
+
h.if(hasSortableOrFilterable)(SortFilterBar),
|
|
257
356
|
dataSheetActions,
|
|
357
|
+
children,
|
|
258
358
|
h(
|
|
259
359
|
"div.data-sheet-holder",
|
|
260
|
-
{
|
|
360
|
+
{
|
|
361
|
+
tabIndex: 0,
|
|
362
|
+
onKeyDown: handleKeyDown,
|
|
363
|
+
onKeyUp: handleKeyUp,
|
|
364
|
+
ref: tableElementRef,
|
|
365
|
+
},
|
|
261
366
|
h(
|
|
262
367
|
Table,
|
|
263
368
|
{
|
|
@@ -265,7 +370,6 @@ function _DataSheet<T>({
|
|
|
265
370
|
numRows,
|
|
266
371
|
className: "data-sheet",
|
|
267
372
|
enableFocusedCell,
|
|
268
|
-
enableColumnReordering,
|
|
269
373
|
onColumnsReordered,
|
|
270
374
|
focusedCell,
|
|
271
375
|
selectedRegions,
|
|
@@ -274,259 +378,63 @@ function _DataSheet<T>({
|
|
|
274
378
|
columnWidths,
|
|
275
379
|
onColumnWidthChanged,
|
|
276
380
|
onSelection,
|
|
277
|
-
|
|
381
|
+
/** TODO: we could enable this, but we need a use-case first... */
|
|
278
382
|
enableRowReordering: false,
|
|
279
383
|
enableRowResizing: false,
|
|
280
384
|
// The cell renderer is memoized internally based on these data dependencies
|
|
281
|
-
cellRendererDependencies
|
|
282
|
-
data,
|
|
283
|
-
//selection,
|
|
284
|
-
updatedData,
|
|
285
|
-
focusedCell,
|
|
286
|
-
deletedRows,
|
|
287
|
-
],
|
|
385
|
+
cellRendererDependencies,
|
|
288
386
|
onVisibleCellsChange: _onVisibleCellsChange,
|
|
289
387
|
rowHeaderCellRenderer,
|
|
388
|
+
selectionModes: _selectionModes,
|
|
389
|
+
...rest,
|
|
390
|
+
getCellClipboardData: null,
|
|
290
391
|
},
|
|
291
|
-
|
|
392
|
+
realizedColumns,
|
|
292
393
|
),
|
|
293
394
|
),
|
|
395
|
+
h.if(showInfoBar)(InfoBar),
|
|
396
|
+
h.if(debugMode)(CellRendererDebugOverlay, {
|
|
397
|
+
cellRendererDependencies,
|
|
398
|
+
names: [
|
|
399
|
+
"data",
|
|
400
|
+
"updatedData",
|
|
401
|
+
"focusedCell",
|
|
402
|
+
"rowStatus",
|
|
403
|
+
"filteredRowIndices",
|
|
404
|
+
],
|
|
405
|
+
}),
|
|
294
406
|
]);
|
|
295
407
|
}
|
|
296
408
|
|
|
297
|
-
|
|
298
|
-
|
|
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
|
|
409
|
+
const columnWidthsIndexAtom = atom((get) => get(storeAtom).columnWidthsIndex);
|
|
410
|
+
const defaultColumnWidthAtom = atom((get) => get(storeAtom).defaultColumnWidth);
|
|
369
411
|
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
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
|
|
412
|
+
const columnWidthsAtom = atom((get) => {
|
|
413
|
+
const ix = get(columnWidthsIndexAtom);
|
|
414
|
+
return get(columnSpecAtom).map(
|
|
415
|
+
(col) => ix.get(col.key) ?? col.width ?? get(defaultColumnWidthAtom),
|
|
416
|
+
);
|
|
417
|
+
});
|
|
404
418
|
|
|
405
|
-
|
|
406
|
-
|
|
419
|
+
function CellRendererDebugOverlay({ cellRendererDependencies, names }) {
|
|
420
|
+
/** Debug overlay for cell renderer dependencies */
|
|
421
|
+
const lastRenderDependencies = useRef<any[]>(cellRendererDependencies);
|
|
422
|
+
useEffect(() => {
|
|
423
|
+
let changeDepNames = [];
|
|
424
|
+
for (const [i, dep] of cellRendererDependencies.entries()) {
|
|
425
|
+
if (dep !== lastRenderDependencies.current[i]) {
|
|
426
|
+
changeDepNames.push(names[i]);
|
|
427
|
+
}
|
|
407
428
|
}
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
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 */
|
|
422
|
-
|
|
423
|
-
const onChange = (e) => {
|
|
424
|
-
if (!editable) return;
|
|
425
|
-
if (value === e.target.value) return;
|
|
426
|
-
onCellEdited(rowIndex, col.key, e.target.value);
|
|
427
|
-
};
|
|
428
|
-
|
|
429
|
-
const isSingleCellSelection = singleFocusedCell(state.selection) != null;
|
|
430
|
-
|
|
431
|
-
let _inlineEditor: ReactNode = null;
|
|
432
|
-
if (typeof inlineEditor == "boolean") {
|
|
433
|
-
let _value = value;
|
|
434
|
-
if (
|
|
435
|
-
typeof _renderedValue === "string" ||
|
|
436
|
-
typeof _renderedValue === "number" ||
|
|
437
|
-
_renderedValue == null
|
|
438
|
-
) {
|
|
439
|
-
_value = _renderedValue;
|
|
429
|
+
if (changeDepNames.length > 0) {
|
|
430
|
+
console.log(
|
|
431
|
+
"Cell renderer dependencies changed:",
|
|
432
|
+
changeDepNames.join(", "),
|
|
433
|
+
);
|
|
440
434
|
}
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
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
|
-
}
|
|
451
|
-
|
|
452
|
-
if (_dataEditor != null) {
|
|
453
|
-
className = "editor-cell";
|
|
454
|
-
cellContents = _dataEditor;
|
|
455
|
-
hiddenInput = null;
|
|
456
|
-
} else if (_inlineEditor != null) {
|
|
457
|
-
cellContents = _inlineEditor;
|
|
458
|
-
className = "input-cell";
|
|
459
|
-
hiddenInput = null;
|
|
460
|
-
}
|
|
461
|
-
|
|
462
|
-
// Hidden html input
|
|
463
|
-
return h(
|
|
464
|
-
_Cell,
|
|
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
|
-
],
|
|
477
|
-
);
|
|
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
|
-
}
|
|
486
|
-
|
|
487
|
-
function DataSheetEditToolbar({ onSaveData, onDeleteRows }) {
|
|
488
|
-
const selection = useSelector((state) => state.selection);
|
|
489
|
-
const resetChanges = useSelector((state) => state.resetChanges);
|
|
490
|
-
const hasUpdates = useSelector((state) => state.hasUpdates);
|
|
491
|
-
|
|
492
|
-
return h("div.data-sheet-toolbar", [
|
|
493
|
-
h(ButtonGroup, { minimal: true }, [
|
|
494
|
-
h(AddRowButton),
|
|
495
|
-
h(
|
|
496
|
-
Button,
|
|
497
|
-
{
|
|
498
|
-
intent: Intent.DANGER,
|
|
499
|
-
disabled: onDeleteRows == null,
|
|
500
|
-
onClick() {
|
|
501
|
-
onDeleteRows?.();
|
|
502
|
-
},
|
|
503
|
-
},
|
|
504
|
-
"Delete",
|
|
505
|
-
),
|
|
506
|
-
]),
|
|
507
|
-
h("div.spacer"),
|
|
508
|
-
h(ButtonGroup, [
|
|
509
|
-
h(
|
|
510
|
-
Button,
|
|
511
|
-
{
|
|
512
|
-
intent: Intent.WARNING,
|
|
513
|
-
disabled: !hasUpdates,
|
|
514
|
-
onClick: resetChanges,
|
|
515
|
-
},
|
|
516
|
-
"Reset",
|
|
517
|
-
),
|
|
518
|
-
h.if(onSaveData != null)(
|
|
519
|
-
Button,
|
|
520
|
-
{
|
|
521
|
-
intent: Intent.SUCCESS,
|
|
522
|
-
icon: "floppy-disk",
|
|
523
|
-
disabled: !hasUpdates,
|
|
524
|
-
onClick: onSaveData,
|
|
525
|
-
},
|
|
526
|
-
"Save",
|
|
527
|
-
),
|
|
528
|
-
]),
|
|
529
|
-
]);
|
|
435
|
+
lastRenderDependencies.current = cellRendererDependencies;
|
|
436
|
+
}, cellRendererDependencies);
|
|
437
|
+
return null;
|
|
530
438
|
}
|
|
531
439
|
|
|
532
440
|
export function ScrollToRowControl() {
|
|
@@ -556,31 +464,36 @@ export function ScrollToRowControl() {
|
|
|
556
464
|
]);
|
|
557
465
|
}
|
|
558
466
|
|
|
559
|
-
function
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
467
|
+
function styleParamsForDensity(density: DataSheetDensity) {
|
|
468
|
+
switch (density) {
|
|
469
|
+
case DataSheetDensity.MEDIUM:
|
|
470
|
+
return {
|
|
471
|
+
rowHeight: 24,
|
|
472
|
+
style: {
|
|
473
|
+
"--data-sheet-row-height": "24px",
|
|
474
|
+
"--data-sheet-font-size": "14px",
|
|
475
|
+
},
|
|
476
|
+
};
|
|
477
|
+
case DataSheetDensity.LOW:
|
|
478
|
+
return {
|
|
479
|
+
rowHeight: 30,
|
|
480
|
+
style: {
|
|
481
|
+
"--data-sheet-row-height": "30px",
|
|
482
|
+
"--data-sheet-font-size": "18px",
|
|
483
|
+
},
|
|
484
|
+
};
|
|
485
|
+
case DataSheetDensity.HIGH:
|
|
486
|
+
default:
|
|
487
|
+
return {
|
|
488
|
+
rowHeight: 20,
|
|
489
|
+
style: {
|
|
490
|
+
"--data-sheet-row-height": "20px",
|
|
491
|
+
"--data-sheet-font-size": "12px",
|
|
492
|
+
},
|
|
493
|
+
};
|
|
494
|
+
}
|
|
580
495
|
}
|
|
581
496
|
|
|
582
|
-
export { Cell };
|
|
583
|
-
|
|
584
497
|
export function getRowsToDelete(selection) {
|
|
585
498
|
let rowIndices: number[] = [];
|
|
586
499
|
for (const sel of selection) {
|