@itcase/forms 1.0.67 → 1.0.69
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.
- package/dist/itcase-forms.cjs.js +271 -282
- package/dist/itcase-forms.esm.js +271 -282
- package/package.json +1 -1
package/dist/itcase-forms.esm.js
CHANGED
|
@@ -16,9 +16,9 @@ import { Choice } from '@itcase/ui/components/Choice';
|
|
|
16
16
|
import { Code } from '@itcase/ui/components/Code';
|
|
17
17
|
import { DatePickerInput } from '@itcase/ui/components/DatePicker';
|
|
18
18
|
import axios from 'axios';
|
|
19
|
-
import { useDropzone } from 'react-dropzone';
|
|
20
19
|
import { fromEvent } from 'file-selector';
|
|
21
20
|
import castArray from 'lodash/castArray';
|
|
21
|
+
import { useDropzone, ErrorCode } from 'react-dropzone';
|
|
22
22
|
import { createFileFromDataURL } from '@itcase/common';
|
|
23
23
|
import { Loader } from '@itcase/ui/components/Loader';
|
|
24
24
|
import { Title } from '@itcase/ui/components/Title';
|
|
@@ -882,160 +882,106 @@ DatePickerField.propTypes = {
|
|
|
882
882
|
onChange: PropTypes.func
|
|
883
883
|
};
|
|
884
884
|
|
|
885
|
-
|
|
886
|
-
try {
|
|
887
|
-
const response = await axios({
|
|
888
|
-
url,
|
|
889
|
-
responseType: 'blob'
|
|
890
|
-
});
|
|
891
|
-
const blobObject = response.data;
|
|
892
|
-
const dirtyFilename = response.headers['content-disposition']?.split('filename=')[1];
|
|
893
|
-
// Remove double quotes
|
|
894
|
-
let filename = dirtyFilename?.substring(1).slice(0, -1);
|
|
895
|
-
if (!filename) {
|
|
896
|
-
const typeParts = blobObject.type.split('/');
|
|
897
|
-
const fileType = typeParts[typeParts.length - 1];
|
|
898
|
-
filename = `${new Date().getTime()}.${fileType}`;
|
|
899
|
-
}
|
|
900
|
-
return new File([blobObject], filename, {
|
|
901
|
-
type: blobObject.type
|
|
902
|
-
});
|
|
903
|
-
} catch (error) {
|
|
904
|
-
console.log('error: ', error);
|
|
905
|
-
return null;
|
|
906
|
-
}
|
|
907
|
-
}
|
|
908
|
-
async function convertToFiles(inputValue, isPreviews) {
|
|
909
|
-
const newFiles = [];
|
|
910
|
-
for await (const value of castArray(inputValue)) {
|
|
911
|
-
let newFile = null;
|
|
912
|
-
|
|
913
|
-
// Download image by url and save as File instance
|
|
914
|
-
const isURL = typeof value === 'string' && value.includes('/');
|
|
915
|
-
if (value.image || isURL) {
|
|
916
|
-
newFile = await getFileByURL(value.image || value);
|
|
917
|
-
if (newFile) {
|
|
918
|
-
setFileDataURL(newFile);
|
|
919
|
-
}
|
|
920
|
-
}
|
|
921
|
-
|
|
922
|
-
// Convert dataURL to File instance
|
|
923
|
-
if (value.dataURL) {
|
|
924
|
-
newFile = createFileFromDataURL(value.name || value.path, value.dataURL);
|
|
925
|
-
newFile.dataURL = value.dataURL;
|
|
926
|
-
}
|
|
927
|
-
|
|
928
|
-
// Save new File to state
|
|
929
|
-
if (newFile) {
|
|
930
|
-
newFile.id = value.id;
|
|
931
|
-
if (isPreviews) {
|
|
932
|
-
newFile.preview = URL.createObjectURL(newFile);
|
|
933
|
-
}
|
|
934
|
-
newFiles.push(newFile);
|
|
935
|
-
}
|
|
936
|
-
|
|
937
|
-
// else if (value) {
|
|
938
|
-
// newFiles.push({
|
|
939
|
-
// name: value,
|
|
940
|
-
// error: 'File is unavailable',
|
|
941
|
-
// })
|
|
942
|
-
// }
|
|
943
|
-
}
|
|
944
|
-
return newFiles;
|
|
945
|
-
}
|
|
946
|
-
function setFileDataURL(file, resolve) {
|
|
947
|
-
resolve = resolve || (() => {});
|
|
948
|
-
// Init reader and save his file
|
|
949
|
-
const reader = new FileReader();
|
|
950
|
-
reader._readedFile = file;
|
|
885
|
+
/* globals File, FileReader, URL, console */
|
|
951
886
|
|
|
952
|
-
// Set handlers
|
|
953
|
-
reader.onabort = () => resolve();
|
|
954
|
-
reader.onerror = () => resolve();
|
|
955
|
-
reader.onload = event => {
|
|
956
|
-
event.target._readedFile.dataURL = reader.result;
|
|
957
|
-
resolve();
|
|
958
|
-
};
|
|
959
|
-
// Run reader
|
|
960
|
-
if (file instanceof File) {
|
|
961
|
-
reader.readAsDataURL(file);
|
|
962
|
-
} else {
|
|
963
|
-
resolve();
|
|
964
|
-
}
|
|
965
|
-
}
|
|
966
887
|
const FileInputDropzone = /*#__PURE__*/React.memo(function FileInputDropzone(props) {
|
|
967
888
|
const {
|
|
968
|
-
|
|
969
|
-
size,
|
|
889
|
+
isPreviews,
|
|
970
890
|
className,
|
|
891
|
+
dropzoneProps = {},
|
|
892
|
+
errorMessageTextColor,
|
|
893
|
+
errorMessageTextSize,
|
|
894
|
+
errorMessageTextWeight,
|
|
971
895
|
fileErrorText,
|
|
972
|
-
|
|
973
|
-
removeThumbTextSize,
|
|
974
|
-
removeThumbTextColor,
|
|
975
|
-
removeThumbTextWeight,
|
|
976
|
-
thumbNameTextSize,
|
|
977
|
-
thumbNameTextColor,
|
|
978
|
-
removeThumbTextHoverColor,
|
|
979
|
-
thumbNameTextWrap,
|
|
980
|
-
thumbNameTextWeight,
|
|
981
|
-
hintTitleTextColor,
|
|
982
|
-
hintTitleTextWrap,
|
|
983
|
-
thumbColumn,
|
|
984
|
-
hintTitleTextWeight,
|
|
985
|
-
hintDescriptionTextSize,
|
|
896
|
+
hintDescription,
|
|
986
897
|
hintDescriptionTextColor,
|
|
987
|
-
|
|
898
|
+
hintDescriptionTextSize,
|
|
988
899
|
hintDescriptionTextWeight,
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
900
|
+
hintDescriptionTextWrap,
|
|
901
|
+
hintTitle,
|
|
902
|
+
hintTitleTextColor,
|
|
903
|
+
hintTitleTextSize,
|
|
904
|
+
hintTitleTextWeight,
|
|
905
|
+
hintTitleTextWrap,
|
|
906
|
+
inputName,
|
|
992
907
|
inputValue,
|
|
993
908
|
maxFiles,
|
|
994
909
|
maxSize,
|
|
995
910
|
removeThumbText,
|
|
996
|
-
|
|
997
|
-
|
|
911
|
+
removeThumbTextColor,
|
|
912
|
+
removeThumbTextHoverColor,
|
|
913
|
+
removeThumbTextSize,
|
|
914
|
+
removeThumbTextWeight,
|
|
998
915
|
showFilename,
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
916
|
+
size,
|
|
917
|
+
thumbColumn,
|
|
918
|
+
thumbNameTextColor,
|
|
919
|
+
thumbNameTextSize,
|
|
920
|
+
thumbNameTextWeight,
|
|
921
|
+
thumbNameTextWrap,
|
|
1002
922
|
onAddFiles,
|
|
1003
923
|
onDeleteFile
|
|
1004
924
|
} = props;
|
|
1005
|
-
const [fileError, setFileError] = useState('');
|
|
1006
|
-
const [fileIsLoading, setFileIsLoading] = useState(false);
|
|
1007
|
-
// State with instances of "File" type
|
|
1008
|
-
const [files, setFiles] = useState(inputValue ? castArray(inputValue) : []);
|
|
1009
925
|
|
|
1010
|
-
//
|
|
926
|
+
// TODO: delete react-final-form things out of here?
|
|
1011
927
|
const {
|
|
1012
928
|
change
|
|
1013
929
|
} = useForm();
|
|
930
|
+
const [fileError, setFileError] = useState('');
|
|
931
|
+
const [fileIsLoading, setFileIsLoading] = useState(false);
|
|
932
|
+
const filesList = useMemo(() => inputValue ? castArray(inputValue) : [], [inputValue]);
|
|
1014
933
|
const changeFormState = useCallback(newFiles => {
|
|
1015
934
|
// If max files in dropzone is 1 - return file as it self, else as array of files
|
|
1016
935
|
// ps: for old projects compatibility
|
|
1017
936
|
const toSave = dropzoneProps.maxFiles == 1 ? newFiles[0] : newFiles;
|
|
1018
937
|
change(inputName, toSave);
|
|
1019
938
|
return toSave;
|
|
1020
|
-
},
|
|
939
|
+
},
|
|
940
|
+
// If "inputName" will be changes, then it should be a different field
|
|
941
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
942
|
+
[dropzoneProps, change]);
|
|
943
|
+
|
|
944
|
+
//
|
|
945
|
+
const convertFiledValueAndSaveAsFiles = useCallback(async newInputValue => {
|
|
946
|
+
setFileIsLoading(true);
|
|
947
|
+
const newFiles = await convertToFiles(newInputValue, isPreviews);
|
|
948
|
+
changeFormState(newFiles);
|
|
949
|
+
setFileIsLoading(false);
|
|
950
|
+
return newFiles;
|
|
951
|
+
}, [isPreviews, changeFormState]);
|
|
952
|
+
|
|
953
|
+
// Delete file from dropzone
|
|
954
|
+
const removeFile = useCallback((event, index) => {
|
|
955
|
+
event.stopPropagation();
|
|
956
|
+
event.preventDefault();
|
|
957
|
+
const newFiles = [...filesList];
|
|
958
|
+
newFiles.splice(index, 1);
|
|
959
|
+
if (onDeleteFile) {
|
|
960
|
+
onDeleteFile(filesList[index], inputName);
|
|
961
|
+
}
|
|
962
|
+
changeFormState(newFiles);
|
|
963
|
+
},
|
|
964
|
+
// If "inputName" will be changes, then it should be a different field
|
|
965
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
966
|
+
[filesList, changeFormState, onDeleteFile]);
|
|
1021
967
|
|
|
1022
968
|
// Create dropzone options
|
|
1023
969
|
const {
|
|
1024
|
-
|
|
1025
|
-
|
|
970
|
+
getInputProps,
|
|
971
|
+
getRootProps
|
|
1026
972
|
} = useDropzone({
|
|
973
|
+
maxFiles: maxFiles || 5,
|
|
1027
974
|
maxSize: maxSize || 10485760,
|
|
1028
975
|
// 10mb
|
|
1029
|
-
maxFiles: maxFiles || 5,
|
|
1030
976
|
// accept: { 'image/*': [] },
|
|
1031
977
|
...dropzoneProps,
|
|
1032
978
|
getFilesFromEvent: async event => {
|
|
1033
979
|
const result = await fromEvent(event);
|
|
1034
980
|
const newFiles = result.filter(item => item instanceof File);
|
|
1035
981
|
// Add exists and new files to accepted(or rejected)
|
|
1036
|
-
return [...
|
|
982
|
+
return [...filesList, ...newFiles];
|
|
1037
983
|
},
|
|
1038
|
-
onDropAccepted:
|
|
984
|
+
onDropAccepted: acceptedFiles => {
|
|
1039
985
|
// If dropped files has accepted and we need a previews
|
|
1040
986
|
if (isPreviews) {
|
|
1041
987
|
// Add preview to every file
|
|
@@ -1046,7 +992,7 @@ const FileInputDropzone = /*#__PURE__*/React.memo(function FileInputDropzone(pro
|
|
|
1046
992
|
});
|
|
1047
993
|
}
|
|
1048
994
|
// Save to form data (including empty when files are not valid)
|
|
1049
|
-
|
|
995
|
+
const filesToSave = changeFormState(acceptedFiles);
|
|
1050
996
|
setFileError('');
|
|
1051
997
|
|
|
1052
998
|
// Save DataURL for all files
|
|
@@ -1055,65 +1001,38 @@ const FileInputDropzone = /*#__PURE__*/React.memo(function FileInputDropzone(pro
|
|
|
1055
1001
|
});
|
|
1056
1002
|
// Save files to form values
|
|
1057
1003
|
Promise.all(readerPromisesList).then(() => {
|
|
1058
|
-
const filesToSave = changeFormState(acceptedFiles);
|
|
1059
1004
|
if (onAddFiles) {
|
|
1060
1005
|
onAddFiles(filesToSave, inputName);
|
|
1061
1006
|
}
|
|
1062
1007
|
});
|
|
1063
1008
|
},
|
|
1064
|
-
onDropRejected:
|
|
1009
|
+
onDropRejected: rejectedFiles => {
|
|
1065
1010
|
// If dropped files has rejected
|
|
1066
1011
|
if (rejectedFiles.length) {
|
|
1067
|
-
|
|
1012
|
+
let fileErrorMessage = 'Ошибка при добавлении файла';
|
|
1013
|
+
const firstFileErrorItem = rejectedFiles[0].errors[0];
|
|
1014
|
+
if (firstFileErrorItem) {
|
|
1015
|
+
if (firstFileErrorItem.code === ErrorCode.TooManyFiles) {
|
|
1016
|
+
fileErrorMessage = `Максимальное количество файлов: ${maxFiles}`;
|
|
1017
|
+
} else {
|
|
1018
|
+
fileErrorMessage = firstFileErrorItem.message;
|
|
1019
|
+
}
|
|
1020
|
+
}
|
|
1068
1021
|
// Show error
|
|
1069
|
-
setFileError(
|
|
1022
|
+
setFileError(fileErrorMessage);
|
|
1070
1023
|
} else {
|
|
1071
1024
|
// Else clean error
|
|
1072
1025
|
setFileError('');
|
|
1073
1026
|
}
|
|
1074
1027
|
}
|
|
1075
1028
|
});
|
|
1076
|
-
|
|
1077
|
-
// Delete file from dropzone
|
|
1078
|
-
const removeFile = useCallback((event, index) => {
|
|
1079
|
-
event.stopPropagation();
|
|
1080
|
-
event.preventDefault();
|
|
1081
|
-
const newFiles = [...files];
|
|
1082
|
-
newFiles.splice(index, 1);
|
|
1083
|
-
if (onDeleteFile) {
|
|
1084
|
-
onDeleteFile(files[index], inputName);
|
|
1085
|
-
}
|
|
1086
|
-
changeFormState(newFiles);
|
|
1087
|
-
}, [files, changeFormState, onDeleteFile]);
|
|
1088
|
-
|
|
1089
|
-
//
|
|
1090
|
-
const convertFiledValueAndSaveAsFiles = useCallback(async newInputValue => {
|
|
1091
|
-
setFileIsLoading(true);
|
|
1092
|
-
const newFiles = await convertToFiles(newInputValue, isPreviews);
|
|
1093
|
-
setFileIsLoading(false);
|
|
1094
|
-
setFiles(newFiles);
|
|
1095
|
-
return newFiles;
|
|
1096
|
-
}, [isPreviews]);
|
|
1097
1029
|
useEffect(() => {
|
|
1098
1030
|
// First time convert value to Files and save to local and form state
|
|
1099
|
-
convertFiledValueAndSaveAsFiles(inputValue)
|
|
1100
|
-
}, []); // eslint-disable-line
|
|
1101
|
-
|
|
1102
|
-
useEffect(() => {
|
|
1103
|
-
// Everytime convert value to Files and save to local state
|
|
1104
|
-
if (!inputValue) {
|
|
1105
|
-
setFiles([]);
|
|
1106
|
-
} else {
|
|
1107
|
-
convertFiledValueAndSaveAsFiles(inputValue);
|
|
1108
|
-
}
|
|
1109
|
-
// only "inputValue"
|
|
1110
|
-
}, [inputValue]); // eslint-disable-line
|
|
1111
|
-
|
|
1112
|
-
useEffect(() => {
|
|
1031
|
+
convertFiledValueAndSaveAsFiles(inputValue);
|
|
1113
1032
|
// Make sure to revoke the data uris to avoid memory leaks, will run on unmount
|
|
1114
|
-
return () =>
|
|
1115
|
-
|
|
1116
|
-
|
|
1033
|
+
return () => filesList.forEach(file => URL.revokeObjectURL(file?.preview));
|
|
1034
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
1035
|
+
}, []);
|
|
1117
1036
|
const fillClass = useDeviceTargetClass(props, {
|
|
1118
1037
|
prefix: 'fill_',
|
|
1119
1038
|
propsKey: 'fill'
|
|
@@ -1168,9 +1087,9 @@ const FileInputDropzone = /*#__PURE__*/React.memo(function FileInputDropzone(pro
|
|
|
1168
1087
|
name: inputName
|
|
1169
1088
|
})), /*#__PURE__*/React.createElement("div", {
|
|
1170
1089
|
className: clsx('form-dropzone__dropzone-wrapper', thumbColumn && `form-dropzone__dropzone-wrapper_column_${thumbColumn}`, fillClass, fillHoverClass, borderWidthClass, borderColorClass, borderColorHoverClass, borderTypeClass)
|
|
1171
|
-
},
|
|
1090
|
+
}, filesList.map((file, index) => /*#__PURE__*/React.createElement("aside", {
|
|
1172
1091
|
className: clsx('form-dropzone__thumb', fillClass, thumbDirectionClass, thumbBorderWidthClass, thumbBorderColorClass, thumbBorderColorHoverClass, thumbBorderTypeClass),
|
|
1173
|
-
key:
|
|
1092
|
+
key: file.id || `${file.name}_${index}`
|
|
1174
1093
|
}, isPreviews && !file.error && /*#__PURE__*/React.createElement("div", {
|
|
1175
1094
|
className: "form-dropzone__thumb-image"
|
|
1176
1095
|
}, /*#__PURE__*/React.createElement("img", {
|
|
@@ -1183,140 +1102,214 @@ const FileInputDropzone = /*#__PURE__*/React.memo(function FileInputDropzone(pro
|
|
|
1183
1102
|
})), file.error && /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(Text, {
|
|
1184
1103
|
size: thumbNameTextSize,
|
|
1185
1104
|
textColor: thumbNameTextColor,
|
|
1186
|
-
|
|
1187
|
-
|
|
1105
|
+
textWeight: thumbNameTextWeight,
|
|
1106
|
+
textWrap: thumbNameTextWrap
|
|
1188
1107
|
}, fileErrorText || file.error)), showFilename && /*#__PURE__*/React.createElement("div", {
|
|
1189
1108
|
className: "form-dropzone__thumb-name"
|
|
1190
1109
|
}, /*#__PURE__*/React.createElement(Text, {
|
|
1110
|
+
className: "form-dropzone__thumb-name-inner",
|
|
1191
1111
|
size: thumbNameTextSize,
|
|
1192
1112
|
textColor: thumbNameTextColor,
|
|
1193
|
-
textWrap: thumbNameTextWrap,
|
|
1194
1113
|
textWeight: thumbNameTextWeight,
|
|
1195
|
-
|
|
1114
|
+
textWrap: thumbNameTextWrap
|
|
1196
1115
|
}, file.name)), fileIsLoading && /*#__PURE__*/React.createElement("div", {
|
|
1197
1116
|
className: "form-dropzone__thumb-loader"
|
|
1198
1117
|
}, /*#__PURE__*/React.createElement(Loader, {
|
|
1199
|
-
|
|
1118
|
+
width: "fill",
|
|
1200
1119
|
height: "fill",
|
|
1120
|
+
fill: "surfacePrimary",
|
|
1201
1121
|
itemFill: "surfaceItemAccent",
|
|
1202
|
-
set: "simple"
|
|
1203
|
-
width: "fill"
|
|
1122
|
+
set: "simple"
|
|
1204
1123
|
})), /*#__PURE__*/React.createElement("div", {
|
|
1205
1124
|
className: "form-dropzone__thumb-remove",
|
|
1206
|
-
onClick: event => removeFile(event,
|
|
1125
|
+
onClick: event => removeFile(event, index)
|
|
1207
1126
|
}, /*#__PURE__*/React.createElement(Text, {
|
|
1127
|
+
className: "form-dropzone__thumb-remove-text",
|
|
1208
1128
|
size: removeThumbTextSize,
|
|
1209
1129
|
textColor: removeThumbTextColor,
|
|
1210
|
-
textWeight: removeThumbTextWeight,
|
|
1211
1130
|
textColorHover: removeThumbTextHoverColor,
|
|
1212
|
-
|
|
1213
|
-
}, removeThumbText || '
|
|
1131
|
+
textWeight: removeThumbTextWeight
|
|
1132
|
+
}, removeThumbText || 'Удалить')))), !filesList.length ? /*#__PURE__*/React.createElement("div", {
|
|
1214
1133
|
className: "form-dropzone__hint"
|
|
1215
1134
|
}, /*#__PURE__*/React.createElement(Text, {
|
|
1135
|
+
className: "form-dropzone__hint-title",
|
|
1216
1136
|
size: hintTitleTextSize,
|
|
1217
1137
|
textColor: hintTitleTextColor,
|
|
1218
|
-
textWrap: hintTitleTextWrap,
|
|
1219
1138
|
textWeight: hintTitleTextWeight,
|
|
1220
|
-
|
|
1139
|
+
textWrap: hintTitleTextWrap
|
|
1221
1140
|
}, hintTitle || 'Select a file or drag in form'), /*#__PURE__*/React.createElement(Text, {
|
|
1141
|
+
className: "form-dropzone__hint-text",
|
|
1222
1142
|
size: hintDescriptionTextSize,
|
|
1223
1143
|
textColor: hintDescriptionTextColor,
|
|
1224
|
-
textWrap: hintDescriptionTextWrap,
|
|
1225
1144
|
textWeight: hintDescriptionTextWeight,
|
|
1226
|
-
|
|
1145
|
+
textWrap: hintDescriptionTextWrap
|
|
1227
1146
|
}, hintDescription)) : /*#__PURE__*/React.createElement("div", {
|
|
1228
1147
|
className: "form-dropzone__hint form-dropzone__hint_type_add-more"
|
|
1229
1148
|
}, /*#__PURE__*/React.createElement(Text, {
|
|
1149
|
+
className: "form-dropzone__hint-title",
|
|
1230
1150
|
size: hintTitleTextSize,
|
|
1231
1151
|
textColor: hintTitleTextColor,
|
|
1232
|
-
textWrap: hintTitleTextWrap,
|
|
1233
1152
|
textWeight: hintTitleTextWeight,
|
|
1234
|
-
|
|
1153
|
+
textWrap: hintTitleTextWrap
|
|
1235
1154
|
}, hintTitle || 'Select a file or drag in form'), /*#__PURE__*/React.createElement(Text, {
|
|
1155
|
+
className: "form-dropzone__hint-text",
|
|
1236
1156
|
size: hintDescriptionTextSize,
|
|
1237
1157
|
textColor: hintDescriptionTextColor,
|
|
1238
|
-
textWrap: hintDescriptionTextWrap,
|
|
1239
1158
|
textWeight: hintDescriptionTextWeight,
|
|
1240
|
-
|
|
1159
|
+
textWrap: hintDescriptionTextWrap
|
|
1241
1160
|
}, hintDescription)))), fileError && /*#__PURE__*/React.createElement("div", {
|
|
1242
1161
|
className: "form-field__message"
|
|
1243
1162
|
}, /*#__PURE__*/React.createElement(Text, {
|
|
1244
1163
|
className: "form-field__message-item form-field__message-item_type_message",
|
|
1245
1164
|
size: errorMessageTextSize,
|
|
1246
|
-
|
|
1247
|
-
|
|
1165
|
+
textColor: errorMessageTextColor,
|
|
1166
|
+
textWeight: errorMessageTextWeight
|
|
1248
1167
|
}, fileError)));
|
|
1249
1168
|
});
|
|
1250
1169
|
FileInputDropzone.propTypes = {
|
|
1170
|
+
isPreviews: PropTypes.bool,
|
|
1251
1171
|
dropzoneProps: PropTypes.object,
|
|
1252
1172
|
hintDescription: PropTypes.string,
|
|
1253
1173
|
hintTitle: PropTypes.string,
|
|
1254
1174
|
inputName: PropTypes.string,
|
|
1255
1175
|
inputValue: PropTypes.any,
|
|
1256
|
-
isPreviews: PropTypes.bool,
|
|
1257
|
-
showFilename: PropTypes.bool,
|
|
1258
|
-
loadingText: PropTypes.string,
|
|
1259
1176
|
metaError: PropTypes.string,
|
|
1260
1177
|
metaTouched: PropTypes.bool,
|
|
1261
1178
|
removeThumbText: PropTypes.string,
|
|
1179
|
+
showFilename: PropTypes.bool,
|
|
1262
1180
|
onAddFiles: PropTypes.func,
|
|
1263
1181
|
onDeleteFile: PropTypes.func
|
|
1264
1182
|
};
|
|
1183
|
+
async function getFileByURL(url) {
|
|
1184
|
+
try {
|
|
1185
|
+
const response = await axios({
|
|
1186
|
+
url: url,
|
|
1187
|
+
responseType: 'blob'
|
|
1188
|
+
});
|
|
1189
|
+
const blobObject = response.data;
|
|
1190
|
+
const dirtyFilename = response.headers['content-disposition']?.split('filename=')[1];
|
|
1191
|
+
// Remove double quotes
|
|
1192
|
+
let filename = dirtyFilename?.substring(1).slice(0, -1);
|
|
1193
|
+
if (!filename) {
|
|
1194
|
+
filename = url.split('/').at(-1);
|
|
1195
|
+
// const typeParts = blobObject.type.split('/')
|
|
1196
|
+
// const fileType = typeParts[typeParts.length - 1]
|
|
1197
|
+
// filename = `${new Date().getTime()}.${fileType}`
|
|
1198
|
+
}
|
|
1199
|
+
return new File([blobObject], filename, {
|
|
1200
|
+
type: blobObject.type
|
|
1201
|
+
});
|
|
1202
|
+
} catch (error) {
|
|
1203
|
+
console.log('error: ', error);
|
|
1204
|
+
return null;
|
|
1205
|
+
}
|
|
1206
|
+
}
|
|
1207
|
+
async function convertToFiles(inputValue, isPreviews) {
|
|
1208
|
+
const newFiles = [];
|
|
1209
|
+
for (const value of castArray(inputValue)) {
|
|
1210
|
+
let newFile = null;
|
|
1211
|
+
|
|
1212
|
+
// Download image by url and save as File instance
|
|
1213
|
+
const isURL = typeof value === 'string' && value.includes('/');
|
|
1214
|
+
if (value.image || isURL) {
|
|
1215
|
+
newFile = await getFileByURL(value.image || value);
|
|
1216
|
+
if (newFile) {
|
|
1217
|
+
setFileDataURL(newFile);
|
|
1218
|
+
}
|
|
1219
|
+
}
|
|
1220
|
+
|
|
1221
|
+
// Convert dataURL to File instance
|
|
1222
|
+
if (value.dataURL) {
|
|
1223
|
+
newFile = createFileFromDataURL(value.name || value.path, value.dataURL);
|
|
1224
|
+
newFile.dataURL = value.dataURL;
|
|
1225
|
+
}
|
|
1226
|
+
|
|
1227
|
+
// Save new File to state
|
|
1228
|
+
if (newFile) {
|
|
1229
|
+
newFile.id = value.id;
|
|
1230
|
+
if (isPreviews) {
|
|
1231
|
+
newFile.preview = URL.createObjectURL(newFile);
|
|
1232
|
+
}
|
|
1233
|
+
newFiles.push(newFile);
|
|
1234
|
+
}
|
|
1235
|
+
}
|
|
1236
|
+
return newFiles;
|
|
1237
|
+
}
|
|
1238
|
+
function setFileDataURL(file, resolve) {
|
|
1239
|
+
resolve = resolve || (() => {});
|
|
1240
|
+
// Init reader and save his file
|
|
1241
|
+
const reader = new FileReader();
|
|
1242
|
+
reader._readedFile = file;
|
|
1243
|
+
|
|
1244
|
+
// Set handlers
|
|
1245
|
+
reader.onabort = () => resolve();
|
|
1246
|
+
reader.onerror = () => resolve();
|
|
1247
|
+
reader.onload = event => {
|
|
1248
|
+
event.target._readedFile.dataURL = reader.result;
|
|
1249
|
+
resolve();
|
|
1250
|
+
};
|
|
1251
|
+
// Run reader
|
|
1252
|
+
if (file instanceof File) {
|
|
1253
|
+
reader.readAsDataURL(file);
|
|
1254
|
+
} else {
|
|
1255
|
+
resolve();
|
|
1256
|
+
}
|
|
1257
|
+
}
|
|
1265
1258
|
|
|
1266
1259
|
const FileInput = /*#__PURE__*/React.memo(function FileInput(props) {
|
|
1267
1260
|
const {
|
|
1268
|
-
|
|
1269
|
-
shape,
|
|
1270
|
-
size,
|
|
1261
|
+
width,
|
|
1271
1262
|
borderWidth,
|
|
1263
|
+
isPreviews,
|
|
1264
|
+
isRequired,
|
|
1272
1265
|
borderColorHover,
|
|
1273
1266
|
borderType,
|
|
1274
|
-
label,
|
|
1275
|
-
thumbBorderWidth,
|
|
1276
|
-
thumbBorderColor,
|
|
1277
|
-
thumbBorderColorHover,
|
|
1278
|
-
thumbBorderType,
|
|
1279
|
-
removeThumbTextHoverColor,
|
|
1280
|
-
labelTextColor,
|
|
1281
|
-
fill,
|
|
1282
|
-
fillHover,
|
|
1283
1267
|
className,
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
thumbNameTextWeight,
|
|
1289
|
-
removeThumbTextSize,
|
|
1290
|
-
removeThumbTextColor,
|
|
1291
|
-
removeThumbTextWeight,
|
|
1292
|
-
hintTitle,
|
|
1293
|
-
errorMessageTextSize,
|
|
1268
|
+
classNameGroupItem,
|
|
1269
|
+
dropzoneProps,
|
|
1270
|
+
errorMessageTextColor = 'errorTextPrimary',
|
|
1271
|
+
errorMessageTextSize = 's',
|
|
1294
1272
|
errorMessageTextWeight,
|
|
1295
|
-
errorMessageTextColor,
|
|
1296
1273
|
fieldProps,
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
hintDescriptionTextSize,
|
|
1274
|
+
fileErrorText,
|
|
1275
|
+
fill,
|
|
1276
|
+
fillHover,
|
|
1277
|
+
hintDescription,
|
|
1302
1278
|
hintDescriptionTextColor,
|
|
1303
|
-
|
|
1279
|
+
hintDescriptionTextSize,
|
|
1304
1280
|
hintDescriptionTextWeight,
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1281
|
+
hintDescriptionTextWrap,
|
|
1282
|
+
hintTitle,
|
|
1283
|
+
hintTitleTextColor,
|
|
1284
|
+
hintTitleTextSize,
|
|
1285
|
+
hintTitleTextWeight,
|
|
1286
|
+
hintTitleTextWrap,
|
|
1287
|
+
label,
|
|
1288
|
+
labelTextColor,
|
|
1311
1289
|
maxFiles,
|
|
1312
1290
|
maxSize,
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1291
|
+
name,
|
|
1292
|
+
removeThumbText,
|
|
1293
|
+
removeThumbTextColor,
|
|
1294
|
+
removeThumbTextHoverColor,
|
|
1295
|
+
removeThumbTextSize,
|
|
1296
|
+
removeThumbTextWeight,
|
|
1297
|
+
shape,
|
|
1298
|
+
showFilename,
|
|
1299
|
+
showMessage,
|
|
1300
|
+
size,
|
|
1301
|
+
thumbBorderColor,
|
|
1302
|
+
thumbBorderColorHover,
|
|
1303
|
+
thumbBorderType,
|
|
1304
|
+
thumbBorderWidth,
|
|
1305
|
+
thumbColumn = 1,
|
|
1306
|
+
thumbDirection = 'vertical',
|
|
1307
|
+
thumbNameTextColor,
|
|
1308
|
+
thumbNameTextSize,
|
|
1309
|
+
thumbNameTextWeight,
|
|
1310
|
+
thumbNameTextWrap,
|
|
1316
1311
|
onAddFiles,
|
|
1317
|
-
onDeleteFile
|
|
1318
|
-
classNameGroupItem,
|
|
1319
|
-
width
|
|
1312
|
+
onDeleteFile
|
|
1320
1313
|
} = props;
|
|
1321
1314
|
return /*#__PURE__*/React.createElement(Field, {
|
|
1322
1315
|
name: name
|
|
@@ -1342,8 +1335,8 @@ const FileInput = /*#__PURE__*/React.memo(function FileInput(props) {
|
|
|
1342
1335
|
meta: meta
|
|
1343
1336
|
});
|
|
1344
1337
|
const updatedInputProps = useValidationAppearanceInputProps({
|
|
1345
|
-
|
|
1346
|
-
|
|
1338
|
+
inputProps: props,
|
|
1339
|
+
validationStateKey: isErrorState ? errorKey : 'success'
|
|
1347
1340
|
});
|
|
1348
1341
|
|
|
1349
1342
|
/** TODO:
|
|
@@ -1351,82 +1344,77 @@ const FileInput = /*#__PURE__*/React.memo(function FileInput(props) {
|
|
|
1351
1344
|
*/
|
|
1352
1345
|
return /*#__PURE__*/React.createElement(FieldWrapper, Object.assign({
|
|
1353
1346
|
className: clsx('form-field_type_dropzone', 'form__item_type_dropzone', classNameGroupItem),
|
|
1347
|
+
width: width,
|
|
1348
|
+
labelTextColor: labelTextColor,
|
|
1349
|
+
errorKey: errorKey,
|
|
1350
|
+
errorMessage: errorMessage,
|
|
1354
1351
|
fieldClassName: "form-dropzone",
|
|
1355
1352
|
inputName: input.name,
|
|
1356
1353
|
inputValue: input.value,
|
|
1357
|
-
isRequired: isRequired,
|
|
1358
1354
|
label: label,
|
|
1359
|
-
labelTextColor: labelTextColor,
|
|
1360
1355
|
metaActive: meta.active,
|
|
1361
1356
|
metaError: meta.error,
|
|
1362
1357
|
metaTouched: meta.touched,
|
|
1363
1358
|
showMessage: showMessage,
|
|
1364
|
-
width: width,
|
|
1365
1359
|
isErrorState: isErrorState,
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
errorMessage: errorMessage
|
|
1360
|
+
isRequired: isRequired,
|
|
1361
|
+
isValidState: isValidState
|
|
1369
1362
|
}, fieldProps), /*#__PURE__*/React.createElement(FileInputDropzone, {
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1363
|
+
className: className,
|
|
1364
|
+
fill: fill,
|
|
1365
|
+
fillHover: fillHover,
|
|
1373
1366
|
borderWidth: borderWidth,
|
|
1374
1367
|
borderColor: updatedInputProps.borderColor,
|
|
1375
1368
|
borderColorHover: borderColorHover,
|
|
1376
1369
|
borderType: borderType,
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1370
|
+
dropzoneProps: dropzoneProps,
|
|
1371
|
+
errorMessageTextColor: errorMessageTextColor,
|
|
1372
|
+
errorMessageTextSize: errorMessageTextSize,
|
|
1373
|
+
errorMessageWeight: errorMessageTextWeight,
|
|
1381
1374
|
fileErrorText: fileErrorText,
|
|
1382
|
-
|
|
1383
|
-
|
|
1375
|
+
hintDescription: hintDescription,
|
|
1376
|
+
hintDescriptionTextColor: hintDescriptionTextColor,
|
|
1377
|
+
hintDescriptionTextSize: hintDescriptionTextSize,
|
|
1378
|
+
hintDescriptionTextWeight: hintDescriptionTextWeight,
|
|
1379
|
+
hintDescriptionTextWrap: hintDescriptionTextWrap,
|
|
1380
|
+
hintTitle: hintTitle,
|
|
1381
|
+
hintTitleTextColor: hintTitleTextColor,
|
|
1382
|
+
hintTitleTextSize: hintTitleTextSize,
|
|
1383
|
+
hintTitleTextWeight: hintTitleTextWeight,
|
|
1384
|
+
hintTitleTextWrap: hintTitleTextWrap,
|
|
1385
|
+
inputName: input.name,
|
|
1386
|
+
inputValue: input.value,
|
|
1384
1387
|
maxFiles: maxFiles,
|
|
1385
1388
|
maxSize: maxSize,
|
|
1389
|
+
metaError: meta.error,
|
|
1390
|
+
metaTouched: meta.touched,
|
|
1391
|
+
removeThumbText: removeThumbText,
|
|
1392
|
+
removeThumbTextColor: removeThumbTextColor,
|
|
1386
1393
|
removeThumbTextHoverColor: removeThumbTextHoverColor,
|
|
1387
|
-
|
|
1388
|
-
|
|
1394
|
+
removeThumbTextSize: removeThumbTextSize,
|
|
1395
|
+
removeThumbTextWeight: removeThumbTextWeight,
|
|
1396
|
+
shape: shape,
|
|
1397
|
+
showFilename: showFilename,
|
|
1398
|
+
size: size,
|
|
1399
|
+
thumbBorderColor: thumbBorderColor,
|
|
1400
|
+
thumbBorderColorHover: thumbBorderColorHover,
|
|
1401
|
+
thumbBorderType: thumbBorderType,
|
|
1402
|
+
thumbBorderWidth: thumbBorderWidth,
|
|
1389
1403
|
thumbColumn: thumbColumn,
|
|
1390
1404
|
thumbDirection: thumbDirection,
|
|
1391
|
-
inputName: input.name,
|
|
1392
|
-
inputValue: input.value,
|
|
1393
|
-
thumbNameTextSize: thumbNameTextSize,
|
|
1394
1405
|
thumbNameTextColor: thumbNameTextColor,
|
|
1395
|
-
|
|
1406
|
+
thumbNameTextSize: thumbNameTextSize,
|
|
1396
1407
|
thumbNameTextWeight: thumbNameTextWeight,
|
|
1397
|
-
|
|
1398
|
-
hintTitleTextColor: hintTitleTextColor,
|
|
1399
|
-
hintTitleTextWrap: hintTitleTextWrap,
|
|
1400
|
-
hintTitleTextWeight: hintTitleTextWeight,
|
|
1401
|
-
removeThumbTextSize: removeThumbTextSize,
|
|
1402
|
-
removeThumbTextColor: removeThumbTextColor,
|
|
1403
|
-
removeThumbTextWeight: removeThumbTextWeight,
|
|
1404
|
-
hintDescriptionTextSize: hintDescriptionTextSize,
|
|
1405
|
-
hintDescriptionTextColor: hintDescriptionTextColor,
|
|
1406
|
-
hintDescriptionTextWrap: hintDescriptionTextWrap,
|
|
1407
|
-
hintDescriptionTextWeight: hintDescriptionTextWeight,
|
|
1408
|
-
errorMessageTextSize: errorMessageTextSize,
|
|
1409
|
-
errorMessageWeight: errorMessageTextWeight,
|
|
1410
|
-
errorMessageTextColor: errorMessageTextColor,
|
|
1408
|
+
thumbNameTextWrap: thumbNameTextWrap,
|
|
1411
1409
|
isPreviews: isPreviews,
|
|
1412
|
-
shape: shape,
|
|
1413
|
-
showFilename: showFilename,
|
|
1414
|
-
metaError: meta.error,
|
|
1415
|
-
metaTouched: meta.touched,
|
|
1416
|
-
removeThumbText: removeThumbText,
|
|
1417
1410
|
onAddFiles: onAddFiles,
|
|
1418
1411
|
onDeleteFile: onDeleteFile
|
|
1419
1412
|
}));
|
|
1420
1413
|
});
|
|
1421
1414
|
});
|
|
1422
|
-
FileInput.defaultProps = {
|
|
1423
|
-
errorMessageTextSize: 's',
|
|
1424
|
-
errorMessageTextColor: 'errorTextPrimary',
|
|
1425
|
-
thumbColumn: 1,
|
|
1426
|
-
thumbDirection: 'vertical'
|
|
1427
|
-
};
|
|
1428
1415
|
FileInput.propTypes = {
|
|
1429
|
-
|
|
1416
|
+
isPreviews: PropTypes.bool,
|
|
1417
|
+
isRequired: PropTypes.bool,
|
|
1430
1418
|
className: PropTypes.string,
|
|
1431
1419
|
classNameGroupItem: PropTypes.string,
|
|
1432
1420
|
classNameInput: PropTypes.string,
|
|
@@ -1435,11 +1423,10 @@ FileInput.propTypes = {
|
|
|
1435
1423
|
hintDescription: PropTypes.string,
|
|
1436
1424
|
hintTitle: PropTypes.string,
|
|
1437
1425
|
inputClass: PropTypes.string,
|
|
1438
|
-
isPreviews: PropTypes.bool,
|
|
1439
|
-
isRequired: PropTypes.bool,
|
|
1440
|
-
showFilename: PropTypes.bool,
|
|
1441
1426
|
label: PropTypes.any,
|
|
1427
|
+
name: PropTypes.string.isRequired,
|
|
1442
1428
|
removeThumbText: PropTypes.string,
|
|
1429
|
+
showFilename: PropTypes.bool,
|
|
1443
1430
|
onAddFiles: PropTypes.func,
|
|
1444
1431
|
onDeleteFile: PropTypes.func
|
|
1445
1432
|
};
|
|
@@ -2001,10 +1988,11 @@ function getDefaultValue(options, selectValue) {
|
|
|
2001
1988
|
}
|
|
2002
1989
|
const SelectField = /*#__PURE__*/React.memo(function SelectField(props) {
|
|
2003
1990
|
const {
|
|
2004
|
-
classNameGroupItem,
|
|
2005
|
-
fieldProps,
|
|
2006
1991
|
isDisabled,
|
|
2007
1992
|
isRequired,
|
|
1993
|
+
classNameGroupItem,
|
|
1994
|
+
fieldProps,
|
|
1995
|
+
initialValue,
|
|
2008
1996
|
name,
|
|
2009
1997
|
options,
|
|
2010
1998
|
selectProps,
|
|
@@ -2014,7 +2002,8 @@ const SelectField = /*#__PURE__*/React.memo(function SelectField(props) {
|
|
|
2014
2002
|
onInputChange
|
|
2015
2003
|
} = props;
|
|
2016
2004
|
return /*#__PURE__*/React.createElement(Field, {
|
|
2017
|
-
name: name
|
|
2005
|
+
name: name,
|
|
2006
|
+
initialValue: initialValue
|
|
2018
2007
|
}, function Render({
|
|
2019
2008
|
input,
|
|
2020
2009
|
meta
|