@etsoo/materialui 1.3.63 → 1.3.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/__tests__/CustomFields.tsx +93 -0
- package/lib/custom/CustomFieldUtils.d.ts +50 -0
- package/lib/custom/CustomFieldUtils.js +138 -0
- package/lib/custom/FieldAmountLabel.d.ts +7 -0
- package/lib/custom/FieldAmountLabel.js +40 -0
- package/lib/custom/FieldCheckbox.d.ts +8 -0
- package/lib/custom/FieldCheckbox.js +49 -0
- package/lib/custom/FieldCombobox.d.ts +8 -0
- package/lib/custom/FieldCombobox.js +40 -0
- package/lib/custom/FieldDateInput.d.ts +7 -0
- package/lib/custom/FieldDateInput.js +43 -0
- package/lib/custom/FieldDivider.d.ts +7 -0
- package/lib/custom/FieldDivider.js +12 -0
- package/lib/custom/FieldInput.d.ts +7 -0
- package/lib/custom/FieldInput.js +27 -0
- package/lib/custom/FieldJson.d.ts +7 -0
- package/lib/custom/FieldJson.js +50 -0
- package/lib/custom/FieldLabel.d.ts +7 -0
- package/lib/custom/FieldLabel.js +28 -0
- package/lib/custom/FieldNumberInput.d.ts +7 -0
- package/lib/custom/FieldNumberInput.js +37 -0
- package/lib/custom/FieldRadio.d.ts +8 -0
- package/lib/custom/FieldRadio.js +49 -0
- package/lib/custom/FieldSelect.d.ts +8 -0
- package/lib/custom/FieldSelect.js +48 -0
- package/lib/custom/FieldSwitch.d.ts +7 -0
- package/lib/custom/FieldSwitch.js +42 -0
- package/lib/custom/FieldTexarea.d.ts +7 -0
- package/lib/custom/FieldTexarea.js +28 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/package.json +14 -14
- package/src/ShowDataComparison.tsx +0 -1
- package/src/custom/CustomFieldUtils.tsx +195 -0
- package/src/custom/FieldAmountLabel.tsx +62 -0
- package/src/custom/FieldCheckbox.tsx +74 -0
- package/src/custom/FieldCombobox.tsx +63 -0
- package/src/custom/FieldDateInput.tsx +75 -0
- package/src/custom/FieldDivider.tsx +18 -0
- package/src/custom/FieldInput.tsx +49 -0
- package/src/custom/FieldJson.tsx +77 -0
- package/src/custom/FieldLabel.tsx +40 -0
- package/src/custom/FieldNumberInput.tsx +60 -0
- package/src/custom/FieldRadio.tsx +73 -0
- package/src/custom/FieldSelect.tsx +70 -0
- package/src/custom/FieldSwitch.tsx +58 -0
- package/src/custom/FieldTexarea.tsx +54 -0
- package/src/index.ts +2 -0
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { act, getByText, render } from "@testing-library/react";
|
|
3
|
+
import { CustomFieldData } from "@etsoo/appscript";
|
|
4
|
+
import { CustomFieldUtils } from "../src/custom/CustomFieldUtils";
|
|
5
|
+
import { CustomFieldReactCollection } from "@etsoo/react";
|
|
6
|
+
import { NumberUtils } from "@etsoo/shared";
|
|
7
|
+
|
|
8
|
+
it("Render FieldAmountLabel", () => {
|
|
9
|
+
// Arrange
|
|
10
|
+
const fields: CustomFieldData[] = [
|
|
11
|
+
{
|
|
12
|
+
type: "amountlabel",
|
|
13
|
+
mainSlotProps: { currency: "$CURRENCY$" }
|
|
14
|
+
}
|
|
15
|
+
];
|
|
16
|
+
|
|
17
|
+
const amount = 100;
|
|
18
|
+
|
|
19
|
+
const collections: CustomFieldReactCollection<CustomFieldData> = {};
|
|
20
|
+
|
|
21
|
+
const { baseElement } = render(
|
|
22
|
+
<div>
|
|
23
|
+
{CustomFieldUtils.create(
|
|
24
|
+
fields,
|
|
25
|
+
collections,
|
|
26
|
+
(field) => {
|
|
27
|
+
switch (field.type) {
|
|
28
|
+
case "amountlabel":
|
|
29
|
+
return amount;
|
|
30
|
+
default:
|
|
31
|
+
return undefined;
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
(name, value) => {},
|
|
35
|
+
(input) => input.replace("$CURRENCY$", "USD")
|
|
36
|
+
)}
|
|
37
|
+
</div>
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
getByText(baseElement, `$ ${NumberUtils.formatMoney(amount)}`);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("Render FieldCheckbox", () => {
|
|
44
|
+
// Arrange
|
|
45
|
+
const fields: CustomFieldData[] = [
|
|
46
|
+
{
|
|
47
|
+
type: "checkbox",
|
|
48
|
+
name: "checkbox",
|
|
49
|
+
options: [
|
|
50
|
+
{ id: "a", name: "A" },
|
|
51
|
+
{ id: "b", name: "B" },
|
|
52
|
+
{ id: "c", name: "C" }
|
|
53
|
+
]
|
|
54
|
+
}
|
|
55
|
+
];
|
|
56
|
+
|
|
57
|
+
const collections: CustomFieldReactCollection<CustomFieldData> = {};
|
|
58
|
+
|
|
59
|
+
act(() => {
|
|
60
|
+
render(
|
|
61
|
+
<div>
|
|
62
|
+
{CustomFieldUtils.create(
|
|
63
|
+
fields,
|
|
64
|
+
collections,
|
|
65
|
+
(field) => {
|
|
66
|
+
switch (field.type) {
|
|
67
|
+
case "checkbox":
|
|
68
|
+
return ["a", "b"];
|
|
69
|
+
default:
|
|
70
|
+
return undefined;
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
(name, value) => {
|
|
74
|
+
expect(name).toBe("checkbox");
|
|
75
|
+
expect(value).toStrictEqual(["a"]);
|
|
76
|
+
}
|
|
77
|
+
)}
|
|
78
|
+
</div>
|
|
79
|
+
);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
const checkboxItems = document.querySelectorAll<HTMLInputElement>(
|
|
83
|
+
"input[type=checkbox]"
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
expect(checkboxItems.length).toBe(3);
|
|
87
|
+
expect(checkboxItems[0].checked).toBeTruthy();
|
|
88
|
+
expect(checkboxItems[2].checked).toBeFalsy();
|
|
89
|
+
|
|
90
|
+
act(() => {
|
|
91
|
+
checkboxItems[1].click();
|
|
92
|
+
});
|
|
93
|
+
});
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { CustomFieldData, CustomFieldSpace } from "@etsoo/appscript";
|
|
2
|
+
import { CustomFieldReactCollection, ICustomFieldReact } from "@etsoo/react";
|
|
3
|
+
import { IdType, ListType2 } from "@etsoo/shared";
|
|
4
|
+
import { GridProps } from "@mui/material";
|
|
5
|
+
import { TypographyProps } from "@mui/material/Typography";
|
|
6
|
+
/**
|
|
7
|
+
* Custom field utilities
|
|
8
|
+
*/
|
|
9
|
+
export declare namespace CustomFieldUtils {
|
|
10
|
+
/**
|
|
11
|
+
* Custom field creators
|
|
12
|
+
*/
|
|
13
|
+
const customFieldCreators: Record<string, ICustomFieldReact<any> | undefined>;
|
|
14
|
+
/**
|
|
15
|
+
* Create layout
|
|
16
|
+
* @param fields Fields
|
|
17
|
+
* @param collections Ref collections
|
|
18
|
+
* @param getValue Get default value callback
|
|
19
|
+
* @param onChange Callback for value change
|
|
20
|
+
* @param globalCallback Global label callback, can be repeated
|
|
21
|
+
* @param fieldCalback Field callback
|
|
22
|
+
* @returns
|
|
23
|
+
*/
|
|
24
|
+
function create<D extends CustomFieldData = CustomFieldData>(fields: D[], collections: CustomFieldReactCollection<D>, getValue: (field: D) => unknown, onChange: (name: string, value: unknown) => void, globalCallback?: (input: string) => string, fieldCalback?: (field: D) => void): import("react/jsx-runtime").JSX.Element[];
|
|
25
|
+
/**
|
|
26
|
+
* Create multiline label
|
|
27
|
+
* @param label Original label
|
|
28
|
+
* @param props Properties
|
|
29
|
+
* @returns Result
|
|
30
|
+
*/
|
|
31
|
+
function createMultilineLabel(label?: string, props?: TypographyProps): import("react/jsx-runtime").JSX.Element[] | undefined;
|
|
32
|
+
/**
|
|
33
|
+
* Transform custom field space
|
|
34
|
+
* @param space Space
|
|
35
|
+
* @returns Result
|
|
36
|
+
*/
|
|
37
|
+
function transformSpace(space?: CustomFieldSpace): GridProps;
|
|
38
|
+
/**
|
|
39
|
+
* Update ref options
|
|
40
|
+
* @param fields Fields
|
|
41
|
+
* @param callback Callback
|
|
42
|
+
*/
|
|
43
|
+
function updateOptions(fields: CustomFieldData[], callback: (key: string, ids: IdType[]) => ListType2[]): void;
|
|
44
|
+
/**
|
|
45
|
+
* Update properties
|
|
46
|
+
* @param input Input object
|
|
47
|
+
* @param globalCallback Global callback
|
|
48
|
+
*/
|
|
49
|
+
function updateProperties(input: object, globalCallback: (input: string) => string): void;
|
|
50
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { Grid, Typography } from "@mui/material";
|
|
3
|
+
import React from "react";
|
|
4
|
+
import { FieldCheckbox } from "./FieldCheckbox";
|
|
5
|
+
import { FieldAmountLabel } from "./FieldAmountLabel";
|
|
6
|
+
import { FieldDateInput } from "./FieldDateInput";
|
|
7
|
+
import { FieldDivider } from "./FieldDivider";
|
|
8
|
+
import { FieldCombobox } from "./FieldCombobox";
|
|
9
|
+
import { FieldInput } from "./FieldInput";
|
|
10
|
+
import { FieldLabel } from "./FieldLabel";
|
|
11
|
+
import { FieldNumberInput } from "./FieldNumberInput";
|
|
12
|
+
import { FieldTexarea } from "./FieldTexarea";
|
|
13
|
+
/**
|
|
14
|
+
* Custom field utilities
|
|
15
|
+
*/
|
|
16
|
+
export var CustomFieldUtils;
|
|
17
|
+
(function (CustomFieldUtils) {
|
|
18
|
+
/**
|
|
19
|
+
* Custom field creators
|
|
20
|
+
*/
|
|
21
|
+
CustomFieldUtils.customFieldCreators = {
|
|
22
|
+
amountlabel: FieldAmountLabel,
|
|
23
|
+
checkbox: FieldCheckbox,
|
|
24
|
+
combobox: FieldCombobox,
|
|
25
|
+
date: FieldDateInput,
|
|
26
|
+
divider: FieldDivider,
|
|
27
|
+
input: FieldInput,
|
|
28
|
+
label: FieldLabel,
|
|
29
|
+
number: FieldNumberInput,
|
|
30
|
+
textarea: FieldTexarea
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Create layout
|
|
34
|
+
* @param fields Fields
|
|
35
|
+
* @param collections Ref collections
|
|
36
|
+
* @param getValue Get default value callback
|
|
37
|
+
* @param onChange Callback for value change
|
|
38
|
+
* @param globalCallback Global label callback, can be repeated
|
|
39
|
+
* @param fieldCalback Field callback
|
|
40
|
+
* @returns
|
|
41
|
+
*/
|
|
42
|
+
function create(fields, collections, getValue, onChange, globalCallback, fieldCalback) {
|
|
43
|
+
return fields.map((field, index) => {
|
|
44
|
+
// Global callback for labels
|
|
45
|
+
if (globalCallback) {
|
|
46
|
+
if (field.label)
|
|
47
|
+
field.label = globalCallback(field.label);
|
|
48
|
+
if (field.helperText)
|
|
49
|
+
field.helperText = globalCallback(field.helperText);
|
|
50
|
+
if (field.mainSlotProps)
|
|
51
|
+
updateProperties(field.mainSlotProps, globalCallback);
|
|
52
|
+
}
|
|
53
|
+
// Field callback for each field
|
|
54
|
+
if (fieldCalback)
|
|
55
|
+
fieldCalback(field);
|
|
56
|
+
const creator = CustomFieldUtils.customFieldCreators[field.type];
|
|
57
|
+
if (creator == null) {
|
|
58
|
+
return (_jsx(Grid, { item: true, ...field.gridItemProps, ...transformSpace(field.space), children: `Type ${field.type} is not supported currently` }, index));
|
|
59
|
+
}
|
|
60
|
+
const Creator = creator;
|
|
61
|
+
const mref = React.createRef();
|
|
62
|
+
let ui = (_jsx(Creator, { field: field, mref: mref, onChange: onChange, defaultValue: getValue(field) }));
|
|
63
|
+
const name = field.name;
|
|
64
|
+
if (name) {
|
|
65
|
+
if (collections[name] == null) {
|
|
66
|
+
collections[name] = [mref, field];
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
ui = `Duplicate custom field ${name}`;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return (_jsx(Grid, { item: true, ...field.gridItemProps, ...transformSpace(field.space), children: ui }, name ?? index));
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
CustomFieldUtils.create = create;
|
|
76
|
+
/**
|
|
77
|
+
* Create multiline label
|
|
78
|
+
* @param label Original label
|
|
79
|
+
* @param props Properties
|
|
80
|
+
* @returns Result
|
|
81
|
+
*/
|
|
82
|
+
function createMultilineLabel(label, props) {
|
|
83
|
+
return label?.split("\n").map((line, index) => (_jsx(Typography, { component: "div", ...props, children: line }, index)));
|
|
84
|
+
}
|
|
85
|
+
CustomFieldUtils.createMultilineLabel = createMultilineLabel;
|
|
86
|
+
/**
|
|
87
|
+
* Transform custom field space
|
|
88
|
+
* @param space Space
|
|
89
|
+
* @returns Result
|
|
90
|
+
*/
|
|
91
|
+
function transformSpace(space) {
|
|
92
|
+
const props = space === "full"
|
|
93
|
+
? { xs: 12 }
|
|
94
|
+
: space === "quater"
|
|
95
|
+
? { sm: 12, md: 6, lg: 3 }
|
|
96
|
+
: space === "five"
|
|
97
|
+
? { sm: 12, md: 5 }
|
|
98
|
+
: space === "seven"
|
|
99
|
+
? { sm: 12, md: 7 }
|
|
100
|
+
: space === "half1"
|
|
101
|
+
? { xs: 12, sm: 6 }
|
|
102
|
+
: { sm: 12, md: 6 };
|
|
103
|
+
return props;
|
|
104
|
+
}
|
|
105
|
+
CustomFieldUtils.transformSpace = transformSpace;
|
|
106
|
+
/**
|
|
107
|
+
* Update ref options
|
|
108
|
+
* @param fields Fields
|
|
109
|
+
* @param callback Callback
|
|
110
|
+
*/
|
|
111
|
+
function updateOptions(fields, callback) {
|
|
112
|
+
fields.forEach((field) => {
|
|
113
|
+
if (field.refs == null || field.refs.length < 2)
|
|
114
|
+
return;
|
|
115
|
+
const key = field.refs[0];
|
|
116
|
+
const ids = field.refs.slice(1);
|
|
117
|
+
field.options = callback(key, ids);
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
CustomFieldUtils.updateOptions = updateOptions;
|
|
121
|
+
/**
|
|
122
|
+
* Update properties
|
|
123
|
+
* @param input Input object
|
|
124
|
+
* @param globalCallback Global callback
|
|
125
|
+
*/
|
|
126
|
+
function updateProperties(input, globalCallback) {
|
|
127
|
+
for (const key in input) {
|
|
128
|
+
const value = Reflect.get(input, key);
|
|
129
|
+
if (typeof value === "string") {
|
|
130
|
+
Reflect.set(input, key, globalCallback(value));
|
|
131
|
+
}
|
|
132
|
+
else if (typeof value === "object" && value != null) {
|
|
133
|
+
updateProperties(value, globalCallback);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
CustomFieldUtils.updateProperties = updateProperties;
|
|
138
|
+
})(CustomFieldUtils || (CustomFieldUtils = {}));
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { jsxs as _jsxs, jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { NumberUtils } from "@etsoo/shared";
|
|
3
|
+
import { FormHelperText, Typography } from "@mui/material";
|
|
4
|
+
import React from "react";
|
|
5
|
+
/**
|
|
6
|
+
* Amount label field creator
|
|
7
|
+
* type: amountlabel
|
|
8
|
+
* @returns Component
|
|
9
|
+
*/
|
|
10
|
+
export const FieldAmountLabel = ({ field, mref, defaultValue }) => {
|
|
11
|
+
// Destruct
|
|
12
|
+
const { label, mainSlotProps = {}, helperText } = field;
|
|
13
|
+
const { currency, ...rest } = mainSlotProps;
|
|
14
|
+
const currencySymbol = React.useMemo(() => (currency ? NumberUtils.getCurrencySymbol(currency) : ""), [currency]);
|
|
15
|
+
// State
|
|
16
|
+
const [amountLabel, setAmountLabel] = React.useState(label);
|
|
17
|
+
React.useEffect(() => {
|
|
18
|
+
setAmountLabel(label);
|
|
19
|
+
}, [label]);
|
|
20
|
+
// Ref
|
|
21
|
+
const getValue = () => amountLabel;
|
|
22
|
+
const setValue = (value) => {
|
|
23
|
+
if (typeof value === "number") {
|
|
24
|
+
setAmountLabel(NumberUtils.formatMoney(value));
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
setAmountLabel(`${value} ?? ''`);
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
React.useImperativeHandle(mref, () => ({
|
|
31
|
+
getValue,
|
|
32
|
+
setValue
|
|
33
|
+
}));
|
|
34
|
+
React.useEffect(() => {
|
|
35
|
+
if (defaultValue == null)
|
|
36
|
+
return;
|
|
37
|
+
setValue(defaultValue);
|
|
38
|
+
}, [defaultValue]);
|
|
39
|
+
return (_jsxs(React.Fragment, { children: [_jsxs(Typography, { ...rest, children: [currencySymbol, " ", amountLabel] }), helperText && _jsx(FormHelperText, { children: helperText })] }));
|
|
40
|
+
};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { jsxs as _jsxs, jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { Typography } from "@mui/material";
|
|
3
|
+
import React from "react";
|
|
4
|
+
import { OptionGroup } from "../OptionGroup";
|
|
5
|
+
/**
|
|
6
|
+
* Checkbox (multiple values) field creator
|
|
7
|
+
* type: checkbox
|
|
8
|
+
* @returns Component
|
|
9
|
+
*/
|
|
10
|
+
export const FieldCheckbox = ({ field, mref, onChange, defaultValue }) => {
|
|
11
|
+
// State
|
|
12
|
+
const [value, setLocalValue] = React.useState();
|
|
13
|
+
// Ref
|
|
14
|
+
const getValue = () => value;
|
|
15
|
+
const setValue = (value) => {
|
|
16
|
+
if (Array.isArray(value)) {
|
|
17
|
+
setLocalValue(value);
|
|
18
|
+
}
|
|
19
|
+
else if (typeof value === "string" || typeof value === "number") {
|
|
20
|
+
setLocalValue([value]);
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
setLocalValue(undefined);
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
React.useImperativeHandle(mref, () => ({
|
|
27
|
+
getValue,
|
|
28
|
+
setValue
|
|
29
|
+
}));
|
|
30
|
+
React.useEffect(() => {
|
|
31
|
+
if (defaultValue == null)
|
|
32
|
+
return;
|
|
33
|
+
setValue(defaultValue);
|
|
34
|
+
}, [defaultValue]);
|
|
35
|
+
// Name
|
|
36
|
+
const name = field.name;
|
|
37
|
+
if (!name) {
|
|
38
|
+
return (_jsxs(Typography, { children: ["No name for FieldCheckbox ", JSON.stringify(field)] }));
|
|
39
|
+
}
|
|
40
|
+
return (_jsx(OptionGroup, { name: name, options: field.options ?? [], multiple: true, row: true, helperText: field.helperText, label: field.label, variant: "outlined", fullWidth: true, defaultValue: value, onValueChange: (value) => {
|
|
41
|
+
const newValue = Array.isArray(value)
|
|
42
|
+
? value
|
|
43
|
+
: value == null
|
|
44
|
+
? undefined
|
|
45
|
+
: [value];
|
|
46
|
+
setLocalValue(newValue);
|
|
47
|
+
onChange(name, newValue);
|
|
48
|
+
}, ...field.mainSlotProps }));
|
|
49
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { jsxs as _jsxs, jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { Typography } from "@mui/material";
|
|
3
|
+
import React from "react";
|
|
4
|
+
import { ComboBoxMultiple } from "../ComboBoxMultiple";
|
|
5
|
+
/**
|
|
6
|
+
* Combobox (multiple values) field creator
|
|
7
|
+
* type combobox
|
|
8
|
+
* @returns Component
|
|
9
|
+
*/
|
|
10
|
+
export const FieldCombobox = ({ field, mref, onChange, defaultValue }) => {
|
|
11
|
+
// State
|
|
12
|
+
const [ids, setIds] = React.useState();
|
|
13
|
+
const getValue = () => ids;
|
|
14
|
+
const setValue = (value) => {
|
|
15
|
+
if (Array.isArray(value))
|
|
16
|
+
setIds(value);
|
|
17
|
+
else if (typeof value === "number" || typeof value === "string")
|
|
18
|
+
setIds([value]);
|
|
19
|
+
};
|
|
20
|
+
// Ref
|
|
21
|
+
React.useImperativeHandle(mref, () => ({
|
|
22
|
+
getValue,
|
|
23
|
+
setValue
|
|
24
|
+
}));
|
|
25
|
+
React.useEffect(() => {
|
|
26
|
+
if (defaultValue == null)
|
|
27
|
+
return;
|
|
28
|
+
setValue(defaultValue);
|
|
29
|
+
}, [defaultValue]);
|
|
30
|
+
// Name
|
|
31
|
+
const name = field.name;
|
|
32
|
+
if (!name) {
|
|
33
|
+
return (_jsxs(Typography, { children: ["No name for FieldCombobox ", JSON.stringify(field)] }));
|
|
34
|
+
}
|
|
35
|
+
return (_jsx(ComboBoxMultiple, { label: field.label ?? "", inputHelperText: field.helperText, name: name, options: field.options, fullWidth: true, idValues: ids, onChange: (_event, value) => {
|
|
36
|
+
const ids = value.map((v) => v.id);
|
|
37
|
+
setIds(ids);
|
|
38
|
+
onChange(name, ids);
|
|
39
|
+
}, ...field.mainSlotProps }));
|
|
40
|
+
};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { jsxs as _jsxs, jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { DateUtils } from "@etsoo/shared";
|
|
3
|
+
import { Typography } from "@mui/material";
|
|
4
|
+
import React from "react";
|
|
5
|
+
import { InputField } from "../InputField";
|
|
6
|
+
/**
|
|
7
|
+
* Date input field creator
|
|
8
|
+
* type: date
|
|
9
|
+
* @returns Component
|
|
10
|
+
*/
|
|
11
|
+
export const FieldDateInput = ({ field, mref, onChange, defaultValue }) => {
|
|
12
|
+
// Ref
|
|
13
|
+
const inputRef = React.useRef();
|
|
14
|
+
const getValue = () => inputRef.current == null
|
|
15
|
+
? undefined
|
|
16
|
+
: DateUtils.parse(inputRef.current.value);
|
|
17
|
+
const setValue = (value) => {
|
|
18
|
+
if (inputRef.current == null)
|
|
19
|
+
return;
|
|
20
|
+
const date = value instanceof Date
|
|
21
|
+
? value
|
|
22
|
+
: typeof value === "string"
|
|
23
|
+
? DateUtils.parse(value)
|
|
24
|
+
: undefined;
|
|
25
|
+
inputRef.current.value =
|
|
26
|
+
DateUtils.formatForInput(date, inputRef.current.type === "date" ? undefined : false) ?? "";
|
|
27
|
+
};
|
|
28
|
+
React.useImperativeHandle(mref, () => ({
|
|
29
|
+
getValue,
|
|
30
|
+
setValue
|
|
31
|
+
}));
|
|
32
|
+
React.useEffect(() => {
|
|
33
|
+
if (defaultValue == null)
|
|
34
|
+
return;
|
|
35
|
+
setValue(defaultValue);
|
|
36
|
+
}, [defaultValue]);
|
|
37
|
+
// Name
|
|
38
|
+
const name = field.name;
|
|
39
|
+
if (!name) {
|
|
40
|
+
return (_jsxs(Typography, { children: ["No name for FieldDateInput ", JSON.stringify(field)] }));
|
|
41
|
+
}
|
|
42
|
+
return (_jsx(InputField, { label: field.label ?? "", helperText: field.helperText, inputRef: inputRef, type: "date", name: name, fullWidth: true, onChange: () => onChange(name, getValue()), ...field.mainSlotProps }));
|
|
43
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { Divider } from "@mui/material";
|
|
3
|
+
import React from "react";
|
|
4
|
+
import { CustomFieldUtils } from "./CustomFieldUtils";
|
|
5
|
+
/**
|
|
6
|
+
* Divider field creator
|
|
7
|
+
* type: divider
|
|
8
|
+
* @returns Component
|
|
9
|
+
*/
|
|
10
|
+
export const FieldDivider = ({ field }) => {
|
|
11
|
+
return (_jsxs(React.Fragment, { children: [CustomFieldUtils.createMultilineLabel(field.label), _jsx(Divider, { ...field.mainSlotProps })] }));
|
|
12
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { jsxs as _jsxs, jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { Typography } from "@mui/material";
|
|
3
|
+
import React from "react";
|
|
4
|
+
import { InputField } from "../InputField";
|
|
5
|
+
/**
|
|
6
|
+
* Input field creator
|
|
7
|
+
* type: input
|
|
8
|
+
* @returns Component
|
|
9
|
+
*/
|
|
10
|
+
export const FieldInput = ({ field, mref, onChange, defaultValue }) => {
|
|
11
|
+
// Ref
|
|
12
|
+
const inputRef = React.useRef();
|
|
13
|
+
const getValue = () => inputRef.current?.value;
|
|
14
|
+
React.useImperativeHandle(mref, () => ({
|
|
15
|
+
getValue,
|
|
16
|
+
setValue(value) {
|
|
17
|
+
if (inputRef.current)
|
|
18
|
+
inputRef.current.value = `${value ?? ""}`;
|
|
19
|
+
}
|
|
20
|
+
}));
|
|
21
|
+
// Name
|
|
22
|
+
const name = field.name;
|
|
23
|
+
if (!name) {
|
|
24
|
+
return (_jsxs(Typography, { children: ["No name for FieldInput ", JSON.stringify(field)] }));
|
|
25
|
+
}
|
|
26
|
+
return (_jsx(InputField, { label: field.label ?? "", helperText: field.helperText, inputRef: inputRef, name: name, fullWidth: true, defaultValue: defaultValue, onChange: () => onChange(name, getValue()), ...field.mainSlotProps }));
|
|
27
|
+
};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { jsxs as _jsxs, jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { Typography } from "@mui/material";
|
|
3
|
+
import React from "react";
|
|
4
|
+
import { InputField } from "../InputField";
|
|
5
|
+
function parseJson(value) {
|
|
6
|
+
if (value) {
|
|
7
|
+
try {
|
|
8
|
+
return JSON.parse(value);
|
|
9
|
+
}
|
|
10
|
+
catch (error) {
|
|
11
|
+
return undefined;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
return undefined;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Textarea field creator
|
|
18
|
+
* type: json
|
|
19
|
+
* @returns Component
|
|
20
|
+
*/
|
|
21
|
+
export const FieldJson = ({ field, mref, onChange, defaultValue }) => {
|
|
22
|
+
// Ref
|
|
23
|
+
const inputRef = React.useRef();
|
|
24
|
+
const getValue = () => parseJson(inputRef.current?.value);
|
|
25
|
+
const setValue = (value) => {
|
|
26
|
+
if (inputRef.current) {
|
|
27
|
+
const obj = typeof value === "object"
|
|
28
|
+
? value
|
|
29
|
+
: typeof value === "string"
|
|
30
|
+
? parseJson(value)
|
|
31
|
+
: undefined;
|
|
32
|
+
inputRef.current.value = obj ? JSON.stringify(obj, null, 4) : "";
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
React.useImperativeHandle(mref, () => ({
|
|
36
|
+
getValue,
|
|
37
|
+
setValue
|
|
38
|
+
}));
|
|
39
|
+
React.useEffect(() => {
|
|
40
|
+
if (defaultValue == null)
|
|
41
|
+
return;
|
|
42
|
+
setValue(defaultValue);
|
|
43
|
+
}, [defaultValue]);
|
|
44
|
+
// Name
|
|
45
|
+
const name = field.name;
|
|
46
|
+
if (!name) {
|
|
47
|
+
return (_jsxs(Typography, { children: ["No name for FieldJson ", JSON.stringify(field)] }));
|
|
48
|
+
}
|
|
49
|
+
return (_jsx(InputField, { label: field.label ?? "", helperText: field.helperText, name: name, fullWidth: true, multiline: true, rows: 4, inputProps: { maxLength: 1280 }, inputRef: inputRef, onChange: () => onChange(name, getValue()), ...field.mainSlotProps }));
|
|
50
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import React from "react";
|
|
3
|
+
import { CustomFieldUtils } from "./CustomFieldUtils";
|
|
4
|
+
/**
|
|
5
|
+
* Label field creator
|
|
6
|
+
* type: label
|
|
7
|
+
* @returns Component
|
|
8
|
+
*/
|
|
9
|
+
export const FieldLabel = ({ field, mref, defaultValue }) => {
|
|
10
|
+
// State
|
|
11
|
+
const [label, setLabel] = React.useState();
|
|
12
|
+
// Ref
|
|
13
|
+
const getValue = () => label;
|
|
14
|
+
const setValue = (value) => setLabel(`${value ?? ""}`);
|
|
15
|
+
React.useImperativeHandle(mref, () => ({
|
|
16
|
+
getValue,
|
|
17
|
+
setValue
|
|
18
|
+
}));
|
|
19
|
+
React.useEffect(() => {
|
|
20
|
+
setLabel(field.label);
|
|
21
|
+
}, [field.label]);
|
|
22
|
+
React.useEffect(() => {
|
|
23
|
+
if (defaultValue == null)
|
|
24
|
+
return;
|
|
25
|
+
setValue(defaultValue);
|
|
26
|
+
}, [defaultValue]);
|
|
27
|
+
return (_jsx(React.Fragment, { children: CustomFieldUtils.createMultilineLabel(label, field.mainSlotProps) }));
|
|
28
|
+
};
|