@dxos/react-ui-table 0.8.4-main.ead640a → 0.8.4-main.effb148878
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/LICENSE +102 -5
- package/dist/lib/browser/chunk-J5LGTIGS.mjs +10 -0
- package/dist/lib/browser/chunk-J5LGTIGS.mjs.map +7 -0
- package/dist/lib/browser/chunk-KC62K4WS.mjs +1696 -0
- package/dist/lib/browser/chunk-KC62K4WS.mjs.map +7 -0
- package/dist/lib/browser/chunk-VWQWF3CP.mjs +67 -0
- package/dist/lib/browser/chunk-VWQWF3CP.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +1128 -1044
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +35 -46
- package/dist/lib/browser/testing/index.mjs.map +3 -3
- package/dist/lib/browser/translations.mjs +36 -0
- package/dist/lib/browser/translations.mjs.map +7 -0
- package/dist/lib/browser/types/index.mjs +4 -3
- package/dist/lib/node-esm/chunk-62FOU4M7.mjs +1698 -0
- package/dist/lib/node-esm/chunk-62FOU4M7.mjs.map +7 -0
- package/dist/lib/node-esm/chunk-HSLMI22Q.mjs +11 -0
- package/dist/lib/node-esm/chunk-HSLMI22Q.mjs.map +7 -0
- package/dist/lib/node-esm/chunk-UBXOF64L.mjs +68 -0
- package/dist/lib/node-esm/chunk-UBXOF64L.mjs.map +7 -0
- package/dist/lib/node-esm/index.mjs +1128 -1044
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/testing/index.mjs +35 -46
- package/dist/lib/node-esm/testing/index.mjs.map +3 -3
- package/dist/lib/node-esm/translations.mjs +37 -0
- package/dist/lib/node-esm/translations.mjs.map +7 -0
- package/dist/lib/node-esm/types/index.mjs +4 -3
- package/dist/types/src/components/Table/ColumnActionsMenu.d.ts +1 -2
- package/dist/types/src/components/Table/ColumnActionsMenu.d.ts.map +1 -1
- package/dist/types/src/components/Table/ColumnSettings.d.ts +2 -2
- package/dist/types/src/components/Table/ColumnSettings.d.ts.map +1 -1
- package/dist/types/src/components/Table/DynamicTable.d.ts +6 -7
- package/dist/types/src/components/Table/DynamicTable.d.ts.map +1 -1
- package/dist/types/src/components/Table/DynamicTable.stories.d.ts +22 -37
- package/dist/types/src/components/Table/DynamicTable.stories.d.ts.map +1 -1
- package/dist/types/src/components/Table/Relations.stories.d.ts +85 -81
- package/dist/types/src/components/Table/Relations.stories.d.ts.map +1 -1
- package/dist/types/src/components/Table/RowActionsMenu.d.ts.map +1 -1
- package/dist/types/src/components/Table/Table.d.ts +20 -21
- package/dist/types/src/components/Table/Table.d.ts.map +1 -1
- package/dist/types/src/components/Table/Table.stories.d.ts +93 -26
- package/dist/types/src/components/Table/Table.stories.d.ts.map +1 -1
- package/dist/types/src/components/Table/TableContent.d.ts +18 -0
- package/dist/types/src/components/Table/TableContent.d.ts.map +1 -0
- package/dist/types/src/components/Table/TableToolbar.d.ts +21 -0
- package/dist/types/src/components/Table/TableToolbar.d.ts.map +1 -0
- package/dist/types/src/components/TableCellEditor/CellValidationMessage.d.ts +1 -1
- package/dist/types/src/components/TableCellEditor/CellValidationMessage.d.ts.map +1 -1
- package/dist/types/src/components/TableCellEditor/FormCellEditor.d.ts +10 -12
- package/dist/types/src/components/TableCellEditor/FormCellEditor.d.ts.map +1 -1
- package/dist/types/src/components/TableCellEditor/TableCellEditor.d.ts +5 -7
- package/dist/types/src/components/TableCellEditor/TableCellEditor.d.ts.map +1 -1
- package/dist/types/src/components/TableCellEditor/TableCellEditor.stories.d.ts +24 -26
- package/dist/types/src/components/TableCellEditor/TableCellEditor.stories.d.ts.map +1 -1
- package/dist/types/src/components/index.d.ts +0 -1
- package/dist/types/src/components/index.d.ts.map +1 -1
- package/dist/types/src/components/testing.d.ts +22 -15
- package/dist/types/src/components/testing.d.ts.map +1 -1
- package/dist/types/src/hooks/index.d.ts +2 -1
- package/dist/types/src/hooks/index.d.ts.map +1 -1
- package/dist/types/src/hooks/useAddRow.d.ts +5 -6
- package/dist/types/src/hooks/useAddRow.d.ts.map +1 -1
- package/dist/types/src/hooks/useProjectionModel.d.ts +6 -0
- package/dist/types/src/hooks/useProjectionModel.d.ts.map +1 -0
- package/dist/types/src/hooks/useTableModel.d.ts +9 -9
- package/dist/types/src/hooks/useTableModel.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +0 -2
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/model/modal-controller.d.ts +5 -1
- package/dist/types/src/model/modal-controller.d.ts.map +1 -1
- package/dist/types/src/model/selection-model.d.ts +18 -9
- package/dist/types/src/model/selection-model.d.ts.map +1 -1
- package/dist/types/src/model/table-model.d.ts +104 -25
- package/dist/types/src/model/table-model.d.ts.map +1 -1
- package/dist/types/src/model/table-presentation.d.ts +3 -1
- package/dist/types/src/model/table-presentation.d.ts.map +1 -1
- package/dist/types/src/playwright/TableManager.d.ts +2 -1
- package/dist/types/src/playwright/TableManager.d.ts.map +1 -1
- package/dist/types/src/playwright/playwright.config.d.ts.map +1 -1
- package/dist/types/src/testing/useTestTableModel.d.ts +18 -15
- package/dist/types/src/testing/useTestTableModel.d.ts.map +1 -1
- package/dist/types/src/translations.d.ts +21 -23
- package/dist/types/src/translations.d.ts.map +1 -1
- package/dist/types/src/types/Table.d.ts +32 -0
- package/dist/types/src/types/Table.d.ts.map +1 -0
- package/dist/types/src/types/index.d.ts +1 -1
- package/dist/types/src/types/index.d.ts.map +1 -1
- package/dist/types/src/util/dynamic-table.d.ts +14 -11
- package/dist/types/src/util/dynamic-table.d.ts.map +1 -1
- package/dist/types/src/util/dynamic-table.test.d.ts +2 -0
- package/dist/types/src/util/dynamic-table.test.d.ts.map +1 -0
- package/dist/types/src/util/index.d.ts +2 -0
- package/dist/types/src/util/index.d.ts.map +1 -1
- package/dist/types/src/util/query-ast.d.ts +7 -0
- package/dist/types/src/util/query-ast.d.ts.map +1 -0
- package/dist/types/src/util/schema.d.ts.map +1 -1
- package/dist/types/src/util/signals.d.ts.map +1 -1
- package/dist/types/src/util/sort.d.ts +12 -0
- package/dist/types/src/util/sort.d.ts.map +1 -0
- package/dist/types/src/util/table-buttons.d.ts +20 -20
- package/dist/types/src/util/table-buttons.d.ts.map +1 -1
- package/dist/types/src/util/table-controls.d.ts +4 -4
- package/dist/types/src/util/table-controls.d.ts.map +1 -1
- package/dist/types/src/util/tag.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +69 -78
- package/src/components/Table/ColumnActionsMenu.tsx +17 -12
- package/src/components/Table/ColumnSettings.tsx +8 -9
- package/src/components/Table/DynamicTable.stories.tsx +32 -32
- package/src/components/Table/DynamicTable.tsx +32 -33
- package/src/components/Table/Relations.stories.tsx +116 -82
- package/src/components/Table/RowActionsMenu.tsx +27 -17
- package/src/components/Table/Table.stories.tsx +129 -116
- package/src/components/Table/Table.tsx +43 -407
- package/src/components/Table/TableContent.tsx +426 -0
- package/src/components/Table/TableToolbar.tsx +111 -0
- package/src/components/TableCellEditor/CellValidationMessage.tsx +5 -6
- package/src/components/TableCellEditor/FormCellEditor.tsx +119 -135
- package/src/components/TableCellEditor/TableCellEditor.stories.tsx +29 -22
- package/src/components/TableCellEditor/TableCellEditor.tsx +35 -33
- package/src/components/index.ts +0 -1
- package/src/components/testing.ts +38 -17
- package/src/hooks/index.ts +2 -1
- package/src/hooks/useAddRow.ts +8 -10
- package/src/hooks/useProjectionModel.ts +38 -0
- package/src/hooks/useTableModel.ts +36 -34
- package/src/index.ts +0 -4
- package/src/model/modal-controller.ts +23 -12
- package/src/model/selection-model.ts +75 -44
- package/src/model/table-model.test.ts +77 -55
- package/src/model/table-model.ts +437 -171
- package/src/model/table-presentation.test.ts +54 -50
- package/src/model/table-presentation.ts +44 -40
- package/src/playwright/TableManager.ts +13 -6
- package/src/playwright/playwright.config.ts +1 -1
- package/src/playwright/smoke.spec.ts +59 -13
- package/src/testing/useTestTableModel.ts +49 -43
- package/src/translations.ts +20 -22
- package/src/types/Table.ts +81 -0
- package/src/types/index.ts +1 -1
- package/src/util/dynamic-table.test.ts +63 -0
- package/src/util/dynamic-table.ts +49 -35
- package/src/util/index.ts +2 -0
- package/src/util/query-ast.ts +22 -0
- package/src/util/sort.ts +90 -0
- package/src/util/table-buttons.ts +1 -1
- package/src/util/table-controls.ts +2 -2
- package/dist/lib/browser/chunk-4K7JPALT.mjs +0 -66
- package/dist/lib/browser/chunk-4K7JPALT.mjs.map +0 -7
- package/dist/lib/browser/chunk-OZTXLZGM.mjs +0 -1804
- package/dist/lib/browser/chunk-OZTXLZGM.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-MK6OVUF6.mjs +0 -67
- package/dist/lib/node-esm/chunk-MK6OVUF6.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-S56Z4V4X.mjs +0 -1805
- package/dist/lib/node-esm/chunk-S56Z4V4X.mjs.map +0 -7
- package/dist/types/src/components/TableToolbar/TableToolbar.d.ts +0 -21
- package/dist/types/src/components/TableToolbar/TableToolbar.d.ts.map +0 -1
- package/dist/types/src/components/TableToolbar/index.d.ts +0 -2
- package/dist/types/src/components/TableToolbar/index.d.ts.map +0 -1
- package/dist/types/src/model/table-sorting.d.ts +0 -52
- package/dist/types/src/model/table-sorting.d.ts.map +0 -1
- package/dist/types/src/types/schema.d.ts +0 -28
- package/dist/types/src/types/schema.d.ts.map +0 -1
- package/src/components/TableToolbar/TableToolbar.tsx +0 -102
- package/src/components/TableToolbar/index.ts +0 -5
- package/src/model/table-sorting.ts +0 -225
- package/src/types/schema.ts +0 -73
package/src/hooks/index.ts
CHANGED
package/src/hooks/useAddRow.ts
CHANGED
|
@@ -2,17 +2,15 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import type * as Schema from 'effect/Schema';
|
|
6
5
|
import { useCallback } from 'react';
|
|
7
6
|
|
|
8
|
-
import { type
|
|
9
|
-
import { Obj } from '@dxos/echo';
|
|
7
|
+
import { type Database, Obj, type Type } from '@dxos/echo';
|
|
10
8
|
|
|
11
9
|
import { type InsertRowResult } from '../model';
|
|
12
10
|
|
|
13
|
-
export type
|
|
14
|
-
|
|
15
|
-
schema?:
|
|
11
|
+
export type UseAddRowProps = {
|
|
12
|
+
db?: Database.Database;
|
|
13
|
+
schema?: Type.AnyObj;
|
|
16
14
|
};
|
|
17
15
|
|
|
18
16
|
/**
|
|
@@ -20,12 +18,12 @@ export type UseAddRowParams = {
|
|
|
20
18
|
* Returns a callback that attempts to create an object and returns boolean success/failure.
|
|
21
19
|
* Can accept optional data to create objects with specific content (used for committing draft rows).
|
|
22
20
|
*/
|
|
23
|
-
export const useAddRow = ({
|
|
21
|
+
export const useAddRow = ({ db, schema }: UseAddRowProps) => {
|
|
24
22
|
return useCallback(
|
|
25
23
|
(data?: any): InsertRowResult => {
|
|
26
|
-
if (
|
|
24
|
+
if (db && schema) {
|
|
27
25
|
try {
|
|
28
|
-
|
|
26
|
+
db.add(Obj.make(schema, data ?? {}));
|
|
29
27
|
return 'final';
|
|
30
28
|
} catch (error) {
|
|
31
29
|
return 'draft';
|
|
@@ -33,6 +31,6 @@ export const useAddRow = ({ space, schema }: UseAddRowParams) => {
|
|
|
33
31
|
}
|
|
34
32
|
return 'final';
|
|
35
33
|
},
|
|
36
|
-
[
|
|
34
|
+
[db, schema],
|
|
37
35
|
);
|
|
38
36
|
};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type Registry } from '@effect-atom/atom-react';
|
|
6
|
+
import { useState } from 'react';
|
|
7
|
+
|
|
8
|
+
import { Type } from '@dxos/echo';
|
|
9
|
+
import { useAsyncEffect } from '@dxos/react-ui';
|
|
10
|
+
import { ProjectionModel, createEchoChangeCallback } from '@dxos/schema';
|
|
11
|
+
|
|
12
|
+
import { type Table } from '../types';
|
|
13
|
+
|
|
14
|
+
export const useProjectionModel = <S extends Type.AnyEntity>(
|
|
15
|
+
schema: S | undefined,
|
|
16
|
+
table: Table.Table | undefined,
|
|
17
|
+
registry: Registry.Registry,
|
|
18
|
+
) => {
|
|
19
|
+
const [projection, setProjection] = useState<ProjectionModel | undefined>();
|
|
20
|
+
|
|
21
|
+
useAsyncEffect(async () => {
|
|
22
|
+
if (schema && table) {
|
|
23
|
+
const view = await table.view.load();
|
|
24
|
+
// Use the live jsonSchema reference for reactivity.
|
|
25
|
+
const jsonSchema = schema.jsonSchema;
|
|
26
|
+
|
|
27
|
+
// Always use createEchoChangeCallback since the view is ECHO-backed.
|
|
28
|
+
// Pass the type entity only when stored, to allow schema mutations.
|
|
29
|
+
const change = createEchoChangeCallback(view, Type.getDatabase(schema) != null ? schema : undefined);
|
|
30
|
+
|
|
31
|
+
const projection = new ProjectionModel({ registry, view, baseSchema: jsonSchema, change });
|
|
32
|
+
projection.normalizeView();
|
|
33
|
+
setProjection(projection);
|
|
34
|
+
}
|
|
35
|
+
}, [schema, table, registry]);
|
|
36
|
+
|
|
37
|
+
return projection;
|
|
38
|
+
};
|
|
@@ -2,58 +2,65 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
6
|
-
import
|
|
7
|
-
import { useEffect, useMemo, useState } from 'react';
|
|
5
|
+
import { RegistryContext } from '@effect-atom/atom-react';
|
|
6
|
+
import { useContext, useEffect, useMemo, useState } from 'react';
|
|
8
7
|
|
|
9
|
-
import { type
|
|
10
|
-
import { type Live } from '@dxos/live-object';
|
|
11
|
-
import { fullyQualifiedId } from '@dxos/react-client/echo';
|
|
8
|
+
import { type Database, Obj } from '@dxos/echo';
|
|
12
9
|
import { useSelected, useSelectionActions } from '@dxos/react-ui-attention';
|
|
13
|
-
import { type
|
|
10
|
+
import { type ProjectionModel } from '@dxos/schema';
|
|
14
11
|
import { isNonNullable } from '@dxos/util';
|
|
15
12
|
|
|
16
|
-
import {
|
|
13
|
+
import {
|
|
14
|
+
TableModel,
|
|
15
|
+
type TableModelProps,
|
|
16
|
+
type TableRow,
|
|
17
|
+
type TableRowAction,
|
|
18
|
+
createEchoChangeCallback,
|
|
19
|
+
} from '../model';
|
|
20
|
+
import { type Table } from '../types';
|
|
17
21
|
|
|
18
|
-
export type
|
|
19
|
-
|
|
20
|
-
schema?: JsonSchemaType;
|
|
22
|
+
export type UseTableModelProps<T extends TableRow = TableRow> = {
|
|
23
|
+
object?: Table.Table;
|
|
21
24
|
projection?: ProjectionModel;
|
|
22
|
-
|
|
25
|
+
db?: Database.Database;
|
|
26
|
+
rows?: T[];
|
|
23
27
|
rowActions?: TableRowAction[];
|
|
24
28
|
onSelectionChanged?: (selection: string[]) => void;
|
|
25
29
|
onRowAction?: (actionId: string, data: T) => void;
|
|
26
30
|
} & Pick<
|
|
27
31
|
TableModelProps<T>,
|
|
28
|
-
'features' | 'onInsertRow' | 'onDeleteRows' | '
|
|
32
|
+
'features' | 'onInsertRow' | 'onDeleteRows' | 'onColumnDelete' | 'onCellUpdate' | 'onRowOrderChange'
|
|
29
33
|
>;
|
|
30
34
|
|
|
31
35
|
export const useTableModel = <T extends TableRow = TableRow>({
|
|
32
|
-
|
|
33
|
-
schema,
|
|
36
|
+
object,
|
|
34
37
|
projection,
|
|
38
|
+
db,
|
|
35
39
|
rows,
|
|
36
40
|
rowActions,
|
|
37
41
|
features,
|
|
38
42
|
onSelectionChanged,
|
|
39
43
|
onRowAction,
|
|
40
44
|
...props
|
|
41
|
-
}:
|
|
42
|
-
const
|
|
43
|
-
const
|
|
45
|
+
}: UseTableModelProps<T>): TableModel<T> | undefined => {
|
|
46
|
+
const registry = useContext(RegistryContext);
|
|
47
|
+
const selected = useSelected(object && Obj.getURI(object), 'multi');
|
|
48
|
+
const initialSelection = useMemo(() => selected, [object]);
|
|
44
49
|
|
|
45
50
|
const [model, setModel] = useState<TableModel<T>>();
|
|
46
51
|
useEffect(() => {
|
|
47
|
-
if (!
|
|
52
|
+
if (!object || !projection) {
|
|
48
53
|
return;
|
|
49
54
|
}
|
|
50
55
|
|
|
51
56
|
let model: TableModel<T> | undefined;
|
|
52
57
|
const t = setTimeout(async () => {
|
|
53
58
|
model = new TableModel<T>({
|
|
54
|
-
|
|
55
|
-
|
|
59
|
+
registry,
|
|
60
|
+
object,
|
|
56
61
|
projection,
|
|
62
|
+
db,
|
|
63
|
+
change: createEchoChangeCallback<T>(object),
|
|
57
64
|
features,
|
|
58
65
|
rowActions,
|
|
59
66
|
initialSelection,
|
|
@@ -68,18 +75,13 @@ export const useTableModel = <T extends TableRow = TableRow>({
|
|
|
68
75
|
clearTimeout(t);
|
|
69
76
|
void model?.close();
|
|
70
77
|
};
|
|
71
|
-
|
|
78
|
+
// TODO(burdon): Trigger if callbacks change?
|
|
79
|
+
}, [registry, object, projection, features, rowActions, initialSelection]);
|
|
72
80
|
|
|
73
|
-
// Update data.
|
|
81
|
+
// Update data when rows change.
|
|
74
82
|
useEffect(() => {
|
|
75
|
-
if (rows) {
|
|
76
|
-
|
|
77
|
-
/*
|
|
78
|
-
* Sort all objects by string id field as a temporary workaround for query ordering issues
|
|
79
|
-
* Reference: https://github.com/dxos/dxos/pull/9409
|
|
80
|
-
*/
|
|
81
|
-
const sortedRows = orderBy(rows, [(row) => String(row.id)], ['asc']);
|
|
82
|
-
model?.setRows(sortedRows);
|
|
83
|
+
if (rows && model) {
|
|
84
|
+
model.setRows(rows);
|
|
83
85
|
}
|
|
84
86
|
}, [model, rows]);
|
|
85
87
|
|
|
@@ -90,8 +92,8 @@ export const useTableModel = <T extends TableRow = TableRow>({
|
|
|
90
92
|
return;
|
|
91
93
|
}
|
|
92
94
|
|
|
93
|
-
const unsubscribe =
|
|
94
|
-
const selectedItems = [...model.selection.selection
|
|
95
|
+
const unsubscribe = registry.subscribe(model.selection.selectionAtom, () => {
|
|
96
|
+
const selectedItems = [...model.selection.selection];
|
|
95
97
|
multiSelect(selectedItems);
|
|
96
98
|
onSelectionChanged?.(selectedItems);
|
|
97
99
|
});
|
|
@@ -101,7 +103,7 @@ export const useTableModel = <T extends TableRow = TableRow>({
|
|
|
101
103
|
clear();
|
|
102
104
|
unsubscribe();
|
|
103
105
|
};
|
|
104
|
-
}, [model, onSelectionChanged]);
|
|
106
|
+
}, [registry, model, onSelectionChanged]);
|
|
105
107
|
|
|
106
108
|
return model;
|
|
107
109
|
};
|
package/src/index.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import { Atom, type Registry } from '@effect-atom/atom-react';
|
|
6
6
|
import { type MutableRefObject, type RefObject } from 'react';
|
|
7
7
|
|
|
8
8
|
export type ColumnSettingsMode = { type: 'create' } | { type: 'edit'; fieldId: string };
|
|
@@ -15,13 +15,23 @@ export type ModalState =
|
|
|
15
15
|
| { type: 'closed' };
|
|
16
16
|
|
|
17
17
|
export class ModalController {
|
|
18
|
-
private readonly
|
|
18
|
+
private readonly _registry: Registry.Registry;
|
|
19
|
+
private readonly _state: Atom.Writable<ModalState>;
|
|
19
20
|
private readonly _triggerRef: MutableRefObject<HTMLElement> = { current: null as unknown as HTMLElement };
|
|
20
21
|
|
|
21
|
-
|
|
22
|
+
constructor(registry: Registry.Registry) {
|
|
23
|
+
this._registry = registry;
|
|
24
|
+
this._state = Atom.make<ModalState>({ type: 'closed' });
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
public get state(): Atom.Atom<ModalState> {
|
|
22
28
|
return this._state;
|
|
23
29
|
}
|
|
24
30
|
|
|
31
|
+
public get stateValue(): ModalState {
|
|
32
|
+
return this._registry.get(this._state);
|
|
33
|
+
}
|
|
34
|
+
|
|
25
35
|
public get trigger() {
|
|
26
36
|
return this._triggerRef as RefObject<HTMLButtonElement>;
|
|
27
37
|
}
|
|
@@ -31,35 +41,36 @@ export class ModalController {
|
|
|
31
41
|
};
|
|
32
42
|
|
|
33
43
|
public showRowMenu = (rowIndex: number) => {
|
|
34
|
-
this._state
|
|
44
|
+
this._registry.set(this._state, { type: 'row', rowIndex });
|
|
35
45
|
};
|
|
36
46
|
|
|
37
47
|
public showColumnMenu = (fieldId: string) => {
|
|
38
|
-
this._state
|
|
48
|
+
this._registry.set(this._state, { type: 'column', fieldId });
|
|
39
49
|
};
|
|
40
50
|
|
|
41
51
|
public showColumnCreator = () => {
|
|
42
|
-
this._state
|
|
52
|
+
this._registry.set(this._state, { type: 'columnSettings', mode: { type: 'create' } });
|
|
43
53
|
};
|
|
44
54
|
|
|
45
55
|
public showReferencePanel = (targetId: string, schemaId: string) => {
|
|
46
|
-
this._state
|
|
56
|
+
this._registry.set(this._state, { type: 'refPanel', targetId, typename: schemaId });
|
|
47
57
|
};
|
|
48
58
|
|
|
49
59
|
public openColumnSettings = () => {
|
|
50
|
-
|
|
51
|
-
|
|
60
|
+
const currentState = this._registry.get(this._state);
|
|
61
|
+
if (currentState.type === 'column') {
|
|
62
|
+
const fieldId = currentState.fieldId;
|
|
52
63
|
// Queue next render to let the column action modal close completely.
|
|
53
64
|
requestAnimationFrame(() => {
|
|
54
|
-
this._state
|
|
65
|
+
this._registry.set(this._state, {
|
|
55
66
|
type: 'columnSettings',
|
|
56
67
|
mode: { type: 'edit', fieldId },
|
|
57
|
-
};
|
|
68
|
+
});
|
|
58
69
|
});
|
|
59
70
|
}
|
|
60
71
|
};
|
|
61
72
|
|
|
62
73
|
public close = () => {
|
|
63
|
-
this._state
|
|
74
|
+
this._registry.set(this._state, { type: 'closed' });
|
|
64
75
|
};
|
|
65
76
|
}
|
|
@@ -2,90 +2,120 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import { Atom, type Registry } from '@effect-atom/atom-react';
|
|
6
6
|
|
|
7
7
|
import { Resource } from '@dxos/context';
|
|
8
8
|
|
|
9
|
-
import { touch } from '../util';
|
|
10
|
-
|
|
11
9
|
import { type TableRow } from './table-model';
|
|
12
10
|
|
|
13
11
|
export type SelectionMode = 'single' | 'multiple';
|
|
14
12
|
|
|
15
13
|
export class SelectionModel<T extends TableRow> extends Resource {
|
|
16
|
-
private readonly
|
|
14
|
+
private readonly _registry: Registry.Registry;
|
|
15
|
+
private readonly _rows: Atom.Atom<T[]>;
|
|
16
|
+
private readonly _selection: Atom.Writable<Set<string>>;
|
|
17
|
+
private readonly _validSelectedIds: Atom.Atom<Set<string>>;
|
|
18
|
+
private readonly _hasSelection: Atom.Atom<boolean>;
|
|
19
|
+
private readonly _allSelected: Atom.Atom<boolean>;
|
|
20
|
+
private readonly _selectionMode: SelectionMode;
|
|
21
|
+
private readonly _onSelectionChanged?: () => void;
|
|
17
22
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
+
constructor(
|
|
24
|
+
registry: Registry.Registry,
|
|
25
|
+
rows: Atom.Atom<T[]>,
|
|
26
|
+
selectionMode: SelectionMode,
|
|
27
|
+
initialSelection: string[],
|
|
28
|
+
onSelectionChanged?: () => void,
|
|
29
|
+
) {
|
|
30
|
+
super();
|
|
31
|
+
this._registry = registry;
|
|
32
|
+
this._rows = rows;
|
|
33
|
+
this._selectionMode = selectionMode;
|
|
34
|
+
this._onSelectionChanged = onSelectionChanged;
|
|
23
35
|
|
|
24
|
-
|
|
25
|
-
return new Set([...this._selection.value].filter((id) => validIds.has(id)));
|
|
26
|
-
});
|
|
36
|
+
this._selection = Atom.make<Set<string>>(new Set(initialSelection));
|
|
27
37
|
|
|
28
|
-
|
|
38
|
+
this._validSelectedIds = Atom.make((get) => {
|
|
39
|
+
const rows = get(this._rows);
|
|
40
|
+
if (!rows) {
|
|
41
|
+
return new Set<string>();
|
|
42
|
+
}
|
|
29
43
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
}
|
|
44
|
+
const validIds = new Set(rows.map((row) => row.id));
|
|
45
|
+
const selection = get(this._selection);
|
|
46
|
+
return new Set([...selection].filter((id) => validIds.has(id)));
|
|
47
|
+
});
|
|
35
48
|
|
|
36
|
-
|
|
37
|
-
});
|
|
49
|
+
this._hasSelection = Atom.make((get) => get(this._validSelectedIds).size > 0);
|
|
38
50
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
51
|
+
this._allSelected = Atom.make((get) => {
|
|
52
|
+
const rows = get(this._rows);
|
|
53
|
+
if (rows.length === 0) {
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const selection = get(this._selection);
|
|
58
|
+
return rows.every((row) => selection.has(row.id));
|
|
59
|
+
});
|
|
47
60
|
}
|
|
48
61
|
|
|
49
62
|
protected override async _open(): Promise<void> {
|
|
50
|
-
const
|
|
51
|
-
touch(this._selection.value);
|
|
63
|
+
const selectionUnsubscribe = this._registry.subscribe(this._selection, () => {
|
|
52
64
|
this._onSelectionChanged?.();
|
|
53
65
|
});
|
|
54
|
-
this._ctx.onDispose(
|
|
66
|
+
this._ctx.onDispose(selectionUnsubscribe);
|
|
55
67
|
}
|
|
56
68
|
|
|
57
69
|
//
|
|
58
70
|
// Getters
|
|
59
71
|
//
|
|
60
72
|
|
|
61
|
-
|
|
73
|
+
/** Get the selection atom for reactive access. */
|
|
74
|
+
public get selectionAtom(): Atom.Atom<Set<string>> {
|
|
62
75
|
return this._selection;
|
|
63
76
|
}
|
|
64
77
|
|
|
65
|
-
|
|
78
|
+
/** Get the current selection value. */
|
|
79
|
+
public get selection(): Set<string> {
|
|
80
|
+
return this._registry.get(this._selection);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Get the hasSelection atom for reactive access. */
|
|
84
|
+
public get hasSelectionAtom(): Atom.Atom<boolean> {
|
|
66
85
|
return this._hasSelection;
|
|
67
86
|
}
|
|
68
87
|
|
|
69
|
-
|
|
88
|
+
/** Check if there is any selection. */
|
|
89
|
+
public get hasSelection(): boolean {
|
|
90
|
+
return this._registry.get(this._hasSelection);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Get the allRowsSelected atom for reactive access. */
|
|
94
|
+
public get allRowsSelectedAtom(): Atom.Atom<boolean> {
|
|
70
95
|
return this._allSelected;
|
|
71
96
|
}
|
|
72
97
|
|
|
98
|
+
/** Check if all rows are selected. */
|
|
99
|
+
public get allRowsSelected(): boolean {
|
|
100
|
+
return this._registry.get(this._allSelected);
|
|
101
|
+
}
|
|
102
|
+
|
|
73
103
|
public get selectionMode(): SelectionMode {
|
|
74
104
|
return this._selectionMode;
|
|
75
105
|
}
|
|
76
106
|
|
|
77
107
|
public getSelectedRows = (): T[] => {
|
|
78
|
-
const selectedIds = this._validSelectedIds
|
|
79
|
-
return this._rows.
|
|
108
|
+
const selectedIds = this._registry.get(this._validSelectedIds);
|
|
109
|
+
return this._registry.get(this._rows).filter((row) => selectedIds.has(row.id));
|
|
80
110
|
};
|
|
81
111
|
|
|
82
112
|
public isRowIndexSelected = (rowIndex: number): boolean => {
|
|
83
|
-
const row = this._rows
|
|
84
|
-
return this._selection.
|
|
113
|
+
const row = this._registry.get(this._rows)[rowIndex];
|
|
114
|
+
return this._registry.get(this._selection).has(row.id);
|
|
85
115
|
};
|
|
86
116
|
|
|
87
117
|
public isObjectSelected = (object: T): boolean => {
|
|
88
|
-
return this._selection.
|
|
118
|
+
return this._registry.get(this._selection).has(object.id);
|
|
89
119
|
};
|
|
90
120
|
|
|
91
121
|
//
|
|
@@ -93,8 +123,9 @@ export class SelectionModel<T extends TableRow> extends Resource {
|
|
|
93
123
|
//
|
|
94
124
|
|
|
95
125
|
public toggleSelectionForRowIndex = (rowIndex: number): void => {
|
|
96
|
-
const row = this._rows
|
|
97
|
-
const
|
|
126
|
+
const row = this._registry.get(this._rows)[rowIndex];
|
|
127
|
+
const currentSelection = this._registry.get(this._selection);
|
|
128
|
+
const newSelection = new Set(currentSelection);
|
|
98
129
|
|
|
99
130
|
if (newSelection.has(row.id)) {
|
|
100
131
|
newSelection.delete(row.id);
|
|
@@ -105,17 +136,17 @@ export class SelectionModel<T extends TableRow> extends Resource {
|
|
|
105
136
|
newSelection.add(row.id);
|
|
106
137
|
}
|
|
107
138
|
|
|
108
|
-
this._selection
|
|
139
|
+
this._registry.set(this._selection, newSelection);
|
|
109
140
|
};
|
|
110
141
|
|
|
111
142
|
public setSelection = (mode: 'all' | 'none'): void => {
|
|
112
143
|
switch (mode) {
|
|
113
144
|
case 'all': {
|
|
114
|
-
this._selection
|
|
145
|
+
this._registry.set(this._selection, new Set(this._registry.get(this._rows).map((row) => row.id)));
|
|
115
146
|
break;
|
|
116
147
|
}
|
|
117
148
|
case 'none': {
|
|
118
|
-
this._selection
|
|
149
|
+
this._registry.set(this._selection, new Set());
|
|
119
150
|
break;
|
|
120
151
|
}
|
|
121
152
|
}
|