@dxos/react-ui-table 0.8.4-main.fd6878d → 0.8.4-staging.60fe92afc8

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 (183) hide show
  1. package/LICENSE +102 -5
  2. package/dist/lib/browser/chunk-4KOQTPFC.mjs +1702 -0
  3. package/dist/lib/browser/chunk-4KOQTPFC.mjs.map +7 -0
  4. package/dist/lib/browser/chunk-62PYOJPY.mjs +67 -0
  5. package/dist/lib/browser/chunk-62PYOJPY.mjs.map +7 -0
  6. package/dist/lib/browser/chunk-J5LGTIGS.mjs +10 -0
  7. package/dist/lib/browser/chunk-J5LGTIGS.mjs.map +7 -0
  8. package/dist/lib/browser/index.mjs +1151 -1175
  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 +37 -45
  12. package/dist/lib/browser/testing/index.mjs.map +3 -3
  13. package/dist/lib/browser/translations.mjs +36 -0
  14. package/dist/lib/browser/translations.mjs.map +7 -0
  15. package/dist/lib/browser/types/index.mjs +4 -3
  16. package/dist/lib/node-esm/chunk-7LMOZUTS.mjs +68 -0
  17. package/dist/lib/node-esm/chunk-7LMOZUTS.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-KOSV2J5N.mjs +1704 -0
  21. package/dist/lib/node-esm/chunk-KOSV2J5N.mjs.map +7 -0
  22. package/dist/lib/node-esm/index.mjs +1151 -1175
  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 +37 -45
  26. package/dist/lib/node-esm/testing/index.mjs.map +3 -3
  27. package/dist/lib/node-esm/translations.mjs +37 -0
  28. package/dist/lib/node-esm/translations.mjs.map +7 -0
  29. package/dist/lib/node-esm/types/index.mjs +4 -3
  30. package/dist/types/src/components/Table/ColumnActionsMenu.d.ts +1 -2
  31. package/dist/types/src/components/Table/ColumnActionsMenu.d.ts.map +1 -1
  32. package/dist/types/src/components/Table/ColumnSettings.d.ts +2 -2
  33. package/dist/types/src/components/Table/ColumnSettings.d.ts.map +1 -1
  34. package/dist/types/src/components/Table/DynamicTable.d.ts +6 -7
  35. package/dist/types/src/components/Table/DynamicTable.d.ts.map +1 -1
  36. package/dist/types/src/components/Table/DynamicTable.stories.d.ts +36 -3
  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 +112 -5
  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 +21 -19
  42. package/dist/types/src/components/Table/Table.d.ts.map +1 -1
  43. package/dist/types/src/components/Table/Table.stories.d.ts +115 -5
  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 +12 -9
  52. package/dist/types/src/components/TableCellEditor/FormCellEditor.d.ts.map +1 -1
  53. package/dist/types/src/components/TableCellEditor/TableCellEditor.d.ts +7 -14
  54. package/dist/types/src/components/TableCellEditor/TableCellEditor.d.ts.map +1 -1
  55. package/dist/types/src/components/TableCellEditor/TableCellEditor.stories.d.ts +38 -5
  56. package/dist/types/src/components/TableCellEditor/TableCellEditor.stories.d.ts.map +1 -1
  57. package/dist/types/src/components/TableCellEditor/index.d.ts +1 -0
  58. package/dist/types/src/components/TableCellEditor/index.d.ts.map +1 -1
  59. package/dist/types/src/components/index.d.ts +0 -1
  60. package/dist/types/src/components/index.d.ts.map +1 -1
  61. package/dist/types/src/components/testing.d.ts +23 -16
  62. package/dist/types/src/components/testing.d.ts.map +1 -1
  63. package/dist/types/src/hooks/index.d.ts +2 -1
  64. package/dist/types/src/hooks/index.d.ts.map +1 -1
  65. package/dist/types/src/hooks/useAddRow.d.ts +6 -6
  66. package/dist/types/src/hooks/useAddRow.d.ts.map +1 -1
  67. package/dist/types/src/hooks/useProjectionModel.d.ts +6 -0
  68. package/dist/types/src/hooks/useProjectionModel.d.ts.map +1 -0
  69. package/dist/types/src/hooks/useTableModel.d.ts +9 -9
  70. package/dist/types/src/hooks/useTableModel.d.ts.map +1 -1
  71. package/dist/types/src/index.d.ts +0 -2
  72. package/dist/types/src/index.d.ts.map +1 -1
  73. package/dist/types/src/model/modal-controller.d.ts +5 -8
  74. package/dist/types/src/model/modal-controller.d.ts.map +1 -1
  75. package/dist/types/src/model/selection-model.d.ts +18 -9
  76. package/dist/types/src/model/selection-model.d.ts.map +1 -1
  77. package/dist/types/src/model/table-model.d.ts +111 -30
  78. package/dist/types/src/model/table-model.d.ts.map +1 -1
  79. package/dist/types/src/model/table-presentation.d.ts +4 -2
  80. package/dist/types/src/model/table-presentation.d.ts.map +1 -1
  81. package/dist/types/src/playwright/TableManager.d.ts +2 -1
  82. package/dist/types/src/playwright/TableManager.d.ts.map +1 -1
  83. package/dist/types/src/playwright/playwright.config.d.ts.map +1 -1
  84. package/dist/types/src/testing/useTestTableModel.d.ts +18 -34
  85. package/dist/types/src/testing/useTestTableModel.d.ts.map +1 -1
  86. package/dist/types/src/translations.d.ts +21 -23
  87. package/dist/types/src/translations.d.ts.map +1 -1
  88. package/dist/types/src/types/Table.d.ts +32 -0
  89. package/dist/types/src/types/Table.d.ts.map +1 -0
  90. package/dist/types/src/types/index.d.ts +1 -1
  91. package/dist/types/src/types/index.d.ts.map +1 -1
  92. package/dist/types/src/util/dynamic-table.d.ts +14 -12
  93. package/dist/types/src/util/dynamic-table.d.ts.map +1 -1
  94. package/dist/types/src/util/dynamic-table.test.d.ts +2 -0
  95. package/dist/types/src/util/dynamic-table.test.d.ts.map +1 -0
  96. package/dist/types/src/util/index.d.ts +2 -1
  97. package/dist/types/src/util/index.d.ts.map +1 -1
  98. package/dist/types/src/util/query-ast.d.ts +7 -0
  99. package/dist/types/src/util/query-ast.d.ts.map +1 -0
  100. package/dist/types/src/util/schema.d.ts +1 -1
  101. package/dist/types/src/util/schema.d.ts.map +1 -1
  102. package/dist/types/src/util/signals.d.ts.map +1 -1
  103. package/dist/types/src/util/sort.d.ts +12 -0
  104. package/dist/types/src/util/sort.d.ts.map +1 -0
  105. package/dist/types/src/util/table-buttons.d.ts +20 -20
  106. package/dist/types/src/util/table-buttons.d.ts.map +1 -1
  107. package/dist/types/src/util/table-controls.d.ts +4 -4
  108. package/dist/types/src/util/table-controls.d.ts.map +1 -1
  109. package/dist/types/src/util/tag.d.ts.map +1 -1
  110. package/dist/types/tsconfig.tsbuildinfo +1 -1
  111. package/package.json +68 -77
  112. package/src/components/Table/ColumnActionsMenu.tsx +17 -12
  113. package/src/components/Table/ColumnSettings.tsx +8 -9
  114. package/src/components/Table/DynamicTable.stories.tsx +41 -38
  115. package/src/components/Table/DynamicTable.tsx +32 -36
  116. package/src/components/Table/Relations.stories.tsx +219 -61
  117. package/src/components/Table/RowActionsMenu.tsx +27 -17
  118. package/src/components/Table/Table.stories.tsx +217 -141
  119. package/src/components/Table/Table.tsx +46 -391
  120. package/src/components/Table/TableContent.tsx +426 -0
  121. package/src/components/Table/TableToolbar.tsx +111 -0
  122. package/src/components/TableCellEditor/CellValidationMessage.tsx +5 -6
  123. package/src/components/TableCellEditor/FormCellEditor.tsx +136 -41
  124. package/src/components/TableCellEditor/TableCellEditor.stories.tsx +39 -73
  125. package/src/components/TableCellEditor/TableCellEditor.tsx +81 -223
  126. package/src/components/TableCellEditor/index.ts +1 -0
  127. package/src/components/index.ts +0 -1
  128. package/src/components/testing.ts +42 -21
  129. package/src/hooks/index.ts +2 -1
  130. package/src/hooks/useAddRow.ts +14 -14
  131. package/src/hooks/useProjectionModel.ts +38 -0
  132. package/src/hooks/useTableModel.ts +36 -34
  133. package/src/index.ts +0 -4
  134. package/src/model/modal-controller.ts +23 -40
  135. package/src/model/selection-model.ts +75 -44
  136. package/src/model/table-model.test.ts +86 -56
  137. package/src/model/table-model.ts +464 -182
  138. package/src/model/table-presentation.test.ts +64 -59
  139. package/src/model/table-presentation.ts +107 -83
  140. package/src/playwright/TableManager.ts +13 -6
  141. package/src/playwright/playwright.config.ts +1 -1
  142. package/src/playwright/smoke.spec.ts +92 -46
  143. package/src/testing/useTestTableModel.ts +51 -42
  144. package/src/translations.ts +20 -22
  145. package/src/types/Table.ts +80 -0
  146. package/src/types/index.ts +1 -1
  147. package/src/util/dynamic-table.test.ts +63 -0
  148. package/src/util/dynamic-table.ts +53 -39
  149. package/src/util/index.ts +2 -1
  150. package/src/util/query-ast.ts +22 -0
  151. package/src/util/schema.ts +4 -4
  152. package/src/util/sort.ts +90 -0
  153. package/src/util/table-buttons.ts +3 -3
  154. package/src/util/table-controls.ts +2 -2
  155. package/dist/lib/browser/chunk-L2LSB6KV.mjs +0 -18
  156. package/dist/lib/browser/chunk-L2LSB6KV.mjs.map +0 -7
  157. package/dist/lib/browser/chunk-ZAOSIG3L.mjs +0 -1814
  158. package/dist/lib/browser/chunk-ZAOSIG3L.mjs.map +0 -7
  159. package/dist/lib/node-esm/chunk-NXZ4CJYX.mjs +0 -20
  160. package/dist/lib/node-esm/chunk-NXZ4CJYX.mjs.map +0 -7
  161. package/dist/lib/node-esm/chunk-ZOFFI3JJ.mjs +0 -1815
  162. package/dist/lib/node-esm/chunk-ZOFFI3JJ.mjs.map +0 -7
  163. package/dist/types/src/components/Table/CreateRefPanel.d.ts +0 -9
  164. package/dist/types/src/components/Table/CreateRefPanel.d.ts.map +0 -1
  165. package/dist/types/src/components/TableCellEditor/extension.d.ts +0 -13
  166. package/dist/types/src/components/TableCellEditor/extension.d.ts.map +0 -1
  167. package/dist/types/src/components/TableToolbar/TableToolbar.d.ts +0 -21
  168. package/dist/types/src/components/TableToolbar/TableToolbar.d.ts.map +0 -1
  169. package/dist/types/src/components/TableToolbar/index.d.ts +0 -2
  170. package/dist/types/src/components/TableToolbar/index.d.ts.map +0 -1
  171. package/dist/types/src/model/table-sorting.d.ts +0 -53
  172. package/dist/types/src/model/table-sorting.d.ts.map +0 -1
  173. package/dist/types/src/types/schema.d.ts +0 -8
  174. package/dist/types/src/types/schema.d.ts.map +0 -1
  175. package/dist/types/src/util/table.d.ts +0 -12
  176. package/dist/types/src/util/table.d.ts.map +0 -1
  177. package/src/components/Table/CreateRefPanel.tsx +0 -86
  178. package/src/components/TableCellEditor/extension.ts +0 -61
  179. package/src/components/TableToolbar/TableToolbar.tsx +0 -102
  180. package/src/components/TableToolbar/index.ts +0 -5
  181. package/src/model/table-sorting.ts +0 -216
  182. package/src/types/schema.ts +0 -16
  183. package/src/util/table.ts +0 -53
@@ -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 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;
@@ -0,0 +1,111 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import { Atom, RegistryContext } from '@effect-atom/atom-react';
6
+ import React, { useContext, useEffect, useMemo } from 'react';
7
+
8
+ import { composable, composableProps } from '@dxos/react-ui';
9
+ import {
10
+ type ActionGraphEdges,
11
+ type ActionGraphNodes,
12
+ type ActionGraphProps,
13
+ Menu,
14
+ type MenuAction,
15
+ MenuRootProps,
16
+ createGapSeparator,
17
+ createMenuAction,
18
+ useMenuActions,
19
+ } from '@dxos/react-ui-menu';
20
+
21
+ import { translationKey } from '#translations';
22
+
23
+ export type TableToolbarActionProperties = { type: TableToolbarActionType };
24
+
25
+ export type TableToolbarAction = MenuAction<TableToolbarActionProperties>;
26
+
27
+ export type TableToolbarActionType = 'add-row' | 'comment' | 'save-view';
28
+
29
+ type TableToolbarState = Partial<{ viewDirty: boolean }>;
30
+
31
+ const createTableToolbarActions = ({
32
+ stateAtom,
33
+ onAdd,
34
+ onSave,
35
+ customActions,
36
+ }: {
37
+ stateAtom: Atom.Atom<TableToolbarState>;
38
+ onAdd?: () => void;
39
+ onSave?: () => void;
40
+ customActions?: Atom.Atom<ActionGraphProps>;
41
+ }) =>
42
+ Atom.make((get) => {
43
+ const nodes: ActionGraphNodes = [];
44
+ if (onAdd) {
45
+ const add = createMenuAction<TableToolbarActionProperties>('add-row', onAdd, {
46
+ type: 'add-row' as const,
47
+ icon: 'ph--plus--regular',
48
+ label: ['add-row.label', { ns: translationKey }],
49
+ testId: 'table.toolbar.add-row',
50
+ });
51
+ nodes.push(add);
52
+ }
53
+ if (onSave) {
54
+ const state = get(stateAtom);
55
+ const save = createMenuAction<TableToolbarActionProperties>('save-view', onSave, {
56
+ type: 'save-view' as const,
57
+ icon: 'ph--floppy-disk--regular',
58
+ label: ['save-view.label', { ns: translationKey }],
59
+ testId: 'table.toolbar.save-view',
60
+ iconOnly: false,
61
+ hidden: !state.viewDirty,
62
+ });
63
+ nodes.push(save);
64
+ }
65
+ const gap = createGapSeparator();
66
+ nodes.push(...gap.nodes);
67
+ const edges: ActionGraphEdges = nodes.map(({ id: target }) => ({ source: 'root', target, relation: 'child' }));
68
+ if (customActions) {
69
+ const custom = get(customActions);
70
+ nodes.push(...custom.nodes);
71
+ edges.push(...custom.edges);
72
+ }
73
+
74
+ return {
75
+ nodes,
76
+ edges,
77
+ };
78
+ });
79
+
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
+ );
110
+
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-fg' : 'text-warning-fg';
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,