@etsoo/react 1.4.61 → 1.4.65

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,16 +10,32 @@ 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
- label: string | (() => React.ReactNode);
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
  */
21
34
  export interface ViewPageProps<T extends {}> extends CommonPageProps {
35
+ /**
36
+ * Actions
37
+ */
38
+ actions?: React.ReactNode;
22
39
  /**
23
40
  * Fields to display
24
41
  */
@@ -1,38 +1,57 @@
1
1
  import { Grid, LinearProgress, Typography } from '@mui/material';
2
2
  import React from 'react';
3
3
  import { globalApp } from '../../app/ReactApp';
4
+ import { HBox } from '../FlexBox';
5
+ import { GridDataFormat } from '../GridDataFormat';
4
6
  import { MUGlobal } from '../MUGlobal';
5
7
  import { CommonPage } from './CommonPage';
6
8
  function formatItemData(fieldData) {
9
+ if (fieldData == null)
10
+ return undefined;
11
+ if (typeof fieldData === 'string')
12
+ return fieldData;
7
13
  if (fieldData instanceof Date)
8
14
  return globalApp.formatDate(fieldData, 'd');
9
- return new String(fieldData);
15
+ return `${fieldData}`;
10
16
  }
11
17
  function getItemField(field, data) {
12
- var _a, _b;
18
+ var _a, _b, _c;
13
19
  // Item data and label
14
- let itemData, itemLabel, gridProps;
15
- if (typeof field === 'object') {
20
+ let itemData, itemLabel, gridProps = {};
21
+ if (Array.isArray(field)) {
22
+ const [fieldData, fieldType, renderProps] = field;
23
+ itemData = GridDataFormat(data[fieldData], fieldType, renderProps);
24
+ itemLabel = (_a = globalApp.get(fieldData)) !== null && _a !== void 0 ? _a : fieldData;
25
+ }
26
+ else if (typeof field === 'object') {
16
27
  // Destruct
17
- const { data: fieldData, label: fieldLabel, ...rest } = field;
18
- gridProps = rest;
28
+ const { data: fieldData, dataType, label: fieldLabel, renderProps, singleRow = false, ...rest } = field;
29
+ gridProps = {
30
+ ...rest,
31
+ ...(singleRow && { xs: 12, sm: 12, md: 12, lg: 12, xl: 12 })
32
+ };
19
33
  // Field data
20
34
  if (typeof fieldData === 'function')
21
35
  itemData = fieldData(data);
36
+ else if (dataType == null)
37
+ itemData = formatItemData(data[fieldData]);
22
38
  else
23
- itemData = formatItemData(formatItemData);
39
+ itemData = GridDataFormat(data[fieldData], dataType, renderProps);
24
40
  // Field label
25
41
  itemLabel =
26
42
  typeof fieldLabel === 'function'
27
43
  ? fieldLabel()
28
- : (_a = globalApp.get(fieldLabel)) !== null && _a !== void 0 ? _a : fieldLabel;
44
+ : (_b = globalApp.get(fieldLabel !== null && fieldLabel !== void 0 ? fieldLabel : (typeof fieldData === 'string' ? fieldData : 'noData'))) !== null && _b !== void 0 ? _b : fieldLabel;
29
45
  }
30
46
  else {
31
47
  itemData = formatItemData(data[field]);
32
- itemLabel = (_b = globalApp.get(field)) !== null && _b !== void 0 ? _b : field;
33
- gridProps = { xs: 12, sm: 6, md: 4, lg: 3 };
48
+ itemLabel = (_c = globalApp.get(field)) !== null && _c !== void 0 ? _c : field;
34
49
  }
35
- return [itemData, itemLabel, gridProps];
50
+ return [
51
+ itemData,
52
+ itemLabel,
53
+ { xs: 12, sm: 6, md: 6, lg: 4, xl: 3, ...gridProps }
54
+ ];
36
55
  }
37
56
  /**
38
57
  * View page
@@ -40,7 +59,7 @@ function getItemField(field, data) {
40
59
  */
41
60
  export function ViewPage(props) {
42
61
  // Destruct
43
- const { children, fields, loadData, paddings = MUGlobal.pagePaddings, supportRefresh = true, ...rest } = props;
62
+ const { actions, children, fields, loadData, paddings = MUGlobal.pagePaddings, supportRefresh = true, ...rest } = props;
44
63
  // Data
45
64
  const [data, setData] = React.useState();
46
65
  // Load data
@@ -52,7 +71,9 @@ export function ViewPage(props) {
52
71
  };
53
72
  return (React.createElement(CommonPage, { paddings: paddings, onRefresh: supportRefresh ? onRefresh : undefined, onUpdate: supportRefresh ? undefined : onRefresh, ...rest, scrollContainer: global }, data == null ? (React.createElement(LinearProgress, null)) : (React.createElement(React.Fragment, null,
54
73
  React.createElement(Grid, { container: true, justifyContent: "left", spacing: paddings, sx: {
55
- '.MuiTypography-subtitle2': { fontWeight: 'bold' }
74
+ '.MuiTypography-subtitle2': {
75
+ fontWeight: 'bold'
76
+ }
56
77
  } }, fields.map((field, index) => {
57
78
  // Get data
58
79
  const [itemData, itemLabel, gridProps] = getItemField(field, data);
@@ -65,5 +86,6 @@ export function ViewPage(props) {
65
86
  ":"),
66
87
  React.createElement(Typography, { variant: "subtitle2" }, itemData)));
67
88
  })),
89
+ actions != null && (React.createElement(HBox, { paddingTop: paddings, paddingBottom: paddings }, actions)),
68
90
  children))));
69
91
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/react",
3
- "version": "1.4.61",
3
+ "version": "1.4.65",
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,12 @@
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 { HBox } from '../FlexBox';
9
+ import { GridDataFormat } from '../GridDataFormat';
4
10
  import { MUGlobal } from '../MUGlobal';
5
11
  import { CommonPage } from './CommonPage';
6
12
  import { CommonPageProps } from './CommonPageProps';
@@ -14,18 +20,41 @@ export interface ViewPageField<T extends {}> extends GridProps {
14
20
  */
15
21
  data: keyof T | ((item: T) => React.ReactNode);
16
22
 
23
+ /**
24
+ * Data type
25
+ */
26
+ dataType?: GridDataType;
27
+
17
28
  /**
18
29
  * Label field
19
30
  */
20
- label: string | (() => React.ReactNode);
31
+ label?: string | (() => React.ReactNode);
32
+
33
+ /**
34
+ * Display as single row
35
+ */
36
+ singleRow?: boolean;
37
+
38
+ /**
39
+ * Render props
40
+ */
41
+ renderProps?: GridColumnRenderProps;
21
42
  }
22
43
 
23
- type ViewPageFieldType<T> = (string & keyof T) | ViewPageField<T>;
44
+ type ViewPageFieldType<T> =
45
+ | (string & keyof T)
46
+ | [string & keyof T, GridDataType, GridColumnRenderProps?]
47
+ | ViewPageField<T>;
24
48
 
25
49
  /**
26
50
  * View page props
27
51
  */
28
52
  export interface ViewPageProps<T extends {}> extends CommonPageProps {
53
+ /**
54
+ * Actions
55
+ */
56
+ actions?: React.ReactNode;
57
+
29
58
  /**
30
59
  * Fields to display
31
60
  */
@@ -42,9 +71,11 @@ export interface ViewPageProps<T extends {}> extends CommonPageProps {
42
71
  supportRefresh?: boolean;
43
72
  }
44
73
 
45
- function formatItemData(fieldData: unknown) {
74
+ function formatItemData(fieldData: unknown): string | undefined {
75
+ if (fieldData == null) return undefined;
76
+ if (typeof fieldData === 'string') return fieldData;
46
77
  if (fieldData instanceof Date) return globalApp.formatDate(fieldData, 'd');
47
- return new String(fieldData);
78
+ return `${fieldData}`;
48
79
  }
49
80
 
50
81
  function getItemField<T>(
@@ -54,29 +85,51 @@ function getItemField<T>(
54
85
  // Item data and label
55
86
  let itemData: React.ReactNode,
56
87
  itemLabel: React.ReactNode,
57
- gridProps: GridProps;
88
+ gridProps: GridProps = {};
58
89
 
59
- if (typeof field === 'object') {
90
+ if (Array.isArray(field)) {
91
+ const [fieldData, fieldType, renderProps] = field;
92
+ itemData = GridDataFormat(data[fieldData], fieldType, renderProps);
93
+ itemLabel = globalApp.get<string>(fieldData) ?? fieldData;
94
+ } else if (typeof field === 'object') {
60
95
  // Destruct
61
- const { data: fieldData, label: fieldLabel, ...rest } = field;
62
- gridProps = rest;
96
+ const {
97
+ data: fieldData,
98
+ dataType,
99
+ label: fieldLabel,
100
+ renderProps,
101
+ singleRow = false,
102
+ ...rest
103
+ } = field;
104
+
105
+ gridProps = {
106
+ ...rest,
107
+ ...(singleRow && { xs: 12, sm: 12, md: 12, lg: 12, xl: 12 })
108
+ };
63
109
 
64
110
  // Field data
65
111
  if (typeof fieldData === 'function') itemData = fieldData(data);
66
- else itemData = formatItemData(formatItemData);
112
+ else if (dataType == null) itemData = formatItemData(data[fieldData]);
113
+ else itemData = GridDataFormat(data[fieldData], dataType, renderProps);
67
114
 
68
115
  // Field label
69
116
  itemLabel =
70
117
  typeof fieldLabel === 'function'
71
118
  ? fieldLabel()
72
- : globalApp.get<string>(fieldLabel) ?? fieldLabel;
119
+ : globalApp.get<string>(
120
+ fieldLabel ??
121
+ (typeof fieldData === 'string' ? fieldData : 'noData')
122
+ ) ?? fieldLabel;
73
123
  } else {
74
124
  itemData = formatItemData(data[field]);
75
125
  itemLabel = globalApp.get<string>(field) ?? field;
76
- gridProps = { xs: 12, sm: 6, md: 4, lg: 3 };
77
126
  }
78
127
 
79
- return [itemData, itemLabel, gridProps];
128
+ return [
129
+ itemData,
130
+ itemLabel,
131
+ { xs: 12, sm: 6, md: 6, lg: 4, xl: 3, ...gridProps }
132
+ ];
80
133
  }
81
134
 
82
135
  /**
@@ -86,6 +139,7 @@ function getItemField<T>(
86
139
  export function ViewPage<T extends {}>(props: ViewPageProps<T>) {
87
140
  // Destruct
88
141
  const {
142
+ actions,
89
143
  children,
90
144
  fields,
91
145
  loadData,
@@ -121,7 +175,9 @@ export function ViewPage<T extends {}>(props: ViewPageProps<T>) {
121
175
  justifyContent="left"
122
176
  spacing={paddings}
123
177
  sx={{
124
- '.MuiTypography-subtitle2': { fontWeight: 'bold' }
178
+ '.MuiTypography-subtitle2': {
179
+ fontWeight: 'bold'
180
+ }
125
181
  }}
126
182
  >
127
183
  {fields.map((field, index) => {
@@ -147,6 +203,11 @@ export function ViewPage<T extends {}>(props: ViewPageProps<T>) {
147
203
  );
148
204
  })}
149
205
  </Grid>
206
+ {actions != null && (
207
+ <HBox paddingTop={paddings} paddingBottom={paddings}>
208
+ {actions}
209
+ </HBox>
210
+ )}
150
211
  {children}
151
212
  </React.Fragment>
152
213
  )}