@dxos/react-ui-table 0.8.4-main.e8ec1fe → 0.8.4-main.effb148878

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