@etsoo/react 1.4.63 → 1.4.68

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,6 +10,10 @@ 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
  */
@@ -17,12 +22,24 @@ export interface ViewPageField<T extends {}> extends GridProps {
17
22
  * Display as single row
18
23
  */
19
24
  singleRow?: boolean;
25
+ /**
26
+ * Render props
27
+ */
28
+ renderProps?: GridColumnRenderProps;
20
29
  }
21
- declare type ViewPageFieldType<T> = (string & keyof T) | ViewPageField<T>;
30
+ declare type ViewPageFieldType<T> = (string & keyof T) | [string & keyof T, GridDataType, GridColumnRenderProps?] | ViewPageField<T>;
22
31
  /**
23
32
  * View page props
24
33
  */
25
- export interface ViewPageProps<T extends {}> extends CommonPageProps {
34
+ export interface ViewPageProps<T extends {}> extends Exclude<CommonPageProps, 'children'> {
35
+ /**
36
+ * Actions
37
+ */
38
+ actions?: (data: T) => React.ReactNode;
39
+ /**
40
+ * Children
41
+ */
42
+ children?: React.ReactNode | ((data: T) => React.ReactNode);
26
43
  /**
27
44
  * Fields to display
28
45
  */
@@ -1,6 +1,9 @@
1
+ import { Utils } from '@etsoo/shared';
1
2
  import { Grid, LinearProgress, Typography } from '@mui/material';
2
3
  import React from 'react';
3
4
  import { globalApp } from '../../app/ReactApp';
5
+ import { HBox } from '../FlexBox';
6
+ import { GridDataFormat } from '../GridDataFormat';
4
7
  import { MUGlobal } from '../MUGlobal';
5
8
  import { CommonPage } from './CommonPage';
6
9
  function formatItemData(fieldData) {
@@ -13,12 +16,17 @@ function formatItemData(fieldData) {
13
16
  return `${fieldData}`;
14
17
  }
15
18
  function getItemField(field, data) {
16
- var _a, _b;
19
+ var _a, _b, _c;
17
20
  // Item data and label
18
21
  let itemData, itemLabel, gridProps = {};
19
- if (typeof field === 'object') {
22
+ if (Array.isArray(field)) {
23
+ const [fieldData, fieldType, renderProps] = field;
24
+ itemData = GridDataFormat(data[fieldData], fieldType, renderProps);
25
+ itemLabel = (_a = globalApp.get(fieldData)) !== null && _a !== void 0 ? _a : fieldData;
26
+ }
27
+ else if (typeof field === 'object') {
20
28
  // Destruct
21
- const { data: fieldData, label: fieldLabel, singleRow = false, ...rest } = field;
29
+ const { data: fieldData, dataType, label: fieldLabel, renderProps, singleRow = false, ...rest } = field;
22
30
  gridProps = {
23
31
  ...rest,
24
32
  ...(singleRow && { xs: 12, sm: 12, md: 12, lg: 12, xl: 12 })
@@ -26,17 +34,19 @@ function getItemField(field, data) {
26
34
  // Field data
27
35
  if (typeof fieldData === 'function')
28
36
  itemData = fieldData(data);
29
- else
37
+ else if (dataType == null)
30
38
  itemData = formatItemData(data[fieldData]);
39
+ else
40
+ itemData = GridDataFormat(data[fieldData], dataType, renderProps);
31
41
  // Field label
32
42
  itemLabel =
33
43
  typeof fieldLabel === 'function'
34
44
  ? fieldLabel()
35
- : (_a = globalApp.get(fieldLabel !== null && fieldLabel !== void 0 ? fieldLabel : (typeof fieldData === 'string' ? fieldData : 'noData'))) !== null && _a !== void 0 ? _a : fieldLabel;
45
+ : (_b = globalApp.get(fieldLabel !== null && fieldLabel !== void 0 ? fieldLabel : (typeof fieldData === 'string' ? fieldData : 'noData'))) !== null && _b !== void 0 ? _b : fieldLabel;
36
46
  }
37
47
  else {
38
48
  itemData = formatItemData(data[field]);
39
- itemLabel = (_b = globalApp.get(field)) !== null && _b !== void 0 ? _b : field;
49
+ itemLabel = (_c = globalApp.get(field)) !== null && _c !== void 0 ? _c : field;
40
50
  }
41
51
  return [
42
52
  itemData,
@@ -50,7 +60,7 @@ function getItemField(field, data) {
50
60
  */
51
61
  export function ViewPage(props) {
52
62
  // Destruct
53
- const { children, fields, loadData, paddings = MUGlobal.pagePaddings, supportRefresh = true, ...rest } = props;
63
+ const { actions, children, fields, loadData, paddings = MUGlobal.pagePaddings, supportRefresh = true, ...rest } = props;
54
64
  // Data
55
65
  const [data, setData] = React.useState();
56
66
  // Load data
@@ -62,7 +72,9 @@ export function ViewPage(props) {
62
72
  };
63
73
  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,
64
74
  React.createElement(Grid, { container: true, justifyContent: "left", spacing: paddings, sx: {
65
- '.MuiTypography-subtitle2': { fontWeight: 'bold' }
75
+ '.MuiTypography-subtitle2': {
76
+ fontWeight: 'bold'
77
+ }
66
78
  } }, fields.map((field, index) => {
67
79
  // Get data
68
80
  const [itemData, itemLabel, gridProps] = getItemField(field, data);
@@ -75,5 +87,6 @@ export function ViewPage(props) {
75
87
  ":"),
76
88
  React.createElement(Typography, { variant: "subtitle2" }, itemData)));
77
89
  })),
78
- children))));
90
+ actions != null && (React.createElement(HBox, { paddingTop: paddings, paddingBottom: paddings }, actions)),
91
+ Utils.getResult(children, data)))));
79
92
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/react",
3
- "version": "1.4.63",
3
+ "version": "1.4.68",
4
4
  "description": "TypeScript ReactJs framework",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -53,8 +53,8 @@
53
53
  "@etsoo/appscript": "^1.2.27",
54
54
  "@etsoo/notificationbase": "^1.1.0",
55
55
  "@etsoo/shared": "^1.1.6",
56
- "@mui/icons-material": "^5.3.0",
57
- "@mui/material": "^5.3.0",
56
+ "@mui/icons-material": "^5.3.1",
57
+ "@mui/material": "^5.3.1",
58
58
  "@reach/router": "^1.3.4",
59
59
  "@types/pica": "^5.1.3",
60
60
  "@types/pulltorefreshjs": "^0.1.5",
@@ -83,8 +83,8 @@
83
83
  "@babel/runtime-corejs3": "^7.16.8",
84
84
  "@types/jest": "^27.4.0",
85
85
  "@types/react-test-renderer": "^17.0.1",
86
- "@typescript-eslint/eslint-plugin": "^5.10.0",
87
- "@typescript-eslint/parser": "^5.10.0",
86
+ "@typescript-eslint/eslint-plugin": "^5.10.1",
87
+ "@typescript-eslint/parser": "^5.10.1",
88
88
  "eslint": "^8.7.0",
89
89
  "eslint-config-airbnb-base": "^15.0.0",
90
90
  "eslint-plugin-import": "^2.25.4",
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,13 @@
1
+ import { Utils } from '@etsoo/shared';
1
2
  import { Grid, GridProps, LinearProgress, Typography } from '@mui/material';
2
3
  import React from 'react';
3
4
  import { globalApp } from '../../app/ReactApp';
5
+ import {
6
+ GridColumnRenderProps,
7
+ GridDataType
8
+ } from '../../components/GridColumn';
9
+ import { HBox } from '../FlexBox';
10
+ import { GridDataFormat } from '../GridDataFormat';
4
11
  import { MUGlobal } from '../MUGlobal';
5
12
  import { CommonPage } from './CommonPage';
6
13
  import { CommonPageProps } from './CommonPageProps';
@@ -14,6 +21,11 @@ export interface ViewPageField<T extends {}> extends GridProps {
14
21
  */
15
22
  data: keyof T | ((item: T) => React.ReactNode);
16
23
 
24
+ /**
25
+ * Data type
26
+ */
27
+ dataType?: GridDataType;
28
+
17
29
  /**
18
30
  * Label field
19
31
  */
@@ -23,14 +35,33 @@ export interface ViewPageField<T extends {}> extends GridProps {
23
35
  * Display as single row
24
36
  */
25
37
  singleRow?: boolean;
38
+
39
+ /**
40
+ * Render props
41
+ */
42
+ renderProps?: GridColumnRenderProps;
26
43
  }
27
44
 
28
- type ViewPageFieldType<T> = (string & keyof T) | ViewPageField<T>;
45
+ type ViewPageFieldType<T> =
46
+ | (string & keyof T)
47
+ | [string & keyof T, GridDataType, GridColumnRenderProps?]
48
+ | ViewPageField<T>;
29
49
 
30
50
  /**
31
51
  * View page props
32
52
  */
33
- export interface ViewPageProps<T extends {}> extends CommonPageProps {
53
+ export interface ViewPageProps<T extends {}>
54
+ extends Exclude<CommonPageProps, 'children'> {
55
+ /**
56
+ * Actions
57
+ */
58
+ actions?: (data: T) => React.ReactNode;
59
+
60
+ /**
61
+ * Children
62
+ */
63
+ children?: React.ReactNode | ((data: T) => React.ReactNode);
64
+
34
65
  /**
35
66
  * Fields to display
36
67
  */
@@ -63,11 +94,17 @@ function getItemField<T>(
63
94
  itemLabel: React.ReactNode,
64
95
  gridProps: GridProps = {};
65
96
 
66
- if (typeof field === 'object') {
97
+ if (Array.isArray(field)) {
98
+ const [fieldData, fieldType, renderProps] = field;
99
+ itemData = GridDataFormat(data[fieldData], fieldType, renderProps);
100
+ itemLabel = globalApp.get<string>(fieldData) ?? fieldData;
101
+ } else if (typeof field === 'object') {
67
102
  // Destruct
68
103
  const {
69
104
  data: fieldData,
105
+ dataType,
70
106
  label: fieldLabel,
107
+ renderProps,
71
108
  singleRow = false,
72
109
  ...rest
73
110
  } = field;
@@ -79,7 +116,8 @@ function getItemField<T>(
79
116
 
80
117
  // Field data
81
118
  if (typeof fieldData === 'function') itemData = fieldData(data);
82
- else itemData = formatItemData(data[fieldData]);
119
+ else if (dataType == null) itemData = formatItemData(data[fieldData]);
120
+ else itemData = GridDataFormat(data[fieldData], dataType, renderProps);
83
121
 
84
122
  // Field label
85
123
  itemLabel =
@@ -108,6 +146,7 @@ function getItemField<T>(
108
146
  export function ViewPage<T extends {}>(props: ViewPageProps<T>) {
109
147
  // Destruct
110
148
  const {
149
+ actions,
111
150
  children,
112
151
  fields,
113
152
  loadData,
@@ -143,7 +182,9 @@ export function ViewPage<T extends {}>(props: ViewPageProps<T>) {
143
182
  justifyContent="left"
144
183
  spacing={paddings}
145
184
  sx={{
146
- '.MuiTypography-subtitle2': { fontWeight: 'bold' }
185
+ '.MuiTypography-subtitle2': {
186
+ fontWeight: 'bold'
187
+ }
147
188
  }}
148
189
  >
149
190
  {fields.map((field, index) => {
@@ -169,7 +210,12 @@ export function ViewPage<T extends {}>(props: ViewPageProps<T>) {
169
210
  );
170
211
  })}
171
212
  </Grid>
172
- {children}
213
+ {actions != null && (
214
+ <HBox paddingTop={paddings} paddingBottom={paddings}>
215
+ {actions}
216
+ </HBox>
217
+ )}
218
+ {Utils.getResult(children, data)}
173
219
  </React.Fragment>
174
220
  )}
175
221
  </CommonPage>