@dxos/react-ui-table 0.8.4-main.fd6878d → 0.9.0

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
@@ -1,1815 +0,0 @@
1
- import { createRequire } from 'node:module';const require = createRequire(import.meta.url);
2
- import {
3
- TableView
4
- } from "./chunk-NXZ4CJYX.mjs";
5
-
6
- // src/util/dynamic-table.ts
7
- import { Obj } from "@dxos/echo";
8
- import { getTypename, toJsonSchema } from "@dxos/echo-schema";
9
- import { ProjectionModel, createView, getSchemaFromPropertyDefinitions } from "@dxos/schema";
10
- var getBaseSchema = ({ typename, properties, jsonSchema, schema }) => {
11
- if (typename && properties) {
12
- const schema2 = getSchemaFromPropertyDefinitions(typename, properties);
13
- return {
14
- typename: schema2.typename,
15
- jsonSchema: schema2.jsonSchema
16
- };
17
- } else if (schema) {
18
- return {
19
- typename: getTypename(schema),
20
- jsonSchema: toJsonSchema(schema)
21
- };
22
- } else if (typename && jsonSchema) {
23
- return {
24
- typename,
25
- jsonSchema
26
- };
27
- } else {
28
- throw new Error("invalid properties");
29
- }
30
- };
31
- var makeDynamicTable = ({ typename, jsonSchema, properties }) => {
32
- const table = Obj.make(TableView, {
33
- sizes: {}
34
- });
35
- const view = createView({
36
- typename,
37
- jsonSchema,
38
- presentation: table,
39
- ...properties && {
40
- fields: properties.map((property) => property.name)
41
- }
42
- });
43
- const projection = new ProjectionModel(jsonSchema, view.projection);
44
- if (properties && projection.fields) {
45
- setProperties(view, projection, table, properties);
46
- }
47
- return {
48
- projection,
49
- view
50
- };
51
- };
52
- var setProperties = (view, projection, table, properties) => {
53
- for (const property of properties) {
54
- const field = projection.fields.find((field2) => field2.path === property.name);
55
- if (field) {
56
- if (property.size !== void 0) {
57
- table.sizes[field.path] = property.size;
58
- }
59
- if (property.title !== void 0) {
60
- const fieldProjection = projection.getFieldProjection(field.id);
61
- projection.setFieldProjection({
62
- ...fieldProjection,
63
- props: {
64
- ...fieldProjection.props,
65
- title: property.title
66
- }
67
- });
68
- }
69
- if (property.sort) {
70
- const fieldId = field.id;
71
- view.query.sort = [
72
- {
73
- fieldId,
74
- direction: property.sort
75
- }
76
- ];
77
- }
78
- }
79
- }
80
- };
81
-
82
- // src/util/signals.ts
83
- var touch = (value) => value;
84
-
85
- // src/util/table.ts
86
- import { Match } from "effect";
87
- import { Obj as Obj2 } from "@dxos/echo";
88
- import { toEffectSchema } from "@dxos/echo-schema";
89
- import { createViewFromSpace, getSchemaProperties } from "@dxos/schema";
90
- var createTable = async ({ sizes, ...props }) => {
91
- const table = Obj2.make(TableView, {
92
- sizes: {}
93
- });
94
- const { jsonSchema, view } = await createViewFromSpace({
95
- ...props,
96
- presentation: table
97
- });
98
- const schema = toEffectSchema(jsonSchema);
99
- const shouldIncludeId = props.fields?.find((field) => field === "id") !== void 0;
100
- const properties = getSchemaProperties(schema.ast, {}, shouldIncludeId);
101
- for (const property of properties) {
102
- if (sizes?.[property.name]) {
103
- table.sizes[property.name] = sizes[property.name];
104
- continue;
105
- }
106
- Match.type().pipe(Match.when("boolean", () => {
107
- table.sizes[property.name] = 100;
108
- }), Match.when("number", () => {
109
- table.sizes[property.name] = 100;
110
- }), Match.orElse(() => {
111
- }))(property.type);
112
- }
113
- return {
114
- jsonSchema,
115
- schema,
116
- view
117
- };
118
- };
119
-
120
- // src/util/table-buttons.ts
121
- var BUTTON_IDENTIFIERS = {
122
- columnSettings: "data-table-column-settings-button",
123
- newColumn: "data-table-new-column-button",
124
- referencedCell: "data-table-ref-cell-button",
125
- rowMenu: "data-table-row-menu-button",
126
- saveDraftRow: "data-table-save-draft-row-button",
127
- sort: "data-table-sort-button"
128
- };
129
- var createButton = ({ attr, data, disabled = false, icon, testId, type = "primary" }) => {
130
- const dataAttrs = Object.entries(data).map(([k, v]) => `${k}="${v}"`).join(" ");
131
- const positionClass = type === "primary" ? "inline-end-2" : "inline-end-9";
132
- return `<button ${attr} data-testid="${testId}" class="dx-button is-6 pli-0.5 min-bs-0 absolute inset-block-1 ${positionClass}" ${dataAttrs} ${disabled ? "disabled" : ""} data-dx-grid-action="accessory"><svg data-size="4"><use href="/icons.svg#${icon}"/></svg></button>`;
133
- };
134
- var addColumnButton = {
135
- attr: BUTTON_IDENTIFIERS.newColumn,
136
- icon: "ph--plus--regular",
137
- render: ({ disabled }) => {
138
- return createButton({
139
- attr: BUTTON_IDENTIFIERS.newColumn,
140
- icon: addColumnButton.icon,
141
- disabled,
142
- data: {},
143
- testId: "table-new-column-button"
144
- });
145
- },
146
- getData: (_el) => ({
147
- type: "newColumn"
148
- })
149
- };
150
- var columnSettingsButton = {
151
- attr: BUTTON_IDENTIFIERS.columnSettings,
152
- icon: "ph--caret-down--regular",
153
- render: ({ fieldId }) => {
154
- return createButton({
155
- attr: BUTTON_IDENTIFIERS.columnSettings,
156
- icon: columnSettingsButton.icon,
157
- data: {
158
- "data-field-id": fieldId
159
- },
160
- testId: "table-column-settings-button"
161
- });
162
- },
163
- getData: (el) => ({
164
- type: "columnSettings",
165
- fieldId: el.getAttribute("data-field-id")
166
- })
167
- };
168
- var rowMenuButton = {
169
- attr: BUTTON_IDENTIFIERS.rowMenu,
170
- icon: "ph--dots-three--regular",
171
- render: ({ rowIndex }) => {
172
- return createButton({
173
- attr: BUTTON_IDENTIFIERS.rowMenu,
174
- icon: rowMenuButton.icon,
175
- data: {
176
- "data-row-index": rowIndex.toString()
177
- },
178
- testId: "table-row-menu-button"
179
- });
180
- },
181
- getData: (el) => ({
182
- type: "rowMenu",
183
- rowIndex: Number(el.getAttribute("data-row-index"))
184
- })
185
- };
186
- var sortButton = {
187
- attr: BUTTON_IDENTIFIERS.sort,
188
- icon: "ph--sort-ascending--regular",
189
- render: ({ fieldId, direction }) => {
190
- return createButton({
191
- attr: BUTTON_IDENTIFIERS.sort,
192
- icon: direction === "desc" ? "ph--sort-descending--regular" : "ph--sort-ascending--regular",
193
- data: {
194
- "data-field-id": fieldId,
195
- "data-direction": direction ?? ""
196
- },
197
- testId: "table-sort-button",
198
- // TODO(ZaymonFC): All buttons should be rendered into an absolutely positioned flex container.
199
- // This is a bit hacky.
200
- type: "secondary"
201
- });
202
- },
203
- getData: (el) => ({
204
- type: "sort",
205
- fieldId: el.getAttribute("data-field-id"),
206
- direction: el.getAttribute("data-direction")
207
- })
208
- };
209
- var saveDraftRowButton = {
210
- attr: BUTTON_IDENTIFIERS.saveDraftRow,
211
- icon: "ph--floppy-disk--regular",
212
- render: ({ rowIndex, disabled }) => {
213
- return createButton({
214
- attr: BUTTON_IDENTIFIERS.saveDraftRow,
215
- icon: saveDraftRowButton.icon,
216
- disabled,
217
- data: {
218
- "data-row-index": rowIndex.toString(),
219
- "data-variant": "primary"
220
- },
221
- testId: "table-save-draft-row-button"
222
- });
223
- },
224
- getData: (el) => ({
225
- type: "saveDraftRow",
226
- rowIndex: Number(el.getAttribute("data-row-index"))
227
- })
228
- };
229
- var tableButtons = {
230
- addColumn: addColumnButton,
231
- columnSettings: columnSettingsButton,
232
- rowMenu: rowMenuButton,
233
- saveDraftRow: saveDraftRowButton,
234
- sort: sortButton
235
- };
236
-
237
- // src/util/table-controls.ts
238
- import { accessoryHandlesPointerdownAttrs } from "@dxos/lit-grid";
239
- var CONTROL_IDENTIFIERS = {
240
- checkbox: "data-table-checkbox",
241
- switch: "data-table-switch"
242
- };
243
- var BASE_CLASSES = {
244
- checkbox: "absolute inset-block-[.375rem] inline-end-[.375rem] dx-checkbox",
245
- switch: "absolute inset-block-[.25rem] inline-end-[.25rem] dx-checkbox--switch"
246
- };
247
- var renderAttributes = (data) => {
248
- return Object.entries(data).map(([k, v]) => `${k}="${v}"`).join(" ");
249
- };
250
- var renderInput = (baseClass, attrs, checked = false, disabled = false, preventToggle = false, testId) => {
251
- return `<input type="checkbox" class="${baseClass}" ${renderAttributes(attrs)} ${checked ? "checked" : ""} ${preventToggle ? 'onclick="return false"' : ""} ${disabled ? "disabled" : ""} data-testid="${testId}" data-dx-grid-action="accessory"/>`;
252
- };
253
- var CheckboxStory = ({ rowIndex, header = false, checked = false, disabled = false }) => {
254
- const attrs = {
255
- [CONTROL_IDENTIFIERS.checkbox]: "",
256
- "data-row-index": rowIndex.toString(),
257
- ...header && {
258
- "data-header": ""
259
- }
260
- };
261
- return renderInput(BASE_CLASSES.checkbox, attrs, checked, disabled, true, "table-selection");
262
- };
263
- var SwitchStory = ({ colIndex, rowIndex, checked = false, disabled = false }) => {
264
- const attrs = {
265
- [CONTROL_IDENTIFIERS.switch]: "",
266
- "data-row-index": rowIndex.toString(),
267
- "data-col-index": colIndex.toString(),
268
- ...accessoryHandlesPointerdownAttrs
269
- };
270
- return renderInput(BASE_CLASSES.switch, attrs, checked, disabled, true, "table-switch");
271
- };
272
- var tableControls = {
273
- checkbox: {
274
- attr: CONTROL_IDENTIFIERS.checkbox,
275
- render: CheckboxStory,
276
- getData: (el) => ({
277
- type: "checkbox",
278
- rowIndex: Number(el.getAttribute("data-row-index")),
279
- header: el.hasAttribute("data-header")
280
- })
281
- },
282
- switch: {
283
- attr: CONTROL_IDENTIFIERS.switch,
284
- render: SwitchStory,
285
- getData: (el) => ({
286
- type: "switch",
287
- rowIndex: Number(el.getAttribute("data-row-index")),
288
- colIndex: Number(el.getAttribute("data-col-index"))
289
- })
290
- }
291
- };
292
-
293
- // src/util/schema.ts
294
- import { Schema } from "effect";
295
- import { TypeLiteral, isTypeLiteral } from "effect/SchemaAST";
296
- var narrowSchema = (schema, paths) => {
297
- const ast = schema?.ast;
298
- if (isTypeLiteral(ast)) {
299
- const propertySignatures = ast.propertySignatures.filter((signature) => paths.includes(signature.name.toString()));
300
- if (propertySignatures.length > 0) {
301
- const narrowType = new TypeLiteral(propertySignatures, []);
302
- return Schema.make(narrowType);
303
- }
304
- }
305
- return void 0;
306
- };
307
-
308
- // src/model/table-model.ts
309
- import { computed as computed3, effect as effect2, signal as signal3 } from "@preact/signals-core";
310
- import { Resource as Resource2 } from "@dxos/context";
311
- import { Obj as Obj3, Ref } from "@dxos/echo";
312
- import { FormatEnum as FormatEnum2, getSchema, getSnapshot, getValue as getValue2, setValue, toEffectSchema as toEffectSchema2 } from "@dxos/echo-schema";
313
- import { invariant } from "@dxos/invariant";
314
- import { ObjectId } from "@dxos/keys";
315
- import { isLiveObject } from "@dxos/live-object";
316
- import { fullyQualifiedId } from "@dxos/react-client/echo";
317
- import { formatForEditing, parseValue } from "@dxos/react-ui-form";
318
- import { ProjectionModel as ProjectionModel2, validateSchema } from "@dxos/schema";
319
-
320
- // src/util/tag.ts
321
- var TAG_PATTERN_REGEX = /\[([^\]]+)\]\(([^)]+)\)/g;
322
- var extractTagIds = (value) => {
323
- if (typeof value !== "string") {
324
- return void 0;
325
- }
326
- const ids = [];
327
- let match;
328
- while ((match = TAG_PATTERN_REGEX.exec(value)) !== null) {
329
- if (match[2]) {
330
- ids.push(match[2]);
331
- }
332
- }
333
- return ids.length > 0 ? ids : void 0;
334
- };
335
-
336
- // src/model/selection-model.ts
337
- import { computed, effect, signal } from "@preact/signals-core";
338
- import { Resource } from "@dxos/context";
339
- var SelectionModel = class extends Resource {
340
- _rows;
341
- _selectionMode;
342
- _initialSelection;
343
- _onSelectionChanged;
344
- _selection;
345
- _validSelectedIds;
346
- _hasSelection;
347
- _allSelected;
348
- constructor(_rows, _selectionMode, _initialSelection, _onSelectionChanged) {
349
- super(), this._rows = _rows, this._selectionMode = _selectionMode, this._initialSelection = _initialSelection, this._onSelectionChanged = _onSelectionChanged, this._validSelectedIds = computed(() => {
350
- const rows = this._rows.value;
351
- if (!rows) {
352
- return /* @__PURE__ */ new Set();
353
- }
354
- const validIds = new Set(rows.map((row) => row.id));
355
- return new Set([
356
- ...this._selection.value
357
- ].filter((id) => validIds.has(id)));
358
- }), this._hasSelection = computed(() => this._validSelectedIds.value.size > 0), this._allSelected = computed(() => {
359
- const rows = this._rows.value;
360
- if (rows.length === 0) {
361
- return false;
362
- }
363
- return rows.every((row) => this._selection.value.has(row.id));
364
- }), this.getSelectedRows = () => {
365
- const selectedIds = this._validSelectedIds.value;
366
- return this._rows.value.filter((row) => selectedIds.has(row.id));
367
- }, this.isRowIndexSelected = (rowIndex) => {
368
- const row = this._rows.value[rowIndex];
369
- return this._selection.value.has(row.id);
370
- }, this.isObjectSelected = (object) => {
371
- return this._selection.value.has(object.id);
372
- }, this.toggleSelectionForRowIndex = (rowIndex) => {
373
- const row = this._rows.value[rowIndex];
374
- const newSelection = new Set(this._selection.value);
375
- if (newSelection.has(row.id)) {
376
- newSelection.delete(row.id);
377
- } else {
378
- if (this._selectionMode === "single") {
379
- newSelection.clear();
380
- }
381
- newSelection.add(row.id);
382
- }
383
- this._selection.value = newSelection;
384
- }, this.setSelection = (mode) => {
385
- switch (mode) {
386
- case "all": {
387
- this._selection.value = new Set(this._rows.value.map((row) => row.id));
388
- break;
389
- }
390
- case "none": {
391
- this._selection.value = /* @__PURE__ */ new Set();
392
- break;
393
- }
394
- }
395
- };
396
- this._selection = signal(new Set(this._initialSelection));
397
- }
398
- async _open() {
399
- const selectionWatcher = effect(() => {
400
- touch(this._selection.value);
401
- this._onSelectionChanged?.();
402
- });
403
- this._ctx.onDispose(selectionWatcher);
404
- }
405
- //
406
- // Getters
407
- //
408
- get selection() {
409
- return this._selection;
410
- }
411
- get hasSelection() {
412
- return this._hasSelection;
413
- }
414
- get allRowsSeleted() {
415
- return this._allSelected;
416
- }
417
- get selectionMode() {
418
- return this._selectionMode;
419
- }
420
- getSelectedRows;
421
- isRowIndexSelected;
422
- isObjectSelected;
423
- //
424
- // Manipulation
425
- //
426
- toggleSelectionForRowIndex;
427
- setSelection;
428
- };
429
-
430
- // src/model/table-sorting.ts
431
- import { computed as computed2, signal as signal2 } from "@preact/signals-core";
432
- import orderBy from "lodash.orderby";
433
- import { FormatEnum, TypeEnum, getValue } from "@dxos/echo-schema";
434
- import { formatForDisplay } from "@dxos/react-ui-form";
435
- var TableSorting = class {
436
- _view;
437
- _projection;
438
- _displayToDataIndex;
439
- _rows;
440
- _localSort;
441
- _isDirty;
442
- sortedRows;
443
- constructor(rows, _view, _projection) {
444
- this._view = _view;
445
- this._projection = _projection;
446
- this._displayToDataIndex = /* @__PURE__ */ new Map();
447
- this._localSort = signal2(void 0);
448
- this._rows = rows;
449
- this._isDirty = computed2(() => {
450
- const local = this._localSort.value;
451
- const viewSort = this._view.query.sort?.[0];
452
- if (local?.type === "cleared") {
453
- return viewSort !== void 0;
454
- }
455
- if (local?.type === "active") {
456
- if (!viewSort) {
457
- return true;
458
- }
459
- return local.sort.fieldId !== viewSort.fieldId || local.sort.direction !== viewSort.direction;
460
- }
461
- return false;
462
- });
463
- this.sortedRows = this.initialiseSortedRows();
464
- }
465
- /**
466
- * @reactive
467
- * @returns Local sort if present, falls back to view sort.
468
- */
469
- get sorting() {
470
- const local = this._localSort.value;
471
- if (local?.type === "cleared") {
472
- return void 0;
473
- }
474
- if (local?.type === "active") {
475
- return local.sort;
476
- }
477
- return this._view.query.sort?.[0];
478
- }
479
- /**
480
- * @reactive
481
- * @returns Whether local sort differs from view sort.
482
- */
483
- get isDirty() {
484
- return this._isDirty.value;
485
- }
486
- getDataIndex(displayIndex) {
487
- return this._displayToDataIndex.get(displayIndex) ?? displayIndex;
488
- }
489
- setSort(fieldId, direction) {
490
- this._localSort.value = {
491
- type: "active",
492
- sort: {
493
- fieldId,
494
- direction
495
- }
496
- };
497
- }
498
- toggleSort(fieldId) {
499
- if (!this.sorting || this.sorting.fieldId !== fieldId) {
500
- return;
501
- }
502
- switch (this.sorting.direction) {
503
- case "asc": {
504
- this.setSort(fieldId, "desc");
505
- break;
506
- }
507
- case "desc": {
508
- this.setSort(fieldId, "asc");
509
- break;
510
- }
511
- }
512
- }
513
- clearSort() {
514
- this._localSort.value = {
515
- type: "cleared"
516
- };
517
- }
518
- save() {
519
- if (this._localSort.value !== void 0) {
520
- if (this._localSort.value.type === "active") {
521
- this._view.query.sort = [
522
- this._localSort.value.sort
523
- ];
524
- } else {
525
- this._view.query.sort = [];
526
- }
527
- this._localSort.value = void 0;
528
- }
529
- }
530
- //
531
- // Initialisation.
532
- //
533
- initialiseSortedRows() {
534
- return computed2(() => {
535
- this._displayToDataIndex.clear();
536
- const sort = this.sorting;
537
- if (!sort) {
538
- return this._rows.value;
539
- }
540
- const field = this._projection.fields.find((f) => f.id === sort.fieldId);
541
- if (!field) {
542
- return this._rows.value;
543
- }
544
- const { props } = this._projection.getFieldProjection(field.id);
545
- const dataWithIndices = this._rows.value.map((item, index) => {
546
- const sortValue = this.getSortValue(props, field, item);
547
- const isEmpty = !(props.type === TypeEnum.Boolean) && (sortValue == null || sortValue === "");
548
- return {
549
- item,
550
- index,
551
- value: sortValue,
552
- isEmpty
553
- };
554
- });
555
- const ordered = orderBy(dataWithIndices, [
556
- "isEmpty",
557
- "value"
558
- ], [
559
- "asc",
560
- sort.direction
561
- ]);
562
- ordered.forEach(({ index: dataIndex }, displayIndex) => {
563
- if (displayIndex !== dataIndex) {
564
- this._displayToDataIndex.set(displayIndex, dataIndex);
565
- }
566
- });
567
- return ordered.map(({ item }) => item);
568
- });
569
- }
570
- getSortValue(props, field, item) {
571
- const { type, format } = props;
572
- const value = getValue(item, field.path);
573
- if (type === TypeEnum.Boolean) {
574
- return !!value;
575
- }
576
- if (type === TypeEnum.Number) {
577
- return value;
578
- }
579
- if (value == null) {
580
- return value;
581
- }
582
- if (format === FormatEnum.SingleSelect) {
583
- const options = props.options;
584
- if (!options) {
585
- return void 0;
586
- }
587
- const option = options.find((option2) => option2.id === value);
588
- return option ? options.indexOf(option) : void 0;
589
- }
590
- if (format === FormatEnum.Ref && field.referencePath) {
591
- const refValue = getValue(value.target, field.referencePath);
592
- return refValue;
593
- }
594
- return formatForDisplay({
595
- type,
596
- format,
597
- value
598
- });
599
- }
600
- };
601
-
602
- // src/model/table-model.ts
603
- var __dxlog_file = "/__w/dxos/dxos/packages/ui/react-ui-table/src/model/table-model.ts";
604
- var defaultFeatures = {
605
- selection: {
606
- enabled: true,
607
- mode: "multiple"
608
- },
609
- dataEditable: false,
610
- schemaEditable: false
611
- };
612
- var TableModel = class extends Resource2 {
613
- _view;
614
- _projection;
615
- _table;
616
- _visibleRange = signal3({
617
- start: {
618
- row: 0,
619
- col: 0
620
- },
621
- end: {
622
- row: 0,
623
- col: 0
624
- }
625
- });
626
- _onInsertRow;
627
- _onDeleteRows;
628
- _onDeleteColumn;
629
- _onCellUpdate;
630
- _onRowOrderChange;
631
- _onRowAction;
632
- _rowActions;
633
- _features;
634
- _rows = signal3([]);
635
- _draftRows = signal3([]);
636
- _sorting;
637
- _pinnedRows;
638
- _selection;
639
- _columnMeta;
640
- constructor({ view, schema, projection, features = {}, sorting = [], initialSelection = [], pinnedRows = {
641
- top: [],
642
- bottom: []
643
- }, rowActions = [], onCellUpdate, onDeleteColumn, onDeleteRows, onInsertRow, onRowOrderChange, onRowAction }) {
644
- super();
645
- this._view = view;
646
- this._projection = projection ?? new ProjectionModel2(schema, view.projection);
647
- this._features = {
648
- ...defaultFeatures,
649
- ...features
650
- };
651
- invariant(!(this._features.dataEditable && this._features.selection.enabled && this._features.selection.mode === "single"), "Single selection is not compatible with editable tables.", {
652
- F: __dxlog_file,
653
- L: 141,
654
- S: this,
655
- A: [
656
- "!(this._features.dataEditable && this._features.selection.enabled && this._features.selection.mode === 'single')",
657
- "'Single selection is not compatible with editable tables.'"
658
- ]
659
- });
660
- this._sorting = new TableSorting(this._rows, this._view, this._projection);
661
- if (sorting.length > 0) {
662
- const [sort] = sorting;
663
- this._sorting.setSort(sort.fieldId, sort.direction);
664
- }
665
- this._selection = new SelectionModel(this._sorting.sortedRows, this._features.selection.mode ?? "multiple", initialSelection, () => this._onRowOrderChange?.());
666
- this._pinnedRows = pinnedRows;
667
- this._rowActions = rowActions;
668
- this._onInsertRow = onInsertRow;
669
- this._onDeleteRows = onDeleteRows;
670
- this._onDeleteColumn = onDeleteColumn;
671
- this._onCellUpdate = onCellUpdate;
672
- this._onRowOrderChange = onRowOrderChange;
673
- this._onRowAction = onRowAction;
674
- }
675
- get id() {
676
- return fullyQualifiedId(this._view);
677
- }
678
- get view() {
679
- return this._view;
680
- }
681
- get table() {
682
- invariant(this._table, "Model not initialized", {
683
- F: __dxlog_file,
684
- L: 179,
685
- S: this,
686
- A: [
687
- "this._table",
688
- "'Model not initialized'"
689
- ]
690
- });
691
- return this._table;
692
- }
693
- get projection() {
694
- return this._projection;
695
- }
696
- get rows() {
697
- return this.sorting.sortedRows;
698
- }
699
- get features() {
700
- return this._features;
701
- }
702
- /**
703
- * @reactive
704
- */
705
- get sorting() {
706
- return this._sorting;
707
- }
708
- /**
709
- * Gets the row data at the specified display index.
710
- */
711
- getRowAt(displayIndex) {
712
- if (displayIndex < 0 || displayIndex >= this.sorting.sortedRows.value.length) {
713
- return void 0;
714
- }
715
- return this.sorting.sortedRows.value[displayIndex];
716
- }
717
- get pinnedRows() {
718
- return this._pinnedRows;
719
- }
720
- get columnMeta() {
721
- invariant(this._columnMeta, void 0, {
722
- F: __dxlog_file,
723
- L: 218,
724
- S: this,
725
- A: [
726
- "this._columnMeta",
727
- ""
728
- ]
729
- });
730
- return this._columnMeta;
731
- }
732
- get selection() {
733
- return this._selection;
734
- }
735
- /** @reactive */
736
- get isViewDirty() {
737
- return this.sorting.isDirty;
738
- }
739
- get rowActions() {
740
- return this._rowActions;
741
- }
742
- //
743
- // Initialisation
744
- //
745
- async _open() {
746
- const presentation = this._view.presentation.target ?? await this._view.presentation.load();
747
- invariant(Obj3.instanceOf(TableView, presentation), void 0, {
748
- F: __dxlog_file,
749
- L: 241,
750
- S: this,
751
- A: [
752
- "Obj.instanceOf(TableView, presentation)",
753
- ""
754
- ]
755
- });
756
- this._table = presentation;
757
- this.initializeColumnMeta();
758
- this.initializeEffects();
759
- await this._selection.open(this._ctx);
760
- }
761
- initializeColumnMeta() {
762
- this._columnMeta = computed3(() => {
763
- const fields = this._projection?.fields ?? [];
764
- const meta = Object.fromEntries(fields.map((field, index) => [
765
- index,
766
- {
767
- size: this.table.sizes[field.path] ?? 256,
768
- resizeable: true
769
- }
770
- ]));
771
- return {
772
- grid: meta,
773
- frozenColsStart: {
774
- 0: {
775
- size: 30,
776
- resizeable: false
777
- }
778
- },
779
- frozenColsEnd: {
780
- 0: {
781
- size: 40,
782
- resizeable: false
783
- }
784
- }
785
- };
786
- });
787
- }
788
- initializeEffects() {
789
- const rowOrderWatcher = effect2(() => {
790
- touch(this.sorting.sortedRows.value);
791
- this._onRowOrderChange?.();
792
- });
793
- this._ctx.onDispose(rowOrderWatcher);
794
- const rowEffectManager = effect2(() => {
795
- const { start, end } = touch(this._visibleRange.value);
796
- const rowEffects = [];
797
- for (let row = start.row; row <= end.row; row++) {
798
- rowEffects.push(effect2(() => {
799
- const obj = this.sorting.sortedRows.value[row];
800
- this._projection?.fields.forEach((field) => touch(getValue2(obj, field.path)));
801
- this._onCellUpdate?.({
802
- row,
803
- col: start.col,
804
- plane: "grid"
805
- });
806
- }));
807
- }
808
- return () => rowEffects.forEach((cleanup) => cleanup());
809
- });
810
- this._ctx.onDispose(rowEffectManager);
811
- const draftRowsWatcher = effect2(() => {
812
- touch(this._draftRows.value);
813
- this._onRowOrderChange?.();
814
- });
815
- this._ctx.onDispose(draftRowsWatcher);
816
- }
817
- //
818
- // Data
819
- //
820
- setRows = (obj) => {
821
- this._rows.value = obj;
822
- };
823
- getRowCount = () => this._rows.value.length;
824
- /**
825
- * @reactive
826
- */
827
- getDraftRowCount = () => this._draftRows.value.length;
828
- // TODO(ZaymonFC): Return .value instead of exposing the signal.
829
- get draftRows() {
830
- return this._draftRows;
831
- }
832
- /**
833
- * Checks if a specific field path has validation errors for a given draft row.
834
- * @param draftRowIndex - The index of the draft row to check
835
- * @param fieldPath - The path of the field to check for validation errors
836
- * @returns true if the field has validation errors, false otherwise
837
- */
838
- hasDraftRowValidationError(draftRowIndex, fieldPath) {
839
- if (draftRowIndex < 0 || draftRowIndex >= this._draftRows.value.length) {
840
- return false;
841
- }
842
- const draftRow = this._draftRows.value[draftRowIndex];
843
- const validationErrors = draftRow.validationErrors || [];
844
- return validationErrors.some((error) => error.path === fieldPath);
845
- }
846
- getColumnCount = () => this._projection?.fields.length ?? 0;
847
- insertRow = () => {
848
- const result = this._onInsertRow?.();
849
- if (result === false && this._draftRows.value.length === 0) {
850
- this.createDraftRow();
851
- }
852
- };
853
- createDraftRow() {
854
- if (!this._projection) {
855
- return;
856
- }
857
- const draftData = {
858
- id: ObjectId.random()
859
- };
860
- const validationErrors = this.validateDraftRowData(draftData);
861
- const draftRow = {
862
- data: draftData,
863
- valid: validationErrors.length === 0,
864
- validationErrors
865
- };
866
- this._draftRows.value = [
867
- ...this._draftRows.value,
868
- draftRow
869
- ];
870
- }
871
- validateDraftRow(draftRowIndex) {
872
- if (draftRowIndex < 0 || draftRowIndex >= this._draftRows.value.length) {
873
- return;
874
- }
875
- const draftRow = this._draftRows.value[draftRowIndex];
876
- const validationErrors = this.validateDraftRowData(draftRow.data);
877
- const updatedDraftRow = {
878
- ...draftRow,
879
- valid: validationErrors.length === 0,
880
- validationErrors
881
- };
882
- const newDraftRows = [
883
- ...this._draftRows.value
884
- ];
885
- newDraftRows[draftRowIndex] = updatedDraftRow;
886
- this._draftRows.value = newDraftRows;
887
- }
888
- validateDraftRowData(data) {
889
- const schema = toEffectSchema2(this._projection.baseSchema);
890
- return validateSchema(schema, data) || [];
891
- }
892
- commitDraftRow = (draftRowIndex) => {
893
- if (draftRowIndex < 0 || draftRowIndex >= this._draftRows.value.length) {
894
- return false;
895
- }
896
- const draftRow = this._draftRows.value[draftRowIndex];
897
- if (!draftRow.valid) {
898
- return false;
899
- }
900
- const success = this._onInsertRow?.(draftRow.data);
901
- if (success) {
902
- const newDraftRows = this._draftRows.value.filter((_, index) => index !== draftRowIndex);
903
- this._draftRows.value = newDraftRows;
904
- }
905
- return success ?? false;
906
- };
907
- deleteRow = (rowIndex) => {
908
- const row = this._sorting.getDataIndex(rowIndex);
909
- const obj = this._rows.value[row];
910
- const objectsToDelete = [];
911
- if (this.features.selection.enabled && this._selection.hasSelection.value) {
912
- const selectedRows = this._selection.getSelectedRows();
913
- objectsToDelete.push(...selectedRows);
914
- } else {
915
- objectsToDelete.push(obj);
916
- }
917
- this._onDeleteRows?.(row, objectsToDelete);
918
- };
919
- handleRowAction = (actionId, rowIndex) => {
920
- if (!this._onRowAction) {
921
- return;
922
- }
923
- const row = this._sorting.getDataIndex(rowIndex);
924
- const data = this._rows.value[row];
925
- if (data) {
926
- this._onRowAction(actionId, data);
927
- }
928
- };
929
- getCellData = (cell) => {
930
- const { col, row, plane } = cell;
931
- const fields = this._projection?.fields ?? [];
932
- if (col < 0 || col >= fields.length) {
933
- return void 0;
934
- }
935
- const field = fields[col];
936
- let value;
937
- if (plane === "frozenRowsEnd") {
938
- if (row >= 0 && row < this._draftRows.value.length) {
939
- value = getValue2(this._draftRows.value[row].data, field.path);
940
- } else {
941
- return void 0;
942
- }
943
- } else {
944
- const dataIndex = this._sorting.getDataIndex(row);
945
- value = getValue2(this._rows.value[dataIndex], field.path);
946
- }
947
- if (value == null) {
948
- return "";
949
- }
950
- const { props } = this._projection.getFieldProjection(field.id);
951
- switch (props.format) {
952
- case FormatEnum2.Ref: {
953
- if (!field.referencePath) {
954
- return "";
955
- }
956
- return getValue2(value.target, field.referencePath);
957
- }
958
- default: {
959
- return formatForEditing({
960
- type: props.type,
961
- format: props.format,
962
- value
963
- });
964
- }
965
- }
966
- };
967
- validateCellData = async ({ col, row, plane }, value) => {
968
- const fields = this._projection?.fields ?? [];
969
- if (col < 0 || col >= fields.length) {
970
- return {
971
- valid: false,
972
- error: "Invalid column index"
973
- };
974
- }
975
- const field = fields[col];
976
- const { props } = this._projection.getFieldProjection(field.id);
977
- const transformedValue = editorTextToCellValue(props, value);
978
- const isDraftRow = plane === "frozenRowsEnd";
979
- if (isDraftRow) {
980
- const isInvalidDraftRowIndex = row < 0 || row >= this._draftRows.value.length;
981
- if (isInvalidDraftRowIndex) {
982
- return {
983
- valid: false,
984
- error: "Invalid draft row index"
985
- };
986
- }
987
- const draftRow = this._draftRows.value[row];
988
- const snapshot = {
989
- ...draftRow.data
990
- };
991
- setValue(snapshot, field.path, transformedValue);
992
- const validationErrors = this.validateDraftRowData(snapshot);
993
- if (validationErrors.length > 0) {
994
- const error = validationErrors.find((err) => err.path === field.path);
995
- if (error) {
996
- return {
997
- valid: false,
998
- error: error.message
999
- };
1000
- }
1001
- }
1002
- return {
1003
- valid: true
1004
- };
1005
- } else {
1006
- const rowIdx = this._sorting.getDataIndex(row);
1007
- const currentRow = this._rows.value[rowIdx];
1008
- invariant(currentRow, "Invalid row index", {
1009
- F: __dxlog_file,
1010
- L: 513,
1011
- S: this,
1012
- A: [
1013
- "currentRow",
1014
- "'Invalid row index'"
1015
- ]
1016
- });
1017
- const snapshot = getSnapshot(currentRow);
1018
- setValue(snapshot, field.path, transformedValue);
1019
- const schema = getSchema(currentRow);
1020
- invariant(schema, void 0, {
1021
- F: __dxlog_file,
1022
- L: 519,
1023
- S: this,
1024
- A: [
1025
- "schema",
1026
- ""
1027
- ]
1028
- });
1029
- const validationResult = validateSchema(schema, snapshot);
1030
- if (validationResult && validationResult.length > 0) {
1031
- const error = validationResult[0];
1032
- invariant(error.path === field.path, void 0, {
1033
- F: __dxlog_file,
1034
- L: 524,
1035
- S: this,
1036
- A: [
1037
- "error.path === field.path",
1038
- ""
1039
- ]
1040
- });
1041
- return {
1042
- valid: false,
1043
- error: error.message
1044
- };
1045
- }
1046
- return {
1047
- valid: true
1048
- };
1049
- }
1050
- };
1051
- setCellData = (cell, value) => {
1052
- const { col, row, plane } = cell;
1053
- const fields = this._projection?.fields ?? [];
1054
- if (col < 0 || col >= fields.length) {
1055
- return;
1056
- }
1057
- const field = fields[col];
1058
- const { props } = this._projection.getFieldProjection(field.id);
1059
- const transformedValue = editorTextToCellValue(props, value);
1060
- if (props.format === FormatEnum2.Ref && !isLiveObject(value)) {
1061
- return;
1062
- }
1063
- if (plane === "frozenRowsEnd") {
1064
- if (row >= 0 && row < this._draftRows.value.length) {
1065
- setValue(this._draftRows.value[row].data, field.path, transformedValue);
1066
- this.validateDraftRow(row);
1067
- }
1068
- } else {
1069
- const rowIdx = this._sorting.getDataIndex(row);
1070
- setValue(this._rows.value[rowIdx], field.path, transformedValue);
1071
- }
1072
- };
1073
- /**
1074
- * Updates a cell's value using a transform function.
1075
- * @param {DxGridPlanePosition} position - The position of the cell to update.
1076
- * @param {(value: any) => any} update - A function that takes the current value and returns the updated value.
1077
- */
1078
- updateCellData({ col, row }, update) {
1079
- const dataRow = this._sorting.getDataIndex(row);
1080
- const fields = this._projection?.fields ?? [];
1081
- const field = fields[col];
1082
- const value = getValue2(this._rows.value[dataRow], field.path);
1083
- const updatedValue = update(value);
1084
- setValue(this._rows.value[dataRow], field.path, updatedValue);
1085
- }
1086
- /**
1087
- * Gets the domain-appropriate cell type for a given cell position.
1088
- * This abstracts away the low-level grid plane concepts and provides domain-specific types.
1089
- * @param cell - The cell position to classify
1090
- * @returns The domain-appropriate cell type
1091
- */
1092
- getCellType(cell) {
1093
- switch (cell.plane) {
1094
- case "frozenRowsEnd":
1095
- return "draft";
1096
- case "frozenRowsStart":
1097
- return "header";
1098
- case "grid":
1099
- default:
1100
- return "standard";
1101
- }
1102
- }
1103
- /**
1104
- * Convenience method to check if a cell is a draft cell.
1105
- * @param cell - The cell position to check
1106
- * @returns true if the cell is a draft cell, false otherwise
1107
- */
1108
- isDraftCell(cell) {
1109
- return this.getCellType(cell) === "draft";
1110
- }
1111
- //
1112
- // Column Operations
1113
- //
1114
- deleteColumn(fieldId) {
1115
- if (!this._projection) {
1116
- return;
1117
- }
1118
- const field = this._projection?.fields.find((field2) => field2.id === fieldId);
1119
- if (field && this._onDeleteColumn) {
1120
- this._onDeleteColumn(field.id);
1121
- }
1122
- }
1123
- //
1124
- // Resize
1125
- //
1126
- setColumnWidth(columnIndex, width) {
1127
- const fields = this._projection?.fields ?? [];
1128
- if (columnIndex < fields.length) {
1129
- const newWidth = Math.max(0, width);
1130
- const field = fields[columnIndex];
1131
- if (field) {
1132
- this.table.sizes[field.path] = newWidth;
1133
- }
1134
- }
1135
- }
1136
- //
1137
- // Pinning
1138
- //
1139
- // TODO(burdon): Change to setPinned(on/off).
1140
- pinRow(rowIndex, side) {
1141
- this._pinnedRows[side].push(rowIndex);
1142
- }
1143
- unpinRow(rowIndex) {
1144
- this._pinnedRows.top = this._pinnedRows.top.filter((index) => index !== rowIndex);
1145
- this._pinnedRows.bottom = this._pinnedRows.bottom.filter((index) => index !== rowIndex);
1146
- }
1147
- //
1148
- // View operations
1149
- //
1150
- saveView() {
1151
- this._sorting.save();
1152
- }
1153
- };
1154
- var editorTextToCellValue = (props, value) => {
1155
- switch (props.format) {
1156
- case FormatEnum2.Ref: {
1157
- if (isLiveObject(value)) {
1158
- return Ref.make(value);
1159
- } else {
1160
- return value;
1161
- }
1162
- }
1163
- case FormatEnum2.SingleSelect: {
1164
- const ids = extractTagIds(value);
1165
- if (ids && ids.length > 0) {
1166
- return ids[0];
1167
- } else {
1168
- return value;
1169
- }
1170
- }
1171
- case FormatEnum2.MultiSelect: {
1172
- const ids = extractTagIds(value);
1173
- return ids || value;
1174
- }
1175
- default: {
1176
- return parseValue({
1177
- type: props.type,
1178
- format: props.format,
1179
- value
1180
- });
1181
- }
1182
- }
1183
- };
1184
-
1185
- // src/model/modal-controller.ts
1186
- import { signal as signal4 } from "@preact/signals-core";
1187
- import { log } from "@dxos/log";
1188
- var __dxlog_file2 = "/__w/dxos/dxos/packages/ui/react-ui-table/src/model/modal-controller.ts";
1189
- var ModalController = class {
1190
- _state = signal4({
1191
- type: "closed"
1192
- });
1193
- _triggerRef = {
1194
- current: null
1195
- };
1196
- get state() {
1197
- return this._state;
1198
- }
1199
- get trigger() {
1200
- return this._triggerRef;
1201
- }
1202
- setTrigger = (trigger) => {
1203
- this._triggerRef.current = trigger;
1204
- };
1205
- showRowMenu = (rowIndex) => {
1206
- this._state.value = {
1207
- type: "row",
1208
- rowIndex
1209
- };
1210
- };
1211
- showColumnMenu = (fieldId) => {
1212
- this._state.value = {
1213
- type: "column",
1214
- fieldId
1215
- };
1216
- };
1217
- showColumnCreator = () => {
1218
- this._state.value = {
1219
- type: "columnSettings",
1220
- mode: {
1221
- type: "create"
1222
- }
1223
- };
1224
- };
1225
- showReferencePanel = (targetId, schemaId) => {
1226
- this._state.value = {
1227
- type: "refPanel",
1228
- targetId,
1229
- typename: schemaId
1230
- };
1231
- };
1232
- openColumnSettings = () => {
1233
- if (this._state.value.type === "column") {
1234
- const fieldId = this._state.value.fieldId;
1235
- requestAnimationFrame(() => {
1236
- this._state.value = {
1237
- type: "columnSettings",
1238
- mode: {
1239
- type: "edit",
1240
- fieldId
1241
- }
1242
- };
1243
- });
1244
- }
1245
- };
1246
- openCreateRef = (typename, anchorCell, initialValues, onCreate) => {
1247
- if (anchorCell) {
1248
- this._triggerRef.current = anchorCell;
1249
- this._state.value = {
1250
- type: "createRefPanel",
1251
- typename,
1252
- initialValues,
1253
- onCreate
1254
- };
1255
- } else {
1256
- log.warn("anchor cell not found while creating new ref", void 0, {
1257
- F: __dxlog_file2,
1258
- L: 86,
1259
- S: this,
1260
- C: (f, a) => f(...a)
1261
- });
1262
- }
1263
- };
1264
- close = () => {
1265
- this._state.value = {
1266
- type: "closed"
1267
- };
1268
- };
1269
- };
1270
-
1271
- // src/model/table-presentation.ts
1272
- import { signal as signal5 } from "@preact/signals-core";
1273
- import { isNotNullable } from "effect/Predicate";
1274
- import { Obj as Obj4 } from "@dxos/echo";
1275
- import { FormatEnum as FormatEnum3, TypeEnum as TypeEnum2, getValue as getValue3 } from "@dxos/echo-schema";
1276
- import { cellClassesForFieldType, formatForDisplay as formatForDisplay2 } from "@dxos/react-ui-form";
1277
- import { toPlaneCellIndex } from "@dxos/react-ui-grid";
1278
- import { mx } from "@dxos/react-ui-theme";
1279
- import { VIEW_FIELD_LIMIT } from "@dxos/schema";
1280
- var TablePresentation = class {
1281
- model;
1282
- _visibleRange;
1283
- fieldProjectionCache;
1284
- constructor(model, _visibleRange = signal5({
1285
- start: {
1286
- row: 0,
1287
- col: 0
1288
- },
1289
- end: {
1290
- row: 0,
1291
- col: 0
1292
- }
1293
- })) {
1294
- this.model = model;
1295
- this._visibleRange = _visibleRange;
1296
- this.fieldProjectionCache = /* @__PURE__ */ new Map();
1297
- }
1298
- getCells(range, plane) {
1299
- this.fieldProjectionCache.clear();
1300
- let cells;
1301
- switch (plane) {
1302
- case "grid":
1303
- this._visibleRange.value = range;
1304
- cells = this.getMainGridCells(range);
1305
- break;
1306
- case "frozenRowsStart":
1307
- cells = this.getHeaderCells(range);
1308
- break;
1309
- case "frozenColsStart":
1310
- cells = this.getSelectionColumnCells(range);
1311
- break;
1312
- case "frozenColsEnd":
1313
- cells = this.getActionColumnCells(range);
1314
- break;
1315
- case "fixedStartStart":
1316
- cells = this.getSelectAllCell();
1317
- break;
1318
- case "fixedStartEnd":
1319
- cells = this.getNewColumnCell();
1320
- break;
1321
- case "frozenRowsEnd":
1322
- cells = this.getDraftRowCells(range);
1323
- break;
1324
- case "fixedEndStart":
1325
- cells = this.getDraftSelectCells(range);
1326
- break;
1327
- case "fixedEndEnd":
1328
- cells = this.getDraftActionCells(range);
1329
- break;
1330
- default:
1331
- cells = {};
1332
- }
1333
- if (plane === "grid" && this.model.features.dataEditable === false) {
1334
- Object.values(cells).forEach((cell) => {
1335
- cell.readonly = "text-select";
1336
- });
1337
- }
1338
- return cells;
1339
- }
1340
- /**
1341
- * Gets a field projection, using cache if available within the current render pass.
1342
- */
1343
- getFieldProjection(fieldId) {
1344
- let projection = this.fieldProjectionCache.get(fieldId);
1345
- if (!projection) {
1346
- projection = this.model.projection.getFieldProjection(fieldId);
1347
- this.fieldProjectionCache.set(fieldId, projection);
1348
- }
1349
- return projection;
1350
- }
1351
- createDataCell(cells, obj, field, colIndex, displayIndex) {
1352
- const { props } = this.getFieldProjection(field.id);
1353
- const cell = {
1354
- get value() {
1355
- const value = getValue3(obj, field.path);
1356
- if (value == null) {
1357
- return "";
1358
- }
1359
- if (props.type === "array") {
1360
- return "";
1361
- }
1362
- switch (props.format) {
1363
- case FormatEnum3.Boolean:
1364
- case FormatEnum3.SingleSelect:
1365
- case FormatEnum3.MultiSelect: {
1366
- return "";
1367
- }
1368
- case FormatEnum3.Ref: {
1369
- if (!field.referencePath) {
1370
- return "";
1371
- }
1372
- return getValue3(value.target, field.referencePath);
1373
- }
1374
- default: {
1375
- return formatForDisplay2({
1376
- type: props.type,
1377
- format: props.format,
1378
- value
1379
- });
1380
- }
1381
- }
1382
- }
1383
- };
1384
- const classes = [];
1385
- const formatClasses = cellClassesForFieldType({
1386
- type: props.type,
1387
- format: props.format
1388
- });
1389
- if (formatClasses) {
1390
- classes.push(formatClasses);
1391
- }
1392
- const rowSelectionClasses = cellClassesForRowSelection(this.model.selection.isObjectSelected(obj), this.model.selection.selectionMode);
1393
- if (rowSelectionClasses) {
1394
- classes.push(rowSelectionClasses);
1395
- }
1396
- if (classes.length > 0) {
1397
- cell.className = mx(classes.flat());
1398
- }
1399
- if (props.type === TypeEnum2.Array) {
1400
- const targetArray = getValue3(obj, field.path);
1401
- if (targetArray && Array.isArray(targetArray)) {
1402
- const getLabel = (value) => {
1403
- if (!value) {
1404
- return void 0;
1405
- }
1406
- if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
1407
- return String(value);
1408
- }
1409
- if (Array.isArray(value)) {
1410
- return "Array";
1411
- }
1412
- if (typeof value === "object" && value.name) {
1413
- return value.name;
1414
- }
1415
- if (typeof value === "object" && value.value) {
1416
- return `${value.value}`;
1417
- }
1418
- return "Object";
1419
- };
1420
- const tags = targetArray.map(getLabel).filter(isNotNullable).map((title) => {
1421
- return `<span class="dx-tag" data-hue="neutral">${title}</span>`;
1422
- }).join("");
1423
- cell.accessoryHtml = `<div role='none' class="flex flex-row gap-1 overflow-x-auto">${tags}</div>`;
1424
- }
1425
- }
1426
- if (props.format === FormatEnum3.Ref && props.referenceSchema) {
1427
- const targetObj = getValue3(obj, field.path)?.target;
1428
- if (targetObj) {
1429
- const dxn = Obj4.getDXN(targetObj)?.toString();
1430
- cell.accessoryHtml = `<dx-ref-tag refId=${dxn} class="dx-button is-6 pli-[3px] pbe-[2px] min-bs-0 absolute inline-end-2 block-start-[.2rem]" data-dx-grid-action="accessory"><dx-icon icon="ph--link-simple--regular"/></dx-ref-tag>`;
1431
- }
1432
- }
1433
- if (props.format === FormatEnum3.Boolean) {
1434
- const value = getValue3(obj, field.path);
1435
- cell.accessoryHtml = tableControls.switch.render({
1436
- colIndex,
1437
- rowIndex: displayIndex,
1438
- checked: value ?? false
1439
- });
1440
- cell.readonly = "no-text-select";
1441
- }
1442
- if (props.format === FormatEnum3.SingleSelect) {
1443
- const value = getValue3(obj, field.path);
1444
- const options = this.model.projection.getFieldProjection(field.id).props.options;
1445
- if (options) {
1446
- const option = options.find((o) => o.id === value);
1447
- if (option) {
1448
- cell.accessoryHtml = `<span class="dx-tag" data-hue="${option.color}">${option.title}</span>`;
1449
- }
1450
- }
1451
- }
1452
- if (props.format === FormatEnum3.MultiSelect) {
1453
- const values = getValue3(obj, field.path);
1454
- const options = this.model.projection.getFieldProjection(field.id).props.options;
1455
- if (options && values && values.length > 0) {
1456
- const tags = values.map((value) => {
1457
- const option = options.find((o) => o.id === value);
1458
- if (option) {
1459
- return `<span class="dx-tag" data-hue="${option.color}">${option.title}</span>`;
1460
- }
1461
- return null;
1462
- }).filter(Boolean).join("");
1463
- if (tags) {
1464
- cell.accessoryHtml = tags;
1465
- }
1466
- }
1467
- }
1468
- const idx = toPlaneCellIndex({
1469
- col: colIndex,
1470
- row: displayIndex
1471
- });
1472
- cells[idx] = cell;
1473
- }
1474
- getMainGridCells(range) {
1475
- const cells = {};
1476
- const fields = this.model.projection?.fields ?? [];
1477
- for (let row = range.start.row; row <= range.end.row && row < this.model.getRowCount(); row++) {
1478
- for (let col = range.start.col; col <= range.end.col && col < fields.length; col++) {
1479
- const field = fields[col];
1480
- if (!field) {
1481
- continue;
1482
- }
1483
- this.createDataCell(cells, this.model.rows.value[row], field, col, row);
1484
- }
1485
- }
1486
- return cells;
1487
- }
1488
- getDraftRowCells(range) {
1489
- const cells = {};
1490
- const fields = this.model.projection?.fields ?? [];
1491
- const draftRows = this.model.draftRows.value;
1492
- for (let row = range.start.row; row <= range.end.row && row < draftRows.length; row++) {
1493
- const draftRow = draftRows[row];
1494
- for (let col = range.start.col; col <= range.end.col && col < fields.length; col++) {
1495
- const cellIndex = toPlaneCellIndex({
1496
- col,
1497
- row
1498
- });
1499
- const field = fields[col];
1500
- if (!field) {
1501
- continue;
1502
- }
1503
- this.createDataCell(cells, draftRow.data, field, col, row);
1504
- if (this.model.hasDraftRowValidationError(row, field.path)) {
1505
- const cellValue = cells[cellIndex];
1506
- if (cellValue) {
1507
- const existingClasses = cellValue.className || "";
1508
- const draftClasses = "dx-grid__cell--flagged";
1509
- cellValue.className = existingClasses ? `${existingClasses} ${draftClasses}` : draftClasses;
1510
- }
1511
- }
1512
- cells[cellIndex].className += " !bg-toolbarSurface";
1513
- }
1514
- }
1515
- return cells;
1516
- }
1517
- getHeaderCells(range) {
1518
- const cells = {};
1519
- const fields = this.model.projection?.fields ?? [];
1520
- for (let col = range.start.col; col <= range.end.col && col < fields.length; col++) {
1521
- const { field, props } = this.model.projection.getFieldProjection(fields[col].id);
1522
- const sorting = this.model.sorting?.sorting;
1523
- const direction = sorting?.fieldId === field.id ? sorting.direction : void 0;
1524
- cells[toPlaneCellIndex({
1525
- col,
1526
- row: 0
1527
- })] = {
1528
- // TODO(burdon): Use same logic as form for fallback title.
1529
- value: props.title ?? field.path,
1530
- readonly: true,
1531
- resizeHandle: "col",
1532
- accessoryHtml: `
1533
- ${direction !== void 0 ? tableButtons.sort.render({
1534
- fieldId: field.id,
1535
- direction
1536
- }) : ""}
1537
- ${tableButtons.columnSettings.render({
1538
- fieldId: field.id
1539
- })}
1540
- `,
1541
- className: "!bg-toolbarSurface"
1542
- };
1543
- }
1544
- return cells;
1545
- }
1546
- getSelectionColumnCells(range) {
1547
- const cells = {};
1548
- for (let row = range.start.row; row <= range.end.row && row < this.model.getRowCount(); row++) {
1549
- const isSelected = this.model.selection.isRowIndexSelected(row);
1550
- const classes = cellClassesForRowSelection(isSelected, this.model.selection.selectionMode);
1551
- cells[toPlaneCellIndex({
1552
- col: 0,
1553
- row
1554
- })] = {
1555
- value: "",
1556
- readonly: "no-text-select",
1557
- className: classes ? mx(classes) : void 0,
1558
- accessoryHtml: tableControls.checkbox.render({
1559
- rowIndex: row,
1560
- checked: isSelected
1561
- })
1562
- };
1563
- }
1564
- return cells;
1565
- }
1566
- getActionColumnCells(range) {
1567
- const cells = {};
1568
- for (let row = range.start.row; row <= range.end.row && row < this.model.getRowCount(); row++) {
1569
- const isSelected = this.model.selection.isRowIndexSelected(row);
1570
- const classes = cellClassesForRowSelection(isSelected, this.model.selection.selectionMode);
1571
- cells[toPlaneCellIndex({
1572
- col: 0,
1573
- row
1574
- })] = {
1575
- value: "",
1576
- readonly: true,
1577
- className: classes ? mx(classes) : void 0,
1578
- accessoryHtml: tableButtons.rowMenu.render({
1579
- rowIndex: row
1580
- })
1581
- };
1582
- }
1583
- return cells;
1584
- }
1585
- getSelectAllCell() {
1586
- if (!this.model.features.selection.enabled || this.model.selection.selectionMode === "single") {
1587
- return {
1588
- [toPlaneCellIndex({
1589
- col: 0,
1590
- row: 0
1591
- })]: {
1592
- className: "!bg-toolbarSurface",
1593
- readonly: true,
1594
- value: ""
1595
- }
1596
- };
1597
- }
1598
- return {
1599
- [toPlaneCellIndex({
1600
- col: 0,
1601
- row: 0
1602
- })]: {
1603
- accessoryHtml: tableControls.checkbox.render({
1604
- rowIndex: 0,
1605
- header: true,
1606
- checked: this.model.selection.allRowsSeleted.value
1607
- }),
1608
- className: "!bg-toolbarSurface",
1609
- readonly: true,
1610
- value: ""
1611
- }
1612
- };
1613
- }
1614
- getNewColumnCell() {
1615
- return {
1616
- [toPlaneCellIndex({
1617
- col: 0,
1618
- row: 0
1619
- })]: {
1620
- accessoryHtml: this.model.features.schemaEditable ? tableButtons.addColumn.render({
1621
- disabled: (this.model.projection?.fields?.length ?? 0) >= VIEW_FIELD_LIMIT
1622
- }) : void 0,
1623
- className: "!bg-toolbarSurface",
1624
- readonly: true,
1625
- value: ""
1626
- }
1627
- };
1628
- }
1629
- getDraftActionCells(range) {
1630
- const cells = {};
1631
- const draftRows = this.model.draftRows.value;
1632
- for (let row = range.start.row; row <= range.end.row && row < draftRows.length; row++) {
1633
- const draftRow = draftRows[row];
1634
- const disabled = !draftRow.valid;
1635
- cells[toPlaneCellIndex({
1636
- col: 0,
1637
- row
1638
- })] = {
1639
- value: "",
1640
- readonly: true,
1641
- accessoryHtml: tableButtons.saveDraftRow.render({
1642
- rowIndex: row,
1643
- disabled
1644
- }),
1645
- className: "!bg-toolbarSurface"
1646
- };
1647
- }
1648
- return cells;
1649
- }
1650
- getDraftSelectCells(range) {
1651
- const cells = {};
1652
- const draftRows = this.model.draftRows.value;
1653
- for (let row = range.start.row; row <= range.end.row && row < draftRows.length; row++) {
1654
- cells[toPlaneCellIndex({
1655
- col: 0,
1656
- row
1657
- })] = {
1658
- value: "",
1659
- readonly: true,
1660
- className: "!bg-toolbarSurface"
1661
- };
1662
- }
1663
- return cells;
1664
- }
1665
- };
1666
- var cellClassesForRowSelection = (selected, selectionMode) => {
1667
- if (!selected) {
1668
- if (selectionMode === "single") {
1669
- return [
1670
- "!cursor-pointer"
1671
- ];
1672
- } else {
1673
- return void 0;
1674
- }
1675
- }
1676
- switch (selectionMode) {
1677
- case "single":
1678
- return [
1679
- "!bg-currentRelated dx-grid__cell--no-focus-unfurl hover:bg-hoverSurface !cursor-pointer"
1680
- ];
1681
- case "multiple":
1682
- return [
1683
- "!bg-gridCellSelected"
1684
- ];
1685
- }
1686
- };
1687
-
1688
- // src/hooks/useTableModel.ts
1689
- import { useSignals as _useSignals } from "@preact-signals/safe-react/tracking";
1690
- import { effect as effect3 } from "@preact/signals-core";
1691
- import orderBy2 from "lodash.orderby";
1692
- import { useEffect, useMemo, useState } from "react";
1693
- import { fullyQualifiedId as fullyQualifiedId2 } from "@dxos/react-client/echo";
1694
- import { useSelected, useSelectionActions } from "@dxos/react-ui-attention";
1695
- import { isNonNullable } from "@dxos/util";
1696
- var useTableModel = ({ view, schema, projection, rows, rowActions, features, onSelectionChanged, onRowAction, ...props }) => {
1697
- var _effect = _useSignals();
1698
- try {
1699
- const selected = useSelected(view && fullyQualifiedId2(view), "multi");
1700
- const initialSelection = useMemo(() => selected, [
1701
- view
1702
- ]);
1703
- const [model, setModel] = useState();
1704
- useEffect(() => {
1705
- if (!view || !schema) {
1706
- return;
1707
- }
1708
- let model2;
1709
- const t = setTimeout(async () => {
1710
- model2 = new TableModel({
1711
- view,
1712
- schema,
1713
- projection,
1714
- features,
1715
- rowActions,
1716
- initialSelection,
1717
- onRowAction,
1718
- ...props
1719
- });
1720
- await model2.open();
1721
- setModel(model2);
1722
- });
1723
- return () => {
1724
- clearTimeout(t);
1725
- void model2?.close();
1726
- };
1727
- }, [
1728
- view,
1729
- schema,
1730
- projection,
1731
- features,
1732
- rowActions,
1733
- initialSelection
1734
- ]);
1735
- useEffect(() => {
1736
- if (rows) {
1737
- const sortedRows = orderBy2(rows, [
1738
- (row) => String(row.id)
1739
- ], [
1740
- "asc"
1741
- ]);
1742
- model?.setRows(sortedRows);
1743
- }
1744
- }, [
1745
- model,
1746
- rows
1747
- ]);
1748
- const { multiSelect, clear } = useSelectionActions([
1749
- model?.id
1750
- ].filter(isNonNullable));
1751
- useEffect(() => {
1752
- if (!model) {
1753
- return;
1754
- }
1755
- const unsubscribe = effect3(() => {
1756
- const selectedItems = [
1757
- ...model.selection.selection.value
1758
- ];
1759
- multiSelect(selectedItems);
1760
- onSelectionChanged?.(selectedItems);
1761
- });
1762
- return () => {
1763
- clear();
1764
- unsubscribe();
1765
- };
1766
- }, [
1767
- model,
1768
- onSelectionChanged
1769
- ]);
1770
- return model;
1771
- } finally {
1772
- _effect.f();
1773
- }
1774
- };
1775
-
1776
- // src/hooks/useAddRow.ts
1777
- import { useCallback } from "react";
1778
- import { Obj as Obj5 } from "@dxos/echo";
1779
- var useAddRow = ({ space, schema }) => {
1780
- return useCallback((data) => {
1781
- if (space && schema) {
1782
- try {
1783
- space.db.add(Obj5.make(schema, data ?? {}));
1784
- return true;
1785
- } catch (error) {
1786
- return false;
1787
- }
1788
- }
1789
- return false;
1790
- }, [
1791
- space,
1792
- schema
1793
- ]);
1794
- };
1795
-
1796
- export {
1797
- getBaseSchema,
1798
- makeDynamicTable,
1799
- touch,
1800
- createTable,
1801
- BUTTON_IDENTIFIERS,
1802
- tableButtons,
1803
- CONTROL_IDENTIFIERS,
1804
- CheckboxStory,
1805
- SwitchStory,
1806
- tableControls,
1807
- narrowSchema,
1808
- TableModel,
1809
- ModalController,
1810
- TablePresentation,
1811
- cellClassesForRowSelection,
1812
- useTableModel,
1813
- useAddRow
1814
- };
1815
- //# sourceMappingURL=chunk-ZOFFI3JJ.mjs.map