drf-react-by-schema 0.3.0 → 0.4.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.
Files changed (88) hide show
  1. package/dist/api.d.ts +83 -0
  2. package/dist/api.js +613 -0
  3. package/dist/components/DataGridBySchemaEditable/BooleanInputCell.d.ts +24 -0
  4. package/dist/components/DataGridBySchemaEditable/BooleanInputCell.js +42 -0
  5. package/dist/components/DataGridBySchemaEditable/ConfirmDialog.d.ts +8 -0
  6. package/dist/components/DataGridBySchemaEditable/ConfirmDialog.js +21 -0
  7. package/dist/components/DataGridBySchemaEditable/CustomToolbar.d.ts +17 -0
  8. package/dist/components/DataGridBySchemaEditable/CustomToolbar.js +77 -0
  9. package/dist/components/DataGridBySchemaEditable/FooterToolbar.d.ts +17 -0
  10. package/dist/components/DataGridBySchemaEditable/FooterToolbar.js +41 -0
  11. package/dist/components/DataGridBySchemaEditable/GridDecimalInput.d.ts +9 -0
  12. package/dist/components/DataGridBySchemaEditable/GridDecimalInput.js +32 -0
  13. package/dist/components/DataGridBySchemaEditable/GridPatternInput.d.ts +9 -0
  14. package/dist/components/DataGridBySchemaEditable/GridPatternInput.js +30 -0
  15. package/dist/components/DataGridBySchemaEditable/InputInterval.d.ts +4 -0
  16. package/dist/components/DataGridBySchemaEditable/InputInterval.js +105 -0
  17. package/dist/components/DataGridBySchemaEditable/SelectEditInputCell.d.ts +33 -0
  18. package/dist/components/DataGridBySchemaEditable/SelectEditInputCell.js +140 -0
  19. package/dist/components/DataGridBySchemaEditable/utils.d.ts +15 -0
  20. package/{src/components/DataGridBySchemaEditable/utils.ts → dist/components/DataGridBySchemaEditable/utils.js} +32 -50
  21. package/dist/components/DataGridBySchemaEditable.d.ts +34 -0
  22. package/dist/components/DataGridBySchemaEditable.js +565 -0
  23. package/dist/components/DataTotals.d.ts +15 -0
  24. package/dist/components/DataTotals.js +23 -0
  25. package/dist/components/DialogActions.d.ts +10 -0
  26. package/dist/components/DialogActions.js +17 -0
  27. package/dist/components/DialogJSONSchemaForm.d.ts +13 -0
  28. package/dist/components/DialogJSONSchemaForm.js +20 -0
  29. package/dist/components/FormButtons.d.ts +21 -0
  30. package/dist/components/FormButtons.js +43 -0
  31. package/dist/components/GenericModelList.d.ts +19 -0
  32. package/dist/components/GenericModelList.js +113 -0
  33. package/dist/components/GenericRelatedModelList.d.ts +25 -0
  34. package/dist/components/GenericRelatedModelList.js +111 -0
  35. package/dist/components/TextFieldBySchema.js +39 -0
  36. package/dist/context/APIWrapper.d.ts +12 -0
  37. package/dist/context/APIWrapper.js +341 -0
  38. package/{src/context/APIWrapperContext.tsx → dist/context/APIWrapperContext.d.ts} +22 -31
  39. package/dist/context/APIWrapperContext.js +15 -0
  40. package/{src/context/DRFReactBySchemaContext.tsx → dist/context/DRFReactBySchemaContext.d.ts} +4 -17
  41. package/dist/context/DRFReactBySchemaContext.js +19 -0
  42. package/dist/context/DRFReactBySchemaProvider.d.ts +13 -0
  43. package/dist/context/DRFReactBySchemaProvider.js +53 -0
  44. package/dist/context/Form.d.ts +21 -0
  45. package/dist/context/Form.js +99 -0
  46. package/dist/context/FormContext.d.ts +72 -0
  47. package/dist/context/FormContext.js +15 -0
  48. package/dist/context/Overlays.d.ts +4 -0
  49. package/dist/context/Overlays.js +87 -0
  50. package/dist/index.d.ts +12 -0
  51. package/dist/index.js +3 -1
  52. package/{src/styles/index.ts → dist/styles/index.d.ts} +0 -1
  53. package/dist/styles/index.js +28 -0
  54. package/dist/styles/layout.d.ts +107 -0
  55. package/dist/styles/layout.js +56 -0
  56. package/dist/styles/theme.d.ts +21 -0
  57. package/dist/styles/theme.js +139 -0
  58. package/dist/utils.d.ts +74 -0
  59. package/dist/utils.js +268 -0
  60. package/package.json +11 -2
  61. package/.eslintrc.js +0 -14
  62. package/.gitlab-ci.yml +0 -14
  63. package/src/api.ts +0 -727
  64. package/src/components/DataGridBySchemaEditable/ConfirmDialog.tsx +0 -41
  65. package/src/components/DataGridBySchemaEditable/CustomToolbar.tsx +0 -93
  66. package/src/components/DataGridBySchemaEditable/FooterToolbar.tsx +0 -77
  67. package/src/components/DataGridBySchemaEditable/GridDecimalInput.tsx +0 -41
  68. package/src/components/DataGridBySchemaEditable/GridPatternInput.tsx +0 -37
  69. package/src/components/DataGridBySchemaEditable/InputInterval.tsx +0 -194
  70. package/src/components/DataGridBySchemaEditable/SelectEditInputCell.tsx +0 -154
  71. package/src/components/DataGridBySchemaEditable.md +0 -50
  72. package/src/components/DataGridBySchemaEditable.tsx +0 -758
  73. package/src/components/DataTotals.tsx +0 -57
  74. package/src/components/DialogActions.tsx +0 -33
  75. package/src/components/DialogJSONSchemaForm.tsx +0 -40
  76. package/src/components/GenericModelList.tsx +0 -165
  77. package/src/components/GenericRelatedModelList.tsx +0 -168
  78. package/src/context/APIWrapper.tsx +0 -447
  79. package/src/context/DRFReactBySchemaProvider.md +0 -57
  80. package/src/context/DRFReactBySchemaProvider.tsx +0 -72
  81. package/src/context/Overlays.tsx +0 -94
  82. package/src/index.ts +0 -77
  83. package/src/styles/layout.ts +0 -104
  84. package/src/styles/theme.ts +0 -190
  85. package/src/utils.ts +0 -327
  86. package/styleguide.config.js +0 -13
  87. package/tsconfig.json +0 -104
  88. package/webpack.config.js +0 -24
@@ -0,0 +1,74 @@
1
+ import { GridActionsColDef, GridColDef } from '@mui/x-data-grid';
2
+ export type Id = string | number;
3
+ export type Item = Record<string, any>;
4
+ export type SchemaType = Record<string, Field>;
5
+ export interface Choice {
6
+ value: string | number;
7
+ display_name: string;
8
+ }
9
+ type FieldChild = Record<string, SchemaType>;
10
+ export interface Field {
11
+ type: string;
12
+ child?: FieldChild;
13
+ children?: SchemaType;
14
+ model_default?: any;
15
+ model_required?: boolean;
16
+ choices?: Choice[];
17
+ max_length?: number | string;
18
+ label: string;
19
+ read_only?: boolean;
20
+ }
21
+ interface GridBySchemaColDef extends GridColDef {
22
+ isIndexField?: boolean;
23
+ creatable?: boolean;
24
+ orderable?: boolean;
25
+ patternFormat?: string;
26
+ }
27
+ interface GridActionsBySchemaColDef extends GridActionsColDef {
28
+ isIndexField?: boolean;
29
+ creatable?: boolean;
30
+ orderable?: boolean;
31
+ patternFormat?: string;
32
+ }
33
+ export type GridEnrichedBySchemaColDef = GridBySchemaColDef | GridActionsBySchemaColDef;
34
+ export interface DataSchemaColumnsType {
35
+ data: Item[];
36
+ schema: SchemaType;
37
+ columns?: GridEnrichedBySchemaColDef[];
38
+ }
39
+ export interface ItemSchemaColumnsType {
40
+ data: Item;
41
+ schema: SchemaType;
42
+ columns?: GridEnrichedBySchemaColDef[];
43
+ }
44
+ export declare const emptyByType: any;
45
+ export declare const getChoiceByValue: (value: number | string, choices: Choice[] | undefined) => string | null | undefined;
46
+ export declare const populateValues: ({ data, schema }: {
47
+ data: Item;
48
+ schema: SchemaType;
49
+ }) => Record<string, any>;
50
+ export declare const buildGenericYupValidationSchema: ({ data, schema, many, skipFields, extraValidators }: {
51
+ data: Item;
52
+ schema: SchemaType;
53
+ many?: boolean | undefined;
54
+ skipFields?: string[] | undefined;
55
+ extraValidators?: Item | undefined;
56
+ }) => any;
57
+ export declare const errorProps: ({ type, errors, fieldKey, fieldKeyProp, index }: {
58
+ type: string;
59
+ errors: Item;
60
+ fieldKey: string;
61
+ fieldKeyProp: string;
62
+ index?: number | undefined;
63
+ }) => {
64
+ error: any;
65
+ helperText: any;
66
+ };
67
+ export declare const getTmpId: () => string;
68
+ export declare const isTmpId: (id: string | number | undefined | null) => boolean;
69
+ export declare const reducer: (state: Record<string, any>, newState: Record<string, any>) => {
70
+ [x: string]: any;
71
+ };
72
+ export declare const getPatternFormat: (type: string) => string;
73
+ export type AddParametersToEnd<TFunction extends (...args: any) => any, TParameters extends [...args: any]> = (...args: [...Parameters<TFunction>, ...TParameters]) => ReturnType<TFunction>;
74
+ export {};
package/dist/utils.js ADDED
@@ -0,0 +1,268 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.getPatternFormat = exports.reducer = exports.isTmpId = exports.getTmpId = exports.errorProps = exports.buildGenericYupValidationSchema = exports.populateValues = exports.getChoiceByValue = exports.emptyByType = void 0;
27
+ const Yup = __importStar(require("yup"));
28
+ ;
29
+ ;
30
+ ;
31
+ ;
32
+ const emptyByType = (field, forDatabase = false) => {
33
+ if (field.model_default) {
34
+ return field.model_default;
35
+ }
36
+ switch (field.type) {
37
+ case 'nested object':
38
+ // emptyByType must be an empty object for the database, but must be null for the mui-autocomplete component. So I had to make this ugly hack here, when we're preparing to sendo to the database:
39
+ return forDatabase ? {} : null;
40
+ case 'field':
41
+ if (field.child) {
42
+ return [];
43
+ }
44
+ return (forDatabase)
45
+ ? undefined
46
+ : null;
47
+ case 'string':
48
+ case 'email':
49
+ return '';
50
+ case 'integer':
51
+ return 0;
52
+ case 'array':
53
+ return [];
54
+ default:
55
+ return null;
56
+ }
57
+ };
58
+ exports.emptyByType = emptyByType;
59
+ const getChoiceByValue = (value, choices) => {
60
+ if (!choices) {
61
+ return null;
62
+ }
63
+ for (const choice of choices) {
64
+ if (choice.value === value) {
65
+ return choice.display_name;
66
+ }
67
+ }
68
+ };
69
+ exports.getChoiceByValue = getChoiceByValue;
70
+ const populateValues = ({ data, schema }) => {
71
+ const values = {};
72
+ for (const entry of Object.entries(schema)) {
73
+ const [key, field] = entry;
74
+ if (key === 'id' && (0, exports.isTmpId)(data[key])) {
75
+ continue;
76
+ }
77
+ if (!data[key]) {
78
+ values[key] = (0, exports.emptyByType)(field);
79
+ continue;
80
+ }
81
+ if (field.type === 'field' && field.child) {
82
+ if (Array.isArray(data[key])) {
83
+ const arValues = [];
84
+ for (const row of data[key]) {
85
+ const value = (0, exports.populateValues)({
86
+ data: row,
87
+ schema: field.child.children
88
+ });
89
+ arValues.push(value);
90
+ }
91
+ values[key] = arValues;
92
+ continue;
93
+ }
94
+ values[key] = (0, exports.populateValues)({
95
+ data: data[key],
96
+ schema: field.child.children
97
+ });
98
+ continue;
99
+ }
100
+ if (field.type === 'choice') {
101
+ values[key] = data[key]
102
+ ? {
103
+ value: data[key],
104
+ display_name: (0, exports.getChoiceByValue)(data[key], field.choices)
105
+ }
106
+ : (0, exports.emptyByType)(field);
107
+ continue;
108
+ }
109
+ values[key] = data[key];
110
+ }
111
+ // console.log(values);
112
+ return values;
113
+ };
114
+ exports.populateValues = populateValues;
115
+ const getYupValidator = (type) => {
116
+ let yupFunc;
117
+ try {
118
+ switch (type) {
119
+ case 'slug':
120
+ yupFunc = Yup.string();
121
+ break;
122
+ case 'email':
123
+ yupFunc = Yup.string().email('Este campo deve ser um e-mail válido.');
124
+ break;
125
+ case 'integer':
126
+ yupFunc = Yup.number().integer('Este campo deve ser um número inteiro');
127
+ break;
128
+ case 'choice':
129
+ yupFunc = Yup.object();
130
+ break;
131
+ case 'field':
132
+ yupFunc = Yup.mixed();
133
+ break;
134
+ case 'nested object':
135
+ yupFunc = Yup.object();
136
+ break;
137
+ case 'date':
138
+ yupFunc = Yup.date();
139
+ break;
140
+ case 'string':
141
+ yupFunc = Yup.string();
142
+ break;
143
+ case 'number':
144
+ yupFunc = Yup.number();
145
+ break;
146
+ case 'boolean':
147
+ yupFunc = Yup.bool();
148
+ break;
149
+ case 'array':
150
+ yupFunc = Yup.array();
151
+ break;
152
+ case 'object':
153
+ yupFunc = Yup.object();
154
+ break;
155
+ default:
156
+ yupFunc = Yup.string();
157
+ break;
158
+ }
159
+ }
160
+ catch (e) {
161
+ yupFunc = Yup.string();
162
+ }
163
+ return yupFunc.nullable();
164
+ };
165
+ const buildGenericYupValidationSchema = ({ data, schema, many = false, skipFields = [], extraValidators = {} }) => {
166
+ const yupValidator = {};
167
+ for (const entry of Object.entries(schema)) {
168
+ const [key, field] = entry;
169
+ if (!data || !(key in data) || key === 'id' || skipFields.includes(key)) {
170
+ continue;
171
+ }
172
+ // console.log({ key, field, data: data[key] });
173
+ // OneToMany or ManyToMany:
174
+ if (field.type === 'field' && field.child) {
175
+ yupValidator[key] = (0, exports.buildGenericYupValidationSchema)({
176
+ schema: field.child.children,
177
+ many: true,
178
+ data: data[key],
179
+ extraValidators: Object.prototype.hasOwnProperty.call(extraValidators, key)
180
+ ? extraValidators[key]
181
+ : {}
182
+ });
183
+ continue;
184
+ }
185
+ // Nested Object:
186
+ if (field.type === 'nested object' && field.children) {
187
+ // yupValidator[key] = buildGenericYupValidationSchema({
188
+ // schema: field.children,
189
+ // many: false,
190
+ // data: data[key],
191
+ // extraValidators: Object.prototype.hasOwnProperty.call(extraValidators, key)
192
+ // ? extraValidators[key]
193
+ // : {}
194
+ // });
195
+ // if (!field.model_required) {
196
+ // yupValidator[key] = yupValidator[key].nullable();
197
+ // }
198
+ // continue;
199
+ }
200
+ yupValidator[key] = Object.prototype.hasOwnProperty.call(extraValidators, key)
201
+ ? extraValidators[key]
202
+ : getYupValidator(field.type);
203
+ if (field.model_required) {
204
+ yupValidator[key] = yupValidator[key].required('Este campo é obrigatório');
205
+ }
206
+ if (field.max_length) {
207
+ yupValidator[key] = yupValidator[key].max(parseInt(field.max_length), `Este campo só pode ter no máximo ${field.max_length} caracteres`);
208
+ }
209
+ }
210
+ // console.log({ yupValidator });
211
+ return (many)
212
+ ? Yup.array().of(Yup.object().shape(yupValidator))
213
+ : Yup.object().shape(yupValidator);
214
+ };
215
+ exports.buildGenericYupValidationSchema = buildGenericYupValidationSchema;
216
+ const errorProps = ({ type, errors, fieldKey, fieldKeyProp, index }) => {
217
+ let error;
218
+ let helperText;
219
+ if (index) {
220
+ const hasErrors = errors && errors[fieldKey] && errors[fieldKey][index] && Boolean(errors[fieldKey][index][fieldKeyProp]);
221
+ error = hasErrors;
222
+ helperText = hasErrors ? errors[fieldKey][index][fieldKeyProp].message : null;
223
+ return { error, helperText };
224
+ }
225
+ const hasErrors = errors && errors[fieldKey] && Boolean(errors[fieldKey][fieldKeyProp]);
226
+ error = hasErrors;
227
+ helperText = hasErrors ? errors[fieldKey][fieldKeyProp].message : null;
228
+ return { error, helperText };
229
+ };
230
+ exports.errorProps = errorProps;
231
+ const getTmpId = () => {
232
+ return 'tmp' + Math.floor(Math.random() * 1000000);
233
+ };
234
+ exports.getTmpId = getTmpId;
235
+ const isTmpId = (id) => {
236
+ if (!id) {
237
+ return true;
238
+ }
239
+ return id.toString().substr(0, 3) === 'tmp';
240
+ };
241
+ exports.isTmpId = isTmpId;
242
+ const reducer = (state, newState) => {
243
+ return Object.assign(Object.assign({}, state), newState);
244
+ };
245
+ exports.reducer = reducer;
246
+ const getPatternFormat = (type) => {
247
+ let format = '';
248
+ switch (type) {
249
+ case 'telefone':
250
+ case 'fone':
251
+ case 'phone':
252
+ case 'contact':
253
+ case 'contato':
254
+ format = '(##)#####-####';
255
+ break;
256
+ case 'cpf':
257
+ format = '###.###.###-##';
258
+ break;
259
+ case 'cnpj':
260
+ format = '##.###.###/####-##';
261
+ break;
262
+ case 'cep':
263
+ format = '##.###-###';
264
+ break;
265
+ }
266
+ return format;
267
+ };
268
+ exports.getPatternFormat = getPatternFormat;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "drf-react-by-schema",
3
- "version": "0.3.0",
3
+ "version": "0.4.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",
@@ -9,7 +9,10 @@
9
9
  "build": "tsc",
10
10
  "prepare": "install-peers",
11
11
  "styleguide": "styleguidist server",
12
- "styleguide:build": "styleguidist build"
12
+ "styleguide:build": "styleguidist build",
13
+ "release:major": "yarn version --version $(semver $npm_package_version -i major) && yarn publish --tag latest",
14
+ "release:minor": "yarn version --new-version $(semver $npm_package_version -i minor) && yarn publish --tag latest",
15
+ "release:patch": "yarn version --new-version $(semver $npm_package_version -i patch) && yarn publish --tag latest"
13
16
  },
14
17
  "repository": {
15
18
  "type": "git",
@@ -49,12 +52,14 @@
49
52
  "install-peers-cli": "^2.2.0",
50
53
  "react-docgen-typescript": "^2.2.2",
51
54
  "react-styleguidist": "^13.0.0",
55
+ "semver": "^7.5.1",
52
56
  "ts-loader": "^9.4.2",
53
57
  "typedoc": "^0.23.23",
54
58
  "typescript": "^4.9.4",
55
59
  "webpack": "^5.75.0"
56
60
  },
57
61
  "peerDependencies": {
62
+ "@hookform/resolvers": "^2.9.10",
58
63
  "@mui/icons-material": "^5.11.0",
59
64
  "@mui/lab": "^5.0.0-alpha.112",
60
65
  "@mui/material": "^5.11.0",
@@ -71,6 +76,7 @@
71
76
  "moment": "^2.29.4",
72
77
  "react": "^18.2.0",
73
78
  "react-dom": "^18.2.0",
79
+ "react-hook-form": "^7.41.3",
74
80
  "react-number-format": "^5.1.2",
75
81
  "string-mask": "^0.3.0",
76
82
  "yup": "^0.32.11"
@@ -81,5 +87,8 @@
81
87
  "last 1 version",
82
88
  "Firefox ESR",
83
89
  "not dead"
90
+ ],
91
+ "files": [
92
+ "dist"
84
93
  ]
85
94
  }
package/.eslintrc.js DELETED
@@ -1,14 +0,0 @@
1
- module.exports = {
2
- parser: '@typescript-eslint/parser',
3
- extends: ['plugin:@typescript-eslint/recommended', 'prettier', 'plugin:react-hooks/recommended'],
4
- parserOptions: {
5
- sourceType: 'module',
6
- },
7
- rules: {
8
- 'prettier/prettier': 'error',
9
- 'react-hooks/rules-of-hooks': 'error',
10
- 'react-hooks/exhaustive-deps': 'warn',
11
- 'tsdoc/syntax': 'warn'
12
- },
13
- plugins: ['@typescript-eslint', 'prettier', 'react-hooks', 'eslint-plugin-tsdoc'],
14
- }
package/.gitlab-ci.yml DELETED
@@ -1,14 +0,0 @@
1
- image: node:latest
2
-
3
- pages:
4
- cache:
5
- paths:
6
- - node_modules/
7
- script:
8
- - yarn install
9
- - yarn styleguide:build
10
- only:
11
- - master
12
- artifacts:
13
- paths:
14
- - public