drf-react-by-schema 0.24.5 → 0.25.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 (105) hide show
  1. package/dist/@types/index.d.ts +151 -91
  2. package/dist/@types/index.d.ts.map +1 -1
  3. package/dist/api.d.ts +22 -17
  4. package/dist/api.d.ts.map +1 -1
  5. package/dist/api.js +612 -572
  6. package/dist/components/DataGridBySchemaEditable/DataGridDesktop.d.ts +19 -19
  7. package/dist/components/DataGridBySchemaEditable/DataGridDesktop.d.ts.map +1 -1
  8. package/dist/components/DataGridBySchemaEditable/DataGridDesktop.js +7 -7
  9. package/dist/components/DataGridBySchemaEditable/DataGridMobile.d.ts +2 -2
  10. package/dist/components/DataGridBySchemaEditable/DataGridMobile.d.ts.map +1 -1
  11. package/dist/components/DataGridBySchemaEditable/SelectEditInputCell.d.ts +2 -2
  12. package/dist/components/DataGridBySchemaEditable/SelectEditInputCell.d.ts.map +1 -1
  13. package/dist/components/DataGridBySchemaEditable/hooks/useDataGridActions.d.ts +12 -12
  14. package/dist/components/DataGridBySchemaEditable/hooks/useDataGridActions.d.ts.map +1 -1
  15. package/dist/components/DataGridBySchemaEditable/hooks/useOptionsAC.d.ts +8 -8
  16. package/dist/components/DataGridBySchemaEditable/hooks/useOptionsAC.d.ts.map +1 -1
  17. package/dist/components/DataGridBySchemaEditable/hooks/useOptionsAC.js +5 -3
  18. package/dist/components/DataGridBySchemaEditable/hooks/usePreparedColumns.d.ts +5 -5
  19. package/dist/components/DataGridBySchemaEditable/hooks/usePreparedColumns.d.ts.map +1 -1
  20. package/dist/components/DataGridBySchemaEditable/hooks/useProcessRowUpdate.d.ts +13 -13
  21. package/dist/components/DataGridBySchemaEditable/hooks/useProcessRowUpdate.d.ts.map +1 -1
  22. package/dist/components/DataGridBySchemaEditable/hooks/useProcessRowUpdate.js +154 -155
  23. package/dist/components/DataGridBySchemaEditable/utils/columnFactory.d.ts +11 -12
  24. package/dist/components/DataGridBySchemaEditable/utils/columnFactory.d.ts.map +1 -1
  25. package/dist/components/DataGridBySchemaEditable/utils/columnFactory.js +149 -148
  26. package/dist/components/DataGridBySchemaEditable.d.ts +17 -17
  27. package/dist/components/DataGridBySchemaEditable.d.ts.map +1 -1
  28. package/dist/components/DataGridBySchemaEditable.js +44 -24
  29. package/dist/components/DataTotals.d.ts.map +1 -1
  30. package/dist/components/DataTotalsServer.d.ts +1 -1
  31. package/dist/components/DataTotalsServer.d.ts.map +1 -1
  32. package/dist/components/DataTotalsServer.js +7 -10
  33. package/dist/components/GenericModelList.d.ts +17 -18
  34. package/dist/components/GenericModelList.d.ts.map +1 -1
  35. package/dist/components/GenericModelList.js +2 -1
  36. package/dist/components/GenericRelatedModelList.d.ts +16 -17
  37. package/dist/components/GenericRelatedModelList.d.ts.map +1 -1
  38. package/dist/components/GenericRelatedModelList.js +2 -2
  39. package/dist/components/GenericRelatedModelListTable.d.ts +15 -15
  40. package/dist/components/GenericRelatedModelListTable.d.ts.map +1 -1
  41. package/dist/components/GenericRelatedModelListTable.js +3 -2
  42. package/dist/components/details/DetailBySchema.d.ts +2 -2
  43. package/dist/components/details/DetailBySchema.d.ts.map +1 -1
  44. package/dist/components/details/DetailBySchema.js +30 -21
  45. package/dist/components/forms/DialogFormBySchema.d.ts +9 -9
  46. package/dist/components/forms/DialogFormBySchema.d.ts.map +1 -1
  47. package/dist/components/forms/DialogFormBySchema.js +1 -1
  48. package/dist/components/forms/FieldBySchema.d.ts.map +1 -1
  49. package/dist/components/forms/FieldBySchema.js +4 -4
  50. package/dist/components/forms/Form.d.ts +17 -16
  51. package/dist/components/forms/Form.d.ts.map +1 -1
  52. package/dist/components/forms/Form.js +6 -4
  53. package/dist/components/forms/FormActions.d.ts +2 -2
  54. package/dist/components/forms/FormActions.d.ts.map +1 -1
  55. package/dist/components/forms/FormBySchema.d.ts +11 -11
  56. package/dist/components/forms/FormBySchema.d.ts.map +1 -1
  57. package/dist/components/forms/FormBySchema.js +45 -22
  58. package/dist/components/forms/FormWatcher.d.ts +7 -5
  59. package/dist/components/forms/FormWatcher.d.ts.map +1 -1
  60. package/dist/components/forms/FormWatcher.js +9 -5
  61. package/dist/components/forms/inputs/AutocompleteFieldBySchema.d.ts.map +1 -1
  62. package/dist/components/forms/inputs/DesktopDatePickerBySchema.js +2 -1
  63. package/dist/components/forms/inputs/DesktopDateTimePickerBySchema.js +2 -1
  64. package/dist/components/forms/inputs/EditableAutocompleteFieldBySchema.d.ts +12 -2
  65. package/dist/components/forms/inputs/EditableAutocompleteFieldBySchema.d.ts.map +1 -1
  66. package/dist/components/forms/inputs/EditableAutocompleteFieldBySchema.js +6 -5
  67. package/dist/components/forms/inputs/FileFieldBySchema.js +2 -1
  68. package/dist/components/forms/inputs/FloatFieldBySchema.js +2 -1
  69. package/dist/components/forms/inputs/PatternFieldBySchema.js +2 -1
  70. package/dist/components/forms/inputs/TextFieldBySchema.js +2 -1
  71. package/dist/context/APIWrapper.d.ts +7 -6
  72. package/dist/context/APIWrapper.d.ts.map +1 -1
  73. package/dist/context/APIWrapper.js +22 -18
  74. package/dist/context/APIWrapperContext.d.ts +51 -51
  75. package/dist/context/APIWrapperContext.d.ts.map +1 -1
  76. package/dist/context/APIWrapperContext.js +16 -18
  77. package/dist/context/DRFReactBySchemaProvider.d.ts.map +1 -1
  78. package/dist/context/PrivateContext.d.ts +6 -5
  79. package/dist/context/PrivateContext.d.ts.map +1 -1
  80. package/dist/utils/utils.d.ts +14 -6
  81. package/dist/utils/utils.d.ts.map +1 -1
  82. package/dist/utils/utils.js +16 -14
  83. package/package.json +4 -1
  84. package/dist/components/DataGridBySchemaEditable/hooks/useRowMode.d.ts +0 -18
  85. package/dist/components/DataGridBySchemaEditable/hooks/useRowMode.d.ts.map +0 -1
  86. package/dist/components/DataGridBySchemaEditable/hooks/useRowMode.js +0 -43
  87. package/dist/components/DialogJSONSchemaForm.d.ts +0 -13
  88. package/dist/components/DialogJSONSchemaForm.js +0 -20
  89. package/dist/components/FilteredButton.d.ts +0 -2
  90. package/dist/components/FilteredButton.js +0 -23
  91. package/dist/components/FilteredGridActionsCellItem.d.ts +0 -2
  92. package/dist/components/FilteredGridActionsCellItem.js +0 -29
  93. package/dist/components/FormButtons.d.ts +0 -23
  94. package/dist/components/FormButtons.d.ts.map +0 -1
  95. package/dist/components/FormButtons.js +0 -38
  96. package/dist/components/forms/inputs/PatternBySchema.d.ts +0 -4
  97. package/dist/components/forms/inputs/PatternBySchema.d.ts.map +0 -1
  98. package/dist/components/forms/inputs/PatternBySchema.js +0 -45
  99. package/dist/context/Form.d.ts +0 -21
  100. package/dist/context/Form.js +0 -95
  101. package/dist/context/FormContext.d.ts +0 -71
  102. package/dist/context/FormContext.js +0 -8
  103. package/dist/utils.d.ts +0 -60
  104. package/dist/utils.d.ts.map +0 -1
  105. package/dist/utils.js +0 -726
package/dist/utils.js DELETED
@@ -1,726 +0,0 @@
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 () {
19
- var ownKeys = function(o) {
20
- ownKeys = Object.getOwnPropertyNames || function (o) {
21
- var ar = [];
22
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
- return ar;
24
- };
25
- return ownKeys(o);
26
- };
27
- return function (mod) {
28
- if (mod && mod.__esModule) return mod;
29
- var result = {};
30
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
- __setModuleDefault(result, mod);
32
- return result;
33
- };
34
- })();
35
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
36
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
37
- return new (P || (P = Promise))(function (resolve, reject) {
38
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
39
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
40
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
41
- step((generator = generator.apply(thisArg, _arguments || [])).next());
42
- });
43
- };
44
- var __importDefault = (this && this.__importDefault) || function (mod) {
45
- return (mod && mod.__esModule) ? mod : { "default": mod };
46
- };
47
- Object.defineProperty(exports, "__esModule", { value: true });
48
- exports.convertToFormData = exports.slugify = exports.slugToCamelCase = exports.getPatternFormat = exports.isTmpId = exports.getTmpId = exports.errorProps = exports.populateValues = exports.getChoiceByValue = exports.emptyByType = void 0;
49
- exports.buildGenericYupValidationSchema = buildGenericYupValidationSchema;
50
- exports.loadSinglePageDataHelper = loadSinglePageDataHelper;
51
- exports.reducer = reducer;
52
- exports.buildDateFormatBySchema = buildDateFormatBySchema;
53
- exports.mergeFilterItems = mergeFilterItems;
54
- exports.initViewColumns = initViewColumns;
55
- exports.deepEqual = deepEqual;
56
- exports.shouldMemoUpdate = shouldMemoUpdate;
57
- exports.formatFileSize = formatFileSize;
58
- exports.getFieldsFromFieldsLayout = getFieldsFromFieldsLayout;
59
- exports.isFieldRequired = isFieldRequired;
60
- exports.showConditionalVisibleField = showConditionalVisibleField;
61
- const dayjs_1 = __importDefault(require("dayjs"));
62
- const Yup = __importStar(require("yup"));
63
- const string_mask_1 = __importDefault(require("string-mask"));
64
- const emptyByType = (field, forDatabase = false) => {
65
- if (field.model_default) {
66
- return field.model_default;
67
- }
68
- switch (field.type) {
69
- case 'nested object':
70
- // 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:
71
- return forDatabase ? {} : null;
72
- case 'field':
73
- if (field.child) {
74
- return [];
75
- }
76
- return forDatabase ? undefined : null;
77
- case 'list':
78
- return [];
79
- case 'string':
80
- case 'email':
81
- return '';
82
- case 'integer':
83
- return 0;
84
- case 'array':
85
- return [];
86
- case 'boolean':
87
- return false;
88
- default:
89
- return null;
90
- }
91
- };
92
- exports.emptyByType = emptyByType;
93
- const getChoiceByValue = (value, choices) => {
94
- if (!choices) {
95
- return null;
96
- }
97
- for (const choice of choices) {
98
- if (choice.value === value) {
99
- return choice.display_name;
100
- }
101
- }
102
- };
103
- exports.getChoiceByValue = getChoiceByValue;
104
- const populateValues = ({ data, schema }) => {
105
- var _a;
106
- const values = {};
107
- for (const [key, field] of Object.entries(schema)) {
108
- if (key === 'id' && (0, exports.isTmpId)(data[key])) {
109
- continue;
110
- }
111
- if (!data[key] && data[key] !== false && data[key] !== 0) {
112
- values[key] = (0, exports.emptyByType)(field);
113
- continue;
114
- }
115
- if (field.type === 'field' && ((_a = field.child) === null || _a === void 0 ? void 0 : _a.children)) {
116
- if (Array.isArray(data[key])) {
117
- const arValues = [];
118
- for (const row of data[key]) {
119
- const value = (0, exports.populateValues)({
120
- data: row,
121
- schema: field.child.children,
122
- });
123
- arValues.push(value);
124
- }
125
- values[key] = arValues;
126
- continue;
127
- }
128
- values[key] = (0, exports.populateValues)({
129
- data: data[key],
130
- schema: field.child.children,
131
- });
132
- continue;
133
- }
134
- if (field.type === 'choice') {
135
- values[key] = data[key]
136
- ? {
137
- value: data[key],
138
- display_name: (0, exports.getChoiceByValue)(data[key], field.choices),
139
- }
140
- : (0, exports.emptyByType)(field);
141
- continue;
142
- }
143
- values[key] = data[key];
144
- }
145
- return values;
146
- };
147
- exports.populateValues = populateValues;
148
- const getYupValidator = (type) => {
149
- let yupFunc;
150
- try {
151
- switch (type) {
152
- case 'slug':
153
- yupFunc = Yup.string();
154
- break;
155
- case 'email':
156
- yupFunc = Yup.string().email('Este campo deve ser um e-mail válido.');
157
- break;
158
- case 'integer':
159
- yupFunc = Yup.number().integer('Este campo deve ser um número inteiro');
160
- break;
161
- case 'choice':
162
- yupFunc = Yup.object();
163
- break;
164
- case 'list':
165
- yupFunc = Yup.object();
166
- break;
167
- case 'field':
168
- yupFunc = Yup.mixed();
169
- break;
170
- case 'nested object':
171
- yupFunc = Yup.object();
172
- break;
173
- case 'date':
174
- yupFunc = Yup.date();
175
- break;
176
- case 'string':
177
- yupFunc = Yup.string();
178
- break;
179
- case 'number':
180
- case 'decimal':
181
- yupFunc = Yup.number();
182
- break;
183
- case 'boolean':
184
- yupFunc = Yup.bool();
185
- break;
186
- case 'array':
187
- yupFunc = Yup.array();
188
- break;
189
- case 'object':
190
- case 'image':
191
- case 'image upload':
192
- case 'file':
193
- case 'file upload':
194
- yupFunc = Yup.object();
195
- break;
196
- default:
197
- yupFunc = Yup.string();
198
- break;
199
- }
200
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
201
- }
202
- catch (e) {
203
- yupFunc = Yup.string();
204
- }
205
- return yupFunc.transform((v) => (v === '' ? null : v)).nullable();
206
- };
207
- function buildGenericYupValidationSchema({ data, schema, many = false, skipFields = [], extraValidators, uiFields, }) {
208
- var _a;
209
- const yupValidator = {};
210
- for (const entry of Object.entries(schema)) {
211
- const [field, fieldSchema] = entry;
212
- if (!data || !(field in data) || field === 'id' || skipFields.includes(field)) {
213
- continue;
214
- }
215
- // OneToMany or ManyToMany:
216
- if (fieldSchema.type === 'field' && ((_a = fieldSchema.child) === null || _a === void 0 ? void 0 : _a.children)) {
217
- yupValidator[field] =
218
- extraValidators && field in extraValidators
219
- ? extraValidators[field]
220
- : buildGenericYupValidationSchema({
221
- schema: fieldSchema.child.children,
222
- many: true,
223
- data: data[field],
224
- uiFields,
225
- });
226
- continue;
227
- }
228
- // Nested Object:
229
- if (fieldSchema.type === 'nested object' && fieldSchema.children) {
230
- // yupValidator[key] = buildGenericYupValidationSchema({
231
- // schema: field.children,
232
- // many: false,
233
- // data: data[key],
234
- // extraValidators: key in extraValidators ? extraValidators[key] : {}
235
- // });
236
- // if (!field.model_required) {
237
- // yupValidator[key] = yupValidator[key].nullable();
238
- // }
239
- // continue;
240
- }
241
- yupValidator[field] =
242
- extraValidators && field in extraValidators
243
- ? extraValidators[field]
244
- : getYupValidator(fieldSchema.type);
245
- if (isFieldRequired(fieldSchema, (uiFields || []).includes(field))) {
246
- yupValidator[field] = yupValidator[field].required('Este campo é obrigatório');
247
- }
248
- if (fieldSchema.max_length && fieldSchema.type === 'string') {
249
- yupValidator[field] = yupValidator[field].max(parseInt(fieldSchema.max_length), `Este campo só pode ter no máximo ${fieldSchema.max_length} caracteres`);
250
- }
251
- if (fieldSchema.max_digits && fieldSchema.type === 'decimal') {
252
- const maxDigits = fieldSchema.max_digits;
253
- yupValidator[field] = yupValidator[field].test('len', `Este número pode ter no máximo ${maxDigits} dígitos`, (val) => {
254
- if (!val) {
255
- return true;
256
- }
257
- return val.toString().length <= maxDigits;
258
- });
259
- }
260
- if (!fieldSchema.read_only && fieldSchema.validators_regex) {
261
- for (const validator of fieldSchema.validators_regex) {
262
- yupValidator[field] = yupValidator[field].matches(validator.regex, validator.message);
263
- }
264
- }
265
- }
266
- return many ? Yup.array().of(Yup.object().shape(yupValidator)) : Yup.object().shape(yupValidator);
267
- }
268
- function loadSinglePageDataHelper({ data, schema, columns, fieldsLayout, defaults, extraValidators, }) {
269
- const mergedData = Object.assign({}, data);
270
- for (const [field, value] of Object.entries(defaults)) {
271
- if (!data[field] && typeof data[field] !== 'boolean') {
272
- mergedData[field] = value;
273
- }
274
- }
275
- const values = (0, exports.populateValues)({ data: mergedData, schema });
276
- const uiFields = fieldsLayout
277
- ? getFieldsFromFieldsLayout(fieldsLayout)
278
- : columns
279
- ? Object.keys(columns)
280
- : undefined;
281
- const validationSchema = buildGenericYupValidationSchema({
282
- data: values,
283
- schema,
284
- extraValidators,
285
- uiFields,
286
- });
287
- const customFieldLabelsAr = fieldsLayout === null || fieldsLayout === void 0 ? void 0 : fieldsLayout.map((fieldLayout) => fieldLayout.customLabels);
288
- const finalSchema = Object.assign({}, schema);
289
- if (customFieldLabelsAr) {
290
- for (const customFieldLabels of customFieldLabelsAr) {
291
- if (!customFieldLabels) {
292
- continue;
293
- }
294
- for (const key of Object.keys(customFieldLabels)) {
295
- if (customFieldLabels[key] && (!uiFields || uiFields.includes(key))) {
296
- finalSchema[key].label = customFieldLabels[key];
297
- }
298
- }
299
- }
300
- }
301
- return { values, finalSchema, validationSchema };
302
- }
303
- const errorProps = ({ errors, fieldKey, fieldKeyProp, index, }) => {
304
- let error;
305
- let helperText;
306
- if (index) {
307
- const hasErrors = errors &&
308
- errors[fieldKey] &&
309
- errors[fieldKey][index] &&
310
- Boolean(errors[fieldKey][index][fieldKeyProp]);
311
- error = hasErrors;
312
- helperText = hasErrors ? errors[fieldKey][index][fieldKeyProp].message : null;
313
- return { error, helperText };
314
- }
315
- const hasErrors = errors && errors[fieldKey] && Boolean(errors[fieldKey][fieldKeyProp]);
316
- error = hasErrors;
317
- helperText = hasErrors ? errors[fieldKey][fieldKeyProp].message : null;
318
- return { error, helperText };
319
- };
320
- exports.errorProps = errorProps;
321
- const getTmpId = () => {
322
- return 'tmp' + Math.floor(Math.random() * 1000000);
323
- };
324
- exports.getTmpId = getTmpId;
325
- const isTmpId = (id) => {
326
- if (!id) {
327
- return true;
328
- }
329
- return id === 'novo' || id.toString().slice(0, 3) === 'tmp';
330
- };
331
- exports.isTmpId = isTmpId;
332
- function reducer(state, newState) {
333
- if (newState === null) {
334
- return null;
335
- }
336
- if (state === null) {
337
- return newState;
338
- }
339
- return Object.assign(Object.assign({}, state), newState);
340
- }
341
- const getPatternFormat = (type) => {
342
- let format = '';
343
- switch (type) {
344
- case 'telefone':
345
- case 'fone':
346
- case 'phone':
347
- case 'contact':
348
- case 'contato':
349
- format = '(##)#####-####';
350
- break;
351
- case 'cpf':
352
- format = '###.###.###-##';
353
- break;
354
- case 'cnpj':
355
- format = '##.###.###/####-##';
356
- break;
357
- case 'cep':
358
- format = '##.###-###';
359
- break;
360
- }
361
- return format;
362
- };
363
- exports.getPatternFormat = getPatternFormat;
364
- function buildDateFormatBySchema(dateViews) {
365
- const defaultFormat = 'DD/MM/YYYY';
366
- if (!dateViews) {
367
- return defaultFormat;
368
- }
369
- const hasDay = dateViews.includes('day');
370
- const hasMonth = dateViews.includes('month');
371
- const hasYear = dateViews.includes('year');
372
- if (hasDay && hasMonth && hasYear) {
373
- return defaultFormat;
374
- }
375
- if (!hasDay && hasMonth && hasYear) {
376
- return 'MM/YYYY';
377
- }
378
- if (!hasDay && !hasMonth && hasYear) {
379
- return 'YYYY';
380
- }
381
- if (!hasDay && hasMonth && !hasYear) {
382
- return 'MM';
383
- }
384
- if (hasDay && !hasMonth && !hasYear) {
385
- return 'DD';
386
- }
387
- return defaultFormat;
388
- }
389
- const slugToCamelCase = (str, includeFirst = false) => {
390
- if (!str) {
391
- return '';
392
- }
393
- const ret = str.replace(/_([a-z])/g, function (g) {
394
- return g[1].toUpperCase();
395
- });
396
- if (includeFirst) {
397
- return ret[0].toUpperCase() + ret.slice(1);
398
- }
399
- return ret;
400
- };
401
- exports.slugToCamelCase = slugToCamelCase;
402
- const slugify = (text) => {
403
- if (!text) {
404
- return '';
405
- }
406
- return text
407
- .toString()
408
- .normalize('NFD')
409
- .replace(/[\u0300-\u036f]/g, '')
410
- .toLowerCase()
411
- .trim()
412
- .replace(/\s+/g, '-')
413
- .replace(/[^\w-]+/g, '')
414
- .replace(/--+/g, '-');
415
- };
416
- exports.slugify = slugify;
417
- function mergeFilterItems(defaultFilter, filter) {
418
- if (!filter && defaultFilter) {
419
- return defaultFilter;
420
- }
421
- if (!defaultFilter && filter) {
422
- return filter;
423
- }
424
- if (filter && defaultFilter) {
425
- const items = filter.items;
426
- const defaultItems = defaultFilter.items;
427
- const mergedItems = defaultItems.map((defaultItem) => {
428
- const itemFound = items.find((item) => item.field === defaultItem.field);
429
- if (itemFound) {
430
- return itemFound;
431
- }
432
- return defaultItem;
433
- });
434
- const unMergedItems = items.filter((item) => !defaultItems.map((defaultItem) => defaultItem.field).includes(item.field));
435
- return Object.assign(Object.assign({}, filter), { items: [...mergedItems, ...unMergedItems] });
436
- }
437
- return undefined;
438
- }
439
- function initViewColumns(_a) {
440
- return __awaiter(this, arguments, void 0, function* ({ schema, columns, customColumnOperations, }) {
441
- const colsPromises = columns.map((column) => __awaiter(this, void 0, void 0, function* () {
442
- const field = column.field;
443
- if (!schema[field]) {
444
- return column;
445
- }
446
- const fieldSchema = schema[field];
447
- // const isIndexField = indexField !== '' && field === indexField;
448
- // let column = col;
449
- // if (isIndexField) {
450
- // col.isIndexField = true;
451
- // }
452
- switch (fieldSchema.type) {
453
- case 'date':
454
- column.type = 'date';
455
- const dateFormat = buildDateFormatBySchema(fieldSchema.date_views);
456
- column.valueFormatter = (value) => (value ? (0, dayjs_1.default)(value).format(dateFormat) : '');
457
- break;
458
- case 'datetime':
459
- column.type = 'dateTime';
460
- column.valueFormatter = (value) => (value ? (0, dayjs_1.default)(value).format('DD/MM/YYYY HH:mm') : '');
461
- break;
462
- case 'nested object':
463
- column.valueFormatter = (value) => {
464
- return !value ? '' : value.label;
465
- };
466
- break;
467
- case 'field':
468
- column.valueFormatter = (value) => {
469
- return !value || !Array.isArray(value) ? '' : value.map((val) => val.label).join(', ');
470
- };
471
- break;
472
- case 'choice':
473
- column.valueFormatter = (value) => {
474
- return !value ? '' : value.display_name;
475
- };
476
- break;
477
- case 'list':
478
- column.valueFormatter = (value) => {
479
- return !value || !Array.isArray(value)
480
- ? ''
481
- : value.map((val) => val.display_name).join(', ');
482
- };
483
- break;
484
- case 'boolean':
485
- column.valueFormatter = (value) => {
486
- return value ? 'Sim' : 'Não';
487
- };
488
- break;
489
- case 'decimal':
490
- case 'float':
491
- const decimalScale = fieldSchema.decimal_places || 2;
492
- column.type = 'number';
493
- column.valueFormatter = (value) => {
494
- return !value
495
- ? ''
496
- : parseFloat(value).toLocaleString('pt-BR', {
497
- minimumFractionDigits: decimalScale,
498
- maximumFractionDigits: decimalScale,
499
- });
500
- };
501
- break;
502
- case 'number':
503
- case 'integer':
504
- column.type = 'number';
505
- if (fieldSchema.pattern_format) {
506
- column.patternFormat = fieldSchema.pattern_format;
507
- }
508
- break;
509
- case 'file upload':
510
- case 'image upload':
511
- column.valueFormatter = (value) => {
512
- return !value ? '' : `${value.name.split('/').pop()} (${formatFileSize(value.size)})`;
513
- };
514
- default:
515
- if (fieldSchema.pattern_format) {
516
- column.patternFormat = fieldSchema.pattern_format;
517
- }
518
- }
519
- // format numbers:
520
- if (column.patternFormat) {
521
- column.valueFormatter = (value) => {
522
- if (!value || typeof value !== 'string') {
523
- return value;
524
- }
525
- const formattedValue = new string_mask_1.default(column.patternFormat, {}).apply(value);
526
- return formattedValue;
527
- };
528
- }
529
- // Custom column operations:
530
- if (customColumnOperations) {
531
- column = yield customColumnOperations(column);
532
- }
533
- // Finally!
534
- return column;
535
- }));
536
- const finalCols = yield Promise.all(colsPromises);
537
- return finalCols;
538
- });
539
- }
540
- function deepEqual(a, b, maxIterations = 10000) {
541
- const stack = [[a, b, 0]];
542
- let iterations = 0;
543
- while (stack.length > 0) {
544
- iterations++;
545
- if (iterations > maxIterations)
546
- return false;
547
- const [currentA, currentB] = stack.pop();
548
- // 1. Strict equality (handles same reference, same primitives)
549
- if (currentA === currentB)
550
- continue;
551
- // 2. Handle Functions (toString comparison)
552
- if (typeof currentA === 'function' && typeof currentB === 'function') {
553
- if (currentA.toString() !== currentB.toString())
554
- return false;
555
- continue;
556
- }
557
- // 3. Handle Dates (Object.keys doesn't work on Dates)
558
- if (currentA instanceof Date && currentB instanceof Date) {
559
- if (currentA.getTime() !== currentB.getTime())
560
- return false;
561
- continue;
562
- }
563
- // 4. Handle Null/Undefined/Primitives that failed the === check
564
- if (currentA === null ||
565
- currentB === null ||
566
- typeof currentA !== 'object' ||
567
- typeof currentB !== 'object') {
568
- return false;
569
- }
570
- // 5. Handle Arrays
571
- if (Array.isArray(currentA) && Array.isArray(currentB)) {
572
- if (currentA.length !== currentB.length)
573
- return false;
574
- for (let i = currentA.length - 1; i >= 0; i--) {
575
- stack.push([currentA[i], currentB[i], 0]);
576
- }
577
- continue;
578
- }
579
- // 6. Handle Objects
580
- const keysA = Object.keys(currentA);
581
- const keysB = Object.keys(currentB);
582
- if (keysA.length !== keysB.length)
583
- return false;
584
- for (const key of keysA) {
585
- if (!Object.prototype.hasOwnProperty.call(currentB, key))
586
- return false;
587
- stack.push([currentA[key], currentB[key], 0]);
588
- }
589
- }
590
- return true;
591
- }
592
- function shouldMemoUpdate(prevProps, nextProps) {
593
- const propKeys = new Set([...Object.keys(prevProps), ...Object.keys(nextProps)]);
594
- for (const key of propKeys) {
595
- if (typeof key === 'string' && key.startsWith('_'))
596
- continue;
597
- const prevVal = prevProps[key];
598
- const nextVal = nextProps[key];
599
- // Fast path for identical references
600
- if (prevVal === nextVal)
601
- continue;
602
- // Handle null/undefined mismatches
603
- if (prevVal == null || nextVal == null) {
604
- return false;
605
- }
606
- // Special handling for functions
607
- if (typeof prevVal === 'function' && typeof nextVal === 'function') {
608
- if (prevVal.toString() !== nextVal.toString()) {
609
- return false;
610
- }
611
- continue;
612
- }
613
- // Deep compare objects
614
- if (typeof prevVal === 'object' && typeof nextVal === 'object') {
615
- if (!deepEqual(prevVal, nextVal)) {
616
- return false;
617
- }
618
- continue;
619
- }
620
- // Final comparison for primitives
621
- if (prevVal !== nextVal) {
622
- return false;
623
- }
624
- }
625
- return true;
626
- }
627
- const convertToFormData = (data) => {
628
- const formData = new FormData();
629
- Object.entries(data).forEach(([key, value]) => {
630
- if (value === null) {
631
- formData.append(key, ''); // Handle null values
632
- }
633
- else if (Array.isArray(value)) {
634
- value.forEach((item) => {
635
- if (typeof item === 'object') {
636
- formData.append(`${key}[]`, JSON.stringify(item));
637
- }
638
- });
639
- }
640
- else if (typeof value === 'object') {
641
- formData.append(key, 'file' in value ? value.file : JSON.stringify(value));
642
- }
643
- else {
644
- formData.append(key, value);
645
- }
646
- });
647
- return formData;
648
- };
649
- exports.convertToFormData = convertToFormData;
650
- function formatFileSize(bytes) {
651
- if (bytes === 0)
652
- return '0 B';
653
- const units = ['B', 'KB', 'MB', 'GB'];
654
- const k = 1024;
655
- const dm = 1;
656
- const i = Math.floor(Math.log(bytes) / Math.log(k));
657
- const value = bytes / Math.pow(k, i);
658
- const formattedValue = value.toLocaleString('pt-BR', {
659
- minimumFractionDigits: 0,
660
- maximumFractionDigits: dm,
661
- });
662
- return `${formattedValue} ${units[i]}`;
663
- }
664
- function getFieldsFromFieldsLayout(fieldsLayout) {
665
- const fields = [];
666
- for (const section of fieldsLayout) {
667
- if (!section.rows) {
668
- continue;
669
- }
670
- for (const row of section.rows) {
671
- if (typeof row === 'string') {
672
- fields.push(row);
673
- continue;
674
- }
675
- if (Array.isArray(row)) {
676
- for (const field of row) {
677
- if (typeof field === 'string') {
678
- fields.push(field);
679
- continue;
680
- }
681
- }
682
- continue;
683
- }
684
- }
685
- }
686
- return fields;
687
- }
688
- function isFieldRequired(fieldSchema, isInForm) {
689
- if ((fieldSchema === null || fieldSchema === void 0 ? void 0 : fieldSchema.type) === 'boolean') {
690
- return false;
691
- }
692
- if (fieldSchema === null || fieldSchema === void 0 ? void 0 : fieldSchema.required) {
693
- return true;
694
- }
695
- if (fieldSchema === null || fieldSchema === void 0 ? void 0 : fieldSchema.model_required) {
696
- return true;
697
- }
698
- if ((fieldSchema === null || fieldSchema === void 0 ? void 0 : fieldSchema.ui_required) && (typeof isInForm === 'undefined' || isInForm === true)) {
699
- return true;
700
- }
701
- return false;
702
- }
703
- function showConditionalVisibleField(field, conditionalVisible, values) {
704
- if (typeof field !== 'string' ||
705
- !conditionalVisible ||
706
- conditionalVisible.filter((item) => item.target === field).length === 0 ||
707
- !values) {
708
- return true;
709
- }
710
- const sources = conditionalVisible
711
- .map((item, index) => {
712
- const refField = item.target;
713
- const refValue = item.sourceValue;
714
- const value = values[index];
715
- return refField === field &&
716
- (value === refValue ||
717
- (value && value.value === refValue) ||
718
- (Array.isArray(value) &&
719
- value.find((singleValue) => singleValue === refValue ||
720
- (typeof singleValue === 'object' && singleValue.value === refValue))))
721
- ? item
722
- : { target: false };
723
- })
724
- .filter((item) => item.target === field);
725
- return sources.length > 0;
726
- }