@trackunit/react-table-helpers 3.0.5 → 3.0.10-alpha-563d99d17d6.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.
package/index.cjs.js CHANGED
@@ -5,6 +5,12 @@ var i18nLibraryTranslation = require('@trackunit/i18n-library-translation');
5
5
  var reactComponents = require('@trackunit/react-components');
6
6
  var reactCoreHooks = require('@trackunit/react-core-hooks');
7
7
  var react = require('react');
8
+ var dateAndTimeUtils = require('@trackunit/date-and-time-utils');
9
+ var irisAppRuntimeCore = require('@trackunit/iris-app-runtime-core');
10
+ var reactTableBaseComponents = require('@trackunit/react-table-base-components');
11
+ var customFieldComponents = require('@trackunit/custom-field-components');
12
+ var filtersGraphqlHook = require('@trackunit/filters-graphql-hook');
13
+ var reactTable = require('@trackunit/react-table');
8
14
 
9
15
  var defaultTranslations = {
10
16
  "export.table.button": "Export"
@@ -104,6 +110,385 @@ const ExportTableButton = ({ columnVisibility, rowCount, allTableColumns, variab
104
110
  }, variant: "secondary", children: t("export.table.button") }));
105
111
  };
106
112
 
113
+ /**
114
+ * Takes a custom fields data object and returns a react node that can be used as a cell in a table.
115
+ */
116
+ const customFieldToCell = (customField, unitTranslation, unitPreference, language) => {
117
+ const type = customField.__typename;
118
+ switch (type) {
119
+ case "BooleanFieldValueAndDefinition": {
120
+ const checked = customField.booleanValue ?? false;
121
+ return jsxRuntime.jsx(reactTableBaseComponents.CheckboxCell, { checked: checked });
122
+ }
123
+ case "StringFieldValueAndDefinition":
124
+ return jsxRuntime.jsx(reactTableBaseComponents.TextCell, { content: customField.stringValue?.toString() ?? "" });
125
+ case "EmailFieldValueAndDefinition":
126
+ return jsxRuntime.jsx(reactTableBaseComponents.LinkCell, { link: customField.emailValue?.toString() ?? "", type: "EMAIL" });
127
+ case "PhoneNumberFieldValueAndDefinition":
128
+ return jsxRuntime.jsx(reactTableBaseComponents.LinkCell, { link: customField.phoneNumberValue?.toString() ?? "", type: "PHONE" });
129
+ case "WebAddressFieldValueAndDefinition": {
130
+ return jsxRuntime.jsx(reactTableBaseComponents.LinkCell, { link: customField.webAddressValue ?? "", type: "LINK" });
131
+ }
132
+ case "DateFieldValueAndDefinition": {
133
+ const date = customField.dateValue;
134
+ return date ? jsxRuntime.jsx(reactTableBaseComponents.PlainDateCell, { date: dateAndTimeUtils.Temporal.PlainDate.from(date), locale: language }) : null;
135
+ }
136
+ case "StringListFieldValueAndDefinition":
137
+ case "DropDownFieldValueAndDefinition": {
138
+ const tags = customField.stringArrayValue ?? [];
139
+ return jsxRuntime.jsx(reactTableBaseComponents.TagsCell, { tags: tags });
140
+ }
141
+ case "JsonFieldValueAndDefinition":
142
+ return jsxRuntime.jsx(reactTableBaseComponents.TextCell, { content: customField.jsonValue?.toString() ?? "" });
143
+ case "NumberFieldValueAndDefinition": {
144
+ const unitType = unitPreference === "SI" ? customField.definition.unitSi : customField.definition.unitUs;
145
+ const value = customField.numberValue !== null && customField.numberValue !== undefined
146
+ ? `${customField.numberValue}`
147
+ : undefined;
148
+ const updatedValue = irisAppRuntimeCore.getCustomFieldValueForDisplayInUI(value, unitType, unitPreference, language);
149
+ return jsxRuntime.jsx(reactTableBaseComponents.NumberCell, { unit: unitTranslation(unitType), value: updatedValue });
150
+ }
151
+ case "MonetaryFieldValueAndDefinition":
152
+ return jsxRuntime.jsx(reactTableBaseComponents.NumberCell, { value: customField.monetaryValue?.toString() ?? "" });
153
+ case "FileFieldValueAndDefinition": {
154
+ const fileName = customField.fileName;
155
+ const downloadLink = customField.downloadLink;
156
+ if (!fileName) {
157
+ return null;
158
+ }
159
+ if (!downloadLink) {
160
+ return jsxRuntime.jsx(reactTableBaseComponents.TextCell, { content: fileName });
161
+ }
162
+ const handleClick = event => {
163
+ event.stopPropagation();
164
+ };
165
+ return (jsxRuntime.jsx(reactComponents.ExternalLink, { color: "neutral", href: downloadLink, onClick: handleClick, title: fileName, children: fileName }));
166
+ }
167
+ default:
168
+ throw new Error(`Unexpected value: ${type}`);
169
+ }
170
+ };
171
+
172
+ /**
173
+ * Takes a custom fields data object and returns a react node that can be used as a cell in a table.
174
+ */
175
+ const customFieldToExportString = (customField, unitTranslation, unitPreference, language) => {
176
+ const type = customField.__typename;
177
+ switch (type) {
178
+ case "BooleanFieldValueAndDefinition": {
179
+ const checked = customField.booleanValue ?? false;
180
+ return checked;
181
+ }
182
+ case "StringFieldValueAndDefinition":
183
+ return customField.stringValue?.toString() ?? "";
184
+ case "EmailFieldValueAndDefinition":
185
+ return customField.emailValue?.toString() ?? "";
186
+ case "PhoneNumberFieldValueAndDefinition":
187
+ return customField.phoneNumberValue?.toString() ?? "";
188
+ case "WebAddressFieldValueAndDefinition": {
189
+ return customField.webAddressValue ?? "";
190
+ }
191
+ case "DateFieldValueAndDefinition": {
192
+ const date = customField.dateValue;
193
+ return date;
194
+ }
195
+ case "StringListFieldValueAndDefinition":
196
+ case "DropDownFieldValueAndDefinition": {
197
+ const tags = customField.stringArrayValue ?? [];
198
+ return tags.join(", ");
199
+ }
200
+ case "JsonFieldValueAndDefinition":
201
+ return customField.jsonValue?.toString() ?? "";
202
+ case "NumberFieldValueAndDefinition": {
203
+ const unitType = unitPreference === "SI" ? customField.definition.unitSi : customField.definition.unitUs;
204
+ const value = Boolean(customField.numberValue) ? `${customField.numberValue}` : undefined;
205
+ const updatedValue = irisAppRuntimeCore.getCustomFieldValueForDisplayInUI(value, unitType, unitPreference, language);
206
+ const unit = unitTranslation(unitType);
207
+ return unit ? `${updatedValue} ${unit}` : `${updatedValue}`;
208
+ }
209
+ case "MonetaryFieldValueAndDefinition":
210
+ return customField.monetaryValue?.toString() ?? "";
211
+ case "FileFieldValueAndDefinition":
212
+ return customField.fileName ?? "";
213
+ default:
214
+ throw new Error(`Unexpected value: ${type}`);
215
+ }
216
+ };
217
+
218
+ const AssetCustomFieldsFragmentDoc = {
219
+ kind: "Document",
220
+ definitions: [
221
+ {
222
+ kind: "FragmentDefinition",
223
+ name: { kind: "Name", value: "AssetCustomFields" },
224
+ typeCondition: { kind: "NamedType", name: { kind: "Name", value: "Asset" } },
225
+ selectionSet: {
226
+ kind: "SelectionSet",
227
+ selections: [
228
+ {
229
+ kind: "Field",
230
+ name: { kind: "Name", value: "customFields" },
231
+ arguments: [
232
+ {
233
+ kind: "Argument",
234
+ name: { kind: "Name", value: "definitionIds" },
235
+ value: { kind: "Variable", name: { kind: "Name", value: "definitionIds" } },
236
+ },
237
+ ],
238
+ selectionSet: {
239
+ kind: "SelectionSet",
240
+ selections: [
241
+ {
242
+ kind: "Field",
243
+ name: { kind: "Name", value: "edges" },
244
+ selectionSet: {
245
+ kind: "SelectionSet",
246
+ selections: [
247
+ {
248
+ kind: "Field",
249
+ name: { kind: "Name", value: "node" },
250
+ selectionSet: {
251
+ kind: "SelectionSet",
252
+ selections: [
253
+ { kind: "Field", name: { kind: "Name", value: "__typename" } },
254
+ {
255
+ kind: "Field",
256
+ name: { kind: "Name", value: "definition" },
257
+ selectionSet: {
258
+ kind: "SelectionSet",
259
+ selections: [
260
+ { kind: "Field", name: { kind: "Name", value: "id" } },
261
+ { kind: "Field", name: { kind: "Name", value: "title" } },
262
+ { kind: "Field", name: { kind: "Name", value: "key" } },
263
+ ],
264
+ },
265
+ },
266
+ { kind: "Field", name: { kind: "Name", value: "valueId" } },
267
+ {
268
+ kind: "InlineFragment",
269
+ typeCondition: {
270
+ kind: "NamedType",
271
+ name: { kind: "Name", value: "BooleanFieldValueAndDefinition" },
272
+ },
273
+ selectionSet: {
274
+ kind: "SelectionSet",
275
+ selections: [{ kind: "Field", name: { kind: "Name", value: "booleanValue" } }],
276
+ },
277
+ },
278
+ {
279
+ kind: "InlineFragment",
280
+ typeCondition: {
281
+ kind: "NamedType",
282
+ name: { kind: "Name", value: "DateFieldValueAndDefinition" },
283
+ },
284
+ selectionSet: {
285
+ kind: "SelectionSet",
286
+ selections: [{ kind: "Field", name: { kind: "Name", value: "dateValue" } }],
287
+ },
288
+ },
289
+ {
290
+ kind: "InlineFragment",
291
+ typeCondition: {
292
+ kind: "NamedType",
293
+ name: { kind: "Name", value: "DropDownFieldValueAndDefinition" },
294
+ },
295
+ selectionSet: {
296
+ kind: "SelectionSet",
297
+ selections: [{ kind: "Field", name: { kind: "Name", value: "stringArrayValue" } }],
298
+ },
299
+ },
300
+ {
301
+ kind: "InlineFragment",
302
+ typeCondition: {
303
+ kind: "NamedType",
304
+ name: { kind: "Name", value: "EmailFieldValueAndDefinition" },
305
+ },
306
+ selectionSet: {
307
+ kind: "SelectionSet",
308
+ selections: [{ kind: "Field", name: { kind: "Name", value: "emailValue" } }],
309
+ },
310
+ },
311
+ {
312
+ kind: "InlineFragment",
313
+ typeCondition: {
314
+ kind: "NamedType",
315
+ name: { kind: "Name", value: "NumberFieldValueAndDefinition" },
316
+ },
317
+ selectionSet: {
318
+ kind: "SelectionSet",
319
+ selections: [
320
+ {
321
+ kind: "Field",
322
+ name: { kind: "Name", value: "definition" },
323
+ selectionSet: {
324
+ kind: "SelectionSet",
325
+ selections: [
326
+ { kind: "Field", name: { kind: "Name", value: "unitSi" } },
327
+ { kind: "Field", name: { kind: "Name", value: "unitUs" } },
328
+ ],
329
+ },
330
+ },
331
+ {
332
+ kind: "Field",
333
+ name: { kind: "Name", value: "numberValue" },
334
+ arguments: [
335
+ {
336
+ kind: "Argument",
337
+ name: { kind: "Name", value: "systemOfMeasurement" },
338
+ value: {
339
+ kind: "Variable",
340
+ name: { kind: "Name", value: "systemOfMeasurement" },
341
+ },
342
+ },
343
+ ],
344
+ },
345
+ ],
346
+ },
347
+ },
348
+ {
349
+ kind: "InlineFragment",
350
+ typeCondition: {
351
+ kind: "NamedType",
352
+ name: { kind: "Name", value: "PhoneNumberFieldValueAndDefinition" },
353
+ },
354
+ selectionSet: {
355
+ kind: "SelectionSet",
356
+ selections: [{ kind: "Field", name: { kind: "Name", value: "phoneNumberValue" } }],
357
+ },
358
+ },
359
+ {
360
+ kind: "InlineFragment",
361
+ typeCondition: {
362
+ kind: "NamedType",
363
+ name: { kind: "Name", value: "StringFieldValueAndDefinition" },
364
+ },
365
+ selectionSet: {
366
+ kind: "SelectionSet",
367
+ selections: [{ kind: "Field", name: { kind: "Name", value: "stringValue" } }],
368
+ },
369
+ },
370
+ {
371
+ kind: "InlineFragment",
372
+ typeCondition: {
373
+ kind: "NamedType",
374
+ name: { kind: "Name", value: "WebAddressFieldValueAndDefinition" },
375
+ },
376
+ selectionSet: {
377
+ kind: "SelectionSet",
378
+ selections: [{ kind: "Field", name: { kind: "Name", value: "webAddressValue" } }],
379
+ },
380
+ },
381
+ {
382
+ kind: "InlineFragment",
383
+ typeCondition: {
384
+ kind: "NamedType",
385
+ name: { kind: "Name", value: "JsonFieldValueAndDefinition" },
386
+ },
387
+ selectionSet: {
388
+ kind: "SelectionSet",
389
+ selections: [{ kind: "Field", name: { kind: "Name", value: "jsonValue" } }],
390
+ },
391
+ },
392
+ {
393
+ kind: "InlineFragment",
394
+ typeCondition: {
395
+ kind: "NamedType",
396
+ name: { kind: "Name", value: "MonetaryFieldValueAndDefinition" },
397
+ },
398
+ selectionSet: {
399
+ kind: "SelectionSet",
400
+ selections: [{ kind: "Field", name: { kind: "Name", value: "monetaryValue" } }],
401
+ },
402
+ },
403
+ {
404
+ kind: "InlineFragment",
405
+ typeCondition: {
406
+ kind: "NamedType",
407
+ name: { kind: "Name", value: "StringListFieldValueAndDefinition" },
408
+ },
409
+ selectionSet: {
410
+ kind: "SelectionSet",
411
+ selections: [{ kind: "Field", name: { kind: "Name", value: "stringArrayValue" } }],
412
+ },
413
+ },
414
+ {
415
+ kind: "InlineFragment",
416
+ typeCondition: {
417
+ kind: "NamedType",
418
+ name: { kind: "Name", value: "FileFieldValueAndDefinition" },
419
+ },
420
+ selectionSet: {
421
+ kind: "SelectionSet",
422
+ selections: [
423
+ { kind: "Field", name: { kind: "Name", value: "fileName" } },
424
+ { kind: "Field", name: { kind: "Name", value: "downloadLink" } },
425
+ ],
426
+ },
427
+ },
428
+ ],
429
+ },
430
+ },
431
+ ],
432
+ },
433
+ },
434
+ ],
435
+ },
436
+ },
437
+ ],
438
+ },
439
+ },
440
+ ],
441
+ };
442
+
443
+ /**
444
+ * Generates an array of table column definitions based on custom field definitions.
445
+ *
446
+ * @template TDataType - The data type with custom fields.
447
+ * @param {CustomFieldDefinitionArray} customFieldDefinitions - An array of custom field definitions.
448
+ * @returns {ColumnDef<TDataType, CustomFieldData | undefined>[]} - An array of column definitions.
449
+ */
450
+ const useColumnDefinitionsFromCustomFieldDefinition = ({ customFieldDefinitions, unitPreference, language, filterBarDefinition, }) => {
451
+ const { unitTranslation } = customFieldComponents.useUnitTranslation();
452
+ return react.useMemo(() => {
453
+ const columnHelper = reactTable.createColumnHelper();
454
+ const result = [];
455
+ customFieldDefinitions?.forEach(customFieldDefinition => {
456
+ if (customFieldDefinition?.node &&
457
+ customFieldDefinition.node.id &&
458
+ customFieldDefinition.node.title &&
459
+ customFieldDefinition.node.__typename) {
460
+ const id = filtersGraphqlHook.CustomFieldPrefix + customFieldDefinition.node.id;
461
+ const isKeyFound = filterBarDefinition ? !!filterBarDefinition[id] : false;
462
+ const column = columnHelper.accessor(data => data.customFields?.edges?.find(field => {
463
+ return field?.node?.definition.id === customFieldDefinition.node?.id;
464
+ })?.node, {
465
+ header: customFieldDefinition.node.title,
466
+ id: customFieldDefinition.node.id,
467
+ enableSorting: customFieldDefinition.node.__typename !== "DropDownFieldDefinition" &&
468
+ customFieldDefinition.node.__typename !== "StringListFieldDefinition" &&
469
+ customFieldDefinition.node.__typename !== "FileFieldDefinition",
470
+ meta: {
471
+ fragment: AssetCustomFieldsFragmentDoc,
472
+ hiddenByDefault: true,
473
+ isCustomField: { definitionId: customFieldDefinition.node.id },
474
+ filterName: isKeyFound && filterBarDefinition ? filterBarDefinition[id]?.filterKey : undefined,
475
+ export: {
476
+ accessor: "customFields.edges[].node",
477
+ transformer: { type: "customFields", id: customFieldDefinition.node.id },
478
+ },
479
+ },
480
+ cell: cell => {
481
+ const cellValue = cell.getValue();
482
+ return cellValue ? customFieldToCell(cellValue, unitTranslation, unitPreference, language) : "";
483
+ },
484
+ });
485
+ result.push(column);
486
+ }
487
+ });
488
+ return result;
489
+ }, [filterBarDefinition, customFieldDefinitions, unitTranslation, unitPreference, language]);
490
+ };
491
+
107
492
  /*
108
493
  * ----------------------------
109
494
  * | SETUP TRANSLATIONS START |
@@ -114,3 +499,6 @@ const ExportTableButton = ({ columnVisibility, rowCount, allTableColumns, variab
114
499
  setupLibraryTranslations();
115
500
 
116
501
  exports.ExportTableButton = ExportTableButton;
502
+ exports.customFieldToCell = customFieldToCell;
503
+ exports.customFieldToExportString = customFieldToExportString;
504
+ exports.useColumnDefinitionsFromCustomFieldDefinition = useColumnDefinitionsFromCustomFieldDefinition;