@dxos/react-ui-table 0.8.4-main.9735255 → 0.8.4-main.abd8ff62ef

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 (137) hide show
  1. package/dist/lib/browser/{chunk-OZBLY7XM.mjs → chunk-FBURZBPH.mjs} +17 -23
  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-I5IFW7UF.mjs → chunk-YGC7LZJN.mjs} +39 -90
  6. package/dist/lib/browser/chunk-YGC7LZJN.mjs.map +7 -0
  7. package/dist/lib/browser/index.mjs +198 -235
  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 +7 -6
  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 +2 -1
  15. package/dist/lib/node-esm/{chunk-WNZSKIRF.mjs → chunk-DKKMSVKP.mjs} +17 -23
  16. package/dist/lib/node-esm/chunk-DKKMSVKP.mjs.map +7 -0
  17. package/dist/lib/node-esm/{chunk-DDJKYJFY.mjs → chunk-GKIWDEZ7.mjs} +39 -90
  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 +198 -235
  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 +7 -6
  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 +2 -1
  29. package/dist/types/src/components/Table/ColumnActionsMenu.d.ts.map +1 -1
  30. package/dist/types/src/components/Table/ColumnSettings.d.ts.map +1 -1
  31. package/dist/types/src/components/Table/DynamicTable.d.ts +5 -4
  32. package/dist/types/src/components/Table/DynamicTable.d.ts.map +1 -1
  33. package/dist/types/src/components/Table/DynamicTable.stories.d.ts +20 -22
  34. package/dist/types/src/components/Table/DynamicTable.stories.d.ts.map +1 -1
  35. package/dist/types/src/components/Table/Relations.stories.d.ts +82 -82
  36. package/dist/types/src/components/Table/Relations.stories.d.ts.map +1 -1
  37. package/dist/types/src/components/Table/RowActionsMenu.d.ts.map +1 -1
  38. package/dist/types/src/components/Table/Table.d.ts +20 -21
  39. package/dist/types/src/components/Table/Table.d.ts.map +1 -1
  40. package/dist/types/src/components/Table/Table.stories.d.ts +84 -84
  41. package/dist/types/src/components/Table/Table.stories.d.ts.map +1 -1
  42. package/dist/types/src/components/Table/TableContent.d.ts +17 -0
  43. package/dist/types/src/components/Table/TableContent.d.ts.map +1 -0
  44. package/dist/types/src/components/Table/TableToolbar.d.ts +21 -0
  45. package/dist/types/src/components/Table/TableToolbar.d.ts.map +1 -0
  46. package/dist/types/src/components/TableCellEditor/CellValidationMessage.d.ts.map +1 -1
  47. package/dist/types/src/components/TableCellEditor/FormCellEditor.d.ts +3 -3
  48. package/dist/types/src/components/TableCellEditor/FormCellEditor.d.ts.map +1 -1
  49. package/dist/types/src/components/TableCellEditor/TableCellEditor.d.ts +2 -2
  50. package/dist/types/src/components/TableCellEditor/TableCellEditor.d.ts.map +1 -1
  51. package/dist/types/src/components/TableCellEditor/TableCellEditor.stories.d.ts +22 -24
  52. package/dist/types/src/components/TableCellEditor/TableCellEditor.stories.d.ts.map +1 -1
  53. package/dist/types/src/components/index.d.ts +0 -1
  54. package/dist/types/src/components/index.d.ts.map +1 -1
  55. package/dist/types/src/components/testing.d.ts.map +1 -1
  56. package/dist/types/src/hooks/useAddRow.d.ts +1 -1
  57. package/dist/types/src/hooks/useAddRow.d.ts.map +1 -1
  58. package/dist/types/src/hooks/useProjectionModel.d.ts +1 -1
  59. package/dist/types/src/hooks/useProjectionModel.d.ts.map +1 -1
  60. package/dist/types/src/hooks/useTableModel.d.ts +1 -1
  61. package/dist/types/src/hooks/useTableModel.d.ts.map +1 -1
  62. package/dist/types/src/index.d.ts +0 -2
  63. package/dist/types/src/index.d.ts.map +1 -1
  64. package/dist/types/src/model/modal-controller.d.ts.map +1 -1
  65. package/dist/types/src/model/selection-model.d.ts +1 -1
  66. package/dist/types/src/model/selection-model.d.ts.map +1 -1
  67. package/dist/types/src/model/table-model.d.ts +7 -6
  68. package/dist/types/src/model/table-model.d.ts.map +1 -1
  69. package/dist/types/src/model/table-presentation.d.ts.map +1 -1
  70. package/dist/types/src/playwright/TableManager.d.ts +2 -1
  71. package/dist/types/src/playwright/TableManager.d.ts.map +1 -1
  72. package/dist/types/src/playwright/playwright.config.d.ts.map +1 -1
  73. package/dist/types/src/testing/useTestTableModel.d.ts +2 -2
  74. package/dist/types/src/testing/useTestTableModel.d.ts.map +1 -1
  75. package/dist/types/src/translations.d.ts +19 -21
  76. package/dist/types/src/translations.d.ts.map +1 -1
  77. package/dist/types/src/types/Table.d.ts +1 -6
  78. package/dist/types/src/types/Table.d.ts.map +1 -1
  79. package/dist/types/src/util/dynamic-table.d.ts +6 -5
  80. package/dist/types/src/util/dynamic-table.d.ts.map +1 -1
  81. package/dist/types/src/util/dynamic-table.test.d.ts +2 -0
  82. package/dist/types/src/util/dynamic-table.test.d.ts.map +1 -0
  83. package/dist/types/src/util/query-ast.d.ts.map +1 -1
  84. package/dist/types/src/util/schema.d.ts.map +1 -1
  85. package/dist/types/src/util/signals.d.ts.map +1 -1
  86. package/dist/types/src/util/sort.d.ts.map +1 -1
  87. package/dist/types/src/util/table-buttons.d.ts +20 -20
  88. package/dist/types/src/util/table-buttons.d.ts.map +1 -1
  89. package/dist/types/src/util/table-controls.d.ts +4 -4
  90. package/dist/types/src/util/table-controls.d.ts.map +1 -1
  91. package/dist/types/src/util/tag.d.ts.map +1 -1
  92. package/dist/types/tsconfig.tsbuildinfo +1 -1
  93. package/package.json +59 -62
  94. package/src/components/Table/ColumnActionsMenu.tsx +7 -6
  95. package/src/components/Table/ColumnSettings.tsx +3 -4
  96. package/src/components/Table/DynamicTable.stories.tsx +19 -18
  97. package/src/components/Table/DynamicTable.tsx +13 -20
  98. package/src/components/Table/Relations.stories.tsx +78 -34
  99. package/src/components/Table/RowActionsMenu.tsx +3 -2
  100. package/src/components/Table/Table.stories.tsx +72 -52
  101. package/src/components/Table/Table.tsx +41 -435
  102. package/src/components/Table/TableContent.tsx +421 -0
  103. package/src/components/{TableToolbar → Table}/TableToolbar.tsx +36 -43
  104. package/src/components/TableCellEditor/CellValidationMessage.tsx +4 -4
  105. package/src/components/TableCellEditor/FormCellEditor.tsx +14 -7
  106. package/src/components/TableCellEditor/TableCellEditor.stories.tsx +13 -12
  107. package/src/components/TableCellEditor/TableCellEditor.tsx +12 -10
  108. package/src/components/index.ts +0 -1
  109. package/src/components/testing.ts +10 -10
  110. package/src/hooks/useAddRow.ts +1 -1
  111. package/src/hooks/useProjectionModel.ts +3 -3
  112. package/src/hooks/useTableModel.ts +1 -1
  113. package/src/index.ts +0 -4
  114. package/src/model/table-model.test.ts +5 -6
  115. package/src/model/table-model.ts +18 -17
  116. package/src/model/table-presentation.test.ts +13 -7
  117. package/src/model/table-presentation.ts +12 -12
  118. package/src/playwright/TableManager.ts +10 -3
  119. package/src/playwright/playwright.config.ts +1 -1
  120. package/src/playwright/smoke.spec.ts +48 -5
  121. package/src/testing/useTestTableModel.ts +6 -6
  122. package/src/translations.ts +19 -21
  123. package/src/types/Table.ts +12 -23
  124. package/src/util/dynamic-table.test.ts +77 -0
  125. package/src/util/dynamic-table.ts +17 -16
  126. package/src/util/query-ast.ts +3 -0
  127. package/src/util/table-buttons.ts +1 -1
  128. package/src/util/table-controls.ts +2 -2
  129. package/dist/lib/browser/chunk-I5IFW7UF.mjs.map +0 -7
  130. package/dist/lib/browser/chunk-OZBLY7XM.mjs.map +0 -7
  131. package/dist/lib/node-esm/chunk-DDJKYJFY.mjs.map +0 -7
  132. package/dist/lib/node-esm/chunk-WNZSKIRF.mjs.map +0 -7
  133. package/dist/types/src/components/TableToolbar/TableToolbar.d.ts +0 -21
  134. package/dist/types/src/components/TableToolbar/TableToolbar.d.ts.map +0 -1
  135. package/dist/types/src/components/TableToolbar/index.d.ts +0 -2
  136. package/dist/types/src/components/TableToolbar/index.d.ts.map +0 -1
  137. package/src/components/TableToolbar/index.ts +0 -5
@@ -0,0 +1,421 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import { Atom, RegistryContext, useAtomValue } from '@effect-atom/atom-react';
6
+ import * as String from 'effect/String';
7
+ import React, { type MouseEvent, type WheelEvent, useCallback, useContext, useEffect, useMemo, useState } from 'react';
8
+
9
+ import { type Type } from '@dxos/echo';
10
+ import { log } from '@dxos/log';
11
+ import { useAttention } from '@dxos/react-ui-attention';
12
+ import {
13
+ type DxGridAxisMeta,
14
+ type DxGridElement,
15
+ type DxGridPlane,
16
+ type DxGridPlaneRange,
17
+ Grid,
18
+ type GridContentProps,
19
+ closestCell,
20
+ gridSeparatorBlockEnd,
21
+ gridSeparatorInlineEnd,
22
+ } from '@dxos/react-ui-grid';
23
+ import { DxEditRequest } from '@dxos/react-ui-grid';
24
+ import { composable, composableProps, mx } from '@dxos/ui-theme';
25
+
26
+ import { type InsertRowResult, ModalController, type TableModel, type TablePresentation } from '../../model';
27
+ import { tableButtons, tableControls } from '../../util';
28
+ import { type OnCreateHandler, type TableCellEditorProps, TableValueEditor } from '../TableCellEditor';
29
+ import { ColumnActionsMenu } from './ColumnActionsMenu';
30
+ import { ColumnSettings } from './ColumnSettings';
31
+ import { RowActionsMenu } from './RowActionsMenu';
32
+ import { TableController, useTableContext } from './Table';
33
+
34
+ const columnDefault = { grid: { minSize: 80, maxSize: 640 } };
35
+ const rowDefault = { frozenRowsStart: { readonly: true, focusUnfurl: false } };
36
+ const emptyColumnMeta = Atom.make<DxGridAxisMeta>({ grid: {} });
37
+ const emptyRowsAtom = Atom.make<unknown[]>([]);
38
+ const emptyCellUpdateAtom = Atom.make<number>(0);
39
+
40
+ //
41
+ // Content
42
+ //
43
+
44
+ const TABLE_CONTENT_NAME = 'Table.Content';
45
+
46
+ export type TableContentProps = {
47
+ schema?: Type.AnyEntity;
48
+ model?: TableModel;
49
+ presentation?: TablePresentation;
50
+ // TODO(burdon): Factor out attention.
51
+ ignoreAttention?: boolean;
52
+ onCreate?: OnCreateHandler;
53
+ onRowClick?: (row: any) => void;
54
+ testId?: string;
55
+ };
56
+
57
+ export const TableContent = composable<HTMLDivElement, TableContentProps>(
58
+ ({ schema, model, presentation, ignoreAttention, onCreate, onRowClick, testId, ...props }, forwardedRef) => {
59
+ const registry = useContext(RegistryContext);
60
+ const [dxGrid, setDxGrid] = useState<DxGridElement | null>(null);
61
+ const { hasAttention } = useAttention(model?.id ?? 'table');
62
+ const modals = useMemo(() => new ModalController(registry), [registry]);
63
+ const columnMeta = useAtomValue(model?.columnMeta ?? emptyColumnMeta);
64
+ // Subscribe to rows atom to trigger re-render when rows change.
65
+ const rows = useAtomValue(model?.rows ?? emptyRowsAtom);
66
+ // Subscribe to cell update counter to trigger re-render when individual cells change.
67
+ const cellUpdateCounter = useAtomValue(model?.cellUpdate ?? emptyCellUpdateAtom);
68
+ // Derive column count from columnMeta (reactive) with fallback.
69
+ const columnCount = Object.keys(columnMeta.grid).length || model?.projection.getFields().length || 0;
70
+
71
+ const draftRowCount = model?.getDraftRowCount() ?? 0;
72
+
73
+ const frozen = useMemo(() => {
74
+ const noActionColumn =
75
+ model?.features.dataEditable === false &&
76
+ model?.features.schemaEditable === false &&
77
+ model.rowActions.length === 0;
78
+
79
+ return {
80
+ frozenRowsStart: 1,
81
+ frozenRowsEnd: Math.max(1, draftRowCount),
82
+ frozenColsStart: model?.features.selection.enabled ? 1 : 0,
83
+ frozenColsEnd: noActionColumn ? 0 : 1,
84
+ };
85
+ }, [model, draftRowCount]);
86
+
87
+ const getCells = useCallback<NonNullable<GridContentProps['getCells']>>(
88
+ (range: DxGridPlaneRange, plane: DxGridPlane) => presentation?.getCells(range, plane) ?? {},
89
+ [presentation],
90
+ );
91
+
92
+ useEffect(() => {
93
+ if (!dxGrid || !presentation) {
94
+ return;
95
+ }
96
+
97
+ dxGrid.getCells = getCells;
98
+ }, [dxGrid, presentation, getCells]);
99
+
100
+ // Trigger grid update when rows or cell values change.
101
+ useEffect(() => {
102
+ if (!dxGrid) {
103
+ return;
104
+ }
105
+
106
+ dxGrid.updateCells(true);
107
+ dxGrid.requestUpdate();
108
+ }, [dxGrid, rows, cellUpdateCounter]);
109
+
110
+ const handleInsertRowResult = useCallback(
111
+ (insertResult?: InsertRowResult) => {
112
+ if (insertResult === 'draft') {
113
+ requestAnimationFrame(() => {
114
+ dxGrid?.setFocus({ plane: 'frozenRowsEnd', col: 0, row: 0 });
115
+ dxGrid?.refocus();
116
+ });
117
+ } else {
118
+ requestAnimationFrame(() => {
119
+ dxGrid?.setFocus({ plane: 'grid', col: 0, row: model ? model.getRowCount() - 1 : 0 });
120
+ dxGrid?.refocus();
121
+ });
122
+ }
123
+ },
124
+ [model, dxGrid],
125
+ );
126
+
127
+ const handleSave = useCallback(() => {
128
+ dxGrid?.updateCells(true);
129
+ dxGrid?.requestUpdate();
130
+ }, [dxGrid]);
131
+
132
+ const handleSaveDraftRow = useCallback(
133
+ (rowIndex = 0) => {
134
+ if (model && dxGrid) {
135
+ const didCommitSuccessfully = model.commitDraftRow(rowIndex);
136
+ if (didCommitSuccessfully) {
137
+ requestAnimationFrame(() => {
138
+ dxGrid.scrollToEndRow();
139
+ });
140
+ }
141
+ }
142
+ },
143
+ [model, dxGrid],
144
+ );
145
+
146
+ const handleGridClick = useCallback(
147
+ (event: MouseEvent) => {
148
+ const cell = closestCell(event.target as HTMLElement);
149
+ if (cell) {
150
+ const { row: rowIndex, plane } = cell;
151
+ if (onRowClick) {
152
+ if (plane === 'grid') {
153
+ const row = model?.getRowAt(rowIndex);
154
+ row && onRowClick(row);
155
+ } else {
156
+ onRowClick(cell);
157
+ }
158
+ }
159
+
160
+ if (model?.features.selection.enabled && model?.selection.selectionMode === 'single') {
161
+ model.selection.toggleSelectionForRowIndex(rowIndex);
162
+ }
163
+ }
164
+
165
+ if (!modals) {
166
+ return;
167
+ }
168
+
169
+ const buttonSelector = Object.values(tableButtons)
170
+ .map((button) => `button[${button.attr}]`)
171
+ .join(',');
172
+
173
+ const tableButtonElement = (event.target as HTMLElement).closest(buttonSelector) as HTMLElement;
174
+ if (tableButtonElement) {
175
+ const button = Object.values(tableButtons).find((btn) => tableButtonElement.hasAttribute(btn.attr));
176
+ if (!button) {
177
+ return;
178
+ }
179
+
180
+ modals.setTrigger(tableButtonElement);
181
+ const data = button.getData(tableButtonElement);
182
+ switch (data.type) {
183
+ case 'rowMenu': {
184
+ modals.showRowMenu(data.rowIndex);
185
+ break;
186
+ }
187
+ case 'columnSettings': {
188
+ modals.showColumnMenu(data.fieldId);
189
+ break;
190
+ }
191
+ case 'newColumn': {
192
+ modals.showColumnCreator();
193
+ break;
194
+ }
195
+ case 'sort': {
196
+ model?.toggleSort(data.fieldId);
197
+ break;
198
+ }
199
+ case 'saveDraftRow': {
200
+ handleSaveDraftRow();
201
+ break;
202
+ }
203
+ }
204
+ return;
205
+ }
206
+
207
+ const controlSelector = Object.values(tableControls)
208
+ .map((control) => `input[${control.attr}]`)
209
+ .join(',');
210
+
211
+ const controlElement = (event.target as HTMLElement).closest(controlSelector) as HTMLElement;
212
+ if (controlElement) {
213
+ const control = Object.values(tableControls).find((ctrl) => controlElement.hasAttribute(ctrl.attr));
214
+ if (!control) {
215
+ return;
216
+ }
217
+
218
+ const data = control.getData(controlElement);
219
+ switch (data.type) {
220
+ case 'checkbox': {
221
+ if (data.header) {
222
+ model?.selection.setSelection(model.selection.allRowsSelected ? 'none' : 'all');
223
+ } else {
224
+ model?.selection.toggleSelectionForRowIndex(data.rowIndex);
225
+ }
226
+ break;
227
+ }
228
+ case 'switch': {
229
+ if (model) {
230
+ model.updateCellData({ row: data.rowIndex, col: data.colIndex }, (value) => !value);
231
+ }
232
+ break;
233
+ }
234
+ }
235
+ }
236
+ },
237
+ [model, modals, dxGrid],
238
+ );
239
+
240
+ const handleFocus = useCallback<NonNullable<TableCellEditorProps['onFocus']>>(
241
+ (increment, delta, cell) => {
242
+ if (dxGrid && model) {
243
+ if (cell?.plane === 'grid' && cell?.row >= model.getRowCount() - 1 && increment !== 'col') {
244
+ handleInsertRowResult(draftRowCount < 1 ? model.insertRow() : 'final');
245
+ } else if (cell?.plane === 'frozenRowsEnd' && increment === 'row') {
246
+ handleSaveDraftRow(cell.row);
247
+ handleInsertRowResult(model.insertRow());
248
+ } else {
249
+ dxGrid.refocus(increment, delta);
250
+ }
251
+ }
252
+ },
253
+ [dxGrid, model],
254
+ );
255
+
256
+ const handleKeyDown = useCallback<NonNullable<GridContentProps['onKeyDown']>>(
257
+ (event) => {
258
+ if (!model?.features.dataEditable) {
259
+ return;
260
+ }
261
+
262
+ const cell = closestCell(event.target);
263
+ if (!model || !cell) {
264
+ return;
265
+ }
266
+
267
+ // Handle Meta+C (Copy) and Meta+V (Paste) commands
268
+ if (event.metaKey || event.ctrlKey) {
269
+ switch (event.key) {
270
+ case 'c': {
271
+ // Copy focused cell's text content to clipboard
272
+ try {
273
+ const cellData = model.getCellData(cell);
274
+ const textContent = cellData?.toString() ?? '';
275
+ void navigator.clipboard.writeText(textContent);
276
+ event.preventDefault();
277
+ } catch (error) {
278
+ log.catch(error);
279
+ }
280
+ break;
281
+ }
282
+ case 'v': {
283
+ // Paste clipboard content to focused cell
284
+ event.preventDefault();
285
+ void navigator.clipboard.readText().then((clipboardText) => {
286
+ try {
287
+ // Attempt to set the cell's content to clipboard content
288
+ model.setCellData(cell, String.trim(clipboardText).replace(/[\n\r]+/, ' '));
289
+ handleSave();
290
+ } catch {
291
+ // If validation fails, emit a DxEditRequest event with initialContent from clipboard
292
+ // TODO(thure): Should `dx-grid` expose a method like this?
293
+ const cellElement = (event.target as HTMLElement).closest(
294
+ '[data-dx-grid-action="cell"]',
295
+ ) as HTMLElement;
296
+ if (cellElement) {
297
+ const rect = cellElement.getBoundingClientRect();
298
+ const editRequest = new DxEditRequest({
299
+ cellIndex: `${cell.plane},${cell.col},${cell.row}`,
300
+ cellBox: {
301
+ insetInlineStart: rect.left,
302
+ insetBlockStart: rect.top,
303
+ inlineSize: rect.width,
304
+ blockSize: rect.height,
305
+ },
306
+ cellElement,
307
+ initialContent: clipboardText,
308
+ });
309
+ cellElement.dispatchEvent(editRequest);
310
+ }
311
+ }
312
+ });
313
+ break;
314
+ }
315
+ }
316
+ }
317
+
318
+ switch (event.key) {
319
+ case 'Backspace':
320
+ case 'Delete': {
321
+ try {
322
+ model.setCellData(cell, undefined);
323
+ event.preventDefault();
324
+ handleSave();
325
+ } catch {
326
+ // Delete results in a validation error; don’t prevent default so dx-grid can emit an edit request.
327
+ }
328
+ break;
329
+ }
330
+ }
331
+ },
332
+ [model, dxGrid, handleSave],
333
+ );
334
+
335
+ const handleAxisResize = useCallback<NonNullable<GridContentProps['onAxisResize']>>(
336
+ (event) => {
337
+ if (event.axis === 'col') {
338
+ const columnIndex = parseInt(event.index, 10);
339
+ model?.setColumnWidth(columnIndex, event.size);
340
+ }
341
+ },
342
+ [model],
343
+ );
344
+
345
+ const handleWheel = useCallback(
346
+ (event: WheelEvent) => {
347
+ if (!ignoreAttention && !hasAttention) {
348
+ event.stopPropagation();
349
+ }
350
+ },
351
+ [hasAttention, ignoreAttention],
352
+ );
353
+
354
+ const handleNewColumn = useCallback(() => {
355
+ const columns = model?.getColumnCount();
356
+ if (dxGrid && columns) {
357
+ dxGrid.scrollToColumn(columns - 1);
358
+ }
359
+ }, [model, dxGrid]);
360
+
361
+ // Publish controller to Root.
362
+ const { controllerRef } = useTableContext(TABLE_CONTENT_NAME);
363
+ controllerRef.current = useMemo<TableController>(() => {
364
+ if (!dxGrid || !presentation) {
365
+ return {};
366
+ }
367
+
368
+ return {
369
+ update: (cell) => {
370
+ if (cell) {
371
+ dxGrid.updateIfWithinBounds(cell, true);
372
+ } else {
373
+ dxGrid.updateCells(true);
374
+ dxGrid.requestUpdate();
375
+ }
376
+ },
377
+ handleInsertRowResult,
378
+ };
379
+ }, [dxGrid, model, presentation]);
380
+
381
+ if (!model || !modals) {
382
+ return <span role='none' className='dx-attention-surface' />;
383
+ }
384
+
385
+ return (
386
+ <div {...composableProps(props, { classNames: 'dx-container relative' })} ref={forwardedRef}>
387
+ <Grid.Root id={model.id ?? 'table-grid'}>
388
+ <TableValueEditor
389
+ model={model}
390
+ modals={modals}
391
+ schema={schema}
392
+ onFocus={handleFocus}
393
+ onSave={handleSave}
394
+ onCreate={onCreate}
395
+ />
396
+ <Grid.Content
397
+ className={mx('[--dx-grid-base:var(--base-surface)]', gridSeparatorInlineEnd, gridSeparatorBlockEnd)}
398
+ frozen={frozen}
399
+ columns={columnMeta}
400
+ columnDefault={columnDefault}
401
+ rowDefault={rowDefault}
402
+ limitRows={rows.length}
403
+ limitColumns={columnCount}
404
+ overscroll='trap'
405
+ onAxisResize={handleAxisResize}
406
+ onClick={handleGridClick}
407
+ onKeyDownCapture={handleKeyDown}
408
+ onWheelCapture={handleWheel}
409
+ {...(testId && { 'data-testid': testId })}
410
+ ref={setDxGrid}
411
+ />
412
+ <RowActionsMenu model={model} modals={modals} />
413
+ <ColumnActionsMenu model={model} modals={modals} />
414
+ <ColumnSettings model={model} modals={modals} onNewColumn={handleNewColumn} />
415
+ </Grid.Root>
416
+ </div>
417
+ );
418
+ },
419
+ );
420
+
421
+ TableContent.displayName = TABLE_CONTENT_NAME;
@@ -5,20 +5,20 @@
5
5
  import { Atom, RegistryContext } from '@effect-atom/atom-react';
6
6
  import React, { useContext, useEffect, useMemo } from 'react';
7
7
 
8
- import { type ThemedClassName } from '@dxos/react-ui';
9
8
  import {
10
9
  type ActionGraphEdges,
11
10
  type ActionGraphNodes,
12
11
  type ActionGraphProps,
12
+ Menu,
13
13
  type MenuAction,
14
- MenuProvider,
15
- ToolbarMenu,
14
+ MenuRootProps,
16
15
  createGapSeparator,
17
16
  createMenuAction,
18
17
  useMenuActions,
19
18
  } from '@dxos/react-ui-menu';
19
+ import { composable, composableProps } from '@dxos/ui-theme';
20
20
 
21
- import { translationKey } from '../../translations';
21
+ import { translationKey } from '#translations';
22
22
 
23
23
  export type TableToolbarActionProperties = { type: TableToolbarActionType };
24
24
 
@@ -28,17 +28,6 @@ export type TableToolbarActionType = 'add-row' | 'comment' | 'save-view';
28
28
 
29
29
  type TableToolbarState = Partial<{ viewDirty: boolean }>;
30
30
 
31
- // TODO(burdon): Radix style layout.
32
-
33
- export type TableToolbarProps = ThemedClassName<
34
- TableToolbarState & {
35
- onAdd?: () => void;
36
- onSave?: () => void;
37
- attendableId?: string;
38
- customActions?: Atom.Atom<ActionGraphProps>;
39
- }
40
- >;
41
-
42
31
  const createTableToolbarActions = ({
43
32
  stateAtom,
44
33
  onAdd,
@@ -56,7 +45,7 @@ const createTableToolbarActions = ({
56
45
  const add = createMenuAction<TableToolbarActionProperties>('add-row', onAdd, {
57
46
  type: 'add-row' as const,
58
47
  icon: 'ph--plus--regular',
59
- label: ['add row label', { ns: translationKey }],
48
+ label: ['add-row.label', { ns: translationKey }],
60
49
  testId: 'table.toolbar.add-row',
61
50
  });
62
51
  nodes.push(add);
@@ -66,7 +55,7 @@ const createTableToolbarActions = ({
66
55
  const save = createMenuAction<TableToolbarActionProperties>('save-view', onSave, {
67
56
  type: 'save-view' as const,
68
57
  icon: 'ph--floppy-disk--regular',
69
- label: ['save view label', { ns: translationKey }],
58
+ label: ['save-view.label', { ns: translationKey }],
70
59
  testId: 'table.toolbar.save-view',
71
60
  iconOnly: false,
72
61
  hidden: !state.viewDirty,
@@ -75,7 +64,7 @@ const createTableToolbarActions = ({
75
64
  }
76
65
  const gap = createGapSeparator();
77
66
  nodes.push(...gap.nodes);
78
- const edges: ActionGraphEdges = nodes.map(({ id: target }) => ({ source: 'root', target }));
67
+ const edges: ActionGraphEdges = nodes.map(({ id: target }) => ({ source: 'root', target, relation: 'child' }));
79
68
  if (customActions) {
80
69
  const custom = get(customActions);
81
70
  nodes.push(...custom.nodes);
@@ -88,31 +77,35 @@ const createTableToolbarActions = ({
88
77
  };
89
78
  });
90
79
 
91
- export const TableToolbar = ({
92
- classNames,
93
- viewDirty,
94
- attendableId,
95
- onAdd,
96
- onSave,
97
- customActions,
98
- }: TableToolbarProps) => {
99
- const registry = useContext(RegistryContext);
100
- const stateAtom = useMemo(() => Atom.make<TableToolbarState>({ viewDirty }), []);
80
+ export type TableToolbarProps = Pick<MenuRootProps, 'attendableId'> &
81
+ TableToolbarState & {
82
+ onAdd?: () => void;
83
+ onSave?: () => void;
84
+ customActions?: Atom.Atom<ActionGraphProps>;
85
+ };
86
+
87
+ export const TableToolbar = composable<HTMLDivElement, TableToolbarProps>(
88
+ ({ attendableId, viewDirty, onAdd, onSave, customActions, ...props }, forwardedRef) => {
89
+ const registry = useContext(RegistryContext);
90
+ const stateAtom = useMemo(() => Atom.make<TableToolbarState>({ viewDirty }), []);
91
+
92
+ // Update state.viewDirty when the prop changes.
93
+ useEffect(() => {
94
+ registry.set(stateAtom, { viewDirty });
95
+ }, [registry, stateAtom, viewDirty]);
101
96
 
102
- // Update state.viewDirty when the prop changes.
103
- useEffect(() => {
104
- registry.set(stateAtom, { viewDirty });
105
- }, [registry, stateAtom, viewDirty]);
97
+ const actionsCreator = useMemo(
98
+ () => createTableToolbarActions({ stateAtom, onAdd, onSave, customActions }),
99
+ [stateAtom, onAdd, onSave, customActions],
100
+ );
101
+ const menuActions = useMenuActions(actionsCreator);
106
102
 
107
- const actionsCreator = useMemo(
108
- () => createTableToolbarActions({ stateAtom, onAdd, onSave, customActions }),
109
- [stateAtom, onAdd, onSave, customActions],
110
- );
111
- const menu = useMenuActions(actionsCreator);
103
+ return (
104
+ <Menu.Root {...menuActions} attendableId={attendableId}>
105
+ <Menu.Toolbar {...composableProps(props)} ref={forwardedRef} />
106
+ </Menu.Root>
107
+ );
108
+ },
109
+ );
112
110
 
113
- return (
114
- <MenuProvider {...menu} attendableId={attendableId}>
115
- <ToolbarMenu classNames={classNames} />
116
- </MenuProvider>
117
- );
118
- };
111
+ TableToolbar.displayName = 'TableToolbar';
@@ -22,15 +22,15 @@ export const CellValidationMessage = ({
22
22
  return null;
23
23
  }
24
24
 
25
- const bgClass = variant === 'error' ? 'bg-errorSurface' : 'bg-warningSurface';
26
- const textClass = variant === 'error' ? 'text-errorSurfaceText' : 'text-warningSurfaceText';
25
+ const bgClass = variant === 'error' ? 'bg-error-surface' : 'bg-warning-surface';
26
+ const textClass = variant === 'error' ? 'text-error-surface-text' : 'text-warning-surface-text';
27
27
 
28
28
  return (
29
29
  <div
30
30
  role='none'
31
- className={`absolute ${bgClass} ${textClass} rounded-bs-sm text-xs p-1`}
31
+ className={`absolute ${bgClass} ${textClass} rounded-h-sm text-xs p-1`}
32
32
  style={{
33
- ...{ '--dx-gridCellWidth': `${box?.inlineSize ?? 200}px` },
33
+ ...{ '--dx-grid-cell-width': `${box?.inlineSize ?? 200}px` },
34
34
  zIndex: 10,
35
35
  insetBlockEnd: `calc(100% - ${box.insetBlockStart}px)`,
36
36
  insetInlineStart: box.insetInlineStart,
@@ -14,15 +14,16 @@ import { parseCellIndex, useGridContext } from '@dxos/react-ui-grid';
14
14
  import { type FieldProjection } from '@dxos/schema';
15
15
  import { getDeep, isTruthy, setDeep } from '@dxos/util';
16
16
 
17
+ import { translationKey } from '#translations';
18
+
17
19
  import { type ModalController, type TableModel, type TableRow } from '../../model';
18
- import { translationKey } from '../../translations';
19
20
  import { narrowSchema } from '../../util';
20
21
 
21
- const createOptionLabel: Label = ['create new object label', { ns: translationKey }];
22
+ const createOptionLabel: Label = ['create-new-object.label', { ns: translationKey }];
22
23
 
23
- export type OnCreateHandler = (schema: Type.Entity.Any, values: any) => Parameters<typeof Ref.make>[0];
24
+ export type OnCreateHandler = (schema: Type.AnyEntity, values: any) => Parameters<typeof Ref.make>[0];
24
25
 
25
- export type FormCellEditorProps<T extends Type.Entity.Any = Type.Entity.Any> = {
26
+ export type FormCellEditorProps<T extends Type.AnyEntity = Type.AnyEntity> = {
26
27
  __gridScope: any;
27
28
  schema?: T;
28
29
  model?: TableModel;
@@ -32,7 +33,7 @@ export type FormCellEditorProps<T extends Type.Entity.Any = Type.Entity.Any> = {
32
33
  onCreate?: OnCreateHandler;
33
34
  } & Omit<FormRootProps<any>, 'values' | 'schema' | 'onCreate'>;
34
35
 
35
- export const FormCellEditor = <T extends Type.Entity.Any = Type.Entity.Any>({
36
+ export const FormCellEditor = <T extends Type.AnyEntity = Type.AnyEntity>({
36
37
  __gridScope,
37
38
  schema,
38
39
  model,
@@ -85,8 +86,12 @@ export const FormCellEditor = <T extends Type.Entity.Any = Type.Entity.Any>({
85
86
 
86
87
  // NOTE: Important to get a mutable deep clone to eject from the echo object.
87
88
  // TODO(wittjosiah): Consider using something like Obj.clone for this use case.
88
- const formValues = useMemo(() => (originalRow ? JSON.parse(JSON.stringify(originalRow)) : {}), [originalRow]);
89
+ const initialFormValues = useMemo(() => (originalRow ? JSON.parse(JSON.stringify(originalRow)) : {}), [originalRow]);
90
+ const [formValues, setFormValues] = useState<any>(initialFormValues);
89
91
 
92
+ const handleValuesChanged = useCallback<NonNullable<FormRootProps<any>['onValuesChanged']>>((values) => {
93
+ setFormValues(values);
94
+ }, []);
90
95
  const handleOpenChange = useCallback((nextOpen: boolean) => {
91
96
  if (nextOpen === false) {
92
97
  setEditing(null);
@@ -162,7 +167,7 @@ export const FormCellEditor = <T extends Type.Entity.Any = Type.Entity.Any>({
162
167
  <Popover.Root open={editing} onOpenChange={handleOpenChange}>
163
168
  <Popover.VirtualTrigger virtualRef={anchorRef} />
164
169
  <Popover.Portal>
165
- <Popover.Content tabIndex={-1} classNames='popover-card-width density-fine'>
170
+ <Popover.Content tabIndex={-1} classNames='dx-card-popover-width dx-density-fine'>
166
171
  <Popover.Arrow />
167
172
  <Popover.Viewport>
168
173
  <Form.Root
@@ -170,6 +175,8 @@ export const FormCellEditor = <T extends Type.Entity.Any = Type.Entity.Any>({
170
175
  autoFocus
171
176
  schema={narrowedSchema}
172
177
  values={formValues}
178
+ onValuesChanged={handleValuesChanged}
179
+ projection={model?.projection}
173
180
  createInitialValuePath={fieldProjection.field.referencePath}
174
181
  createOptionIcon='ph--plus--regular'
175
182
  createOptionLabel={createOptionLabel}