drf-react-by-schema 0.14.5 → 0.15.0
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.
|
@@ -1,12 +1,30 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import { UseFormRegister } from 'react-hook-form';
|
|
3
|
+
import { SxProps } from '@mui/material';
|
|
3
4
|
import { FieldBySchemaProps, Item } from '../../utils';
|
|
4
5
|
import { OptionsACType } from '../../context/APIWrapperContext';
|
|
6
|
+
interface CustomElementProps {
|
|
7
|
+
onValueChange?: (x: any) => void;
|
|
8
|
+
sx?: SxProps;
|
|
9
|
+
disabled?: boolean;
|
|
10
|
+
optionsACModels?: string[];
|
|
11
|
+
value?: any;
|
|
12
|
+
}
|
|
13
|
+
interface CustomField {
|
|
14
|
+
key: string;
|
|
15
|
+
CustomElement: (x: CustomElementProps) => JSX.Element;
|
|
16
|
+
}
|
|
5
17
|
interface FieldLayout {
|
|
6
18
|
title?: string;
|
|
7
|
-
rows?: (string | string[])[];
|
|
19
|
+
rows?: (string | (string | CustomField)[] | CustomField)[];
|
|
8
20
|
CustomElement?: React.ReactNode;
|
|
9
21
|
}
|
|
22
|
+
interface FieldProps {
|
|
23
|
+
sx?: SxProps;
|
|
24
|
+
onValueChange?: (x: any) => void;
|
|
25
|
+
disabled?: boolean;
|
|
26
|
+
value?: any;
|
|
27
|
+
}
|
|
10
28
|
interface FormBySchemaProps extends Omit<FieldBySchemaProps, 'name'> {
|
|
11
29
|
fieldsLayout?: FieldLayout[];
|
|
12
30
|
hiddenFields?: string[];
|
|
@@ -14,6 +32,7 @@ interface FormBySchemaProps extends Omit<FieldBySchemaProps, 'name'> {
|
|
|
14
32
|
setOptionsAC?: (x: OptionsACType) => void;
|
|
15
33
|
forceReload?: boolean;
|
|
16
34
|
isolatedGetAutoComplete?: (model: string) => Promise<false | Item[]>;
|
|
35
|
+
fieldsProps?: Record<string, FieldProps>;
|
|
17
36
|
}
|
|
18
|
-
export default function FormBySchema({ schema, control, errors, register, multiline, setValue, getValues, fieldKey, labelKey, index, optionsAC, setOptionsAC, forceReload, optionsModel, getOptionLabel, renderOption, onEditModel,
|
|
37
|
+
export default function FormBySchema({ schema, control, errors, register, multiline, setValue, getValues, fieldKey, labelKey, index, optionsAC, setOptionsAC, forceReload, optionsModel, getOptionLabel, renderOption, onEditModel, options, isSemaphoric, label, decimalScale, fieldsLayout: fieldsLayoutInitial, fieldsProps, hiddenFields, isolatedGetAutoComplete, }: FormBySchemaProps): JSX.Element;
|
|
19
38
|
export {};
|
|
@@ -31,17 +31,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
31
31
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
32
|
});
|
|
33
33
|
};
|
|
34
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
35
|
-
var t = {};
|
|
36
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
37
|
-
t[p] = s[p];
|
|
38
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
39
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
40
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
41
|
-
t[p[i]] = s[p[i]];
|
|
42
|
-
}
|
|
43
|
-
return t;
|
|
44
|
-
};
|
|
45
34
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
46
35
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
47
36
|
};
|
|
@@ -55,11 +44,11 @@ const CircularProgress_1 = __importDefault(require("@mui/material/CircularProgre
|
|
|
55
44
|
const FieldBySchema_1 = __importDefault(require("./FieldBySchema"));
|
|
56
45
|
const styles_1 = require("../../styles");
|
|
57
46
|
const APIWrapperContext_1 = require("../../context/APIWrapperContext");
|
|
58
|
-
function FormBySchema(
|
|
59
|
-
var { schema, control, errors, register, multiline = false, setValue, getValues, fieldKey, labelKey = 'nome', index, optionsAC, setOptionsAC, forceReload, optionsModel, getOptionLabel, renderOption, onEditModel, sx = { mr: 0 }, options, isSemaphoric = false, label, onValueChange, decimalScale = 2, fieldsLayout: fieldsLayoutInitial, hiddenFields = [], isolatedGetAutoComplete } = _a, props = __rest(_a, ["schema", "control", "errors", "register", "multiline", "setValue", "getValues", "fieldKey", "labelKey", "index", "optionsAC", "setOptionsAC", "forceReload", "optionsModel", "getOptionLabel", "renderOption", "onEditModel", "sx", "options", "isSemaphoric", "label", "onValueChange", "decimalScale", "fieldsLayout", "hiddenFields", "isolatedGetAutoComplete"]);
|
|
47
|
+
function FormBySchema({ schema, control, errors, register, multiline = false, setValue, getValues, fieldKey, labelKey = 'nome', index, optionsAC, setOptionsAC, forceReload, optionsModel, getOptionLabel, renderOption, onEditModel, options, isSemaphoric = false, label, decimalScale = 2, fieldsLayout: fieldsLayoutInitial, fieldsProps, hiddenFields = [], isolatedGetAutoComplete, }) {
|
|
60
48
|
const { getAutoComplete } = (0, APIWrapperContext_1.useAPIWrapper)();
|
|
61
49
|
const [fieldsLayout, setFieldsLayout] = (0, react_1.useState)([]);
|
|
62
50
|
const [localOptionsAC, setLocalOptionsAC] = (0, react_1.useState)(undefined);
|
|
51
|
+
const defaultSx = { mr: 0 };
|
|
63
52
|
const getColumns = () => {
|
|
64
53
|
const optionsACModels = [];
|
|
65
54
|
if (fieldsLayoutInitial && fieldsLayoutInitial.length > 0) {
|
|
@@ -76,11 +65,18 @@ function FormBySchema(_a) {
|
|
|
76
65
|
}
|
|
77
66
|
continue;
|
|
78
67
|
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
68
|
+
if (Array.isArray(row)) {
|
|
69
|
+
for (const field of row) {
|
|
70
|
+
if (typeof field === 'string' &&
|
|
71
|
+
['field', 'nested object'].includes(schema[field].type) &&
|
|
72
|
+
!optionsACModels.includes(field)) {
|
|
73
|
+
optionsACModels.push(field);
|
|
74
|
+
}
|
|
83
75
|
}
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
if (optionsACModels.includes(row.key)) {
|
|
79
|
+
optionsACModels.push(row.key);
|
|
84
80
|
}
|
|
85
81
|
}
|
|
86
82
|
}
|
|
@@ -150,14 +146,43 @@ function FormBySchema(_a) {
|
|
|
150
146
|
section.rows && (react_1.default.createElement(react_1.default.Fragment, null, section.rows.map((row, rowIndex) => {
|
|
151
147
|
if (typeof row === 'string') {
|
|
152
148
|
const field = row;
|
|
149
|
+
const fieldsPropsObj = fieldsProps && fieldsProps.hasOwnProperty(field)
|
|
150
|
+
? fieldsProps[field]
|
|
151
|
+
: {};
|
|
152
|
+
fieldsPropsObj.sx = fieldsPropsObj.hasOwnProperty('sx')
|
|
153
|
+
? fieldsPropsObj.sx
|
|
154
|
+
: defaultSx;
|
|
153
155
|
return (react_1.default.createElement(react_1.default.Fragment, { key: field },
|
|
154
156
|
react_1.default.createElement(FieldBySchema_1.default, Object.assign({ name: field, schema: schema, control: control, errors: errors, multiline: multiline, setValue: setValue, getValues: getValues, fieldKey: fieldKey, labelKey: labelKey, index: index, optionsAC: setOptionsAC
|
|
155
157
|
? optionsAC
|
|
156
|
-
: localOptionsAC, optionsModel: optionsModel, getOptionLabel: getOptionLabel, renderOption: renderOption, onEditModel: onEditModel,
|
|
158
|
+
: localOptionsAC, optionsModel: optionsModel, getOptionLabel: getOptionLabel, renderOption: renderOption, onEditModel: onEditModel, options: options, isSemaphoric: isSemaphoric, label: label, decimalScale: decimalScale }, fieldsPropsObj))));
|
|
159
|
+
}
|
|
160
|
+
if (Array.isArray(row)) {
|
|
161
|
+
return (react_1.default.createElement(Box_1.default, { sx: styles_1.Layout.inLineForm, key: `row_${rowIndex}` },
|
|
162
|
+
react_1.default.createElement(react_1.default.Fragment, null, row.map((field, i, j) => {
|
|
163
|
+
const mr = i + 1 === j.length ? 0 : 2;
|
|
164
|
+
const key = typeof field === 'string'
|
|
165
|
+
? field
|
|
166
|
+
: field.key;
|
|
167
|
+
const fieldsPropsObj = fieldsProps &&
|
|
168
|
+
fieldsProps.hasOwnProperty(key)
|
|
169
|
+
? fieldsProps[key]
|
|
170
|
+
: {};
|
|
171
|
+
fieldsPropsObj.sx =
|
|
172
|
+
fieldsPropsObj.hasOwnProperty('sx')
|
|
173
|
+
? Object.assign(Object.assign({}, fieldsPropsObj.sx), { mr }) : {
|
|
174
|
+
mr,
|
|
175
|
+
};
|
|
176
|
+
return (react_1.default.createElement(react_1.default.Fragment, { key: `field_${rowIndex}_${i}` }, typeof field === 'string' ? (react_1.default.createElement(FieldBySchema_1.default, Object.assign({ name: field, schema: schema, control: control, errors: errors, multiline: multiline, setValue: setValue, getValues: getValues, fieldKey: fieldKey, labelKey: labelKey, index: index, optionsAC: optionsAC, optionsModel: optionsModel, getOptionLabel: getOptionLabel, renderOption: renderOption, onEditModel: onEditModel, options: options, isSemaphoric: isSemaphoric, label: label, decimalScale: decimalScale }, fieldsPropsObj))) : (react_1.default.createElement(field.CustomElement, Object.assign({}, fieldsPropsObj)))));
|
|
177
|
+
}))));
|
|
157
178
|
}
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
179
|
+
const fieldsPropsObj = fieldsProps && fieldsProps.hasOwnProperty(row.key)
|
|
180
|
+
? fieldsProps[row.key]
|
|
181
|
+
: {};
|
|
182
|
+
fieldsPropsObj.sx = fieldsPropsObj.hasOwnProperty('sx')
|
|
183
|
+
? fieldsPropsObj.sx
|
|
184
|
+
: defaultSx;
|
|
185
|
+
return react_1.default.createElement(row.CustomElement, Object.assign({}, fieldsPropsObj));
|
|
161
186
|
}))))));
|
|
162
187
|
})));
|
|
163
188
|
}
|
|
@@ -29,6 +29,6 @@ function BooleanFieldBySchema(_a) {
|
|
|
29
29
|
onValueChange(e);
|
|
30
30
|
}
|
|
31
31
|
field.onChange(e);
|
|
32
|
-
} })), label: schema[model].label, required: schema[model].required, sx: sx })) }));
|
|
32
|
+
} })), label: schema[model].label, required: schema[model].required, sx: Object.assign({ width: '100%' }, sx) })) }));
|
|
33
33
|
}
|
|
34
34
|
exports.default = BooleanFieldBySchema;
|
|
@@ -51,17 +51,22 @@ const filter = (0, Autocomplete_1.createFilterOptions)();
|
|
|
51
51
|
const EditableAutocompleteFieldBySchema = react_1.default.forwardRef((_a, ref) => {
|
|
52
52
|
var { control, schema, errors, setValue, getValues, fieldKey, labelKey = 'nome', index, name = 'name', optionsAC, optionsModel, getOptionLabel, renderOption, onEditModel, sx = { mr: 2 }, multiple = false, disabled = false } = _a, other = __rest(_a, ["control", "schema", "errors", "setValue", "getValues", "fieldKey", "labelKey", "index", "name", "optionsAC", "optionsModel", "getOptionLabel", "renderOption", "onEditModel", "sx", "multiple", "disabled"]);
|
|
53
53
|
const { setDialog } = (0, APIWrapperContext_1.useAPIWrapper)();
|
|
54
|
+
const [options, setOptions] = (0, react_1.useState)(null);
|
|
54
55
|
const model = name;
|
|
55
56
|
const label = schema[model].label;
|
|
56
57
|
if (!optionsModel) {
|
|
57
58
|
optionsModel = model;
|
|
58
59
|
}
|
|
59
|
-
const options = optionsAC && optionsAC[optionsModel] ? optionsAC[optionsModel] : [];
|
|
60
60
|
if (fieldKey && index && index >= 0) {
|
|
61
61
|
name = `${fieldKey}.${index}.${name}`;
|
|
62
62
|
}
|
|
63
63
|
(0, react_1.useEffect)(() => {
|
|
64
|
-
if (
|
|
64
|
+
if (optionsAC && optionsModel && optionsAC[optionsModel]) {
|
|
65
|
+
setOptions(optionsAC[optionsModel]);
|
|
66
|
+
}
|
|
67
|
+
}, [optionsAC, optionsModel]);
|
|
68
|
+
(0, react_1.useEffect)(() => {
|
|
69
|
+
if (!getValues || !setValue || !options) {
|
|
65
70
|
return;
|
|
66
71
|
}
|
|
67
72
|
const valuesInitial = getValues(model);
|
|
@@ -81,7 +86,7 @@ const EditableAutocompleteFieldBySchema = react_1.default.forwardRef((_a, ref) =
|
|
|
81
86
|
}
|
|
82
87
|
const harmonizedValues = options.find((option) => option.id === valuesInitial.id);
|
|
83
88
|
setValue(model, harmonizedValues);
|
|
84
|
-
}, [options]);
|
|
89
|
+
}, [options, setValue, getValues, model, multiple]);
|
|
85
90
|
const { error, helperText } = fieldKey && index && index >= 0
|
|
86
91
|
? (0, utils_1.errorProps)({
|
|
87
92
|
fieldKey,
|
|
@@ -95,6 +100,9 @@ const EditableAutocompleteFieldBySchema = react_1.default.forwardRef((_a, ref) =
|
|
|
95
100
|
? errors[name].message
|
|
96
101
|
: schema[name].help_text || '',
|
|
97
102
|
};
|
|
103
|
+
if (options === null) {
|
|
104
|
+
return react_1.default.createElement(react_1.default.Fragment, null);
|
|
105
|
+
}
|
|
98
106
|
if (!disabled && !schema[model].disabled && onEditModel && optionsModel) {
|
|
99
107
|
return (react_1.default.createElement(react_hook_form_1.Controller, { control: control, name: name, render: ({ field }) => (react_1.default.createElement(Autocomplete_1.default, Object.assign({ key: name }, field, { id: name, options: options, disabled: schema[model].disabled === true, autoHighlight: true, isOptionEqualToValue: (option, value) => option.id === value.id, fullWidth: true, multiple: multiple, sx: sx, onChange: (e, value) => {
|
|
100
108
|
let valueAr = value;
|
|
@@ -149,12 +157,15 @@ const EditableAutocompleteFieldBySchema = react_1.default.forwardRef((_a, ref) =
|
|
|
149
157
|
}
|
|
150
158
|
} },
|
|
151
159
|
react_1.default.createElement(Edit_1.default, null))),
|
|
152
|
-
params.InputProps.endAdornment)) }) }, other))), freeSolo: true, filterOptions: (
|
|
153
|
-
|
|
160
|
+
params.InputProps.endAdornment)) }) }, other))), freeSolo: true, filterOptions: (filteredOptions, params) => {
|
|
161
|
+
if (filteredOptions.length === 0) {
|
|
162
|
+
return [];
|
|
163
|
+
}
|
|
164
|
+
const filtered = filter(filteredOptions, params);
|
|
154
165
|
const { inputValue } = params;
|
|
155
166
|
const inputValueSlug = (0, utils_1.slugify)(inputValue);
|
|
156
167
|
// Suggest the creation of a new value
|
|
157
|
-
const isExisting =
|
|
168
|
+
const isExisting = filteredOptions.some((option) => inputValueSlug === (0, utils_1.slugify)(option.label));
|
|
158
169
|
if (inputValue !== '' && !isExisting) {
|
|
159
170
|
filtered.push({
|
|
160
171
|
inputValue,
|
package/package.json
CHANGED