@trackunit/react-table-helpers 2.33.5-alpha-c071292d523.0 → 3.0.1

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