drf-react-by-schema 0.14.6 → 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, sx, options, isSemaphoric, label, onValueChange, decimalScale, fieldsLayout: fieldsLayoutInitial, hiddenFields, isolatedGetAutoComplete, ...props }: FormBySchemaProps): JSX.Element;
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(_a) {
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
- for (const field of row) {
80
- if (['field', 'nested object'].includes(schema[field].type) &&
81
- !optionsACModels.includes(field)) {
82
- optionsACModels.push(field);
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, sx: sx, options: options, isSemaphoric: isSemaphoric, label: label, onValueChange: onValueChange, decimalScale: decimalScale }, props))));
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
- return (react_1.default.createElement(Box_1.default, { sx: styles_1.Layout.inLineForm, key: `row_${rowIndex}` },
159
- react_1.default.createElement(react_1.default.Fragment, null, row.map((field, i, j) => (react_1.default.createElement(react_1.default.Fragment, { key: field },
160
- 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, sx: Object.assign(Object.assign({}, sx), { mr: i + 1 === j.length ? 0 : 2 }), options: options, isSemaphoric: isSemaphoric, label: label, onValueChange: onValueChange, decimalScale: decimalScale }, props))))))));
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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "drf-react-by-schema",
3
- "version": "0.14.6",
3
+ "version": "0.15.0",
4
4
  "description": "Components and Tools for building a React App having Django Rest Framework (DRF) as server",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",