drf-react-by-schema 0.15.0 → 0.15.1

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,30 +1,3 @@
1
- import React from 'react';
2
- import { SxProps } from '@mui/material';
3
- import { Item, SchemaType } from '../../utils';
4
- import { OptionsACType } from '../../context/APIWrapperContext';
5
- interface FieldLayout {
6
- title?: string;
7
- rows?: (string | string[])[];
8
- CustomElement?: React.ReactNode;
9
- }
10
- interface DetailBySchemaProps {
11
- values: Item;
12
- schema: SchemaType;
13
- editLink?: string;
14
- editLabel?: string;
15
- labelKey?: string;
16
- optionsAC?: OptionsACType;
17
- setOptionsAC?: (x: OptionsACType) => void;
18
- decimalScale?: number;
19
- fieldsLayout?: FieldLayout[];
20
- sxRow?: SxProps;
21
- sxRowMultiple?: SxProps;
22
- sxField?: SxProps;
23
- sxLabel?: SxProps;
24
- sxValue?: SxProps;
25
- sxValueList?: SxProps;
26
- sxValueListItem?: SxProps;
27
- sxValueListItemText?: SxProps;
28
- }
29
- export default function DetailBySchema({ values, schema, editLink, editLabel, labelKey, optionsAC, setOptionsAC, decimalScale, fieldsLayout: fieldsLayoutInitial, sxRow, sxRowMultiple, sxField, sxLabel, sxValue, sxValueList, sxValueListItem, sxValueListItemText, }: DetailBySchemaProps): JSX.Element;
30
- export {};
1
+ /// <reference types="react" />
2
+ import { DetailBySchemaProps } from '../../utils';
3
+ export default function DetailBySchema({ values, schema, editLink, editLabel, labelKey, decimalScale, fieldsLayout: fieldsLayoutInitial, fieldsProps, sxRow, sxRowMultiple, sxField, sxLabel, sxValue, sxValueList, sxValueListItem, sxValueListItemText, }: DetailBySchemaProps): JSX.Element;
@@ -36,11 +36,8 @@ const material_1 = require("@mui/material");
36
36
  const Stack_1 = __importDefault(require("@mui/material/Stack"));
37
37
  const DetailFieldBySchema_1 = __importDefault(require("./DetailFieldBySchema"));
38
38
  const styles_1 = require("../../styles");
39
- const APIWrapperContext_1 = require("../../context/APIWrapperContext");
40
- function DetailBySchema({ values, schema, editLink, editLabel, labelKey = 'label', optionsAC, setOptionsAC, decimalScale = 2, fieldsLayout: fieldsLayoutInitial, sxRow, sxRowMultiple, sxField, sxLabel, sxValue, sxValueList, sxValueListItem, sxValueListItemText, }) {
41
- const { getAutoComplete } = (0, APIWrapperContext_1.useAPIWrapper)();
39
+ function DetailBySchema({ values, schema, editLink, editLabel, labelKey = 'label', decimalScale = 2, fieldsLayout: fieldsLayoutInitial, fieldsProps, sxRow, sxRowMultiple, sxField, sxLabel, sxValue, sxValueList, sxValueListItem, sxValueListItemText, }) {
42
40
  const [fieldsLayout, setFieldsLayout] = (0, react_1.useState)([]);
43
- const [localOptionsAC, setLocalOptionsAC] = (0, react_1.useState)(undefined);
44
41
  const getColumns = () => {
45
42
  if (fieldsLayoutInitial && fieldsLayoutInitial.length > 0) {
46
43
  return fieldsLayoutInitial;
@@ -66,20 +63,35 @@ function DetailBySchema({ values, schema, editLink, editLabel, labelKey = 'label
66
63
  }
67
64
  return (react_1.default.createElement(react_1.default.Fragment, null, fieldsLayout.map((section, sectionIndex) => {
68
65
  return (react_1.default.createElement(Card_1.default, { sx: styles_1.Layout.formCard, key: `section_${sectionIndex}` },
69
- section.title && react_1.default.createElement(CardHeader_1.default, { title: section.title }),
66
+ section.title && (react_1.default.createElement(CardHeader_1.default, { title: section.title, action: sectionIndex === 0 && editLink ? (react_1.default.createElement(Stack_1.default, { direction: "row", justifyContent: "flex-end" },
67
+ react_1.default.createElement(material_1.Button, { href: editLink, variant: "contained" }, editLabel))) : (react_1.default.createElement(react_1.default.Fragment, null)) })),
70
68
  react_1.default.createElement(CardContent_1.default, null,
71
- editLink &&
72
- react_1.default.createElement(Stack_1.default, { direction: "row", justifyContent: "flex-end" },
73
- react_1.default.createElement(material_1.Button, { href: editLink, variant: "contained" }, editLabel)),
74
69
  section.CustomElement && react_1.default.createElement(react_1.default.Fragment, null, section.CustomElement),
75
70
  section.rows && (react_1.default.createElement(react_1.default.Fragment, null, section.rows.map((row, rowIndex) => {
76
71
  if (typeof row === 'string') {
77
72
  const field = row;
73
+ const fieldProps = fieldsProps && fieldsProps.hasOwnProperty(field)
74
+ ? fieldsProps[field]
75
+ : {};
78
76
  return (react_1.default.createElement(Box_1.default, { key: field, sx: sxRow },
79
- react_1.default.createElement(DetailFieldBySchema_1.default, { name: field, value: values[field], schema: schema, labelKey: labelKey, decimalScale: decimalScale, sxField: sxField, sxLabel: sxLabel, sxValue: sxValue, sxValueList: sxValueList, sxValueListItem: sxValueListItem, sxValueListItemText: sxValueListItemText })));
77
+ react_1.default.createElement(DetailFieldBySchema_1.default, Object.assign({ name: field, value: values[field], schema: schema, labelKey: labelKey, decimalScale: decimalScale, sxField: sxField, sxLabel: sxLabel, sxValue: sxValue, sxValueList: sxValueList, sxValueListItem: sxValueListItem, sxValueListItemText: sxValueListItemText }, fieldProps))));
80
78
  }
81
- return (react_1.default.createElement(Box_1.default, { key: `row_${rowIndex}`, sx: sxRowMultiple }, row.map((field, i, j) => (react_1.default.createElement(react_1.default.Fragment, { key: field },
82
- react_1.default.createElement(DetailFieldBySchema_1.default, { name: field, value: values[field], schema: schema, labelKey: labelKey, decimalScale: decimalScale, sxField: sxField, sxLabel: sxLabel, sxValue: sxValue, sxValueList: sxValueList, sxValueListItem: sxValueListItem, sxValueListItemText: sxValueListItemText }))))));
79
+ if (Array.isArray(row)) {
80
+ return (react_1.default.createElement(Box_1.default, { key: `row_${rowIndex}`, sx: sxRowMultiple }, row.map((field, i) => {
81
+ const key = typeof field === 'string'
82
+ ? field
83
+ : field.key;
84
+ const fieldProps = fieldsProps &&
85
+ fieldsProps.hasOwnProperty(key)
86
+ ? fieldsProps[key]
87
+ : {};
88
+ return (react_1.default.createElement(react_1.default.Fragment, { key: `field_${rowIndex}_${i}` }, typeof field === 'string' ? (react_1.default.createElement(DetailFieldBySchema_1.default, Object.assign({ name: field, value: values[field], schema: schema, labelKey: labelKey, decimalScale: decimalScale, sxField: sxField, sxLabel: sxLabel, sxValue: sxValue, sxValueList: sxValueList, sxValueListItem: sxValueListItem, sxValueListItemText: sxValueListItemText }, fieldProps))) : (react_1.default.createElement(field.CustomElement, Object.assign({ sxField: sxField, sxLabel: sxLabel, sxValue: sxValue, sxValueList: sxValueList, sxValueListItem: sxValueListItem, sxValueListItemText: sxValueListItemText }, fieldProps)))));
89
+ })));
90
+ }
91
+ const fieldProps = fieldsProps && fieldsProps.hasOwnProperty(row.key)
92
+ ? fieldsProps[row.key]
93
+ : {};
94
+ return (react_1.default.createElement(row.CustomElement, Object.assign({ sxField: sxField, sxLabel: sxLabel, sxValue: sxValue, sxValueList: sxValueList, sxValueListItem: sxValueListItem, sxValueListItemText: sxValueListItemText }, fieldProps)));
83
95
  }))))));
84
96
  })));
85
97
  }
@@ -1,3 +1,3 @@
1
1
  /// <reference types="react" />
2
2
  import { DetailFieldBySchemaProps } from '../../utils';
3
- export default function DetailFieldBySchema({ name, value, schema, labelKey, decimalScale, optionIdKey, optionLabelKey, sxField, sxLabel, sxValue, sxValueList, sxValueListItem, sxValueListItemText, }: DetailFieldBySchemaProps): JSX.Element;
3
+ export default function DetailFieldBySchema({ name, value, schema, labelKey, optionIdKey, optionLabelKey, sxField, sxLabel, sxValue, sxValueList, sxValueListItem, sxValueListItemText, }: DetailFieldBySchemaProps): JSX.Element;
@@ -10,7 +10,7 @@ const Box_1 = __importDefault(require("@mui/material/Box"));
10
10
  const List_1 = __importDefault(require("@mui/material/List"));
11
11
  const ListItem_1 = __importDefault(require("@mui/material/ListItem"));
12
12
  const ListItemText_1 = __importDefault(require("@mui/material/ListItemText"));
13
- function DetailFieldBySchema({ name, value, schema, labelKey = 'label', decimalScale = 2, optionIdKey = 'value', optionLabelKey = 'display_name', sxField, sxLabel, sxValue, sxValueList, sxValueListItem, sxValueListItemText, }) {
13
+ function DetailFieldBySchema({ name, value, schema, labelKey = 'label', optionIdKey = 'value', optionLabelKey = 'display_name', sxField, sxLabel, sxValue, sxValueList, sxValueListItem, sxValueListItemText, }) {
14
14
  if (!value) {
15
15
  return (react_1.default.createElement(Box_1.default, { sx: sxField },
16
16
  react_1.default.createElement(Typography_1.default, { variant: "h5", sx: sxLabel }, schema[name].label),
@@ -57,6 +57,11 @@ function DetailFieldBySchema({ name, value, schema, labelKey = 'label', decimalS
57
57
  return (react_1.default.createElement(Box_1.default, { sx: sxField },
58
58
  react_1.default.createElement(Typography_1.default, { variant: "h5", sx: sxLabel }, schema[name].label),
59
59
  react_1.default.createElement(Typography_1.default, { variant: "body2", sx: sxValue }, value.toLocaleString())));
60
+ case 'email':
61
+ return (react_1.default.createElement(Box_1.default, { sx: sxField },
62
+ react_1.default.createElement(Typography_1.default, { variant: "h5", sx: sxLabel }, schema[name].label),
63
+ react_1.default.createElement(Typography_1.default, { variant: "body2", sx: sxValue },
64
+ react_1.default.createElement("a", { href: `mailto: ${value}` }, value))));
60
65
  case 'number':
61
66
  case 'integer':
62
67
  case 'password':
@@ -1,38 +1,15 @@
1
- import React from 'react';
1
+ /// <reference types="react" />
2
2
  import { UseFormRegister } from 'react-hook-form';
3
- import { SxProps } from '@mui/material';
4
- import { FieldBySchemaProps, Item } from '../../utils';
3
+ import { CommonFieldProps, FieldBySchemaProps, FormFieldLayout, Item } from '../../utils';
5
4
  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
- }
17
- interface FieldLayout {
18
- title?: string;
19
- rows?: (string | (string | CustomField)[] | CustomField)[];
20
- CustomElement?: React.ReactNode;
21
- }
22
- interface FieldProps {
23
- sx?: SxProps;
24
- onValueChange?: (x: any) => void;
25
- disabled?: boolean;
26
- value?: any;
27
- }
28
5
  interface FormBySchemaProps extends Omit<FieldBySchemaProps, 'name'> {
29
- fieldsLayout?: FieldLayout[];
6
+ fieldsLayout?: FormFieldLayout[];
30
7
  hiddenFields?: string[];
31
8
  register?: UseFormRegister<Item>;
32
9
  setOptionsAC?: (x: OptionsACType) => void;
33
10
  forceReload?: boolean;
34
11
  isolatedGetAutoComplete?: (model: string) => Promise<false | Item[]>;
35
- fieldsProps?: Record<string, FieldProps>;
12
+ fieldsProps?: Record<string, CommonFieldProps>;
36
13
  }
37
14
  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;
38
15
  export {};
@@ -146,16 +146,16 @@ function FormBySchema({ schema, control, errors, register, multiline = false, se
146
146
  section.rows && (react_1.default.createElement(react_1.default.Fragment, null, section.rows.map((row, rowIndex) => {
147
147
  if (typeof row === 'string') {
148
148
  const field = row;
149
- const fieldsPropsObj = fieldsProps && fieldsProps.hasOwnProperty(field)
149
+ const fieldProps = fieldsProps && fieldsProps.hasOwnProperty(field)
150
150
  ? fieldsProps[field]
151
151
  : {};
152
- fieldsPropsObj.sx = fieldsPropsObj.hasOwnProperty('sx')
153
- ? fieldsPropsObj.sx
152
+ fieldProps.sx = fieldProps.hasOwnProperty('sx')
153
+ ? fieldProps.sx
154
154
  : defaultSx;
155
155
  return (react_1.default.createElement(react_1.default.Fragment, { key: field },
156
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
157
157
  ? optionsAC
158
- : localOptionsAC, optionsModel: optionsModel, getOptionLabel: getOptionLabel, renderOption: renderOption, onEditModel: onEditModel, options: options, isSemaphoric: isSemaphoric, label: label, decimalScale: decimalScale }, fieldsPropsObj))));
158
+ : localOptionsAC, optionsModel: optionsModel, getOptionLabel: getOptionLabel, renderOption: renderOption, onEditModel: onEditModel, options: options, isSemaphoric: isSemaphoric, label: label, decimalScale: decimalScale }, fieldProps))));
159
159
  }
160
160
  if (Array.isArray(row)) {
161
161
  return (react_1.default.createElement(Box_1.default, { sx: styles_1.Layout.inLineForm, key: `row_${rowIndex}` },
@@ -164,25 +164,25 @@ function FormBySchema({ schema, control, errors, register, multiline = false, se
164
164
  const key = typeof field === 'string'
165
165
  ? field
166
166
  : field.key;
167
- const fieldsPropsObj = fieldsProps &&
167
+ const fieldProps = fieldsProps &&
168
168
  fieldsProps.hasOwnProperty(key)
169
169
  ? fieldsProps[key]
170
170
  : {};
171
- fieldsPropsObj.sx =
172
- fieldsPropsObj.hasOwnProperty('sx')
173
- ? Object.assign(Object.assign({}, fieldsPropsObj.sx), { mr }) : {
171
+ fieldProps.sx =
172
+ fieldProps.hasOwnProperty('sx')
173
+ ? Object.assign(Object.assign({}, fieldProps.sx), { mr }) : {
174
174
  mr,
175
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)))));
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 }, fieldProps))) : (react_1.default.createElement(field.CustomElement, Object.assign({}, fieldProps)))));
177
177
  }))));
178
178
  }
179
- const fieldsPropsObj = fieldsProps && fieldsProps.hasOwnProperty(row.key)
179
+ const fieldProps = fieldsProps && fieldsProps.hasOwnProperty(row.key)
180
180
  ? fieldsProps[row.key]
181
181
  : {};
182
- fieldsPropsObj.sx = fieldsPropsObj.hasOwnProperty('sx')
183
- ? fieldsPropsObj.sx
182
+ fieldProps.sx = fieldProps.hasOwnProperty('sx')
183
+ ? fieldProps.sx
184
184
  : defaultSx;
185
- return react_1.default.createElement(row.CustomElement, Object.assign({}, fieldsPropsObj));
185
+ return react_1.default.createElement(row.CustomElement, Object.assign({}, fieldProps));
186
186
  }))))));
187
187
  })));
188
188
  }
package/dist/utils.d.ts CHANGED
@@ -80,11 +80,8 @@ export interface ItemSchemaColumnsType {
80
80
  modelOptions: modelOptionsType;
81
81
  columns?: GridEnrichedBySchemaColDef[];
82
82
  }
83
- export interface FieldBySchemaProps {
84
- name: string;
85
- schema: SchemaType;
86
- control: Control;
87
- errors: Item;
83
+ export interface CommonFieldProps {
84
+ value?: any;
88
85
  multiline?: boolean;
89
86
  setValue?: UseFormSetValue<FieldValues>;
90
87
  getValues?: UseFormGetValues<FieldValues>;
@@ -111,6 +108,12 @@ export interface FieldBySchemaProps {
111
108
  optionLabelKey?: string;
112
109
  disabled?: boolean;
113
110
  }
111
+ export interface FieldBySchemaProps extends Omit<CommonFieldProps, 'value'> {
112
+ name: string;
113
+ schema: SchemaType;
114
+ control: Control;
115
+ errors: Item;
116
+ }
114
117
  export interface DetailFieldBySchemaProps {
115
118
  name: string;
116
119
  schema: SchemaType;
@@ -172,4 +175,43 @@ export type OnSelectActions = {
172
175
  title: string;
173
176
  action: OnSelectActionTypes;
174
177
  };
178
+ interface CustomFormField {
179
+ key: string;
180
+ CustomElement: (x: CommonFieldProps) => JSX.Element;
181
+ }
182
+ export interface FormFieldLayout {
183
+ title?: string;
184
+ rows?: (string | (string | CustomFormField)[] | CustomFormField)[];
185
+ CustomElement?: React.ReactNode;
186
+ }
187
+ export interface ExtraSxCommonFieldProps {
188
+ sxRow?: SxProps;
189
+ sxRowMultiple?: SxProps;
190
+ sxField?: SxProps;
191
+ sxLabel?: SxProps;
192
+ sxValue?: SxProps;
193
+ sxValueList?: SxProps;
194
+ sxValueListItem?: SxProps;
195
+ sxValueListItemText?: SxProps;
196
+ }
197
+ export type DetailCommonFieldProps = CommonFieldProps & ExtraSxCommonFieldProps;
198
+ interface CustomDetailField {
199
+ key: string;
200
+ CustomElement: (x: DetailCommonFieldProps) => JSX.Element;
201
+ }
202
+ export interface DetailFieldLayout {
203
+ title?: string;
204
+ rows?: (string | (string | CustomDetailField)[] | CustomDetailField)[];
205
+ CustomElement?: React.ReactNode;
206
+ }
207
+ export interface DetailBySchemaProps extends ExtraSxCommonFieldProps {
208
+ values: Item;
209
+ schema: SchemaType;
210
+ editLink?: string;
211
+ editLabel?: string;
212
+ labelKey?: string;
213
+ decimalScale?: number;
214
+ fieldsLayout?: DetailFieldLayout[];
215
+ fieldsProps?: Record<string, DetailCommonFieldProps>;
216
+ }
175
217
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "drf-react-by-schema",
3
- "version": "0.15.0",
3
+ "version": "0.15.1",
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",