intelicoreact 1.4.98 → 1.4.99
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.
|
@@ -27,6 +27,7 @@ const uploadTriggerStucture = {
|
|
|
27
27
|
accept: ACCEPT_EXTENSIONS_DEFAULT
|
|
28
28
|
};
|
|
29
29
|
const getStructure = data => ({
|
|
30
|
+
//? Расчет на то, что data донесет свой id и перетрёт пустые велью
|
|
30
31
|
value: "",
|
|
31
32
|
base64: "",
|
|
32
33
|
maxItemSizeBytes: MAX_FILE_SIZE,
|
|
@@ -54,27 +55,49 @@ const accordionData = (accordionTitle, accordionListContent) => {
|
|
|
54
55
|
}, item.description)));
|
|
55
56
|
})));
|
|
56
57
|
};
|
|
58
|
+
const getSafelyInitFiles = (initFiles, maxFilesCount) => {
|
|
59
|
+
if (!Array.isArray(initFiles)) return [];
|
|
60
|
+
let output = (0, _utils.clone)(initFiles);
|
|
61
|
+
if (typeof maxFilesCount === 'number' && output.length > maxFilesCount) {
|
|
62
|
+
output = output.slice(0, maxFilesCount);
|
|
63
|
+
}
|
|
64
|
+
return output.map(getStructure);
|
|
65
|
+
};
|
|
66
|
+
const checkOverflow = (files, maxFilesCount, additional) => {
|
|
67
|
+
const {
|
|
68
|
+
error = "",
|
|
69
|
+
outerError = ""
|
|
70
|
+
} = additional || {};
|
|
71
|
+
return typeof maxFilesCount === 'number' && files.length < maxFilesCount ? [...files, {
|
|
72
|
+
...(0, _utils.clone)(uploadTriggerStucture),
|
|
73
|
+
error,
|
|
74
|
+
outerError
|
|
75
|
+
}] : files;
|
|
76
|
+
};
|
|
57
77
|
const FileLoaderDescription = _ref => {
|
|
58
|
-
var _files$map;
|
|
59
78
|
let {
|
|
60
79
|
//? Для корректности работы стоит пробрасывать какой-то из id
|
|
61
80
|
id,
|
|
62
81
|
testId,
|
|
63
82
|
fieldKey,
|
|
64
|
-
|
|
83
|
+
files: initFiles,
|
|
65
84
|
onChange,
|
|
85
|
+
className,
|
|
66
86
|
filesDescription = "Documents must be less than 10MB and must be in PDF, JPG, JPEG or PNG format.",
|
|
67
87
|
documentName = "Document name",
|
|
68
88
|
accordionTitle = "There is a list of documents that we accept:",
|
|
69
89
|
accordionListContent = [],
|
|
70
90
|
maxFilesCount = 10,
|
|
71
91
|
isWithoutAccordion,
|
|
72
|
-
files,
|
|
73
92
|
error: outerError,
|
|
74
93
|
...props
|
|
75
94
|
} = _ref;
|
|
76
95
|
const uniqueFileLoaderDescriptionId = "".concat(id || testId || fieldKey || "component-".concat(_utils.uuid === null || _utils.uuid === void 0 ? void 0 : (0, _utils.uuid)()));
|
|
77
|
-
const
|
|
96
|
+
const safelyInitFales = (0, _react.useMemo)(() => getSafelyInitFiles(initFiles, maxFilesCount), [initFiles, maxFilesCount]);
|
|
97
|
+
const [files, setFiles] = (0, _react.useState)((() => {
|
|
98
|
+
if (!safelyInitFales.length) return [(0, _utils.clone)(uploadTriggerStucture)];
|
|
99
|
+
return checkOverflow(safelyInitFales, maxFilesCount);
|
|
100
|
+
})());
|
|
78
101
|
const generateUniqueFileName = (files, name) => {
|
|
79
102
|
let fileName = name;
|
|
80
103
|
const [nameWithoutExtension, extension] = name.split(/(?=\.[^.]+$)/);
|
|
@@ -86,17 +109,8 @@ const FileLoaderDescription = _ref => {
|
|
|
86
109
|
return fileName;
|
|
87
110
|
};
|
|
88
111
|
const handle = {
|
|
89
|
-
checkOverflow: function (fields) {
|
|
90
|
-
let error = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "";
|
|
91
|
-
let outerError = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : "";
|
|
92
|
-
return fields.length < maxFilesCount ? [...fields, {
|
|
93
|
-
...(0, _utils.clone)(uploadTriggerStucture),
|
|
94
|
-
error,
|
|
95
|
-
outerError
|
|
96
|
-
}] : fields;
|
|
97
|
-
},
|
|
98
112
|
applyFields: newFields => {
|
|
99
|
-
|
|
113
|
+
setFiles(checkOverflow(newFields, maxFilesCount));
|
|
100
114
|
onChange(newFields);
|
|
101
115
|
},
|
|
102
116
|
change: _ref2 => {
|
|
@@ -107,7 +121,7 @@ const FileLoaderDescription = _ref => {
|
|
|
107
121
|
file
|
|
108
122
|
} = _ref2;
|
|
109
123
|
//? Работаем с филдами БЕЗ плашки добавления
|
|
110
|
-
let output = (0, _utils.clone)(
|
|
124
|
+
let output = (0, _utils.clone)(files).filter(item => item.value && item.base64).map(item => ({
|
|
111
125
|
...item,
|
|
112
126
|
error: ""
|
|
113
127
|
}));
|
|
@@ -122,7 +136,9 @@ const FileLoaderDescription = _ref => {
|
|
|
122
136
|
error
|
|
123
137
|
} : item);
|
|
124
138
|
//? Установка в локальный стейт БЕЗ верхнего onChange (файл по итогу НЕ загрузили, НЕ мутируем верхнее велью)
|
|
125
|
-
|
|
139
|
+
setFiles(checkOverflow(output, maxFilesCount, {
|
|
140
|
+
error: !changedField ? error : null
|
|
141
|
+
}));
|
|
126
142
|
} else {
|
|
127
143
|
//? Во всех остальных случаях - происходит добавленеи/изменение содержимого
|
|
128
144
|
const uniqueFileName = generateUniqueFileName(output, name);
|
|
@@ -142,8 +158,21 @@ const FileLoaderDescription = _ref => {
|
|
|
142
158
|
}
|
|
143
159
|
};
|
|
144
160
|
(0, _react.useEffect)(() => {
|
|
145
|
-
|
|
161
|
+
setFiles(checkOverflow(files.filter(item => item.id !== UPLOAD_TRIGGER_KEY), maxFilesCount, {
|
|
162
|
+
outerError
|
|
163
|
+
}));
|
|
146
164
|
}, [outerError]);
|
|
165
|
+
|
|
166
|
+
//? Обеспечивает чтобы верхний стейт был доминирующим
|
|
167
|
+
//? Осознанно и намерянно в зависимости стоит initFiles, но расчеты идут от safelyInitFales
|
|
168
|
+
(0, _react.useEffect)(() => {
|
|
169
|
+
//? Если изменились верхние филды и они отличаются от локальных (без триггера) - перезаписать
|
|
170
|
+
if (!(0, _utils.compare)(safelyInitFales, (0, _utils.clone)(files).filter(item => item.id !== UPLOAD_TRIGGER_KEY))) {
|
|
171
|
+
setFiles(checkOverflow(safelyInitFales, maxFilesCount, {
|
|
172
|
+
outerError
|
|
173
|
+
}));
|
|
174
|
+
}
|
|
175
|
+
}, [initFiles, maxFilesCount]);
|
|
147
176
|
return /*#__PURE__*/_react.default.createElement("div", {
|
|
148
177
|
id: uniqueFileLoaderDescriptionId,
|
|
149
178
|
testId: uniqueFileLoaderDescriptionId,
|
|
@@ -160,7 +189,7 @@ const FileLoaderDescription = _ref => {
|
|
|
160
189
|
}), !!filesDescription && /*#__PURE__*/_react.default.createElement("span", {
|
|
161
190
|
className: "".concat(RC, "__file-descr"),
|
|
162
191
|
title: filesDescription
|
|
163
|
-
}, filesDescription),
|
|
192
|
+
}, filesDescription), files.map((field, i) => {
|
|
164
193
|
return /*#__PURE__*/_react.default.createElement(_FileLoaderLocal.default, {
|
|
165
194
|
className: "".concat(RC, "__list-item"),
|
|
166
195
|
key: "".concat(field.id, "-").concat(i),
|
|
@@ -173,7 +202,7 @@ const FileLoaderDescription = _ref => {
|
|
|
173
202
|
value: field.value,
|
|
174
203
|
maxItemSizeBytes: field.maxItemSizeBytes,
|
|
175
204
|
onChange: value => handle.change(value),
|
|
176
|
-
isRequired:
|
|
205
|
+
isRequired: files.length <= 1,
|
|
177
206
|
isNoTrashIcon: false
|
|
178
207
|
});
|
|
179
208
|
}));
|