@dxos/react-ui-table 0.8.4-main.dedc0f3 → 0.8.4-main.dfabb4ec29

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 (176) hide show
  1. package/dist/lib/browser/chunk-FBURZBPH.mjs +70 -0
  2. package/dist/lib/browser/chunk-FBURZBPH.mjs.map +7 -0
  3. package/dist/lib/browser/chunk-J5LGTIGS.mjs +10 -0
  4. package/dist/lib/browser/chunk-J5LGTIGS.mjs.map +7 -0
  5. package/dist/lib/browser/chunk-YGC7LZJN.mjs +1695 -0
  6. package/dist/lib/browser/chunk-YGC7LZJN.mjs.map +7 -0
  7. package/dist/lib/browser/index.mjs +1139 -1193
  8. package/dist/lib/browser/index.mjs.map +4 -4
  9. package/dist/lib/browser/meta.json +1 -1
  10. package/dist/lib/browser/testing/index.mjs +33 -42
  11. package/dist/lib/browser/testing/index.mjs.map +3 -3
  12. package/dist/lib/browser/translations.mjs +36 -0
  13. package/dist/lib/browser/translations.mjs.map +7 -0
  14. package/dist/lib/browser/types/index.mjs +4 -3
  15. package/dist/lib/node-esm/chunk-DKKMSVKP.mjs +71 -0
  16. package/dist/lib/node-esm/chunk-DKKMSVKP.mjs.map +7 -0
  17. package/dist/lib/node-esm/chunk-GKIWDEZ7.mjs +1697 -0
  18. package/dist/lib/node-esm/chunk-GKIWDEZ7.mjs.map +7 -0
  19. package/dist/lib/node-esm/chunk-HSLMI22Q.mjs +11 -0
  20. package/dist/lib/node-esm/chunk-HSLMI22Q.mjs.map +7 -0
  21. package/dist/lib/node-esm/index.mjs +1139 -1193
  22. package/dist/lib/node-esm/index.mjs.map +4 -4
  23. package/dist/lib/node-esm/meta.json +1 -1
  24. package/dist/lib/node-esm/testing/index.mjs +33 -42
  25. package/dist/lib/node-esm/testing/index.mjs.map +3 -3
  26. package/dist/lib/node-esm/translations.mjs +37 -0
  27. package/dist/lib/node-esm/translations.mjs.map +7 -0
  28. package/dist/lib/node-esm/types/index.mjs +4 -3
  29. package/dist/types/src/components/Table/ColumnActionsMenu.d.ts +1 -2
  30. package/dist/types/src/components/Table/ColumnActionsMenu.d.ts.map +1 -1
  31. package/dist/types/src/components/Table/ColumnSettings.d.ts +2 -2
  32. package/dist/types/src/components/Table/ColumnSettings.d.ts.map +1 -1
  33. package/dist/types/src/components/Table/DynamicTable.d.ts +7 -7
  34. package/dist/types/src/components/Table/DynamicTable.d.ts.map +1 -1
  35. package/dist/types/src/components/Table/DynamicTable.stories.d.ts +23 -39
  36. package/dist/types/src/components/Table/DynamicTable.stories.d.ts.map +1 -1
  37. package/dist/types/src/components/Table/Relations.stories.d.ts +96 -29
  38. package/dist/types/src/components/Table/Relations.stories.d.ts.map +1 -1
  39. package/dist/types/src/components/Table/RowActionsMenu.d.ts.map +1 -1
  40. package/dist/types/src/components/Table/Table.d.ts +20 -18
  41. package/dist/types/src/components/Table/Table.d.ts.map +1 -1
  42. package/dist/types/src/components/Table/Table.stories.d.ts +96 -31
  43. package/dist/types/src/components/Table/Table.stories.d.ts.map +1 -1
  44. package/dist/types/src/components/Table/TableContent.d.ts +17 -0
  45. package/dist/types/src/components/Table/TableContent.d.ts.map +1 -0
  46. package/dist/types/src/components/Table/TableToolbar.d.ts +21 -0
  47. package/dist/types/src/components/Table/TableToolbar.d.ts.map +1 -0
  48. package/dist/types/src/components/TableCellEditor/CellValidationMessage.d.ts +1 -1
  49. package/dist/types/src/components/TableCellEditor/CellValidationMessage.d.ts.map +1 -1
  50. package/dist/types/src/components/TableCellEditor/FormCellEditor.d.ts +12 -12
  51. package/dist/types/src/components/TableCellEditor/FormCellEditor.d.ts.map +1 -1
  52. package/dist/types/src/components/TableCellEditor/TableCellEditor.d.ts +7 -7
  53. package/dist/types/src/components/TableCellEditor/TableCellEditor.d.ts.map +1 -1
  54. package/dist/types/src/components/TableCellEditor/TableCellEditor.stories.d.ts +25 -28
  55. package/dist/types/src/components/TableCellEditor/TableCellEditor.stories.d.ts.map +1 -1
  56. package/dist/types/src/components/TableCellEditor/index.d.ts +1 -0
  57. package/dist/types/src/components/TableCellEditor/index.d.ts.map +1 -1
  58. package/dist/types/src/components/index.d.ts +0 -1
  59. package/dist/types/src/components/index.d.ts.map +1 -1
  60. package/dist/types/src/components/testing.d.ts +23 -16
  61. package/dist/types/src/components/testing.d.ts.map +1 -1
  62. package/dist/types/src/hooks/index.d.ts +2 -1
  63. package/dist/types/src/hooks/index.d.ts.map +1 -1
  64. package/dist/types/src/hooks/useAddRow.d.ts +5 -6
  65. package/dist/types/src/hooks/useAddRow.d.ts.map +1 -1
  66. package/dist/types/src/hooks/useProjectionModel.d.ts +6 -0
  67. package/dist/types/src/hooks/useProjectionModel.d.ts.map +1 -0
  68. package/dist/types/src/hooks/useTableModel.d.ts +9 -9
  69. package/dist/types/src/hooks/useTableModel.d.ts.map +1 -1
  70. package/dist/types/src/index.d.ts +0 -2
  71. package/dist/types/src/index.d.ts.map +1 -1
  72. package/dist/types/src/model/modal-controller.d.ts +5 -8
  73. package/dist/types/src/model/modal-controller.d.ts.map +1 -1
  74. package/dist/types/src/model/selection-model.d.ts +18 -9
  75. package/dist/types/src/model/selection-model.d.ts.map +1 -1
  76. package/dist/types/src/model/table-model.d.ts +106 -26
  77. package/dist/types/src/model/table-model.d.ts.map +1 -1
  78. package/dist/types/src/model/table-presentation.d.ts +3 -1
  79. package/dist/types/src/model/table-presentation.d.ts.map +1 -1
  80. package/dist/types/src/playwright/TableManager.d.ts +2 -1
  81. package/dist/types/src/playwright/TableManager.d.ts.map +1 -1
  82. package/dist/types/src/playwright/playwright.config.d.ts.map +1 -1
  83. package/dist/types/src/testing/useTestTableModel.d.ts +18 -15
  84. package/dist/types/src/testing/useTestTableModel.d.ts.map +1 -1
  85. package/dist/types/src/translations.d.ts +21 -23
  86. package/dist/types/src/translations.d.ts.map +1 -1
  87. package/dist/types/src/types/Table.d.ts +26 -0
  88. package/dist/types/src/types/Table.d.ts.map +1 -0
  89. package/dist/types/src/types/index.d.ts +1 -1
  90. package/dist/types/src/types/index.d.ts.map +1 -1
  91. package/dist/types/src/util/dynamic-table.d.ts +14 -12
  92. package/dist/types/src/util/dynamic-table.d.ts.map +1 -1
  93. package/dist/types/src/util/dynamic-table.test.d.ts +2 -0
  94. package/dist/types/src/util/dynamic-table.test.d.ts.map +1 -0
  95. package/dist/types/src/util/index.d.ts +2 -0
  96. package/dist/types/src/util/index.d.ts.map +1 -1
  97. package/dist/types/src/util/query-ast.d.ts +7 -0
  98. package/dist/types/src/util/query-ast.d.ts.map +1 -0
  99. package/dist/types/src/util/schema.d.ts +1 -1
  100. package/dist/types/src/util/schema.d.ts.map +1 -1
  101. package/dist/types/src/util/signals.d.ts.map +1 -1
  102. package/dist/types/src/util/sort.d.ts +12 -0
  103. package/dist/types/src/util/sort.d.ts.map +1 -0
  104. package/dist/types/src/util/table-buttons.d.ts +20 -20
  105. package/dist/types/src/util/table-buttons.d.ts.map +1 -1
  106. package/dist/types/src/util/table-controls.d.ts +4 -4
  107. package/dist/types/src/util/table-controls.d.ts.map +1 -1
  108. package/dist/types/src/util/tag.d.ts.map +1 -1
  109. package/dist/types/tsconfig.tsbuildinfo +1 -1
  110. package/package.json +67 -76
  111. package/src/components/Table/ColumnActionsMenu.tsx +17 -12
  112. package/src/components/Table/ColumnSettings.tsx +8 -9
  113. package/src/components/Table/DynamicTable.stories.tsx +34 -33
  114. package/src/components/Table/DynamicTable.tsx +33 -36
  115. package/src/components/Table/Relations.stories.tsx +196 -48
  116. package/src/components/Table/RowActionsMenu.tsx +27 -17
  117. package/src/components/Table/Table.stories.tsx +144 -127
  118. package/src/components/Table/Table.tsx +43 -403
  119. package/src/components/Table/TableContent.tsx +421 -0
  120. package/src/components/Table/TableToolbar.tsx +111 -0
  121. package/src/components/TableCellEditor/CellValidationMessage.tsx +5 -6
  122. package/src/components/TableCellEditor/FormCellEditor.tsx +125 -131
  123. package/src/components/TableCellEditor/TableCellEditor.stories.tsx +32 -55
  124. package/src/components/TableCellEditor/TableCellEditor.tsx +39 -124
  125. package/src/components/TableCellEditor/index.ts +1 -0
  126. package/src/components/index.ts +0 -1
  127. package/src/components/testing.ts +44 -18
  128. package/src/hooks/index.ts +2 -1
  129. package/src/hooks/useAddRow.ts +8 -10
  130. package/src/hooks/useProjectionModel.ts +39 -0
  131. package/src/hooks/useTableModel.ts +36 -34
  132. package/src/index.ts +0 -4
  133. package/src/model/modal-controller.ts +23 -40
  134. package/src/model/selection-model.ts +75 -44
  135. package/src/model/table-model.test.ts +93 -56
  136. package/src/model/table-model.ts +437 -170
  137. package/src/model/table-presentation.test.ts +67 -56
  138. package/src/model/table-presentation.ts +45 -41
  139. package/src/playwright/TableManager.ts +13 -6
  140. package/src/playwright/playwright.config.ts +1 -1
  141. package/src/playwright/smoke.spec.ts +59 -13
  142. package/src/testing/useTestTableModel.ts +48 -39
  143. package/src/translations.ts +20 -22
  144. package/src/types/Table.ts +79 -0
  145. package/src/types/index.ts +1 -1
  146. package/src/util/dynamic-table.test.ts +70 -0
  147. package/src/util/dynamic-table.ts +43 -31
  148. package/src/util/index.ts +2 -0
  149. package/src/util/query-ast.ts +22 -0
  150. package/src/util/schema.ts +4 -4
  151. package/src/util/sort.ts +90 -0
  152. package/src/util/table-buttons.ts +1 -1
  153. package/src/util/table-controls.ts +2 -2
  154. package/dist/lib/browser/chunk-63UUF4IG.mjs +0 -66
  155. package/dist/lib/browser/chunk-63UUF4IG.mjs.map +0 -7
  156. package/dist/lib/browser/chunk-GPZSXPSA.mjs +0 -1822
  157. package/dist/lib/browser/chunk-GPZSXPSA.mjs.map +0 -7
  158. package/dist/lib/node-esm/chunk-K734DJB7.mjs +0 -67
  159. package/dist/lib/node-esm/chunk-K734DJB7.mjs.map +0 -7
  160. package/dist/lib/node-esm/chunk-XUD6W42C.mjs +0 -1823
  161. package/dist/lib/node-esm/chunk-XUD6W42C.mjs.map +0 -7
  162. package/dist/types/src/components/Table/CreateRefPanel.d.ts +0 -9
  163. package/dist/types/src/components/Table/CreateRefPanel.d.ts.map +0 -1
  164. package/dist/types/src/components/TableToolbar/TableToolbar.d.ts +0 -21
  165. package/dist/types/src/components/TableToolbar/TableToolbar.d.ts.map +0 -1
  166. package/dist/types/src/components/TableToolbar/index.d.ts +0 -2
  167. package/dist/types/src/components/TableToolbar/index.d.ts.map +0 -1
  168. package/dist/types/src/model/table-sorting.d.ts +0 -52
  169. package/dist/types/src/model/table-sorting.d.ts.map +0 -1
  170. package/dist/types/src/types/schema.d.ts +0 -28
  171. package/dist/types/src/types/schema.d.ts.map +0 -1
  172. package/src/components/Table/CreateRefPanel.tsx +0 -103
  173. package/src/components/TableToolbar/TableToolbar.tsx +0 -102
  174. package/src/components/TableToolbar/index.ts +0 -5
  175. package/src/model/table-sorting.ts +0 -225
  176. package/src/types/schema.ts +0 -73
@@ -4,15 +4,13 @@
4
4
 
5
5
  import { type Completion } from '@codemirror/autocomplete';
6
6
  import { EditorView } from '@codemirror/view';
7
- import { type Schema } from 'effect/Schema';
8
7
  import React, { useCallback, useMemo, useRef, useState } from 'react';
9
8
 
10
9
  import { debounce } from '@dxos/async';
11
- import type { Client } from '@dxos/client';
12
- import { FormatEnum, TypeEnum } from '@dxos/echo-schema';
10
+ import { type Type } from '@dxos/echo';
11
+ import { Format, TypeEnum } from '@dxos/echo/internal';
13
12
  import { invariant } from '@dxos/invariant';
14
13
  import { type DxGridAxis, type DxGridPosition } from '@dxos/lit-grid';
15
- import { createMarkdownExtensions } from '@dxos/react-ui-editor';
16
14
  import {
17
15
  type EditorBlurHandler,
18
16
  type EditorKeyEvent,
@@ -24,40 +22,37 @@ import {
24
22
  parseCellIndex,
25
23
  useGridContext,
26
24
  } from '@dxos/react-ui-grid';
27
- import { createLinks, tagPicker } from '@dxos/react-ui-tag-picker';
28
25
  import { type FieldProjection } from '@dxos/schema';
29
26
 
30
27
  import { type ModalController, type TableModel } from '../../model';
31
-
32
28
  import { CellValidationMessage } from './CellValidationMessage';
33
- import { FormCellEditor } from './FormCellEditor';
29
+ import { FormCellEditor, type OnCreateHandler } from './FormCellEditor';
34
30
 
35
31
  /**
36
32
  * Option to create new object/value.
37
33
  */
38
- export type QueryResult = Pick<Completion, 'label'> & { data: any };
34
+ export type QueryResult = Pick<Completion, 'label'> & {
35
+ data: any;
36
+ };
39
37
 
40
- export type TableCellEditorProps = {
38
+ export type TableCellEditorProps<T extends Type.AnyEntity = Type.AnyEntity> = {
39
+ schema?: T;
41
40
  model?: TableModel;
42
41
  modals?: ModalController;
43
- schema?: Schema.AnyNoContext;
44
42
  onFocus?: (axis?: DxGridAxis, delta?: -1 | 0 | 1, cell?: DxGridPosition) => void;
43
+ onCreate?: OnCreateHandler;
45
44
  onSave?: () => void;
46
- client?: Client;
47
45
  };
48
46
 
49
- const adaptValidationMessage = (message: string | null) =>
50
- message ? (message.endsWith('is missing') ? 'Can’t be blank' : message) : null;
51
-
52
- export const TableValueEditor = ({
47
+ export const TableValueEditor = <T extends Type.AnyEntity = Type.AnyEntity>({
48
+ __gridScope,
49
+ schema,
53
50
  model,
54
51
  modals,
55
- schema,
56
52
  onFocus,
57
53
  onSave,
58
- client,
59
- __gridScope,
60
- }: GridScopedProps<TableCellEditorProps>) => {
54
+ onCreate,
55
+ }: GridScopedProps<TableCellEditorProps<T>>) => {
61
56
  const { editing } = useGridContext('TableValueEditor', __gridScope);
62
57
 
63
58
  const fieldProjection = useMemo<FieldProjection | undefined>(() => {
@@ -66,7 +61,7 @@ export const TableValueEditor = ({
66
61
  }
67
62
 
68
63
  const { col } = parseCellIndex(editing.index);
69
- const field = model.projection.fields[col];
64
+ const field = model.projection.getFields()[col];
70
65
  const fieldProjection = model.projection.getFieldProjection(field.id);
71
66
  invariant(fieldProjection);
72
67
  return fieldProjection;
@@ -74,35 +69,33 @@ export const TableValueEditor = ({
74
69
 
75
70
  if (
76
71
  fieldProjection?.props.type === TypeEnum.Array ||
77
- fieldProjection?.props.format === FormatEnum.SingleSelect ||
78
- fieldProjection?.props.format === FormatEnum.Ref
79
- // TODO(thure): Support `FormatEnum.MultiSelect`
72
+ fieldProjection?.props.format === Format.TypeFormat.SingleSelect ||
73
+ fieldProjection?.props.format === Format.TypeFormat.Ref
74
+ // TODO(thure): Support `Format.TypeFormat.MultiSelect`
80
75
  ) {
81
76
  return (
82
- <FormCellEditor
83
- fieldProjection={fieldProjection}
84
- model={model}
85
- schema={schema}
77
+ <FormCellEditor<T>
86
78
  __gridScope={__gridScope}
87
- onSave={onSave}
88
- client={client}
79
+ schema={schema}
80
+ model={model}
81
+ fieldProjection={fieldProjection}
89
82
  modals={modals}
83
+ onSave={onSave}
84
+ onCreate={onCreate}
90
85
  />
91
86
  );
92
87
  }
93
88
 
94
- // For all other types, use the existing cell editor
89
+ // For all other types, use the existing cell editor.
95
90
  return <TableCellEditor model={model} modals={modals} onFocus={onFocus} onSave={onSave} __gridScope={__gridScope} />;
96
91
  };
97
92
 
98
- const editorSlots = { scroll: { className: '!plb-[--dx-grid-cell-editor-padding-block]' } };
99
-
100
93
  export const TableCellEditor = ({
94
+ __gridScope,
101
95
  model,
102
96
  modals,
103
97
  onFocus,
104
98
  onSave,
105
- __gridScope,
106
99
  }: GridScopedProps<TableCellEditorProps>) => {
107
100
  const { editing, setEditing } = useGridContext('TableCellEditor', __gridScope);
108
101
  const suppressNextBlur = useRef(false);
@@ -115,12 +108,13 @@ export const TableCellEditor = ({
115
108
  }
116
109
 
117
110
  const { col } = parseCellIndex(editing.index);
118
- const field = model.projection.fields[col];
111
+ const field = model.projection.getFields()[col];
119
112
  const fieldProjection = model.projection.getFieldProjection(field.id);
120
113
  invariant(fieldProjection);
121
114
  return fieldProjection;
122
115
  }, [model, editing]);
123
116
 
117
+ // TOOD(burdon): Attach to event handler?
124
118
  const handleEnter = useCallback(
125
119
  async (value: any) => {
126
120
  if (!model || !editing) {
@@ -137,7 +131,7 @@ export const TableCellEditor = ({
137
131
  setEditing(null);
138
132
  onSave?.();
139
133
  } else {
140
- setValidationError(adaptValidationMessage(validationResult.error));
134
+ setValidationError(validationResult.error);
141
135
  setValidationVariant('error');
142
136
  }
143
137
  },
@@ -213,48 +207,6 @@ export const TableCellEditor = ({
213
207
  ...(editing?.initialContent && { onNav: handleClose }),
214
208
  }),
215
209
  ];
216
-
217
- const format = fieldProjection.props.format;
218
- if (format === FormatEnum.SingleSelect || format === FormatEnum.MultiSelect) {
219
- // TODO(ZaymonFC): Reconcile this with the TagPicker component?
220
- const options = fieldProjection.props.options || [];
221
- const mode = format === FormatEnum.SingleSelect ? ('single-select' as const) : ('multi-select' as const);
222
-
223
- // Add markdown extensions needed by tag picker.
224
- extensions.push(createMarkdownExtensions());
225
- extensions.push(
226
- tagPicker({
227
- mode,
228
- keymap: false,
229
- onSearch: (text, selectedIds) => {
230
- return options
231
- .filter(
232
- (option) =>
233
- selectedIds.indexOf(option.id) === -1 &&
234
- (text.length === 0 || option.title.toLowerCase().includes(text.toLowerCase())),
235
- )
236
- .map((option) => ({
237
- id: option.id,
238
- label: option.title,
239
- hue: option.color as any,
240
- }));
241
- },
242
- onUpdate: (ids) => {
243
- if (model && editing) {
244
- if (ids.length === 0) {
245
- return;
246
- }
247
- if (mode === 'single-select') {
248
- void handleEnter(ids[0]);
249
- } else {
250
- void handleEnter(ids);
251
- }
252
- }
253
- },
254
- }),
255
- );
256
- }
257
-
258
210
  // Add validation extension to handle content changes.
259
211
  if (model && editing) {
260
212
  extensions.push(
@@ -283,52 +235,6 @@ export const TableCellEditor = ({
283
235
  const getCellContent = useCallback<GridCellEditorProps['getCellContent']>(() => {
284
236
  if (model && editing) {
285
237
  const cell = parseCellIndex(editing.index);
286
- const { col } = cell;
287
- const field = model.projection.fields[col];
288
- const fieldProjection = model.projection.getFieldProjection(field.id);
289
-
290
- if (
291
- fieldProjection?.props.format === FormatEnum.SingleSelect ||
292
- fieldProjection?.props.format === FormatEnum.MultiSelect
293
- ) {
294
- const value = model.getCellData(cell);
295
- if (value !== undefined) {
296
- const options = fieldProjection.props.options || [];
297
- if (fieldProjection.props.format === FormatEnum.MultiSelect) {
298
- const tagItems = value
299
- .split(',')
300
- .map((id: string) => {
301
- const option = options.find((o) => o.id === id);
302
- if (option) {
303
- return {
304
- id,
305
- label: option.title,
306
- hue: option.color as any,
307
- };
308
- }
309
- return undefined;
310
- })
311
- .filter((item: any): item is { id: any; label: string; hue: any } => item !== undefined);
312
-
313
- return createLinks(tagItems);
314
- } else {
315
- const option = options.find((o) => o.id === value);
316
-
317
- if (option) {
318
- const tagItem = {
319
- id: value,
320
- label: option.title,
321
- hue: option.color as any,
322
- };
323
-
324
- return createLinks([tagItem]);
325
- }
326
- }
327
- }
328
-
329
- return '';
330
- }
331
-
332
238
  const value = model.getCellData(cell);
333
239
  return value !== undefined ? String(value) : '';
334
240
  }
@@ -337,7 +243,16 @@ export const TableCellEditor = ({
337
243
  return (
338
244
  <>
339
245
  <CellValidationMessage validationError={validationError} variant={validationVariant} __gridScope={__gridScope} />
340
- <GridCellEditor extensions={extensions} getCellContent={getCellContent} onBlur={handleBlur} slots={editorSlots} />
246
+ <GridCellEditor
247
+ extensions={extensions}
248
+ slots={{
249
+ content: {
250
+ className: '!py-(--dx-grid-cell-editor-padding-block)',
251
+ },
252
+ }}
253
+ getCellContent={getCellContent}
254
+ onBlur={handleBlur}
255
+ />
341
256
  </>
342
257
  );
343
258
  };
@@ -3,3 +3,4 @@
3
3
  //
4
4
 
5
5
  export * from './TableCellEditor';
6
+ export { type OnCreateHandler } from './FormCellEditor';
@@ -3,4 +3,3 @@
3
3
  //
4
4
 
5
5
  export * from './Table';
6
- export * from './TableToolbar';
@@ -2,33 +2,59 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- import { Schema } from 'effect';
5
+ import { Atom } from '@effect-atom/atom-react';
6
+ import * as Schema from 'effect/Schema';
6
7
  import { useEffect } from 'react';
7
8
 
8
- import { FormatEnum, ObjectId, TypeEnum, TypedObject, setValue } from '@dxos/echo-schema';
9
- import { live } from '@dxos/live-object';
10
- import { faker } from '@dxos/random';
9
+ import { Format, Type } from '@dxos/echo';
10
+ import { TypeEnum } from '@dxos/echo/internal';
11
+ import { setValue } from '@dxos/effect';
12
+ import { random } from '@dxos/random';
11
13
  import { type ProjectionModel } from '@dxos/schema';
12
14
 
13
- export const TestSchema = TypedObject({ typename: 'example.com/type/Test', version: '0.1.0' })({
14
- id: ObjectId,
15
+ export const TestSchema = Schema.Struct({
15
16
  name: Schema.optional(Schema.String),
16
17
  age: Schema.optional(Schema.Number),
17
18
  active: Schema.optional(Schema.Boolean),
18
19
  netWorth: Schema.optional(Schema.Number),
19
- });
20
+ }).pipe(
21
+ Type.object({
22
+ typename: 'com.example.type.test',
23
+ version: '0.1.0',
24
+ }),
25
+ );
20
26
 
21
- export const createItems = (n: number) => {
22
- const { data } = live({
27
+ export type TestItem = {
28
+ name: string;
29
+ age: number;
30
+ active: boolean;
31
+ netWorth: number;
32
+ };
33
+
34
+ /**
35
+ * Creates an Atom containing test items data.
36
+ */
37
+ export const createItemsAtom = (n: number) => {
38
+ return Atom.make<{ data: TestItem[] }>({
23
39
  data: Array.from({ length: n }, () => ({
24
- name: faker.person.fullName(),
25
- age: faker.number.int({ min: 20, max: 70 }),
26
- active: faker.datatype.boolean(),
27
- netWorth: faker.number.int(),
40
+ name: random.person.fullName(),
41
+ age: random.number.int({ min: 20, max: 70 }),
42
+ active: random.datatype.boolean(),
43
+ netWorth: random.number.int(),
28
44
  })),
29
- });
45
+ }).pipe(Atom.keepAlive);
46
+ };
30
47
 
31
- return data;
48
+ /**
49
+ * Creates plain test items (non-reactive).
50
+ */
51
+ export const createItems = (n: number): TestItem[] => {
52
+ return Array.from({ length: n }, () => ({
53
+ name: random.person.fullName(),
54
+ age: random.number.int({ min: 20, max: 70 }),
55
+ active: random.datatype.boolean(),
56
+ netWorth: random.number.int(),
57
+ }));
32
58
  };
33
59
 
34
60
  export type SimulatorProps = {
@@ -59,7 +85,7 @@ export const useSimulator = ({ items, projection, insertInterval, updateInterval
59
85
 
60
86
  const i = setInterval(() => {
61
87
  const rowIdx = Math.floor(Math.random() * items.length);
62
- const fields = projection.fields ?? [];
88
+ const fields = projection.getFields() ?? [];
63
89
  const columnIdx = Math.floor(Math.random() * fields.length);
64
90
  const field = fields[columnIdx];
65
91
  const item = items[rowIdx];
@@ -88,7 +114,7 @@ export const useSimulator = ({ items, projection, insertInterval, updateInterval
88
114
 
89
115
  if (format) {
90
116
  switch (format) {
91
- case FormatEnum.Currency: {
117
+ case Format.TypeFormat.Currency: {
92
118
  item[path] = Math.floor(Math.random() * 1000);
93
119
  break;
94
120
  }
@@ -98,5 +124,5 @@ export const useSimulator = ({ items, projection, insertInterval, updateInterval
98
124
  }, updateInterval);
99
125
 
100
126
  return () => clearInterval(i);
101
- }, [items, projection.fields, updateInterval]);
127
+ }, [items, projection.getFields(), updateInterval]);
102
128
  };
@@ -2,5 +2,6 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- export * from './useTableModel';
6
5
  export * from './useAddRow';
6
+ export * from './useProjectionModel';
7
+ export * from './useTableModel';
@@ -2,17 +2,15 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- import { type Schema } from 'effect';
6
5
  import { useCallback } from 'react';
7
6
 
8
- import { type Space } from '@dxos/client/echo';
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 UseAddRowParams = {
14
- space?: Space;
15
- schema?: Schema.Schema.AnyNoContext;
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 = ({ space, schema }: UseAddRowParams) => {
21
+ export const useAddRow = ({ db, schema }: UseAddRowProps) => {
24
22
  return useCallback(
25
23
  (data?: any): InsertRowResult => {
26
- if (space && schema) {
24
+ if (db && schema) {
27
25
  try {
28
- space.db.add(Obj.make(schema, data ?? {}));
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
- [space, schema],
34
+ [db, schema],
37
35
  );
38
36
  };
@@ -0,0 +1,39 @@
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 { JsonSchema, 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
+ // For mutable schemas (EchoSchema), use the live jsonSchema reference for reactivity.
25
+ // For immutable schemas, create a snapshot.
26
+ const jsonSchema = Type.isMutable(schema) ? schema.jsonSchema : JsonSchema.toJsonSchema(schema);
27
+
28
+ // Always use createEchoChangeCallback since the view is ECHO-backed.
29
+ // Pass schema only when mutable to allow schema mutations.
30
+ const change = createEchoChangeCallback(view, Type.isMutable(schema) ? schema : undefined);
31
+
32
+ const projection = new ProjectionModel({ registry, view, baseSchema: jsonSchema, change });
33
+ projection.normalizeView();
34
+ setProjection(projection);
35
+ }
36
+ }, [schema, table, registry]);
37
+
38
+ return projection;
39
+ };
@@ -2,58 +2,65 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- import { effect } from '@preact/signals-core';
6
- import orderBy from 'lodash.orderby';
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 JsonSchemaType } from '@dxos/echo-schema';
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 DataType, type ProjectionModel } from '@dxos/schema';
10
+ import { type ProjectionModel } from '@dxos/schema';
14
11
  import { isNonNullable } from '@dxos/util';
15
12
 
16
- import { TableModel, type TableModelProps, type TableRow, type TableRowAction } from '../model';
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 UseTableModelParams<T extends TableRow = TableRow> = {
19
- view?: DataType.View;
20
- schema?: JsonSchemaType;
22
+ export type UseTableModelProps<T extends TableRow = TableRow> = {
23
+ object?: Table.Table;
21
24
  projection?: ProjectionModel;
22
- rows?: Live<T>[];
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' | 'onDeleteColumn' | 'onCellUpdate' | 'onRowOrderChange'
32
+ 'features' | 'onInsertRow' | 'onDeleteRows' | 'onColumnDelete' | 'onCellUpdate' | 'onRowOrderChange'
29
33
  >;
30
34
 
31
35
  export const useTableModel = <T extends TableRow = TableRow>({
32
- view,
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
- }: UseTableModelParams<T>): TableModel<T> | undefined => {
42
- const selected = useSelected(view && fullyQualifiedId(view), 'multi');
43
- const initialSelection = useMemo(() => selected, [view]);
45
+ }: UseTableModelProps<T>): TableModel<T> | undefined => {
46
+ const registry = useContext(RegistryContext);
47
+ const selected = useSelected(object && Obj.getDXN(object).toString(), 'multi');
48
+ const initialSelection = useMemo(() => selected, [object]);
44
49
 
45
50
  const [model, setModel] = useState<TableModel<T>>();
46
51
  useEffect(() => {
47
- if (!view || !schema) {
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
- view,
55
- schema,
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
- }, [view, schema, projection, features, rowActions, initialSelection]); // TODO(burdon): Trigger if callbacks change?
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
- // TODO(ZaymonFC): Remove this workaround once unstable query ordering issue is resolved
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 = effect(() => {
94
- const selectedItems = [...model.selection.selection.value];
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,11 +2,7 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- import { translations } from './translations';
6
-
7
5
  export * from './components';
8
6
  export * from './hooks';
9
7
  export * from './model';
10
8
  export * from './util';
11
-
12
- export { translations };