@trackunit/react-table-helpers 0.0.7-alpha-a6869e1d4c.0 → 0.0.8

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
@@ -6,6 +6,7 @@ var jsxRuntime = require('react/jsx-runtime');
6
6
  var i18nLibraryTranslation = require('@trackunit/i18n-library-translation');
7
7
  var reactTable = require('@trackunit/react-table');
8
8
  var react = require('react');
9
+ var irisAppRuntimeCore = require('@trackunit/iris-app-runtime-core');
9
10
  var reactTableBaseComponents = require('@trackunit/react-table-base-components');
10
11
 
11
12
  var defaultTranslations = {
@@ -50,37 +51,45 @@ const setupLibraryTranslations = () => {
50
51
  i18nLibraryTranslation.registerTranslations(translations);
51
52
  };
52
53
 
54
+ const extractNumberAsString = (value, unitPreference, unitSi, unitUs, language) => {
55
+ const unit = unitPreference === "SI" ? unitSi : unitUs;
56
+ const updatedValue = irisAppRuntimeCore.CustomFieldRuntime.getCustomFieldValueForDisplayInUI(value, unit, unitPreference, language);
57
+ return `${updatedValue}${unit && updatedValue ? " " + unit : ""}`;
58
+ };
53
59
  /**
54
60
  * Takes a custom fields data object and returns a react node that can be used as a cell in a table.
55
61
  */
56
- const customFieldToCell = (customField) => {
57
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x;
62
+ const customFieldToCell = (customField, unitPreference, language) => {
63
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
58
64
  const type = customField.type;
59
65
  switch (type) {
60
66
  case "BOOLEAN": {
61
- const checked = (_b = (_a = customField.booleanValue) !== null && _a !== void 0 ? _a : customField.definition.defaultBooleanValue) !== null && _b !== void 0 ? _b : false;
67
+ const checked = (_a = customField.booleanValue) !== null && _a !== void 0 ? _a : false;
62
68
  return jsxRuntime.jsx(reactTableBaseComponents.CheckboxCell, { checked: checked });
63
69
  }
64
70
  case "STRING":
65
- return (_f = (_d = (_c = customField.stringValue) === null || _c === void 0 ? void 0 : _c.toString()) !== null && _d !== void 0 ? _d : (_e = customField.definition.defaultStringValue) === null || _e === void 0 ? void 0 : _e.toString()) !== null && _f !== void 0 ? _f : "";
71
+ return (_c = (_b = customField.stringValue) === null || _b === void 0 ? void 0 : _b.toString()) !== null && _c !== void 0 ? _c : "";
66
72
  case "EMAIL":
67
73
  case "PHONE_NUMBER":
68
74
  case "WEB_ADDRESS": {
69
- const link = (_h = (_g = customField.stringValue) !== null && _g !== void 0 ? _g : customField.definition.defaultStringValue) !== null && _h !== void 0 ? _h : "";
75
+ const link = (_d = customField.stringValue) !== null && _d !== void 0 ? _d : "";
70
76
  const linkType = customField.type === "WEB_ADDRESS" ? "LINK" : customField.type === "PHONE_NUMBER" ? "PHONE" : "EMAIL";
71
77
  return jsxRuntime.jsx(reactTableBaseComponents.LinkCell, { link: link, type: linkType });
72
78
  }
73
79
  case "DATE":
74
- return (_m = (_k = (_j = customField.dateValue) === null || _j === void 0 ? void 0 : _j.toString()) !== null && _k !== void 0 ? _k : (_l = customField.definition.defaultDateValue) === null || _l === void 0 ? void 0 : _l.toString()) !== null && _m !== void 0 ? _m : "";
80
+ return (_f = (_e = customField.dateValue) === null || _e === void 0 ? void 0 : _e.toString()) !== null && _f !== void 0 ? _f : "";
75
81
  case "DROPDOWN": {
76
- const tags = (_p = (_o = customField.stringArrayValue) !== null && _o !== void 0 ? _o : customField.definition.defaultStringArrayValue) !== null && _p !== void 0 ? _p : [];
82
+ const tags = (_g = customField.stringArrayValue) !== null && _g !== void 0 ? _g : [];
77
83
  return jsxRuntime.jsx(reactTableBaseComponents.TagsCell, { tags: tags });
78
84
  }
79
85
  case "JSON":
80
- return (_t = (_r = (_q = customField.jsonValue) === null || _q === void 0 ? void 0 : _q.toString()) !== null && _r !== void 0 ? _r : (_s = customField.definition.defaultJsonValue) === null || _s === void 0 ? void 0 : _s.toString()) !== null && _t !== void 0 ? _t : "";
81
- case "NUMBER":
86
+ return (_j = (_h = customField.jsonValue) === null || _h === void 0 ? void 0 : _h.toString()) !== null && _j !== void 0 ? _j : "";
87
+ case "NUMBER": {
88
+ const value = customField.numberValue ? customField.numberValue + "" : undefined;
89
+ return extractNumberAsString(value, unitPreference, customField.definition.unitSi, customField.definition.unitUs, language);
90
+ }
82
91
  case "MONETARY":
83
- return (_x = (_v = (_u = customField.numberValue) === null || _u === void 0 ? void 0 : _u.toString()) !== null && _v !== void 0 ? _v : (_w = customField.definition.defaultNumberValue) === null || _w === void 0 ? void 0 : _w.toString()) !== null && _x !== void 0 ? _x : "";
92
+ return (_l = (_k = customField.numberValue) === null || _k === void 0 ? void 0 : _k.toString()) !== null && _l !== void 0 ? _l : "";
84
93
  default:
85
94
  return assertNever(type);
86
95
  }
@@ -96,7 +105,7 @@ function assertNever(value) {
96
105
  * @param {CustomFieldDefinitionArray} customFieldDefinitions - An array of custom field definitions.
97
106
  * @returns {ColumnDef<TDataType, CustomFieldData | undefined>[]} - An array of column definitions.
98
107
  */
99
- const useColumnDefinitionsFromCustomFieldDefinition = ({ customFieldDefinitions, }) => {
108
+ const useColumnDefinitionsFromCustomFieldDefinition = ({ customFieldDefinitions, unitPreference, language, }) => {
100
109
  return react.useMemo(() => {
101
110
  const columnHelper = reactTable.createColumnHelper();
102
111
  const result = [];
@@ -117,14 +126,14 @@ const useColumnDefinitionsFromCustomFieldDefinition = ({ customFieldDefinitions,
117
126
  },
118
127
  cell: cell => {
119
128
  const cellValue = cell.getValue();
120
- return cellValue ? customFieldToCell(cellValue) : "";
129
+ return cellValue ? customFieldToCell(cellValue, unitPreference, language) : "";
121
130
  },
122
131
  });
123
132
  result.push(column);
124
133
  }
125
134
  });
126
135
  return result;
127
- }, [customFieldDefinitions]);
136
+ }, [customFieldDefinitions, unitPreference, language]);
128
137
  };
129
138
 
130
139
  /*
package/index.esm.js CHANGED
@@ -2,6 +2,7 @@ import { jsx } from 'react/jsx-runtime';
2
2
  import { registerTranslations } from '@trackunit/i18n-library-translation';
3
3
  import { createColumnHelper } from '@trackunit/react-table';
4
4
  import { useMemo } from 'react';
5
+ import { CustomFieldRuntime } from '@trackunit/iris-app-runtime-core';
5
6
  import { TagsCell, LinkCell, CheckboxCell } from '@trackunit/react-table-base-components';
6
7
 
7
8
  var defaultTranslations = {
@@ -46,37 +47,45 @@ const setupLibraryTranslations = () => {
46
47
  registerTranslations(translations);
47
48
  };
48
49
 
50
+ const extractNumberAsString = (value, unitPreference, unitSi, unitUs, language) => {
51
+ const unit = unitPreference === "SI" ? unitSi : unitUs;
52
+ const updatedValue = CustomFieldRuntime.getCustomFieldValueForDisplayInUI(value, unit, unitPreference, language);
53
+ return `${updatedValue}${unit && updatedValue ? " " + unit : ""}`;
54
+ };
49
55
  /**
50
56
  * Takes a custom fields data object and returns a react node that can be used as a cell in a table.
51
57
  */
52
- const customFieldToCell = (customField) => {
53
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x;
58
+ const customFieldToCell = (customField, unitPreference, language) => {
59
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
54
60
  const type = customField.type;
55
61
  switch (type) {
56
62
  case "BOOLEAN": {
57
- const checked = (_b = (_a = customField.booleanValue) !== null && _a !== void 0 ? _a : customField.definition.defaultBooleanValue) !== null && _b !== void 0 ? _b : false;
63
+ const checked = (_a = customField.booleanValue) !== null && _a !== void 0 ? _a : false;
58
64
  return jsx(CheckboxCell, { checked: checked });
59
65
  }
60
66
  case "STRING":
61
- return (_f = (_d = (_c = customField.stringValue) === null || _c === void 0 ? void 0 : _c.toString()) !== null && _d !== void 0 ? _d : (_e = customField.definition.defaultStringValue) === null || _e === void 0 ? void 0 : _e.toString()) !== null && _f !== void 0 ? _f : "";
67
+ return (_c = (_b = customField.stringValue) === null || _b === void 0 ? void 0 : _b.toString()) !== null && _c !== void 0 ? _c : "";
62
68
  case "EMAIL":
63
69
  case "PHONE_NUMBER":
64
70
  case "WEB_ADDRESS": {
65
- const link = (_h = (_g = customField.stringValue) !== null && _g !== void 0 ? _g : customField.definition.defaultStringValue) !== null && _h !== void 0 ? _h : "";
71
+ const link = (_d = customField.stringValue) !== null && _d !== void 0 ? _d : "";
66
72
  const linkType = customField.type === "WEB_ADDRESS" ? "LINK" : customField.type === "PHONE_NUMBER" ? "PHONE" : "EMAIL";
67
73
  return jsx(LinkCell, { link: link, type: linkType });
68
74
  }
69
75
  case "DATE":
70
- return (_m = (_k = (_j = customField.dateValue) === null || _j === void 0 ? void 0 : _j.toString()) !== null && _k !== void 0 ? _k : (_l = customField.definition.defaultDateValue) === null || _l === void 0 ? void 0 : _l.toString()) !== null && _m !== void 0 ? _m : "";
76
+ return (_f = (_e = customField.dateValue) === null || _e === void 0 ? void 0 : _e.toString()) !== null && _f !== void 0 ? _f : "";
71
77
  case "DROPDOWN": {
72
- const tags = (_p = (_o = customField.stringArrayValue) !== null && _o !== void 0 ? _o : customField.definition.defaultStringArrayValue) !== null && _p !== void 0 ? _p : [];
78
+ const tags = (_g = customField.stringArrayValue) !== null && _g !== void 0 ? _g : [];
73
79
  return jsx(TagsCell, { tags: tags });
74
80
  }
75
81
  case "JSON":
76
- return (_t = (_r = (_q = customField.jsonValue) === null || _q === void 0 ? void 0 : _q.toString()) !== null && _r !== void 0 ? _r : (_s = customField.definition.defaultJsonValue) === null || _s === void 0 ? void 0 : _s.toString()) !== null && _t !== void 0 ? _t : "";
77
- case "NUMBER":
82
+ return (_j = (_h = customField.jsonValue) === null || _h === void 0 ? void 0 : _h.toString()) !== null && _j !== void 0 ? _j : "";
83
+ case "NUMBER": {
84
+ const value = customField.numberValue ? customField.numberValue + "" : undefined;
85
+ return extractNumberAsString(value, unitPreference, customField.definition.unitSi, customField.definition.unitUs, language);
86
+ }
78
87
  case "MONETARY":
79
- return (_x = (_v = (_u = customField.numberValue) === null || _u === void 0 ? void 0 : _u.toString()) !== null && _v !== void 0 ? _v : (_w = customField.definition.defaultNumberValue) === null || _w === void 0 ? void 0 : _w.toString()) !== null && _x !== void 0 ? _x : "";
88
+ return (_l = (_k = customField.numberValue) === null || _k === void 0 ? void 0 : _k.toString()) !== null && _l !== void 0 ? _l : "";
80
89
  default:
81
90
  return assertNever(type);
82
91
  }
@@ -92,7 +101,7 @@ function assertNever(value) {
92
101
  * @param {CustomFieldDefinitionArray} customFieldDefinitions - An array of custom field definitions.
93
102
  * @returns {ColumnDef<TDataType, CustomFieldData | undefined>[]} - An array of column definitions.
94
103
  */
95
- const useColumnDefinitionsFromCustomFieldDefinition = ({ customFieldDefinitions, }) => {
104
+ const useColumnDefinitionsFromCustomFieldDefinition = ({ customFieldDefinitions, unitPreference, language, }) => {
96
105
  return useMemo(() => {
97
106
  const columnHelper = createColumnHelper();
98
107
  const result = [];
@@ -113,14 +122,14 @@ const useColumnDefinitionsFromCustomFieldDefinition = ({ customFieldDefinitions,
113
122
  },
114
123
  cell: cell => {
115
124
  const cellValue = cell.getValue();
116
- return cellValue ? customFieldToCell(cellValue) : "";
125
+ return cellValue ? customFieldToCell(cellValue, unitPreference, language) : "";
117
126
  },
118
127
  });
119
128
  result.push(column);
120
129
  }
121
130
  });
122
131
  return result;
123
- }, [customFieldDefinitions]);
132
+ }, [customFieldDefinitions, unitPreference, language]);
124
133
  };
125
134
 
126
135
  /*
package/package.json CHANGED
@@ -1,16 +1,18 @@
1
1
  {
2
2
  "name": "@trackunit/react-table-helpers",
3
- "version": "0.0.7-alpha-a6869e1d4c.0",
3
+ "version": "0.0.8",
4
4
  "repository": "https://github.com/Trackunit/manager",
5
5
  "license": "SEE LICENSE IN LICENSE.txt",
6
6
  "engines": {
7
7
  "node": ">=18.x"
8
8
  },
9
9
  "dependencies": {
10
- "@trackunit/i18n-library-translation": "0.0.74-alpha-a6869e1d4c.0",
11
- "@trackunit/react-core-contexts-test": "0.1.102-alpha-a6869e1d4c.0",
12
- "@trackunit/react-table": "0.0.12-alpha-a6869e1d4c.0",
13
- "@trackunit/react-table-base-components": "0.0.6-alpha-a6869e1d4c.0",
10
+ "@trackunit/i18n-library-translation": "0.0.74",
11
+ "@trackunit/iris-app-runtime-core": "0.3.71",
12
+ "@trackunit/iris-app-runtime-core-api": "0.3.64",
13
+ "@trackunit/react-core-contexts-test": "0.1.103",
14
+ "@trackunit/react-table": "0.0.13",
15
+ "@trackunit/react-table-base-components": "0.0.7",
14
16
  "react": "18.2.0"
15
17
  },
16
18
  "module": "./index.esm.js",
@@ -1,5 +1,6 @@
1
+ import { SystemOfMeasurement } from "@trackunit/iris-app-runtime-core";
1
2
  import { CustomFieldData } from "./types";
2
3
  /**
3
4
  * Takes a custom fields data object and returns a react node that can be used as a cell in a table.
4
5
  */
5
- export declare const customFieldToCell: (customField: CustomFieldData) => React.ReactNode;
6
+ export declare const customFieldToCell: (customField: CustomFieldData, unitPreference: SystemOfMeasurement, language?: string) => React.ReactNode;
@@ -1,3 +1,4 @@
1
+ import { UnitSiType, UnitUsType } from "@trackunit/iris-app-runtime-core-api";
1
2
  /**
2
3
  * The different types of custom fields that are available.
3
4
  * Hardcoded to match the GraphQL API, to keep the lib independent of the API.
@@ -75,6 +76,8 @@ export type CustomFieldData = {
75
76
  title?: string | null;
76
77
  key?: string | null;
77
78
  definitionId?: string | null;
79
+ unitSi?: UnitSiType | null;
80
+ unitUs?: UnitUsType | null;
78
81
  };
79
82
  } | {
80
83
  valueId?: string | null;
@@ -1,7 +1,10 @@
1
+ import { SystemOfMeasurement } from "@trackunit/iris-app-runtime-core";
1
2
  import { ColumnDef } from "@trackunit/react-table";
2
3
  import { CustomFieldData, CustomFieldDefinitionArray, DataWithCustomFields } from "./types";
3
4
  export interface UseColumnDefinitionsFromCustomFieldDefinitionProps {
4
5
  customFieldDefinitions?: CustomFieldDefinitionArray;
6
+ unitPreference: SystemOfMeasurement;
7
+ language?: string;
5
8
  }
6
9
  /**
7
10
  * Generates an array of table column definitions based on custom field definitions.
@@ -10,4 +13,4 @@ export interface UseColumnDefinitionsFromCustomFieldDefinitionProps {
10
13
  * @param {CustomFieldDefinitionArray} customFieldDefinitions - An array of custom field definitions.
11
14
  * @returns {ColumnDef<TDataType, CustomFieldData | undefined>[]} - An array of column definitions.
12
15
  */
13
- export declare const useColumnDefinitionsFromCustomFieldDefinition: <TDataType extends DataWithCustomFields>({ customFieldDefinitions, }: UseColumnDefinitionsFromCustomFieldDefinitionProps) => ColumnDef<TDataType, CustomFieldData | undefined>[];
16
+ export declare const useColumnDefinitionsFromCustomFieldDefinition: <TDataType extends DataWithCustomFields>({ customFieldDefinitions, unitPreference, language, }: UseColumnDefinitionsFromCustomFieldDefinitionProps) => ColumnDef<TDataType, CustomFieldData | undefined>[];