@etsoo/materialui 1.3.67 → 1.3.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/custom/CustomFieldViewer.d.ts +50 -0
- package/lib/custom/CustomFieldViewer.js +54 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/package.json +1 -1
- package/src/custom/CustomFieldViewer.tsx +138 -0
- package/src/index.ts +1 -0
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { CustomFieldData } from "@etsoo/appscript";
|
|
2
|
+
import { GridProps, TypographyProps } from "@mui/material";
|
|
3
|
+
/**
|
|
4
|
+
* Custom field viewer props
|
|
5
|
+
* 自定义字段查看器属性
|
|
6
|
+
*/
|
|
7
|
+
export type CustomFieldViewerProps = {
|
|
8
|
+
/**
|
|
9
|
+
* Custom fields
|
|
10
|
+
* 自定义字段
|
|
11
|
+
*/
|
|
12
|
+
fields: CustomFieldData[];
|
|
13
|
+
/**
|
|
14
|
+
* Grid props
|
|
15
|
+
* 网格属性
|
|
16
|
+
*/
|
|
17
|
+
gridProps?: GridProps;
|
|
18
|
+
/**
|
|
19
|
+
* JSON data
|
|
20
|
+
* JSON 数据
|
|
21
|
+
*/
|
|
22
|
+
jsonData: unknown;
|
|
23
|
+
/**
|
|
24
|
+
* Title label props
|
|
25
|
+
* 标题标签属性
|
|
26
|
+
*/
|
|
27
|
+
titleProps?: TypographyProps;
|
|
28
|
+
/**
|
|
29
|
+
* Vertical gap
|
|
30
|
+
* 垂直间距
|
|
31
|
+
*/
|
|
32
|
+
verticalGap?: number;
|
|
33
|
+
/**
|
|
34
|
+
* Value label formatter
|
|
35
|
+
* 值标签格式化
|
|
36
|
+
*/
|
|
37
|
+
valueLabelFormatter?: (value: any, field: CustomFieldData) => string;
|
|
38
|
+
/**
|
|
39
|
+
* Value label props
|
|
40
|
+
* 值标签属性
|
|
41
|
+
*/
|
|
42
|
+
valueProps?: TypographyProps;
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* Custom field viewer
|
|
46
|
+
* 自定义字段查看器
|
|
47
|
+
* @param props Props
|
|
48
|
+
* @returns Component
|
|
49
|
+
*/
|
|
50
|
+
export declare function CustomFieldViewer(props: CustomFieldViewerProps): import("react/jsx-runtime").JSX.Element;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { Grid, Typography } from "@mui/material";
|
|
3
|
+
import { CustomFieldUtils } from "./CustomFieldUtils";
|
|
4
|
+
import { MUGlobal } from "../MUGlobal";
|
|
5
|
+
import { VBox } from "../FlexBox";
|
|
6
|
+
import { DataTypes, Utils } from "@etsoo/shared";
|
|
7
|
+
/**
|
|
8
|
+
* Custom field viewer
|
|
9
|
+
* 自定义字段查看器
|
|
10
|
+
* @param props Props
|
|
11
|
+
* @returns Component
|
|
12
|
+
*/
|
|
13
|
+
export function CustomFieldViewer(props) {
|
|
14
|
+
// Destruct
|
|
15
|
+
const { fields, gridProps, jsonData, titleProps, verticalGap = 0.5, valueLabelFormatter = (value, field) => {
|
|
16
|
+
if (value == null)
|
|
17
|
+
return "";
|
|
18
|
+
if (field.options) {
|
|
19
|
+
const option = field.options.find((o) => o.id === value);
|
|
20
|
+
if (option) {
|
|
21
|
+
return DataTypes.getListItemLabel(option);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
if (typeof value === "object") {
|
|
25
|
+
if (value instanceof Date) {
|
|
26
|
+
return value.toLocaleString();
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
return JSON.stringify(value);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
return `${value}`;
|
|
34
|
+
}
|
|
35
|
+
}, valueProps } = props;
|
|
36
|
+
const spacing = MUGlobal.half(MUGlobal.pagePaddings);
|
|
37
|
+
const data = typeof jsonData === "string" ? JSON.parse(jsonData) : jsonData;
|
|
38
|
+
if (data == null || typeof data !== "object" || Array.isArray(data)) {
|
|
39
|
+
throw new Error("Invalid JSON data");
|
|
40
|
+
}
|
|
41
|
+
return (_jsx(Grid, { container: true, justifyContent: "left", spacing: spacing, sx: {
|
|
42
|
+
".MuiTypography-subtitle2": {
|
|
43
|
+
fontWeight: "bold"
|
|
44
|
+
}
|
|
45
|
+
}, ...gridProps, children: fields.map((field, index) => {
|
|
46
|
+
// Field name
|
|
47
|
+
const name = field.name;
|
|
48
|
+
if (!name)
|
|
49
|
+
return;
|
|
50
|
+
// Field value
|
|
51
|
+
const value = Utils.getNestedValue(data, name);
|
|
52
|
+
return (_jsx(Grid, { item: true, ...field.gridItemProps, ...CustomFieldUtils.transformSpace(field.space), children: _jsxs(VBox, { gap: verticalGap, children: [_jsx(Typography, { fontWeight: "bold", fontSize: "small", ...titleProps, children: field.label ?? name }), _jsx(Typography, { ...valueProps, children: valueLabelFormatter(value, field) })] }) }, name ?? index));
|
|
53
|
+
}) }));
|
|
54
|
+
}
|
package/lib/index.d.ts
CHANGED
|
@@ -9,6 +9,7 @@ export * from "./app/Labels";
|
|
|
9
9
|
export * from "./app/ReactApp";
|
|
10
10
|
export * from "./app/ServiceApp";
|
|
11
11
|
export * from "./custom/CustomFieldUtils";
|
|
12
|
+
export * from "./custom/CustomFieldViewer";
|
|
12
13
|
export * from "./custom/CustomFieldWindow";
|
|
13
14
|
export * from "./messages/MessageUtils";
|
|
14
15
|
export * from "./messages/OperationMessageContainer";
|
package/lib/index.js
CHANGED
|
@@ -9,6 +9,7 @@ export * from "./app/Labels";
|
|
|
9
9
|
export * from "./app/ReactApp";
|
|
10
10
|
export * from "./app/ServiceApp";
|
|
11
11
|
export * from "./custom/CustomFieldUtils";
|
|
12
|
+
export * from "./custom/CustomFieldViewer";
|
|
12
13
|
export * from "./custom/CustomFieldWindow";
|
|
13
14
|
export * from "./messages/MessageUtils";
|
|
14
15
|
export * from "./messages/OperationMessageContainer";
|
package/package.json
CHANGED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { CustomFieldData } from "@etsoo/appscript";
|
|
2
|
+
import { Grid, GridProps, Typography, TypographyProps } from "@mui/material";
|
|
3
|
+
import { CustomFieldUtils } from "./CustomFieldUtils";
|
|
4
|
+
import { MUGlobal } from "../MUGlobal";
|
|
5
|
+
import { VBox } from "../FlexBox";
|
|
6
|
+
import { DataTypes, Utils } from "@etsoo/shared";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Custom field viewer props
|
|
10
|
+
* 自定义字段查看器属性
|
|
11
|
+
*/
|
|
12
|
+
export type CustomFieldViewerProps = {
|
|
13
|
+
/**
|
|
14
|
+
* Custom fields
|
|
15
|
+
* 自定义字段
|
|
16
|
+
*/
|
|
17
|
+
fields: CustomFieldData[];
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Grid props
|
|
21
|
+
* 网格属性
|
|
22
|
+
*/
|
|
23
|
+
gridProps?: GridProps;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* JSON data
|
|
27
|
+
* JSON 数据
|
|
28
|
+
*/
|
|
29
|
+
jsonData: unknown;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Title label props
|
|
33
|
+
* 标题标签属性
|
|
34
|
+
*/
|
|
35
|
+
titleProps?: TypographyProps;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Vertical gap
|
|
39
|
+
* 垂直间距
|
|
40
|
+
*/
|
|
41
|
+
verticalGap?: number;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Value label formatter
|
|
45
|
+
* 值标签格式化
|
|
46
|
+
*/
|
|
47
|
+
valueLabelFormatter?: (value: any, field: CustomFieldData) => string;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Value label props
|
|
51
|
+
* 值标签属性
|
|
52
|
+
*/
|
|
53
|
+
valueProps?: TypographyProps;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Custom field viewer
|
|
58
|
+
* 自定义字段查看器
|
|
59
|
+
* @param props Props
|
|
60
|
+
* @returns Component
|
|
61
|
+
*/
|
|
62
|
+
export function CustomFieldViewer(props: CustomFieldViewerProps) {
|
|
63
|
+
// Destruct
|
|
64
|
+
const {
|
|
65
|
+
fields,
|
|
66
|
+
gridProps,
|
|
67
|
+
jsonData,
|
|
68
|
+
titleProps,
|
|
69
|
+
verticalGap = 0.5,
|
|
70
|
+
valueLabelFormatter = (value, field) => {
|
|
71
|
+
if (value == null) return "";
|
|
72
|
+
if (field.options) {
|
|
73
|
+
const option = field.options.find((o) => o.id === value);
|
|
74
|
+
if (option) {
|
|
75
|
+
return DataTypes.getListItemLabel(option);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (typeof value === "object") {
|
|
80
|
+
if (value instanceof Date) {
|
|
81
|
+
return value.toLocaleString();
|
|
82
|
+
} else {
|
|
83
|
+
return JSON.stringify(value);
|
|
84
|
+
}
|
|
85
|
+
} else {
|
|
86
|
+
return `${value}`;
|
|
87
|
+
}
|
|
88
|
+
},
|
|
89
|
+
valueProps
|
|
90
|
+
} = props;
|
|
91
|
+
|
|
92
|
+
const spacing = MUGlobal.half(MUGlobal.pagePaddings);
|
|
93
|
+
const data = typeof jsonData === "string" ? JSON.parse(jsonData) : jsonData;
|
|
94
|
+
if (data == null || typeof data !== "object" || Array.isArray(data)) {
|
|
95
|
+
throw new Error("Invalid JSON data");
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return (
|
|
99
|
+
<Grid
|
|
100
|
+
container
|
|
101
|
+
justifyContent="left"
|
|
102
|
+
spacing={spacing}
|
|
103
|
+
sx={{
|
|
104
|
+
".MuiTypography-subtitle2": {
|
|
105
|
+
fontWeight: "bold"
|
|
106
|
+
}
|
|
107
|
+
}}
|
|
108
|
+
{...gridProps}
|
|
109
|
+
>
|
|
110
|
+
{fields.map((field, index) => {
|
|
111
|
+
// Field name
|
|
112
|
+
const name = field.name;
|
|
113
|
+
if (!name) return;
|
|
114
|
+
|
|
115
|
+
// Field value
|
|
116
|
+
const value = Utils.getNestedValue(data, name);
|
|
117
|
+
|
|
118
|
+
return (
|
|
119
|
+
<Grid
|
|
120
|
+
item
|
|
121
|
+
key={name ?? index}
|
|
122
|
+
{...field.gridItemProps}
|
|
123
|
+
{...CustomFieldUtils.transformSpace(field.space)}
|
|
124
|
+
>
|
|
125
|
+
<VBox gap={verticalGap}>
|
|
126
|
+
<Typography fontWeight="bold" fontSize="small" {...titleProps}>
|
|
127
|
+
{field.label ?? name}
|
|
128
|
+
</Typography>
|
|
129
|
+
<Typography {...valueProps}>
|
|
130
|
+
{valueLabelFormatter(value, field)}
|
|
131
|
+
</Typography>
|
|
132
|
+
</VBox>
|
|
133
|
+
</Grid>
|
|
134
|
+
);
|
|
135
|
+
})}
|
|
136
|
+
</Grid>
|
|
137
|
+
);
|
|
138
|
+
}
|
package/src/index.ts
CHANGED