@dxos/react-ui-table 0.8.4-main.c4373fc → 0.8.4-main.d05539e30a

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 (169) hide show
  1. package/LICENSE +102 -5
  2. package/dist/lib/browser/chunk-4URYOFDL.mjs +69 -0
  3. package/dist/lib/browser/chunk-4URYOFDL.mjs.map +7 -0
  4. package/dist/lib/browser/chunk-J5LGTIGS.mjs +10 -0
  5. package/dist/lib/browser/chunk-J5LGTIGS.mjs.map +7 -0
  6. package/dist/lib/browser/chunk-YDXPC7HA.mjs +1695 -0
  7. package/dist/lib/browser/chunk-YDXPC7HA.mjs.map +7 -0
  8. package/dist/lib/browser/index.mjs +1136 -1044
  9. package/dist/lib/browser/index.mjs.map +4 -4
  10. package/dist/lib/browser/meta.json +1 -1
  11. package/dist/lib/browser/testing/index.mjs +35 -45
  12. package/dist/lib/browser/testing/index.mjs.map +3 -3
  13. package/dist/lib/browser/translations.mjs +36 -0
  14. package/dist/lib/browser/translations.mjs.map +7 -0
  15. package/dist/lib/browser/types/index.mjs +4 -3
  16. package/dist/lib/node-esm/chunk-2AN3QQ3D.mjs +1697 -0
  17. package/dist/lib/node-esm/chunk-2AN3QQ3D.mjs.map +7 -0
  18. package/dist/lib/node-esm/chunk-B2SZYAEC.mjs +70 -0
  19. package/dist/lib/node-esm/chunk-B2SZYAEC.mjs.map +7 -0
  20. package/dist/lib/node-esm/chunk-HSLMI22Q.mjs +11 -0
  21. package/dist/lib/node-esm/chunk-HSLMI22Q.mjs.map +7 -0
  22. package/dist/lib/node-esm/index.mjs +1136 -1044
  23. package/dist/lib/node-esm/index.mjs.map +4 -4
  24. package/dist/lib/node-esm/meta.json +1 -1
  25. package/dist/lib/node-esm/testing/index.mjs +35 -45
  26. package/dist/lib/node-esm/testing/index.mjs.map +3 -3
  27. package/dist/lib/node-esm/translations.mjs +37 -0
  28. package/dist/lib/node-esm/translations.mjs.map +7 -0
  29. package/dist/lib/node-esm/types/index.mjs +4 -3
  30. package/dist/types/src/components/Table/ColumnActionsMenu.d.ts +1 -2
  31. package/dist/types/src/components/Table/ColumnActionsMenu.d.ts.map +1 -1
  32. package/dist/types/src/components/Table/ColumnSettings.d.ts +2 -2
  33. package/dist/types/src/components/Table/ColumnSettings.d.ts.map +1 -1
  34. package/dist/types/src/components/Table/DynamicTable.d.ts +6 -7
  35. package/dist/types/src/components/Table/DynamicTable.d.ts.map +1 -1
  36. package/dist/types/src/components/Table/DynamicTable.stories.d.ts +21 -37
  37. package/dist/types/src/components/Table/DynamicTable.stories.d.ts.map +1 -1
  38. package/dist/types/src/components/Table/Relations.stories.d.ts +84 -81
  39. package/dist/types/src/components/Table/Relations.stories.d.ts.map +1 -1
  40. package/dist/types/src/components/Table/RowActionsMenu.d.ts.map +1 -1
  41. package/dist/types/src/components/Table/Table.d.ts +20 -21
  42. package/dist/types/src/components/Table/Table.d.ts.map +1 -1
  43. package/dist/types/src/components/Table/Table.stories.d.ts +92 -26
  44. package/dist/types/src/components/Table/Table.stories.d.ts.map +1 -1
  45. package/dist/types/src/components/Table/TableContent.d.ts +18 -0
  46. package/dist/types/src/components/Table/TableContent.d.ts.map +1 -0
  47. package/dist/types/src/components/Table/TableToolbar.d.ts +21 -0
  48. package/dist/types/src/components/Table/TableToolbar.d.ts.map +1 -0
  49. package/dist/types/src/components/TableCellEditor/CellValidationMessage.d.ts +1 -1
  50. package/dist/types/src/components/TableCellEditor/CellValidationMessage.d.ts.map +1 -1
  51. package/dist/types/src/components/TableCellEditor/FormCellEditor.d.ts +10 -12
  52. package/dist/types/src/components/TableCellEditor/FormCellEditor.d.ts.map +1 -1
  53. package/dist/types/src/components/TableCellEditor/TableCellEditor.d.ts +5 -7
  54. package/dist/types/src/components/TableCellEditor/TableCellEditor.d.ts.map +1 -1
  55. package/dist/types/src/components/TableCellEditor/TableCellEditor.stories.d.ts +24 -26
  56. package/dist/types/src/components/TableCellEditor/TableCellEditor.stories.d.ts.map +1 -1
  57. package/dist/types/src/components/index.d.ts +0 -1
  58. package/dist/types/src/components/index.d.ts.map +1 -1
  59. package/dist/types/src/components/testing.d.ts +22 -15
  60. package/dist/types/src/components/testing.d.ts.map +1 -1
  61. package/dist/types/src/hooks/index.d.ts +2 -1
  62. package/dist/types/src/hooks/index.d.ts.map +1 -1
  63. package/dist/types/src/hooks/useAddRow.d.ts +5 -6
  64. package/dist/types/src/hooks/useAddRow.d.ts.map +1 -1
  65. package/dist/types/src/hooks/useProjectionModel.d.ts +6 -0
  66. package/dist/types/src/hooks/useProjectionModel.d.ts.map +1 -0
  67. package/dist/types/src/hooks/useTableModel.d.ts +9 -9
  68. package/dist/types/src/hooks/useTableModel.d.ts.map +1 -1
  69. package/dist/types/src/index.d.ts +0 -2
  70. package/dist/types/src/index.d.ts.map +1 -1
  71. package/dist/types/src/model/modal-controller.d.ts +5 -1
  72. package/dist/types/src/model/modal-controller.d.ts.map +1 -1
  73. package/dist/types/src/model/selection-model.d.ts +18 -9
  74. package/dist/types/src/model/selection-model.d.ts.map +1 -1
  75. package/dist/types/src/model/table-model.d.ts +104 -25
  76. package/dist/types/src/model/table-model.d.ts.map +1 -1
  77. package/dist/types/src/model/table-presentation.d.ts +3 -1
  78. package/dist/types/src/model/table-presentation.d.ts.map +1 -1
  79. package/dist/types/src/playwright/TableManager.d.ts +2 -1
  80. package/dist/types/src/playwright/TableManager.d.ts.map +1 -1
  81. package/dist/types/src/playwright/playwright.config.d.ts.map +1 -1
  82. package/dist/types/src/testing/useTestTableModel.d.ts +18 -15
  83. package/dist/types/src/testing/useTestTableModel.d.ts.map +1 -1
  84. package/dist/types/src/translations.d.ts +21 -23
  85. package/dist/types/src/translations.d.ts.map +1 -1
  86. package/dist/types/src/types/Table.d.ts +25 -0
  87. package/dist/types/src/types/Table.d.ts.map +1 -0
  88. package/dist/types/src/types/index.d.ts +1 -1
  89. package/dist/types/src/types/index.d.ts.map +1 -1
  90. package/dist/types/src/util/dynamic-table.d.ts +14 -11
  91. package/dist/types/src/util/dynamic-table.d.ts.map +1 -1
  92. package/dist/types/src/util/dynamic-table.test.d.ts +2 -0
  93. package/dist/types/src/util/dynamic-table.test.d.ts.map +1 -0
  94. package/dist/types/src/util/index.d.ts +2 -0
  95. package/dist/types/src/util/index.d.ts.map +1 -1
  96. package/dist/types/src/util/query-ast.d.ts +7 -0
  97. package/dist/types/src/util/query-ast.d.ts.map +1 -0
  98. package/dist/types/src/util/schema.d.ts.map +1 -1
  99. package/dist/types/src/util/signals.d.ts.map +1 -1
  100. package/dist/types/src/util/sort.d.ts +12 -0
  101. package/dist/types/src/util/sort.d.ts.map +1 -0
  102. package/dist/types/src/util/table-buttons.d.ts +20 -20
  103. package/dist/types/src/util/table-buttons.d.ts.map +1 -1
  104. package/dist/types/src/util/table-controls.d.ts +4 -4
  105. package/dist/types/src/util/table-controls.d.ts.map +1 -1
  106. package/dist/types/src/util/tag.d.ts.map +1 -1
  107. package/dist/types/tsconfig.tsbuildinfo +1 -1
  108. package/package.json +67 -76
  109. package/src/components/Table/ColumnActionsMenu.tsx +17 -12
  110. package/src/components/Table/ColumnSettings.tsx +8 -9
  111. package/src/components/Table/DynamicTable.stories.tsx +30 -30
  112. package/src/components/Table/DynamicTable.tsx +31 -32
  113. package/src/components/Table/Relations.stories.tsx +113 -81
  114. package/src/components/Table/RowActionsMenu.tsx +27 -17
  115. package/src/components/Table/Table.stories.tsx +125 -110
  116. package/src/components/Table/Table.tsx +43 -407
  117. package/src/components/Table/TableContent.tsx +425 -0
  118. package/src/components/Table/TableToolbar.tsx +111 -0
  119. package/src/components/TableCellEditor/CellValidationMessage.tsx +5 -6
  120. package/src/components/TableCellEditor/FormCellEditor.tsx +119 -135
  121. package/src/components/TableCellEditor/TableCellEditor.stories.tsx +26 -22
  122. package/src/components/TableCellEditor/TableCellEditor.tsx +35 -33
  123. package/src/components/index.ts +0 -1
  124. package/src/components/testing.ts +43 -17
  125. package/src/hooks/index.ts +2 -1
  126. package/src/hooks/useAddRow.ts +8 -10
  127. package/src/hooks/useProjectionModel.ts +39 -0
  128. package/src/hooks/useTableModel.ts +36 -34
  129. package/src/index.ts +0 -4
  130. package/src/model/modal-controller.ts +23 -12
  131. package/src/model/selection-model.ts +75 -44
  132. package/src/model/table-model.test.ts +82 -55
  133. package/src/model/table-model.ts +435 -170
  134. package/src/model/table-presentation.test.ts +59 -50
  135. package/src/model/table-presentation.ts +43 -39
  136. package/src/playwright/TableManager.ts +13 -6
  137. package/src/playwright/playwright.config.ts +1 -1
  138. package/src/playwright/smoke.spec.ts +59 -13
  139. package/src/testing/useTestTableModel.ts +49 -42
  140. package/src/translations.ts +20 -22
  141. package/src/types/Table.ts +78 -0
  142. package/src/types/index.ts +1 -1
  143. package/src/util/dynamic-table.test.ts +70 -0
  144. package/src/util/dynamic-table.ts +40 -28
  145. package/src/util/index.ts +2 -0
  146. package/src/util/query-ast.ts +22 -0
  147. package/src/util/sort.ts +90 -0
  148. package/src/util/table-buttons.ts +1 -1
  149. package/src/util/table-controls.ts +2 -2
  150. package/dist/lib/browser/chunk-4K7JPALT.mjs +0 -66
  151. package/dist/lib/browser/chunk-4K7JPALT.mjs.map +0 -7
  152. package/dist/lib/browser/chunk-OZTXLZGM.mjs +0 -1804
  153. package/dist/lib/browser/chunk-OZTXLZGM.mjs.map +0 -7
  154. package/dist/lib/node-esm/chunk-MK6OVUF6.mjs +0 -67
  155. package/dist/lib/node-esm/chunk-MK6OVUF6.mjs.map +0 -7
  156. package/dist/lib/node-esm/chunk-S56Z4V4X.mjs +0 -1805
  157. package/dist/lib/node-esm/chunk-S56Z4V4X.mjs.map +0 -7
  158. package/dist/types/src/components/TableToolbar/TableToolbar.d.ts +0 -21
  159. package/dist/types/src/components/TableToolbar/TableToolbar.d.ts.map +0 -1
  160. package/dist/types/src/components/TableToolbar/index.d.ts +0 -2
  161. package/dist/types/src/components/TableToolbar/index.d.ts.map +0 -1
  162. package/dist/types/src/model/table-sorting.d.ts +0 -52
  163. package/dist/types/src/model/table-sorting.d.ts.map +0 -1
  164. package/dist/types/src/types/schema.d.ts +0 -28
  165. package/dist/types/src/types/schema.d.ts.map +0 -1
  166. package/src/components/TableToolbar/TableToolbar.tsx +0 -102
  167. package/src/components/TableToolbar/index.ts +0 -5
  168. package/src/model/table-sorting.ts +0 -225
  169. package/src/types/schema.ts +0 -73
@@ -2,43 +2,52 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- import { useCallback, useMemo, useRef } from 'react';
5
+ import { RegistryContext } from '@effect-atom/atom-react';
6
+ import { type RefObject, useCallback, useContext, useMemo, useRef } from 'react';
6
7
 
7
- import { isMutable, toJsonSchema } from '@dxos/echo/internal';
8
- import { useGlobalFilteredObjects } from '@dxos/plugin-search';
9
- import { faker } from '@dxos/random';
10
- import { useClient } from '@dxos/react-client';
11
- import { Filter, useQuery, useSchema } from '@dxos/react-client/echo';
12
- import { useClientProvider } from '@dxos/react-client/testing';
13
- import { DataType, ProjectionModel, getTypenameFromQuery } from '@dxos/schema';
8
+ import { type Database, Filter, type Type } from '@dxos/echo';
9
+ import { isMutable } from '@dxos/echo/internal';
10
+ import { random } from '@dxos/random';
11
+ import { useQuery, useSchema } from '@dxos/react-client/echo';
12
+ import { useClientStory } from '@dxos/react-client/testing';
13
+ import { useGlobalFilteredObjects } from '@dxos/react-ui-search';
14
+ import { type ProjectionModel, getTypenameFromQuery } from '@dxos/schema';
14
15
 
15
16
  import { type TableController } from '../components';
16
- import { useAddRow, useTableModel } from '../hooks';
17
- import { TablePresentation } from '../model';
18
-
19
- faker.seed(0); // NOTE(ZaymonFC): Required for smoke tests.
17
+ import { useAddRow, useProjectionModel, useTableModel } from '../hooks';
18
+ import { type TableModel, TablePresentation } from '../model';
19
+ import { Table } from '../types';
20
+
21
+ random.seed(0); // NOTE(ZaymonFC): Required for smoke tests.
22
+
23
+ export type TestTableModel<T extends Type.AnyObj = Type.AnyObj> = {
24
+ schema: T | undefined;
25
+ table: Table.Table | undefined;
26
+ projection: ProjectionModel | undefined;
27
+ tableRef: RefObject<TableController | null>;
28
+ model: TableModel | undefined;
29
+ presentation: TablePresentation | undefined;
30
+ db: Database.Database | undefined;
31
+ handleInsertRow: () => void;
32
+ handleSaveView: () => void;
33
+ handleDeleteRows: (rowIndex: number, objects: any[]) => void;
34
+ handleDeleteColumn: (fieldId: string) => void;
35
+ };
20
36
 
21
37
  /**
22
38
  * Custom hook to create and manage a test table model for storybook demonstrations.
23
39
  * Provides table data, schema, and handlers for table operations.
24
40
  */
25
- export const useTestTableModel = () => {
26
- const client = useClient();
27
- const { space } = useClientProvider();
28
-
29
- const views = useQuery(space, Filter.type(DataType.View));
30
- const view = useMemo(() => views.at(0), [views]);
31
- const typename = view?.query ? getTypenameFromQuery(view.query.ast) : undefined;
32
- const schema = useSchema(client, space, typename);
33
- const jsonSchema = useMemo(() => (schema ? toJsonSchema(schema) : undefined), [schema]);
34
-
35
- const projection = useMemo(() => {
36
- if (schema && view?.projection) {
37
- const projection = new ProjectionModel(toJsonSchema(schema), view.projection);
38
- projection.normalizeView();
39
- return projection;
40
- }
41
- }, [schema, view?.projection]);
41
+ export const useTestTableModel = <T extends Type.AnyObj = Type.AnyObj>(): TestTableModel<T> => {
42
+ const registry = useContext(RegistryContext);
43
+ const { space } = useClientStory();
44
+ const db = space?.db;
45
+
46
+ const tables = useQuery(space?.db, Filter.type(Table.Table));
47
+ const table = tables.at(0);
48
+ const typename = table?.view.target?.query ? getTypenameFromQuery(table.view.target.query.ast) : undefined;
49
+ const schema = useSchema<T>(space?.db, typename);
50
+ const projection = useProjectionModel(schema, table, registry);
42
51
 
43
52
  const features = useMemo(
44
53
  () => ({
@@ -49,7 +58,7 @@ export const useTestTableModel = () => {
49
58
  [schema],
50
59
  );
51
60
 
52
- const objects = useQuery(space, schema ? Filter.type(schema) : Filter.nothing());
61
+ const objects = useQuery(db, schema ? Filter.type(schema) : Filter.nothing());
53
62
  const filteredObjects = useGlobalFilteredObjects(objects);
54
63
 
55
64
  const tableRef = useRef<TableController>(null);
@@ -61,15 +70,15 @@ export const useTestTableModel = () => {
61
70
  tableRef.current?.update?.();
62
71
  }, []);
63
72
 
64
- const addRow = useAddRow({ space, schema });
73
+ const addRow = useAddRow({ db, schema });
65
74
 
66
75
  const handleDeleteRows = useCallback(
67
76
  (_: number, objects: any[]) => {
68
77
  for (const object of objects) {
69
- space?.db.remove(object);
78
+ db?.remove(object);
70
79
  }
71
80
  },
72
- [space],
81
+ [db],
73
82
  );
74
83
 
75
84
  const handleDeleteColumn = useCallback(
@@ -82,14 +91,13 @@ export const useTestTableModel = () => {
82
91
  );
83
92
 
84
93
  const model = useTableModel({
85
- view,
86
- schema: jsonSchema,
94
+ object: table,
87
95
  projection,
88
96
  features,
89
97
  rows: filteredObjects,
90
98
  onInsertRow: addRow,
91
99
  onDeleteRows: handleDeleteRows,
92
- onDeleteColumn: handleDeleteColumn,
100
+ onColumnDelete: handleDeleteColumn,
93
101
  onCellUpdate: handleCellUpdate,
94
102
  onRowOrderChange: handleRowOrderChange,
95
103
  });
@@ -97,7 +105,7 @@ export const useTestTableModel = () => {
97
105
  const handleInsertRow = useCallback(() => {
98
106
  const insertResult = model?.insertRow();
99
107
  tableRef.current?.handleInsertRowResult?.(insertResult);
100
- }, [model, tableRef.current]);
108
+ }, [model]);
101
109
 
102
110
  const handleSaveView = useCallback(() => {
103
111
  model?.saveView();
@@ -105,19 +113,18 @@ export const useTestTableModel = () => {
105
113
 
106
114
  const presentation = useMemo(() => {
107
115
  if (model) {
108
- return new TablePresentation(model);
116
+ return new TablePresentation(registry, model);
109
117
  }
110
- }, [model]);
118
+ }, [registry, model]);
111
119
 
112
120
  return {
113
121
  schema,
114
- view,
122
+ table,
115
123
  projection,
116
124
  tableRef,
117
125
  model,
118
126
  presentation,
119
- space,
120
- client,
127
+ db,
121
128
  handleInsertRow,
122
129
  handleSaveView,
123
130
  handleDeleteRows,
@@ -4,33 +4,31 @@
4
4
 
5
5
  import { type Resource } from '@dxos/react-ui';
6
6
 
7
- export const translationKey = 'react-ui-table';
7
+ export const translationKey = '@dxos/react-ui-table';
8
8
 
9
9
  export const translations = [
10
10
  {
11
11
  'en-US': {
12
12
  [translationKey]: {
13
- 'object placeholder': 'New table',
14
- 'create object label': 'Create table',
15
- 'table name placeholder': 'Table name',
16
- 'settings title': 'Table settings',
17
- 'table schema label': 'Record type',
18
- 'new schema': 'New record type',
19
- 'continue label': 'Continue',
20
- 'create stack section label': 'Create table',
21
- 'add row label': 'Add row',
22
- 'create comment': 'Create thread',
23
- 'column action sort descending': 'Sort Descending',
24
- 'column action sort ascending': 'Sort Ascending',
25
- 'column action clear sorting': 'Clear Sorting',
26
- 'column action settings': 'Column Settings',
27
- 'column action delete': 'Delete Column',
28
- 'delete row label': 'Delete row',
29
- 'bulk delete row label': 'Delete selected rows',
30
- 'column deleted label': 'Column deleted',
31
- 'new column button label': 'Create column',
32
- 'create new object label': 'Create new object “{{text}}”',
33
- 'save view label': 'Save changes to view',
13
+ 'object.placeholder': 'New table',
14
+ 'create-object.label': 'Create table',
15
+ 'table-name.placeholder': 'Table name',
16
+ 'settings.title': 'Table settings',
17
+ 'table-schema.label': 'Type',
18
+ 'continue.label': 'Continue',
19
+ 'create-stack-section.label': 'Create table',
20
+ 'add-row.label': 'Add row',
21
+ 'column-action-sort-descending.menu': 'Sort Descending',
22
+ 'column-action-sort-ascending.menu': 'Sort Ascending',
23
+ 'column-action-clear-sorting.menu': 'Clear Sorting',
24
+ 'column-action-settings.menu': 'Column Settings',
25
+ 'column-action-delete.menu': 'Delete Column',
26
+ 'delete-row.label': 'Delete row',
27
+ 'bulk-delete-row.label': 'Delete selected rows',
28
+ 'column-deleted.label': 'Column deleted',
29
+ 'new-column-button.label': 'Create column',
30
+ 'create-new-object.label': 'Create new object “{{text}}”',
31
+ 'save-view.label': 'Save changes to view',
34
32
  },
35
33
  },
36
34
  },
@@ -0,0 +1,78 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import * as Match from 'effect/Match';
6
+ import * as Schema from 'effect/Schema';
7
+ import * as SchemaAST from 'effect/SchemaAST';
8
+
9
+ import { Annotation, JsonSchema, Obj, Ref, Type, View } from '@dxos/echo';
10
+ import { FormInputAnnotation, type JsonPath, type JsonSchemaType, LabelAnnotation } from '@dxos/echo/internal';
11
+ import { ViewAnnotation } from '@dxos/schema';
12
+
13
+ export const Table = Schema.Struct({
14
+ name: Schema.String.pipe(Schema.optional),
15
+
16
+ view: Ref.Ref(View.View).pipe(FormInputAnnotation.set(false)),
17
+
18
+ sizes: Schema.Record({
19
+ // TODO(wittjosiah): Should be JsonPath.
20
+ key: Schema.String,
21
+ value: Schema.Number,
22
+ }).pipe(Schema.mutable, FormInputAnnotation.set(false)),
23
+ }).pipe(
24
+ Type.object({
25
+ typename: 'org.dxos.type.table',
26
+ version: '0.1.0',
27
+ }),
28
+ LabelAnnotation.set(['name']),
29
+ ViewAnnotation.set(['view']),
30
+ Annotation.IconAnnotation.set({
31
+ icon: 'ph--table--regular',
32
+ hue: 'green',
33
+ }),
34
+ );
35
+
36
+ export interface Table extends Schema.Schema.Type<typeof Table> {}
37
+
38
+ type MakeProps = {
39
+ name?: string;
40
+ sizes?: Record<string, number>;
41
+ view: View.View;
42
+ /** Required to auto-size columns. */
43
+ jsonSchema?: JsonSchemaType;
44
+ };
45
+
46
+ /**
47
+ * Make a table as a view of a data set.
48
+ */
49
+ export const make = ({ name, sizes = {}, view, jsonSchema }: MakeProps): Table => {
50
+ const table = Obj.make(Table, { name, view: Ref.make(view), sizes } as Obj.MakeProps<typeof Table>);
51
+
52
+ // Preset sizes.
53
+ if (jsonSchema) {
54
+ const schema = JsonSchema.toEffectSchema(jsonSchema);
55
+ const properties = SchemaAST.getPropertySignatures(schema.ast);
56
+ for (const property of properties) {
57
+ const name = property.name.toString() as JsonPath;
58
+ if (sizes?.[name]) {
59
+ table.sizes[name] = sizes[name];
60
+ continue;
61
+ }
62
+
63
+ Match.type<SchemaAST.AST>().pipe(
64
+ Match.when({ _tag: 'BooleanKeyword' }, () => {
65
+ table.sizes[name] = 100;
66
+ }),
67
+ Match.when({ _tag: 'NumberKeyword' }, () => {
68
+ table.sizes[name] = 100;
69
+ }),
70
+ Match.orElse(() => {
71
+ // Noop.
72
+ }),
73
+ )(property.type);
74
+ }
75
+ }
76
+
77
+ return table;
78
+ };
@@ -2,4 +2,4 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- export * as Table from './schema';
5
+ export * as Table from './Table';
@@ -0,0 +1,70 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import { Registry } from '@effect-atom/atom-react';
6
+ import { describe, expect, test } from 'vitest';
7
+
8
+ import { Format } from '@dxos/echo';
9
+
10
+ import { type TablePropertyDefinition, getBaseSchema, makeDynamicTable } from './dynamic-table';
11
+
12
+ describe('makeDynamicTable', () => {
13
+ /**
14
+ * Base case: plain jsonSchema (not from Echo / JsonSchema.toJsonSchema). Does not exercise the path
15
+ * where projection or schema are reactive, so this does not reproduce the Obj.update regression.
16
+ * Confirms makeDynamicTable + setProperties (title) works with plain objects.
17
+ */
18
+ test('makeDynamicTable with plain jsonSchema and properties with title does not throw', () => {
19
+ const registry = Registry.make();
20
+ const jsonSchema = {
21
+ type: 'object' as const,
22
+ properties: {
23
+ id: { type: 'string' as const },
24
+ name: { type: 'string' as const },
25
+ },
26
+ required: ['id'],
27
+ };
28
+ const properties: TablePropertyDefinition[] = [
29
+ { name: 'id', format: Format.TypeFormat.String },
30
+ { name: 'name', format: Format.TypeFormat.String, title: 'Name' },
31
+ ];
32
+
33
+ expect(() => {
34
+ const { projection, object } = makeDynamicTable({
35
+ registry,
36
+ jsonSchema,
37
+ properties,
38
+ });
39
+ expect(projection).toBeDefined();
40
+ expect(object).toBeDefined();
41
+ expect(projection.getFields().length).toBeGreaterThan(0);
42
+ }).not.toThrow();
43
+ });
44
+
45
+ /**
46
+ * Regression test for the path where jsonSchema comes from getBaseSchema(typename, properties).
47
+ * That path uses Echo (ViewModel.make, JsonSchema.toJsonSchema); mutations must run inside Obj.update and on a
48
+ * cloned schema. This test ensures that flow does not throw.
49
+ */
50
+ test('makeDynamicTable with jsonSchema from getBaseSchema(typename, properties) and properties with title does not throw', () => {
51
+ const registry = Registry.make();
52
+ const typename = 'com.example.type.dynamicTableTest';
53
+ const properties: TablePropertyDefinition[] = [
54
+ { name: 'id', format: Format.TypeFormat.String },
55
+ { name: 'name', format: Format.TypeFormat.String, title: 'Name' },
56
+ ];
57
+ const { jsonSchema } = getBaseSchema({ typename, properties });
58
+
59
+ expect(() => {
60
+ const { projection, object } = makeDynamicTable({
61
+ registry,
62
+ jsonSchema,
63
+ properties,
64
+ });
65
+ expect(projection).toBeDefined();
66
+ expect(object).toBeDefined();
67
+ expect(projection.getFields().length).toBeGreaterThan(0);
68
+ }).not.toThrow();
69
+ });
70
+ });
@@ -2,17 +2,15 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- import type * as Schema from 'effect/Schema';
5
+ import { type Registry } from '@effect-atom/atom-react';
6
+ import type * as Types from 'effect/Types';
6
7
 
7
- import { Filter, Obj, Query } from '@dxos/echo';
8
- import { getTypename, toJsonSchema } from '@dxos/echo/internal';
9
- import type { JsonSchemaType } from '@dxos/echo/internal';
8
+ import { Filter, JsonSchema, Obj, Order, Query, type QueryAST, Ref, Type, type View } from '@dxos/echo';
10
9
  import {
11
- type DataType,
12
10
  ProjectionModel,
13
11
  type SchemaPropertyDefinition,
14
- type SortDirectionType,
15
- createView,
12
+ ViewModel,
13
+ createEchoChangeCallback,
16
14
  getSchemaFromPropertyDefinitions,
17
15
  } from '@dxos/schema';
18
16
 
@@ -22,7 +20,7 @@ import { Table } from '../types';
22
20
  type PropertyDisplayProps = {
23
21
  size: number;
24
22
  title: string;
25
- sort: SortDirectionType;
23
+ sort: QueryAST.OrderDirection;
26
24
  };
27
25
 
28
26
  export type TablePropertyDefinition = SchemaPropertyDefinition & Partial<PropertyDisplayProps>;
@@ -32,21 +30,21 @@ export type TablePropertyDefinition = SchemaPropertyDefinition & Partial<Propert
32
30
  */
33
31
  // TODO(burdon): Remove variance.
34
32
  export const getBaseSchema = ({
33
+ schema,
35
34
  typename,
36
35
  properties,
37
36
  jsonSchema,
38
- schema,
39
37
  }: {
38
+ schema?: Type.AnyEntity;
40
39
  typename?: string;
41
40
  properties?: TablePropertyDefinition[];
42
- jsonSchema?: JsonSchemaType;
43
- schema?: Schema.Schema.AnyNoContext;
44
- }): { typename: string; jsonSchema: JsonSchemaType } => {
41
+ jsonSchema?: Types.DeepMutable<JsonSchema.JsonSchema>;
42
+ }): { typename: string; jsonSchema: Types.DeepMutable<JsonSchema.JsonSchema> } => {
45
43
  if (typename && properties) {
46
44
  const schema = getSchemaFromPropertyDefinitions(typename, properties);
47
- return { typename: schema.typename, jsonSchema: schema.jsonSchema };
45
+ return { typename: schema.typename, jsonSchema: JsonSchema.toJsonSchema(schema) };
48
46
  } else if (schema) {
49
- return { typename: getTypename(schema)!, jsonSchema: toJsonSchema(schema) };
47
+ return { typename: Type.getTypename(schema)!, jsonSchema: JsonSchema.toJsonSchema(schema) };
50
48
  } else if (typename && jsonSchema) {
51
49
  return { typename, jsonSchema };
52
50
  } else {
@@ -55,40 +53,48 @@ export const getBaseSchema = ({
55
53
  };
56
54
 
57
55
  export const makeDynamicTable = ({
56
+ registry,
58
57
  jsonSchema,
59
58
  properties,
60
59
  }: {
61
- jsonSchema: JsonSchemaType;
60
+ registry: Registry.Registry;
61
+ jsonSchema: Types.DeepMutable<JsonSchema.JsonSchema>;
62
62
  properties?: TablePropertyDefinition[];
63
- }): { projection: ProjectionModel; view: DataType.View } => {
64
- const table = Obj.make(Table.Table, { sizes: {} });
65
- const view = createView({
63
+ }): { projection: ProjectionModel; object: Table.Table } => {
64
+ const view = ViewModel.make({
66
65
  query: Query.select(Filter.everything()),
67
66
  jsonSchema,
68
- presentation: table,
69
67
  ...(properties && { fields: properties.map((property) => property.name) }),
70
68
  });
69
+ const object = Obj.make(Table.Table, { view: Ref.make(view), sizes: {} });
71
70
 
72
- const projection = new ProjectionModel(jsonSchema, view.projection);
71
+ const projection = new ProjectionModel({
72
+ registry,
73
+ view,
74
+ baseSchema: jsonSchema,
75
+ change: createEchoChangeCallback(view, jsonSchema),
76
+ });
73
77
  projection.normalizeView();
74
- if (properties && projection.fields) {
75
- setProperties(view, projection, table, properties);
78
+ if (properties && projection.getFields()) {
79
+ setProperties(view, projection, object, properties);
76
80
  }
77
81
 
78
- return { projection, view };
82
+ return { projection, object };
79
83
  };
80
84
 
81
85
  const setProperties = (
82
- view: DataType.View,
86
+ view: View.View,
83
87
  projection: ProjectionModel,
84
88
  table: Table.Table,
85
89
  properties: TablePropertyDefinition[],
86
90
  ) => {
87
91
  for (const property of properties) {
88
- const field = projection.fields.find((field) => field.path === property.name);
92
+ const field = projection.getFields().find((field) => field.path === property.name);
89
93
  if (field) {
90
94
  if (property.size !== undefined) {
91
- table.sizes[field.path] = property.size;
95
+ Obj.update(table, (table) => {
96
+ table.sizes[field.path] = property.size!;
97
+ });
92
98
  }
93
99
 
94
100
  if (property.title !== undefined) {
@@ -100,8 +106,14 @@ const setProperties = (
100
106
  }
101
107
 
102
108
  if (property.sort) {
103
- const fieldId = field.id;
104
- view.sort = [{ fieldId, direction: property.sort }];
109
+ // Apply sort to query instead of deprecated view.sort
110
+ const currentQuery = Query.fromAst(Obj.getSnapshot(view).query.ast);
111
+ // Use any type parameter since we're working with dynamic field paths
112
+ const newQuery = currentQuery.orderBy(Order.property<any>(field.path as string, property.sort));
113
+ Obj.update(view, (view) => {
114
+ // Type assertion needed because Query AST types have some variance issues.
115
+ view.query.ast = newQuery.ast as typeof view.query.ast;
116
+ });
105
117
  }
106
118
  }
107
119
  }
package/src/util/index.ts CHANGED
@@ -3,7 +3,9 @@
3
3
  //
4
4
 
5
5
  export * from './dynamic-table';
6
+ export * from './query-ast';
6
7
  export * from './signals';
8
+ export * from './sort';
7
9
  export * from './table-buttons';
8
10
  export * from './table-controls';
9
11
  export * from './schema';
@@ -0,0 +1,22 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import { type QueryAST } from '@dxos/echo';
6
+
7
+ /**
8
+ * Extracts order clauses from a query AST.
9
+ * Handles nested structures (options, order, etc.).
10
+ */
11
+ export const extractOrder = (queryAst: QueryAST.Query): readonly QueryAST.Order[] | undefined => {
12
+ if (queryAst.type === 'order') {
13
+ return queryAst.order;
14
+ }
15
+ if (queryAst.type === 'options') {
16
+ return extractOrder(queryAst.query);
17
+ }
18
+ if (queryAst.type === 'from') {
19
+ return extractOrder(queryAst.query);
20
+ }
21
+ return undefined;
22
+ };
@@ -0,0 +1,90 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import { Format, type QueryAST } from '@dxos/echo';
6
+
7
+ /**
8
+ * Gets a normalized sort value for a given value and format.
9
+ * This ensures consistent sorting behavior across different data types.
10
+ */
11
+ export const getSortValue = (value: any, format: Format.TypeFormat): any => {
12
+ // Handle null/undefined
13
+ if (value === undefined || value === null) {
14
+ return null;
15
+ }
16
+
17
+ // Handle dates chronologically if schema indicates date format
18
+ const isDateFormat = format === Format.TypeFormat.DateTime || format === Format.TypeFormat.Date;
19
+ if (isDateFormat) {
20
+ const date = new Date(value);
21
+ if (!isNaN(date.getTime())) {
22
+ return date.getTime();
23
+ }
24
+ // Fallback to string if date parsing fails
25
+ return String(value);
26
+ }
27
+
28
+ // Handle numbers if schema indicates number format
29
+ if (
30
+ format === Format.TypeFormat.Number ||
31
+ format === Format.TypeFormat.Integer ||
32
+ format === Format.TypeFormat.Currency ||
33
+ format === Format.TypeFormat.Percent ||
34
+ format === Format.TypeFormat.Duration
35
+ ) {
36
+ const num = typeof value === 'number' ? value : Number(value);
37
+ if (!isNaN(num)) {
38
+ return num;
39
+ }
40
+ // Fallback to string if number parsing fails
41
+ return String(value);
42
+ }
43
+
44
+ // Default to string comparison
45
+ if (typeof value === 'string') {
46
+ return value;
47
+ }
48
+ if (typeof value === 'number') {
49
+ return value;
50
+ }
51
+
52
+ // Fallback to string representation
53
+ return String(value);
54
+ };
55
+
56
+ /**
57
+ * Compares two values for sorting based on the field format.
58
+ * Handles dates, numbers, and strings appropriately.
59
+ */
60
+ export const compareValues = (
61
+ aValue: any,
62
+ bValue: any,
63
+ format: Format.TypeFormat,
64
+ direction: QueryAST.OrderDirection,
65
+ ): number => {
66
+ // Handle null/undefined values - nulls sort to the end
67
+ if (aValue === undefined || aValue === null) {
68
+ return bValue === undefined || bValue === null ? 0 : 1;
69
+ }
70
+ if (bValue === undefined || bValue === null) {
71
+ return -1;
72
+ }
73
+
74
+ // Get normalized sort values
75
+ const aSortValue = getSortValue(aValue, format);
76
+ const bSortValue = getSortValue(bValue, format);
77
+
78
+ // Compare based on type
79
+ let comparison = 0;
80
+ if (typeof aSortValue === 'number' && typeof bSortValue === 'number') {
81
+ comparison = aSortValue - bSortValue;
82
+ } else if (typeof aSortValue === 'string' && typeof bSortValue === 'string') {
83
+ comparison = aSortValue.localeCompare(bSortValue);
84
+ } else {
85
+ // Mixed types - convert both to strings
86
+ comparison = String(aSortValue).localeCompare(String(bSortValue));
87
+ }
88
+
89
+ return direction === 'desc' ? -comparison : comparison;
90
+ };
@@ -39,7 +39,7 @@ const createButton = ({
39
39
  .map(([k, v]) => `${k}="${v}"`)
40
40
  .join(' ');
41
41
 
42
- return `<button ${attr} data-testid="${testId}" class="dx-button is-6 aspect-square pli-0.5 min-bs-0" ${dataAttrs} ${disabled ? 'disabled' : ''} data-dx-grid-action="accessory"><svg data-size="4"><use href="/icons.svg#${icon}"/></svg></button>`;
42
+ return `<button ${attr} data-testid="${testId}" class="dx-button w-6 aspect-square px-0.5 min-h-0" ${dataAttrs} ${disabled ? 'disabled' : ''} data-dx-grid-action="accessory"><svg data-size="4"><use href="/icons.svg#${icon}"/></svg></button>`;
43
43
  };
44
44
 
45
45
  const addColumnButton = {
@@ -25,8 +25,8 @@ export const CONTROL_IDENTIFIERS = {
25
25
  } as const;
26
26
 
27
27
  const BASE_CLASSES = {
28
- checkbox: 'absolute inset-block-[.375rem] inline-end-[.375rem] dx-checkbox',
29
- switch: 'absolute inset-block-[.25rem] inline-end-[.25rem] dx-checkbox--switch',
28
+ checkbox: 'absolute inset-y-[.375rem] end-[.375rem] dx-checkbox',
29
+ switch: 'absolute inset-y-[.25rem] end-[.25rem] dx-checkbox--switch',
30
30
  } as const;
31
31
 
32
32
  const renderAttributes = (data: Record<string, string>) => {