@etsoo/react 1.4.58 → 1.4.59

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.
@@ -8,6 +8,10 @@ import { IStateProps } from '../states/IState';
8
8
  import { IPageData, PageAction, PageState } from '../states/PageState';
9
9
  import { UserAction, UserState } from '../states/UserState';
10
10
  import { InputDialogProps } from './InputDialogProps';
11
+ /**
12
+ * Global application
13
+ */
14
+ export declare let globalApp: IReactApp<IAppSettings, any, IPageData>;
11
15
  /**
12
16
  * React app state detector
13
17
  * Case 1: undefined, when refresh the whole page
@@ -7,8 +7,10 @@ import { CultureState } from '../states/CultureState';
7
7
  import { PageActionType, PageState } from '../states/PageState';
8
8
  import { UserActionType, UserState } from '../states/UserState';
9
9
  import { Labels } from './Labels';
10
- // Global application
11
- let globalApp;
10
+ /**
11
+ * Global application
12
+ */
13
+ export let globalApp;
12
14
  /**
13
15
  * React app state detector
14
16
  * Case 1: undefined, when refresh the whole page
@@ -1,12 +1,40 @@
1
- /// <reference types="react" />
1
+ import { GridProps } from '@mui/material';
2
+ import React from 'react';
2
3
  import { CommonPageProps } from './CommonPageProps';
4
+ /**
5
+ * View page display field
6
+ */
7
+ export interface ViewPageField<T extends {}> extends GridProps {
8
+ /**
9
+ * Data field
10
+ */
11
+ data: keyof T | ((item: T) => React.ReactNode);
12
+ /**
13
+ * Label field
14
+ */
15
+ label: string | (() => React.ReactNode);
16
+ }
17
+ declare type ViewPageFieldType<T> = (string & keyof T) | ViewPageField<T>;
3
18
  /**
4
19
  * View page props
5
20
  */
6
- export interface ViewPageProps extends CommonPageProps {
21
+ export interface ViewPageProps<T extends {}> extends CommonPageProps {
22
+ /**
23
+ * Fields to display
24
+ */
25
+ fields: ViewPageFieldType<T>[];
26
+ /**
27
+ * Load data
28
+ */
29
+ loadData: () => PromiseLike<T | undefined>;
30
+ /**
31
+ * Support refresh
32
+ */
33
+ supportRefresh?: boolean;
7
34
  }
8
35
  /**
9
36
  * View page
10
37
  * @param props Props
11
38
  */
12
- export declare function ViewPage(props: ViewPageProps): JSX.Element;
39
+ export declare function ViewPage<T extends {}>(props: ViewPageProps<T>): JSX.Element;
40
+ export {};
@@ -1,11 +1,64 @@
1
+ import { Grid, LinearProgress, Typography } from '@mui/material';
1
2
  import React from 'react';
3
+ import { globalApp } from '../../app/ReactApp';
2
4
  import { CommonPage } from './CommonPage';
5
+ function formatItemData(fieldData) {
6
+ if (fieldData instanceof Date)
7
+ return globalApp.formatDate(fieldData, 'd');
8
+ return new String(fieldData);
9
+ }
10
+ function getItemField(field, data) {
11
+ var _a, _b;
12
+ // Item data and label
13
+ let itemData, itemLabel, gridProps;
14
+ if (typeof field === 'object') {
15
+ // Destruct
16
+ const { data: fieldData, label: fieldLabel, ...rest } = field;
17
+ gridProps = rest;
18
+ // Field data
19
+ if (typeof fieldData === 'function')
20
+ itemData = fieldData(data);
21
+ else
22
+ itemData = formatItemData(formatItemData);
23
+ // Field label
24
+ itemLabel =
25
+ typeof fieldLabel === 'function'
26
+ ? fieldLabel()
27
+ : (_a = globalApp.get(fieldLabel)) !== null && _a !== void 0 ? _a : fieldLabel;
28
+ }
29
+ else {
30
+ itemData = formatItemData(data[field]);
31
+ itemLabel = (_b = globalApp.get(field)) !== null && _b !== void 0 ? _b : field;
32
+ gridProps = { xs: 12, sm: 6 };
33
+ }
34
+ return [itemData, itemLabel, gridProps];
35
+ }
3
36
  /**
4
37
  * View page
5
38
  * @param props Props
6
39
  */
7
40
  export function ViewPage(props) {
8
41
  // Destruct
9
- const { ...rest } = props;
10
- return React.createElement(CommonPage, { ...rest, scrollContainer: global });
42
+ const { children, fields, loadData, paddings, supportRefresh = true, ...rest } = props;
43
+ // Data
44
+ const [data, setData] = React.useState();
45
+ // Load data
46
+ const onRefresh = async () => {
47
+ const result = await loadData();
48
+ if (result == null)
49
+ return;
50
+ setData(result);
51
+ };
52
+ 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,
53
+ React.createElement(Grid, { container: true, justifyContent: "left", spacing: paddings }, fields.map((field) => {
54
+ // Get data
55
+ const [itemData, itemLabel, gridProps] = getItemField(field, data);
56
+ // Layout
57
+ return (React.createElement(Grid, { item: true, ...gridProps },
58
+ React.createElement(Typography, { variant: "caption", component: "div" },
59
+ itemLabel,
60
+ ":"),
61
+ React.createElement(Typography, { variant: "subtitle2" }, itemData)));
62
+ })),
63
+ children))));
11
64
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/react",
3
- "version": "1.4.58",
3
+ "version": "1.4.59",
4
4
  "description": "TypeScript ReactJs framework",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -77,7 +77,7 @@
77
77
  },
78
78
  "devDependencies": {
79
79
  "@babel/cli": "^7.16.8",
80
- "@babel/core": "^7.16.10",
80
+ "@babel/core": "^7.16.12",
81
81
  "@babel/plugin-transform-runtime": "^7.16.10",
82
82
  "@babel/preset-env": "^7.16.11",
83
83
  "@babel/runtime-corejs3": "^7.16.8",
@@ -34,8 +34,10 @@ import {
34
34
  import { InputDialogProps } from './InputDialogProps';
35
35
  import { Labels } from './Labels';
36
36
 
37
- // Global application
38
- let globalApp: IReactApp<IAppSettings, any, IPageData>;
37
+ /**
38
+ * Global application
39
+ */
40
+ export let globalApp: IReactApp<IAppSettings, any, IPageData>;
39
41
 
40
42
  /**
41
43
  * React app state detector
@@ -1,19 +1,145 @@
1
+ import { Grid, GridProps, LinearProgress, Typography } from '@mui/material';
1
2
  import React from 'react';
3
+ import { globalApp } from '../../app/ReactApp';
2
4
  import { CommonPage } from './CommonPage';
3
5
  import { CommonPageProps } from './CommonPageProps';
4
6
 
7
+ /**
8
+ * View page display field
9
+ */
10
+ export interface ViewPageField<T extends {}> extends GridProps {
11
+ /**
12
+ * Data field
13
+ */
14
+ data: keyof T | ((item: T) => React.ReactNode);
15
+
16
+ /**
17
+ * Label field
18
+ */
19
+ label: string | (() => React.ReactNode);
20
+ }
21
+
22
+ type ViewPageFieldType<T> = (string & keyof T) | ViewPageField<T>;
23
+
5
24
  /**
6
25
  * View page props
7
26
  */
8
- export interface ViewPageProps extends CommonPageProps {}
27
+ export interface ViewPageProps<T extends {}> extends CommonPageProps {
28
+ /**
29
+ * Fields to display
30
+ */
31
+ fields: ViewPageFieldType<T>[];
32
+
33
+ /**
34
+ * Load data
35
+ */
36
+ loadData: () => PromiseLike<T | undefined>;
37
+
38
+ /**
39
+ * Support refresh
40
+ */
41
+ supportRefresh?: boolean;
42
+ }
43
+
44
+ function formatItemData(fieldData: unknown) {
45
+ if (fieldData instanceof Date) return globalApp.formatDate(fieldData, 'd');
46
+ return new String(fieldData);
47
+ }
48
+
49
+ function getItemField<T>(
50
+ field: ViewPageFieldType<T>,
51
+ data: T
52
+ ): [React.ReactNode, React.ReactNode, GridProps] {
53
+ // Item data and label
54
+ let itemData: React.ReactNode,
55
+ itemLabel: React.ReactNode,
56
+ gridProps: GridProps;
57
+
58
+ if (typeof field === 'object') {
59
+ // Destruct
60
+ const { data: fieldData, label: fieldLabel, ...rest } = field;
61
+ gridProps = rest;
62
+
63
+ // Field data
64
+ if (typeof fieldData === 'function') itemData = fieldData(data);
65
+ else itemData = formatItemData(formatItemData);
66
+
67
+ // Field label
68
+ itemLabel =
69
+ typeof fieldLabel === 'function'
70
+ ? fieldLabel()
71
+ : globalApp.get<string>(fieldLabel) ?? fieldLabel;
72
+ } else {
73
+ itemData = formatItemData(data[field]);
74
+ itemLabel = globalApp.get<string>(field) ?? field;
75
+ gridProps = { xs: 12, sm: 6 };
76
+ }
77
+
78
+ return [itemData, itemLabel, gridProps];
79
+ }
9
80
 
10
81
  /**
11
82
  * View page
12
83
  * @param props Props
13
84
  */
14
- export function ViewPage(props: ViewPageProps) {
85
+ export function ViewPage<T extends {}>(props: ViewPageProps<T>) {
15
86
  // Destruct
16
- const { ...rest } = props;
87
+ const {
88
+ children,
89
+ fields,
90
+ loadData,
91
+ paddings,
92
+ supportRefresh = true,
93
+ ...rest
94
+ } = props;
95
+
96
+ // Data
97
+ const [data, setData] = React.useState<T>();
98
+
99
+ // Load data
100
+ const onRefresh = async () => {
101
+ const result = await loadData();
102
+ if (result == null) return;
103
+ setData(result);
104
+ };
105
+
106
+ return (
107
+ <CommonPage
108
+ paddings={paddings}
109
+ onRefresh={supportRefresh ? onRefresh : undefined}
110
+ onUpdate={supportRefresh ? undefined : onRefresh}
111
+ {...rest}
112
+ scrollContainer={global}
113
+ >
114
+ {data == null ? (
115
+ <LinearProgress />
116
+ ) : (
117
+ <React.Fragment>
118
+ <Grid container justifyContent="left" spacing={paddings}>
119
+ {fields.map((field) => {
120
+ // Get data
121
+ const [itemData, itemLabel, gridProps] =
122
+ getItemField(field, data);
17
123
 
18
- return <CommonPage {...rest} scrollContainer={global}></CommonPage>;
124
+ // Layout
125
+ return (
126
+ <Grid item {...gridProps}>
127
+ <Typography
128
+ variant="caption"
129
+ component="div"
130
+ >
131
+ {itemLabel}:
132
+ </Typography>
133
+ <Typography variant="subtitle2">
134
+ {itemData}
135
+ </Typography>
136
+ </Grid>
137
+ );
138
+ })}
139
+ </Grid>
140
+ {children}
141
+ </React.Fragment>
142
+ )}
143
+ </CommonPage>
144
+ );
19
145
  }