@etsoo/react 1.4.60 → 1.4.64

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/lib/index.d.ts CHANGED
@@ -42,6 +42,7 @@ export * from './mu/DraggablePaperComponent';
42
42
  export * from './mu/EmailInput';
43
43
  export * from './mu/FabBox';
44
44
  export * from './mu/FlexBox';
45
+ export * from './mu/GridDataFormat';
45
46
  export * from './mu/IconButtonLink';
46
47
  export * from './mu/InputField';
47
48
  export * from './mu/ItemList';
package/lib/index.js CHANGED
@@ -45,6 +45,7 @@ export * from './mu/DraggablePaperComponent';
45
45
  export * from './mu/EmailInput';
46
46
  export * from './mu/FabBox';
47
47
  export * from './mu/FlexBox';
48
+ export * from './mu/GridDataFormat';
48
49
  export * from './mu/IconButtonLink';
49
50
  export * from './mu/InputField';
50
51
  export * from './mu/ItemList';
@@ -0,0 +1,10 @@
1
+ import React from 'react';
2
+ import { GridColumnRenderProps, GridDataType } from '../components/GridColumn';
3
+ /**
4
+ * Grid data format
5
+ * @param data Input data
6
+ * @param type Data type
7
+ * @param renderProps Render props
8
+ * @returns Result
9
+ */
10
+ export declare function GridDataFormat(data: unknown, type: GridDataType, renderProps?: GridColumnRenderProps): React.ReactNode;
@@ -0,0 +1,43 @@
1
+ import { DateUtils, NumberUtils } from '@etsoo/shared';
2
+ import React from 'react';
3
+ import { DateText } from './texts/DateText';
4
+ import { GridDataType } from '../components/GridColumn';
5
+ /**
6
+ * Grid data format
7
+ * @param data Input data
8
+ * @param type Data type
9
+ * @param renderProps Render props
10
+ * @returns Result
11
+ */
12
+ export function GridDataFormat(data, type, renderProps) {
13
+ // Null
14
+ if (data == null)
15
+ return undefined;
16
+ // For date time
17
+ // Conversion if necessary
18
+ if (type === GridDataType.Date || type === GridDataType.DateTime) {
19
+ const dateValue = data instanceof Date
20
+ ? data
21
+ : typeof data === 'number' || typeof data === 'string'
22
+ ? new Date(data)
23
+ : undefined;
24
+ if (dateValue == null)
25
+ return undefined;
26
+ const option = type === GridDataType.DateTime ? 'ds' : 'd';
27
+ const nearDays = renderProps === null || renderProps === void 0 ? void 0 : renderProps.nearDays;
28
+ if (nearDays != null) {
29
+ return (React.createElement(DateText, { value: dateValue, locale: renderProps === null || renderProps === void 0 ? void 0 : renderProps.culture, timeZone: renderProps === null || renderProps === void 0 ? void 0 : renderProps.timeZone, options: option, nearDays: nearDays }));
30
+ }
31
+ return DateUtils.format(dateValue, renderProps === null || renderProps === void 0 ? void 0 : renderProps.culture, option, renderProps === null || renderProps === void 0 ? void 0 : renderProps.timeZone);
32
+ }
33
+ // For numbers
34
+ if (typeof data === 'number') {
35
+ if (type === GridDataType.Money || type === GridDataType.IntMoney)
36
+ return NumberUtils.formatMoney(data, renderProps === null || renderProps === void 0 ? void 0 : renderProps.currency, renderProps === null || renderProps === void 0 ? void 0 : renderProps.culture, type === GridDataType.IntMoney, renderProps === null || renderProps === void 0 ? void 0 : renderProps.numberFormatOptions);
37
+ else
38
+ return NumberUtils.format(data, renderProps === null || renderProps === void 0 ? void 0 : renderProps.culture, renderProps === null || renderProps === void 0 ? void 0 : renderProps.numberFormatOptions);
39
+ }
40
+ if (typeof data === 'string')
41
+ return data;
42
+ return `${data}`;
43
+ }
@@ -1,5 +1,6 @@
1
1
  import { GridProps } from '@mui/material';
2
2
  import React from 'react';
3
+ import { GridColumnRenderProps, GridDataType } from '../../components/GridColumn';
3
4
  import { CommonPageProps } from './CommonPageProps';
4
5
  /**
5
6
  * View page display field
@@ -9,12 +10,24 @@ export interface ViewPageField<T extends {}> extends GridProps {
9
10
  * Data field
10
11
  */
11
12
  data: keyof T | ((item: T) => React.ReactNode);
13
+ /**
14
+ * Data type
15
+ */
16
+ dataType?: GridDataType;
12
17
  /**
13
18
  * Label field
14
19
  */
15
20
  label?: string | (() => React.ReactNode);
21
+ /**
22
+ * Display as single row
23
+ */
24
+ singleRow?: boolean;
25
+ /**
26
+ * Render props
27
+ */
28
+ renderProps?: GridColumnRenderProps;
16
29
  }
17
- declare type ViewPageFieldType<T> = (string & keyof T) | ViewPageField<T>;
30
+ declare type ViewPageFieldType<T> = (string & keyof T) | [string & keyof T, GridDataType, GridColumnRenderProps?] | ViewPageField<T>;
18
31
  /**
19
32
  * View page props
20
33
  */
@@ -1,42 +1,56 @@
1
1
  import { Grid, LinearProgress, Typography } from '@mui/material';
2
2
  import React from 'react';
3
3
  import { globalApp } from '../../app/ReactApp';
4
+ import { GridDataFormat } from '../GridDataFormat';
4
5
  import { MUGlobal } from '../MUGlobal';
5
6
  import { CommonPage } from './CommonPage';
6
7
  function formatItemData(fieldData) {
8
+ if (fieldData == null)
9
+ return undefined;
10
+ if (typeof fieldData === 'string')
11
+ return fieldData;
7
12
  if (fieldData instanceof Date)
8
13
  return globalApp.formatDate(fieldData, 'd');
9
- return new String(fieldData);
14
+ return `${fieldData}`;
10
15
  }
11
16
  function getItemField(field, data) {
12
- var _a, _b;
17
+ var _a, _b, _c;
13
18
  // Item data and label
14
- let itemData, itemLabel, gridProps;
15
- if (typeof field === 'object') {
19
+ let itemData, itemLabel, gridProps = {};
20
+ if (Array.isArray(field)) {
21
+ const [fieldData, fieldType, renderProps] = field;
22
+ itemData = GridDataFormat(data[fieldData], fieldType, renderProps);
23
+ itemLabel = (_a = globalApp.get(fieldData)) !== null && _a !== void 0 ? _a : fieldData;
24
+ }
25
+ else if (typeof field === 'object') {
16
26
  // Destruct
17
- if (field.label == null && typeof field.data === 'string')
18
- field.label = field.data;
19
- const { data: fieldData, label: fieldLabel, ...rest } = field;
20
- gridProps = rest;
27
+ const { data: fieldData, dataType, label: fieldLabel, renderProps, singleRow = false, ...rest } = field;
28
+ gridProps = {
29
+ ...rest,
30
+ ...(singleRow && { xs: 12, sm: 12, md: 12, lg: 12, xl: 12 })
31
+ };
21
32
  // Field data
22
33
  if (typeof fieldData === 'function')
23
34
  itemData = fieldData(data);
35
+ else if (dataType == null)
36
+ itemData = formatItemData(data[fieldData]);
24
37
  else
25
- itemData = formatItemData(formatItemData);
38
+ itemData = GridDataFormat(data[fieldData], dataType, renderProps);
26
39
  // Field label
27
40
  itemLabel =
28
41
  typeof fieldLabel === 'function'
29
42
  ? fieldLabel()
30
- : fieldLabel == null
31
- ? 'No Label'
32
- : (_a = globalApp.get(fieldLabel)) !== null && _a !== void 0 ? _a : fieldLabel;
43
+ : (_b = globalApp.get(fieldLabel !== null && fieldLabel !== void 0 ? fieldLabel : (typeof fieldData === 'string' ? fieldData : 'noData'))) !== null && _b !== void 0 ? _b : fieldLabel;
33
44
  }
34
45
  else {
35
46
  itemData = formatItemData(data[field]);
36
- itemLabel = (_b = globalApp.get(field)) !== null && _b !== void 0 ? _b : field;
37
- gridProps = { xs: 12, sm: 6, md: 4, lg: 3 };
47
+ itemLabel = (_c = globalApp.get(field)) !== null && _c !== void 0 ? _c : field;
38
48
  }
39
- return [itemData, itemLabel, gridProps];
49
+ return [
50
+ itemData,
51
+ itemLabel,
52
+ { xs: 12, sm: 6, md: 6, lg: 4, xl: 3, ...gridProps }
53
+ ];
40
54
  }
41
55
  /**
42
56
  * View page
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/react",
3
- "version": "1.4.60",
3
+ "version": "1.4.64",
4
4
  "description": "TypeScript ReactJs framework",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
package/src/index.ts CHANGED
@@ -49,6 +49,7 @@ export * from './mu/DraggablePaperComponent';
49
49
  export * from './mu/EmailInput';
50
50
  export * from './mu/FabBox';
51
51
  export * from './mu/FlexBox';
52
+ export * from './mu/GridDataFormat';
52
53
  export * from './mu/IconButtonLink';
53
54
  export * from './mu/InputField';
54
55
  export * from './mu/ItemList';
@@ -0,0 +1,77 @@
1
+ import { DateUtils, NumberUtils } from '@etsoo/shared';
2
+ import React from 'react';
3
+ import { DateText } from './texts/DateText';
4
+ import { GridColumnRenderProps, GridDataType } from '../components/GridColumn';
5
+
6
+ /**
7
+ * Grid data format
8
+ * @param data Input data
9
+ * @param type Data type
10
+ * @param renderProps Render props
11
+ * @returns Result
12
+ */
13
+ export function GridDataFormat(
14
+ data: unknown,
15
+ type: GridDataType,
16
+ renderProps?: GridColumnRenderProps
17
+ ): React.ReactNode {
18
+ // Null
19
+ if (data == null) return undefined;
20
+
21
+ // For date time
22
+ // Conversion if necessary
23
+ if (type === GridDataType.Date || type === GridDataType.DateTime) {
24
+ const dateValue =
25
+ data instanceof Date
26
+ ? data
27
+ : typeof data === 'number' || typeof data === 'string'
28
+ ? new Date(data)
29
+ : undefined;
30
+
31
+ if (dateValue == null) return undefined;
32
+
33
+ const option = type === GridDataType.DateTime ? 'ds' : 'd';
34
+
35
+ const nearDays = renderProps?.nearDays;
36
+ if (nearDays != null) {
37
+ return (
38
+ <DateText
39
+ value={dateValue}
40
+ locale={renderProps?.culture}
41
+ timeZone={renderProps?.timeZone}
42
+ options={option}
43
+ nearDays={nearDays}
44
+ />
45
+ );
46
+ }
47
+
48
+ return DateUtils.format(
49
+ dateValue,
50
+ renderProps?.culture,
51
+ option,
52
+ renderProps?.timeZone
53
+ );
54
+ }
55
+
56
+ // For numbers
57
+ if (typeof data === 'number') {
58
+ if (type === GridDataType.Money || type === GridDataType.IntMoney)
59
+ return NumberUtils.formatMoney(
60
+ data,
61
+ renderProps?.currency,
62
+ renderProps?.culture,
63
+ type === GridDataType.IntMoney,
64
+ renderProps?.numberFormatOptions
65
+ );
66
+ else
67
+ return NumberUtils.format(
68
+ data,
69
+ renderProps?.culture,
70
+ renderProps?.numberFormatOptions
71
+ );
72
+ }
73
+
74
+ if (typeof data === 'string') return data;
75
+
76
+ return `${data}`;
77
+ }
@@ -1,6 +1,11 @@
1
1
  import { Grid, GridProps, LinearProgress, Typography } from '@mui/material';
2
2
  import React from 'react';
3
3
  import { globalApp } from '../../app/ReactApp';
4
+ import {
5
+ GridColumnRenderProps,
6
+ GridDataType
7
+ } from '../../components/GridColumn';
8
+ import { GridDataFormat } from '../GridDataFormat';
4
9
  import { MUGlobal } from '../MUGlobal';
5
10
  import { CommonPage } from './CommonPage';
6
11
  import { CommonPageProps } from './CommonPageProps';
@@ -14,13 +19,31 @@ export interface ViewPageField<T extends {}> extends GridProps {
14
19
  */
15
20
  data: keyof T | ((item: T) => React.ReactNode);
16
21
 
22
+ /**
23
+ * Data type
24
+ */
25
+ dataType?: GridDataType;
26
+
17
27
  /**
18
28
  * Label field
19
29
  */
20
30
  label?: string | (() => React.ReactNode);
31
+
32
+ /**
33
+ * Display as single row
34
+ */
35
+ singleRow?: boolean;
36
+
37
+ /**
38
+ * Render props
39
+ */
40
+ renderProps?: GridColumnRenderProps;
21
41
  }
22
42
 
23
- type ViewPageFieldType<T> = (string & keyof T) | ViewPageField<T>;
43
+ type ViewPageFieldType<T> =
44
+ | (string & keyof T)
45
+ | [string & keyof T, GridDataType, GridColumnRenderProps?]
46
+ | ViewPageField<T>;
24
47
 
25
48
  /**
26
49
  * View page props
@@ -42,9 +65,11 @@ export interface ViewPageProps<T extends {}> extends CommonPageProps {
42
65
  supportRefresh?: boolean;
43
66
  }
44
67
 
45
- function formatItemData(fieldData: unknown) {
68
+ function formatItemData(fieldData: unknown): string | undefined {
69
+ if (fieldData == null) return undefined;
70
+ if (typeof fieldData === 'string') return fieldData;
46
71
  if (fieldData instanceof Date) return globalApp.formatDate(fieldData, 'd');
47
- return new String(fieldData);
72
+ return `${fieldData}`;
48
73
  }
49
74
 
50
75
  function getItemField<T>(
@@ -54,33 +79,51 @@ function getItemField<T>(
54
79
  // Item data and label
55
80
  let itemData: React.ReactNode,
56
81
  itemLabel: React.ReactNode,
57
- gridProps: GridProps;
82
+ gridProps: GridProps = {};
58
83
 
59
- if (typeof field === 'object') {
84
+ if (Array.isArray(field)) {
85
+ const [fieldData, fieldType, renderProps] = field;
86
+ itemData = GridDataFormat(data[fieldData], fieldType, renderProps);
87
+ itemLabel = globalApp.get<string>(fieldData) ?? fieldData;
88
+ } else if (typeof field === 'object') {
60
89
  // Destruct
61
- if (field.label == null && typeof field.data === 'string')
62
- field.label = field.data;
63
- const { data: fieldData, label: fieldLabel, ...rest } = field;
64
- gridProps = rest;
90
+ const {
91
+ data: fieldData,
92
+ dataType,
93
+ label: fieldLabel,
94
+ renderProps,
95
+ singleRow = false,
96
+ ...rest
97
+ } = field;
98
+
99
+ gridProps = {
100
+ ...rest,
101
+ ...(singleRow && { xs: 12, sm: 12, md: 12, lg: 12, xl: 12 })
102
+ };
65
103
 
66
104
  // Field data
67
105
  if (typeof fieldData === 'function') itemData = fieldData(data);
68
- else itemData = formatItemData(formatItemData);
106
+ else if (dataType == null) itemData = formatItemData(data[fieldData]);
107
+ else itemData = GridDataFormat(data[fieldData], dataType, renderProps);
69
108
 
70
109
  // Field label
71
110
  itemLabel =
72
111
  typeof fieldLabel === 'function'
73
112
  ? fieldLabel()
74
- : fieldLabel == null
75
- ? 'No Label'
76
- : globalApp.get<string>(fieldLabel) ?? fieldLabel;
113
+ : globalApp.get<string>(
114
+ fieldLabel ??
115
+ (typeof fieldData === 'string' ? fieldData : 'noData')
116
+ ) ?? fieldLabel;
77
117
  } else {
78
118
  itemData = formatItemData(data[field]);
79
119
  itemLabel = globalApp.get<string>(field) ?? field;
80
- gridProps = { xs: 12, sm: 6, md: 4, lg: 3 };
81
120
  }
82
121
 
83
- return [itemData, itemLabel, gridProps];
122
+ return [
123
+ itemData,
124
+ itemLabel,
125
+ { xs: 12, sm: 6, md: 6, lg: 4, xl: 3, ...gridProps }
126
+ ];
84
127
  }
85
128
 
86
129
  /**