@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
|
@@ -0,0 +1,388 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Column header with client-side sort and filter controls.
|
|
3
|
+
*
|
|
4
|
+
* Mirrors the PostgREST column header UX but operates on in-memory data.
|
|
5
|
+
* Sort state is stored in the DataSheet zustand store; filtering uses the
|
|
6
|
+
* existing `setFilter` / `removeFilter` mechanism with auto-generated
|
|
7
|
+
* predicate filters for simple text search.
|
|
8
|
+
*/
|
|
9
|
+
import h from "@macrostrat/hyper";
|
|
10
|
+
import { ColumnHeaderCell } from "@blueprintjs/table";
|
|
11
|
+
import {
|
|
12
|
+
Button,
|
|
13
|
+
ButtonGroup,
|
|
14
|
+
Icon,
|
|
15
|
+
InputGroup,
|
|
16
|
+
Menu,
|
|
17
|
+
Tag as BPTag,
|
|
18
|
+
} from "@blueprintjs/core";
|
|
19
|
+
import { useCallback, useRef } from "react";
|
|
20
|
+
import { useSelector, useStoreAPI } from "../provider";
|
|
21
|
+
import type { ColumnSpec } from "../utils/column-spec";
|
|
22
|
+
import type { ColumnSort, TableFilter } from "../actions/types";
|
|
23
|
+
import type {
|
|
24
|
+
PostgrestColumnFilter,
|
|
25
|
+
ColumnSortEntry,
|
|
26
|
+
} from "../postgrest-table";
|
|
27
|
+
|
|
28
|
+
export interface ColumnActionsConfig {
|
|
29
|
+
activeSort?: ColumnSortEntry | null;
|
|
30
|
+
activeFilter?: PostgrestColumnFilter | null;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface ColumnHeaderRendererProps extends ColumnActionsConfig {
|
|
34
|
+
col: ColumnSpec;
|
|
35
|
+
colIndex: number;
|
|
36
|
+
//actions: ColumnHeaderActions;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// ---- Auto-generated column filter helpers ----
|
|
40
|
+
|
|
41
|
+
/** Build a simple text-contains filter for a column. */
|
|
42
|
+
function buildAutoFilter(col: ColumnSpec): TableFilter {
|
|
43
|
+
return {
|
|
44
|
+
id: autoFilterId(col.key),
|
|
45
|
+
name: col.name,
|
|
46
|
+
columnKey: col.key,
|
|
47
|
+
icon: "filter",
|
|
48
|
+
defaultState: { search: "" },
|
|
49
|
+
predicate(row, state) {
|
|
50
|
+
if (!state?.search) return true;
|
|
51
|
+
const val = String(row[col.key] ?? "").toLowerCase();
|
|
52
|
+
return val.includes(state.search.toLowerCase());
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Stable filter-ID prefix for auto-generated column filters. */
|
|
58
|
+
export function autoFilterId(key: string) {
|
|
59
|
+
return `__col_${key}`;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// ---- Column header cell ----
|
|
63
|
+
|
|
64
|
+
/** Client-side column header cell with sort/filter controls.
|
|
65
|
+
* Reads sort state from the store and manages column filters via
|
|
66
|
+
* the existing `setFilter`/`removeFilter` store methods. */
|
|
67
|
+
export function renderColumnHeaderCell({
|
|
68
|
+
col,
|
|
69
|
+
colIndex,
|
|
70
|
+
activeSort,
|
|
71
|
+
activeFilter,
|
|
72
|
+
}: ColumnHeaderRendererProps) {
|
|
73
|
+
return h(ColumnHeaderCell, { name: col.name });
|
|
74
|
+
|
|
75
|
+
const isSortable = col.sortable === true;
|
|
76
|
+
const isFilterable =
|
|
77
|
+
col.filterable === true || typeof col.filterable === "object";
|
|
78
|
+
|
|
79
|
+
if (!isSortable && !isFilterable) {
|
|
80
|
+
return h(ColumnHeaderCell, { name: col.name });
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const hasFilterActive =
|
|
84
|
+
activeFilter != null &&
|
|
85
|
+
activeFilter.state?.search !== "" &&
|
|
86
|
+
activeFilter.state?.search != null;
|
|
87
|
+
|
|
88
|
+
const hasSortActive = activeSort != null;
|
|
89
|
+
|
|
90
|
+
return h(ColumnHeaderCell, {
|
|
91
|
+
name: col.name,
|
|
92
|
+
nameRenderer: () =>
|
|
93
|
+
h(ColumnHeaderName, { col, hasSortActive, hasFilterActive, activeSort }),
|
|
94
|
+
menuRenderer: () =>
|
|
95
|
+
h(ClientColumnActionsMenu, {
|
|
96
|
+
col,
|
|
97
|
+
isSortable,
|
|
98
|
+
isFilterable,
|
|
99
|
+
activeSort,
|
|
100
|
+
activeFilter,
|
|
101
|
+
}),
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Column name with active sort/filter indicator icons. */
|
|
106
|
+
function ColumnHeaderName({ col, hasSortActive, hasFilterActive, activeSort }) {
|
|
107
|
+
return h(
|
|
108
|
+
"div",
|
|
109
|
+
{ style: { display: "flex", alignItems: "center", width: "100%" } },
|
|
110
|
+
[
|
|
111
|
+
h(
|
|
112
|
+
"span",
|
|
113
|
+
{
|
|
114
|
+
style: {
|
|
115
|
+
flex: 1,
|
|
116
|
+
overflow: "hidden",
|
|
117
|
+
textOverflow: "ellipsis",
|
|
118
|
+
whiteSpace: "nowrap",
|
|
119
|
+
},
|
|
120
|
+
},
|
|
121
|
+
col.name,
|
|
122
|
+
),
|
|
123
|
+
h.if(hasSortActive || hasFilterActive)(
|
|
124
|
+
"span",
|
|
125
|
+
{
|
|
126
|
+
style: {
|
|
127
|
+
display: "flex",
|
|
128
|
+
alignItems: "center",
|
|
129
|
+
gap: "2px",
|
|
130
|
+
marginLeft: "4px",
|
|
131
|
+
flexShrink: 0,
|
|
132
|
+
},
|
|
133
|
+
},
|
|
134
|
+
[
|
|
135
|
+
h.if(hasSortActive)(Icon, {
|
|
136
|
+
icon: activeSort?.ascending ? "sort-asc" : "sort-desc",
|
|
137
|
+
size: 12,
|
|
138
|
+
style: { color: "var(--intent-primary, #2965cc)" },
|
|
139
|
+
}),
|
|
140
|
+
h.if(hasFilterActive)(Icon, {
|
|
141
|
+
icon: "filter",
|
|
142
|
+
size: 12,
|
|
143
|
+
style: { color: "var(--intent-warning, #d99e0b)" },
|
|
144
|
+
}),
|
|
145
|
+
],
|
|
146
|
+
),
|
|
147
|
+
],
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
interface ClientColumnActionsMenuProps extends ColumnActionsConfig {
|
|
152
|
+
col: ColumnSpec;
|
|
153
|
+
isSortable: boolean;
|
|
154
|
+
isFilterable: boolean;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** Menu content rendered inside ColumnHeaderCell's built-in dropdown. */
|
|
158
|
+
function ClientColumnActionsMenu({
|
|
159
|
+
col,
|
|
160
|
+
isSortable,
|
|
161
|
+
isFilterable,
|
|
162
|
+
activeSort,
|
|
163
|
+
activeFilter,
|
|
164
|
+
}) {
|
|
165
|
+
const storeAPI = useStoreAPI();
|
|
166
|
+
const filterInputRef = useRef<HTMLInputElement>(null);
|
|
167
|
+
|
|
168
|
+
const applyFilter = useCallback(() => {
|
|
169
|
+
const val = filterInputRef.current?.value ?? "";
|
|
170
|
+
const store = storeAPI.getState();
|
|
171
|
+
if (val.trim() === "") {
|
|
172
|
+
store.removeFilter(autoFilterId(col.key));
|
|
173
|
+
} else {
|
|
174
|
+
const filter = buildAutoFilter(col);
|
|
175
|
+
store.setFilter(filter.id, filter, { search: val });
|
|
176
|
+
}
|
|
177
|
+
}, [col, storeAPI]);
|
|
178
|
+
|
|
179
|
+
const hasAnyActive =
|
|
180
|
+
activeSort != null ||
|
|
181
|
+
(activeFilter != null &&
|
|
182
|
+
activeFilter.state?.search !== "" &&
|
|
183
|
+
activeFilter.state?.search != null);
|
|
184
|
+
|
|
185
|
+
return h(Menu, { className: "column-actions-menu" }, [
|
|
186
|
+
h("div", { style: { padding: "12px", minWidth: "220px" } }, [
|
|
187
|
+
h.if(isSortable)("div", { style: { marginBottom: "12px" } }, [
|
|
188
|
+
h("div", { style: sortFilterLabelStyle }, "Sort"),
|
|
189
|
+
h("div", { style: { display: "flex", gap: "4px" } }, [
|
|
190
|
+
h(ButtonGroup, { minimal: true }, [
|
|
191
|
+
h(
|
|
192
|
+
Button,
|
|
193
|
+
{
|
|
194
|
+
icon: "sort-asc",
|
|
195
|
+
active: activeSort?.ascending === true,
|
|
196
|
+
intent: activeSort?.ascending === true ? "primary" : "none",
|
|
197
|
+
onClick() {
|
|
198
|
+
const store = storeAPI.getState();
|
|
199
|
+
if (activeSort?.ascending === true) {
|
|
200
|
+
store.setColumnSort(col.key, null);
|
|
201
|
+
} else {
|
|
202
|
+
store.setColumnSort(col.key, true);
|
|
203
|
+
}
|
|
204
|
+
},
|
|
205
|
+
},
|
|
206
|
+
"A→Z",
|
|
207
|
+
),
|
|
208
|
+
h(
|
|
209
|
+
Button,
|
|
210
|
+
{
|
|
211
|
+
icon: "sort-desc",
|
|
212
|
+
active: activeSort?.ascending === false,
|
|
213
|
+
intent: activeSort?.ascending === false ? "primary" : "none",
|
|
214
|
+
onClick() {
|
|
215
|
+
const store = storeAPI.getState();
|
|
216
|
+
if (activeSort?.ascending === false) {
|
|
217
|
+
store.setColumnSort(col.key, null);
|
|
218
|
+
} else {
|
|
219
|
+
store.setColumnSort(col.key, false);
|
|
220
|
+
}
|
|
221
|
+
},
|
|
222
|
+
},
|
|
223
|
+
"Z→A",
|
|
224
|
+
),
|
|
225
|
+
]),
|
|
226
|
+
]),
|
|
227
|
+
]),
|
|
228
|
+
h.if(isFilterable)("div", { style: { marginBottom: "12px" } }, [
|
|
229
|
+
h("div", { style: sortFilterLabelStyle }, "Filter"),
|
|
230
|
+
h(
|
|
231
|
+
"div",
|
|
232
|
+
{ style: { display: "flex", gap: "4px", alignItems: "center" } },
|
|
233
|
+
[
|
|
234
|
+
h(InputGroup, {
|
|
235
|
+
className: "filter-value-input",
|
|
236
|
+
small: true,
|
|
237
|
+
placeholder: "Search…",
|
|
238
|
+
defaultValue: activeFilterEntry?.state?.search ?? "",
|
|
239
|
+
inputRef: filterInputRef,
|
|
240
|
+
onKeyDown(e) {
|
|
241
|
+
if (e.key === "Enter") {
|
|
242
|
+
applyFilter();
|
|
243
|
+
}
|
|
244
|
+
},
|
|
245
|
+
rightElement: h(Button, {
|
|
246
|
+
icon: "arrow-right",
|
|
247
|
+
minimal: true,
|
|
248
|
+
small: true,
|
|
249
|
+
onClick: applyFilter,
|
|
250
|
+
}),
|
|
251
|
+
}),
|
|
252
|
+
],
|
|
253
|
+
),
|
|
254
|
+
]),
|
|
255
|
+
h.if(hasAnyActive)(
|
|
256
|
+
"div",
|
|
257
|
+
{
|
|
258
|
+
style: {
|
|
259
|
+
display: "flex",
|
|
260
|
+
justifyContent: "flex-end",
|
|
261
|
+
paddingTop: "4px",
|
|
262
|
+
borderTop: "1px solid var(--divider-color, rgba(16, 22, 26, 0.15))",
|
|
263
|
+
},
|
|
264
|
+
},
|
|
265
|
+
[
|
|
266
|
+
h(
|
|
267
|
+
Button,
|
|
268
|
+
{
|
|
269
|
+
small: true,
|
|
270
|
+
minimal: true,
|
|
271
|
+
intent: "danger",
|
|
272
|
+
icon: "cross",
|
|
273
|
+
onClick() {
|
|
274
|
+
const store = storeAPI.getState();
|
|
275
|
+
store.setColumnSort(col.key, null);
|
|
276
|
+
store.removeFilter(autoFilterId(col.key));
|
|
277
|
+
if (filterInputRef.current) {
|
|
278
|
+
filterInputRef.current.value = "";
|
|
279
|
+
}
|
|
280
|
+
},
|
|
281
|
+
},
|
|
282
|
+
"Clear all",
|
|
283
|
+
),
|
|
284
|
+
],
|
|
285
|
+
),
|
|
286
|
+
]),
|
|
287
|
+
]);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
const sortFilterLabelStyle = {
|
|
291
|
+
fontWeight: 600,
|
|
292
|
+
fontSize: "11px",
|
|
293
|
+
textTransform: "uppercase" as const,
|
|
294
|
+
letterSpacing: "0.5px",
|
|
295
|
+
color: "var(--secondary-color, #5c7080)",
|
|
296
|
+
marginBottom: "6px",
|
|
297
|
+
};
|
|
298
|
+
|
|
299
|
+
// ---- Sort/filter state bar ----
|
|
300
|
+
|
|
301
|
+
/** Bar showing active client-side sort and filter state as removable tags.
|
|
302
|
+
* Rendered automatically by `_DataSheet` when sorts or column filters
|
|
303
|
+
* are active. */
|
|
304
|
+
export function SortFilterBar() {
|
|
305
|
+
const columnSorts = useSelector((state) => state.columnSorts);
|
|
306
|
+
const activeFilters = useSelector((state) => state.activeFilters);
|
|
307
|
+
const storeAPI = useStoreAPI();
|
|
308
|
+
|
|
309
|
+
// Collect auto-generated column filters
|
|
310
|
+
const columnFilterEntries: { key: string; search: string }[] = [];
|
|
311
|
+
for (const [id, entry] of activeFilters) {
|
|
312
|
+
if (id.startsWith("__col_") && entry.state?.search) {
|
|
313
|
+
columnFilterEntries.push({
|
|
314
|
+
key: id.replace("__col_", ""),
|
|
315
|
+
search: entry.state.search,
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
const hasAnything = columnSorts.length > 0 || columnFilterEntries.length > 0;
|
|
321
|
+
if (!hasAnything) return null;
|
|
322
|
+
|
|
323
|
+
return h(
|
|
324
|
+
"div",
|
|
325
|
+
{
|
|
326
|
+
style: {
|
|
327
|
+
display: "flex",
|
|
328
|
+
flexDirection: "row",
|
|
329
|
+
flexWrap: "wrap",
|
|
330
|
+
gap: "4px",
|
|
331
|
+
marginBottom: "4px",
|
|
332
|
+
alignItems: "center",
|
|
333
|
+
},
|
|
334
|
+
},
|
|
335
|
+
[
|
|
336
|
+
columnSorts.map((s) =>
|
|
337
|
+
h(
|
|
338
|
+
BPTag,
|
|
339
|
+
{
|
|
340
|
+
key: `sort-${s.key}`,
|
|
341
|
+
icon: s.ascending ? "sort-asc" : "sort-desc",
|
|
342
|
+
intent: "primary",
|
|
343
|
+
onRemove() {
|
|
344
|
+
storeAPI.getState().setColumnSort(s.key, null);
|
|
345
|
+
},
|
|
346
|
+
minimal: true,
|
|
347
|
+
},
|
|
348
|
+
`${s.key}: ${s.ascending ? "A→Z" : "Z→A"}`,
|
|
349
|
+
),
|
|
350
|
+
),
|
|
351
|
+
columnFilterEntries.map((f) =>
|
|
352
|
+
h(
|
|
353
|
+
BPTag,
|
|
354
|
+
{
|
|
355
|
+
key: `filter-${f.key}`,
|
|
356
|
+
icon: "filter",
|
|
357
|
+
intent: "warning",
|
|
358
|
+
onRemove() {
|
|
359
|
+
storeAPI.getState().removeFilter(autoFilterId(f.key));
|
|
360
|
+
},
|
|
361
|
+
minimal: true,
|
|
362
|
+
},
|
|
363
|
+
`${f.key} contains "${f.search}"`,
|
|
364
|
+
),
|
|
365
|
+
),
|
|
366
|
+
h(
|
|
367
|
+
Button,
|
|
368
|
+
{
|
|
369
|
+
minimal: true,
|
|
370
|
+
small: true,
|
|
371
|
+
icon: "cross",
|
|
372
|
+
onClick() {
|
|
373
|
+
const store = storeAPI.getState();
|
|
374
|
+
// Clear all column sorts
|
|
375
|
+
store.clearColumnSorts();
|
|
376
|
+
// Clear auto-generated column filters
|
|
377
|
+
for (const [id] of activeFilters) {
|
|
378
|
+
if (id.startsWith("__col_")) {
|
|
379
|
+
store.removeFilter(id);
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
},
|
|
383
|
+
},
|
|
384
|
+
"Clear all",
|
|
385
|
+
),
|
|
386
|
+
],
|
|
387
|
+
);
|
|
388
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { Filter } from "./filter";
|
|
2
|
+
|
|
3
|
+
interface DataParameters {
|
|
4
|
+
group?: string;
|
|
5
|
+
select: {
|
|
6
|
+
page?: string;
|
|
7
|
+
pageSize?: string;
|
|
8
|
+
};
|
|
9
|
+
filter: {
|
|
10
|
+
[key: string]: Filter; // Used for filters
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const cloneDataParameters = (
|
|
15
|
+
dataParameters: DataParameters
|
|
16
|
+
): DataParameters => {
|
|
17
|
+
return {
|
|
18
|
+
group: dataParameters?.group,
|
|
19
|
+
select: { ...dataParameters?.select },
|
|
20
|
+
filter: Object.entries(dataParameters?.filter).reduce(
|
|
21
|
+
(acc, [key, value]) => {
|
|
22
|
+
acc[key] = value.clone();
|
|
23
|
+
return acc;
|
|
24
|
+
},
|
|
25
|
+
{}
|
|
26
|
+
),
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/*
|
|
31
|
+
* Check if a column is active in the data parameters
|
|
32
|
+
*/
|
|
33
|
+
export const isColumnActive = (
|
|
34
|
+
dataParameters: DataParameters,
|
|
35
|
+
column: string
|
|
36
|
+
) => {
|
|
37
|
+
return (
|
|
38
|
+
(column in dataParameters.filter &&
|
|
39
|
+
dataParameters.filter[column].is_valid()) ||
|
|
40
|
+
column == dataParameters?.group
|
|
41
|
+
);
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export { cloneDataParameters, DataParameters };
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { ColumnOperators } from "../tables/defs";
|
|
2
|
+
|
|
3
|
+
export class Filter {
|
|
4
|
+
readonly column_name: string;
|
|
5
|
+
readonly operator: ColumnOperators | undefined;
|
|
6
|
+
readonly value: string | boolean | number | null;
|
|
7
|
+
|
|
8
|
+
constructor(
|
|
9
|
+
column_name: string,
|
|
10
|
+
operator: ColumnOperators | undefined,
|
|
11
|
+
value: string | boolean | number | null
|
|
12
|
+
) {
|
|
13
|
+
this.column_name = column_name;
|
|
14
|
+
this.operator = operator;
|
|
15
|
+
this.value = value;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
get formattedValue() {
|
|
19
|
+
switch (this.operator) {
|
|
20
|
+
case "in":
|
|
21
|
+
return `(${this.value})`;
|
|
22
|
+
default:
|
|
23
|
+
return this.value;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
get urlValue() {
|
|
28
|
+
return `${this.operator}.${this.formattedValue}`;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
clone = () => {
|
|
32
|
+
return new Filter(this.column_name, this.operator, this.value);
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
passes = (data: Record<string, string | boolean | number | null>) => {
|
|
36
|
+
const filterValue = data[this.column_name];
|
|
37
|
+
switch (this.operator) {
|
|
38
|
+
case "eq":
|
|
39
|
+
return filterValue == this.value;
|
|
40
|
+
case "lt":
|
|
41
|
+
return filterValue < this.value;
|
|
42
|
+
case "le":
|
|
43
|
+
return filterValue <= this.value;
|
|
44
|
+
case "gt":
|
|
45
|
+
return filterValue > this.value;
|
|
46
|
+
case "ge":
|
|
47
|
+
return filterValue >= this.value;
|
|
48
|
+
case "ne":
|
|
49
|
+
return filterValue != this.value;
|
|
50
|
+
case "is_distinct_from":
|
|
51
|
+
return filterValue != this.value;
|
|
52
|
+
case "is_not_distinct_from":
|
|
53
|
+
return filterValue == this.value;
|
|
54
|
+
case "like":
|
|
55
|
+
if (typeof filterValue != "string" || typeof this.value != "string") {
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
return filterValue.includes(this.value);
|
|
59
|
+
case "in":
|
|
60
|
+
if (typeof filterValue != "string" || typeof this.value != "string") {
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
return this.value.includes(filterValue);
|
|
64
|
+
case "is":
|
|
65
|
+
return filterValue == this.value;
|
|
66
|
+
default:
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
is_valid = () => {
|
|
72
|
+
if (this.operator == undefined) {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
return true;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
to_array = () => {
|
|
79
|
+
return [this.column_name, this.operator + "." + this.formattedValue];
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/*
|
|
84
|
+
* Convert url and filter to a parameterized URL
|
|
85
|
+
*/
|
|
86
|
+
export const addFilterToURL = (url: URL, filter: Filter) => {
|
|
87
|
+
if (filter.is_valid()) {
|
|
88
|
+
const [columnName, filterValue] = filter.to_array();
|
|
89
|
+
url.searchParams.append(columnName, filterValue);
|
|
90
|
+
}
|
|
91
|
+
return url;
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
/*
|
|
95
|
+
* Sort the filters by column name and stringify
|
|
96
|
+
*/
|
|
97
|
+
export const createFiltersKey = (filters: Filter[]) => {
|
|
98
|
+
const sortFilterEntries = (a: Filter, b: Filter) => {
|
|
99
|
+
return a.column_name.localeCompare(b.column_name);
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
return filters
|
|
103
|
+
.filter((filter) => filter.is_valid())
|
|
104
|
+
.sort(sortFilterEntries)
|
|
105
|
+
.map((filter) => filter.urlValue)
|
|
106
|
+
.join("&");
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
export const rowPassesFilters = (
|
|
110
|
+
row: Record<string, string | boolean | number | null>,
|
|
111
|
+
filters: Filter[] | undefined
|
|
112
|
+
) => {
|
|
113
|
+
for (const filter of filters) {
|
|
114
|
+
// If the filter is invalid skip it
|
|
115
|
+
if (!filter.is_valid()) {
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (!filter.passes(row)) {
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return true;
|
|
124
|
+
};
|