@itcase/forms 1.0.67 → 1.0.68
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 +265 -279
- package/dist/itcase-forms.esm.js +265 -279
- 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;
|
|
885
|
+
/* globals File, FileReader, URL, console */
|
|
912
886
|
|
|
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;
|
|
951
|
-
|
|
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,213 @@ 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
|
+
const typeParts = blobObject.type.split('/');
|
|
1195
|
+
const fileType = typeParts[typeParts.length - 1];
|
|
1196
|
+
filename = `${new Date().getTime()}.${fileType}`;
|
|
1197
|
+
}
|
|
1198
|
+
return new File([blobObject], filename, {
|
|
1199
|
+
type: blobObject.type
|
|
1200
|
+
});
|
|
1201
|
+
} catch (error) {
|
|
1202
|
+
console.log('error: ', error);
|
|
1203
|
+
return null;
|
|
1204
|
+
}
|
|
1205
|
+
}
|
|
1206
|
+
async function convertToFiles(inputValue, isPreviews) {
|
|
1207
|
+
const newFiles = [];
|
|
1208
|
+
for (const value of castArray(inputValue)) {
|
|
1209
|
+
let newFile = null;
|
|
1210
|
+
|
|
1211
|
+
// Download image by url and save as File instance
|
|
1212
|
+
const isURL = typeof value === 'string' && value.includes('/');
|
|
1213
|
+
if (value.image || isURL) {
|
|
1214
|
+
newFile = await getFileByURL(value.image || value);
|
|
1215
|
+
if (newFile) {
|
|
1216
|
+
setFileDataURL(newFile);
|
|
1217
|
+
}
|
|
1218
|
+
}
|
|
1219
|
+
|
|
1220
|
+
// Convert dataURL to File instance
|
|
1221
|
+
if (value.dataURL) {
|
|
1222
|
+
newFile = createFileFromDataURL(value.name || value.path, value.dataURL);
|
|
1223
|
+
newFile.dataURL = value.dataURL;
|
|
1224
|
+
}
|
|
1225
|
+
|
|
1226
|
+
// Save new File to state
|
|
1227
|
+
if (newFile) {
|
|
1228
|
+
newFile.id = value.id;
|
|
1229
|
+
if (isPreviews) {
|
|
1230
|
+
newFile.preview = URL.createObjectURL(newFile);
|
|
1231
|
+
}
|
|
1232
|
+
newFiles.push(newFile);
|
|
1233
|
+
}
|
|
1234
|
+
}
|
|
1235
|
+
return newFiles;
|
|
1236
|
+
}
|
|
1237
|
+
function setFileDataURL(file, resolve) {
|
|
1238
|
+
resolve = resolve || (() => {});
|
|
1239
|
+
// Init reader and save his file
|
|
1240
|
+
const reader = new FileReader();
|
|
1241
|
+
reader._readedFile = file;
|
|
1242
|
+
|
|
1243
|
+
// Set handlers
|
|
1244
|
+
reader.onabort = () => resolve();
|
|
1245
|
+
reader.onerror = () => resolve();
|
|
1246
|
+
reader.onload = event => {
|
|
1247
|
+
event.target._readedFile.dataURL = reader.result;
|
|
1248
|
+
resolve();
|
|
1249
|
+
};
|
|
1250
|
+
// Run reader
|
|
1251
|
+
if (file instanceof File) {
|
|
1252
|
+
reader.readAsDataURL(file);
|
|
1253
|
+
} else {
|
|
1254
|
+
resolve();
|
|
1255
|
+
}
|
|
1256
|
+
}
|
|
1265
1257
|
|
|
1266
1258
|
const FileInput = /*#__PURE__*/React.memo(function FileInput(props) {
|
|
1267
1259
|
const {
|
|
1268
|
-
|
|
1269
|
-
shape,
|
|
1270
|
-
size,
|
|
1260
|
+
width,
|
|
1271
1261
|
borderWidth,
|
|
1262
|
+
isPreviews,
|
|
1263
|
+
isRequired,
|
|
1272
1264
|
borderColorHover,
|
|
1273
1265
|
borderType,
|
|
1274
|
-
label,
|
|
1275
|
-
thumbBorderWidth,
|
|
1276
|
-
thumbBorderColor,
|
|
1277
|
-
thumbBorderColorHover,
|
|
1278
|
-
thumbBorderType,
|
|
1279
|
-
removeThumbTextHoverColor,
|
|
1280
|
-
labelTextColor,
|
|
1281
|
-
fill,
|
|
1282
|
-
fillHover,
|
|
1283
1266
|
className,
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
thumbNameTextWeight,
|
|
1289
|
-
removeThumbTextSize,
|
|
1290
|
-
removeThumbTextColor,
|
|
1291
|
-
removeThumbTextWeight,
|
|
1292
|
-
hintTitle,
|
|
1293
|
-
errorMessageTextSize,
|
|
1267
|
+
classNameGroupItem,
|
|
1268
|
+
dropzoneProps,
|
|
1269
|
+
errorMessageTextColor = 'errorTextPrimary',
|
|
1270
|
+
errorMessageTextSize = 's',
|
|
1294
1271
|
errorMessageTextWeight,
|
|
1295
|
-
errorMessageTextColor,
|
|
1296
1272
|
fieldProps,
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
hintDescriptionTextSize,
|
|
1273
|
+
fileErrorText,
|
|
1274
|
+
fill,
|
|
1275
|
+
fillHover,
|
|
1276
|
+
hintDescription,
|
|
1302
1277
|
hintDescriptionTextColor,
|
|
1303
|
-
|
|
1278
|
+
hintDescriptionTextSize,
|
|
1304
1279
|
hintDescriptionTextWeight,
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1280
|
+
hintDescriptionTextWrap,
|
|
1281
|
+
hintTitle,
|
|
1282
|
+
hintTitleTextColor,
|
|
1283
|
+
hintTitleTextSize,
|
|
1284
|
+
hintTitleTextWeight,
|
|
1285
|
+
hintTitleTextWrap,
|
|
1286
|
+
label,
|
|
1287
|
+
labelTextColor,
|
|
1311
1288
|
maxFiles,
|
|
1312
1289
|
maxSize,
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1290
|
+
name,
|
|
1291
|
+
removeThumbText,
|
|
1292
|
+
removeThumbTextColor,
|
|
1293
|
+
removeThumbTextHoverColor,
|
|
1294
|
+
removeThumbTextSize,
|
|
1295
|
+
removeThumbTextWeight,
|
|
1296
|
+
shape,
|
|
1297
|
+
showFilename,
|
|
1298
|
+
showMessage,
|
|
1299
|
+
size,
|
|
1300
|
+
thumbBorderColor,
|
|
1301
|
+
thumbBorderColorHover,
|
|
1302
|
+
thumbBorderType,
|
|
1303
|
+
thumbBorderWidth,
|
|
1304
|
+
thumbColumn = 1,
|
|
1305
|
+
thumbDirection = 'vertical',
|
|
1306
|
+
thumbNameTextColor,
|
|
1307
|
+
thumbNameTextSize,
|
|
1308
|
+
thumbNameTextWeight,
|
|
1309
|
+
thumbNameTextWrap,
|
|
1316
1310
|
onAddFiles,
|
|
1317
|
-
onDeleteFile
|
|
1318
|
-
classNameGroupItem,
|
|
1319
|
-
width
|
|
1311
|
+
onDeleteFile
|
|
1320
1312
|
} = props;
|
|
1321
1313
|
return /*#__PURE__*/React.createElement(Field, {
|
|
1322
1314
|
name: name
|
|
@@ -1342,8 +1334,8 @@ const FileInput = /*#__PURE__*/React.memo(function FileInput(props) {
|
|
|
1342
1334
|
meta: meta
|
|
1343
1335
|
});
|
|
1344
1336
|
const updatedInputProps = useValidationAppearanceInputProps({
|
|
1345
|
-
|
|
1346
|
-
|
|
1337
|
+
inputProps: props,
|
|
1338
|
+
validationStateKey: isErrorState ? errorKey : 'success'
|
|
1347
1339
|
});
|
|
1348
1340
|
|
|
1349
1341
|
/** TODO:
|
|
@@ -1351,82 +1343,77 @@ const FileInput = /*#__PURE__*/React.memo(function FileInput(props) {
|
|
|
1351
1343
|
*/
|
|
1352
1344
|
return /*#__PURE__*/React.createElement(FieldWrapper, Object.assign({
|
|
1353
1345
|
className: clsx('form-field_type_dropzone', 'form__item_type_dropzone', classNameGroupItem),
|
|
1346
|
+
width: width,
|
|
1347
|
+
labelTextColor: labelTextColor,
|
|
1348
|
+
errorKey: errorKey,
|
|
1349
|
+
errorMessage: errorMessage,
|
|
1354
1350
|
fieldClassName: "form-dropzone",
|
|
1355
1351
|
inputName: input.name,
|
|
1356
1352
|
inputValue: input.value,
|
|
1357
|
-
isRequired: isRequired,
|
|
1358
1353
|
label: label,
|
|
1359
|
-
labelTextColor: labelTextColor,
|
|
1360
1354
|
metaActive: meta.active,
|
|
1361
1355
|
metaError: meta.error,
|
|
1362
1356
|
metaTouched: meta.touched,
|
|
1363
1357
|
showMessage: showMessage,
|
|
1364
|
-
width: width,
|
|
1365
1358
|
isErrorState: isErrorState,
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
errorMessage: errorMessage
|
|
1359
|
+
isRequired: isRequired,
|
|
1360
|
+
isValidState: isValidState
|
|
1369
1361
|
}, fieldProps), /*#__PURE__*/React.createElement(FileInputDropzone, {
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1362
|
+
className: className,
|
|
1363
|
+
fill: fill,
|
|
1364
|
+
fillHover: fillHover,
|
|
1373
1365
|
borderWidth: borderWidth,
|
|
1374
1366
|
borderColor: updatedInputProps.borderColor,
|
|
1375
1367
|
borderColorHover: borderColorHover,
|
|
1376
1368
|
borderType: borderType,
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1369
|
+
dropzoneProps: dropzoneProps,
|
|
1370
|
+
errorMessageTextColor: errorMessageTextColor,
|
|
1371
|
+
errorMessageTextSize: errorMessageTextSize,
|
|
1372
|
+
errorMessageWeight: errorMessageTextWeight,
|
|
1381
1373
|
fileErrorText: fileErrorText,
|
|
1382
|
-
|
|
1383
|
-
|
|
1374
|
+
hintDescription: hintDescription,
|
|
1375
|
+
hintDescriptionTextColor: hintDescriptionTextColor,
|
|
1376
|
+
hintDescriptionTextSize: hintDescriptionTextSize,
|
|
1377
|
+
hintDescriptionTextWeight: hintDescriptionTextWeight,
|
|
1378
|
+
hintDescriptionTextWrap: hintDescriptionTextWrap,
|
|
1379
|
+
hintTitle: hintTitle,
|
|
1380
|
+
hintTitleTextColor: hintTitleTextColor,
|
|
1381
|
+
hintTitleTextSize: hintTitleTextSize,
|
|
1382
|
+
hintTitleTextWeight: hintTitleTextWeight,
|
|
1383
|
+
hintTitleTextWrap: hintTitleTextWrap,
|
|
1384
|
+
inputName: input.name,
|
|
1385
|
+
inputValue: input.value,
|
|
1384
1386
|
maxFiles: maxFiles,
|
|
1385
1387
|
maxSize: maxSize,
|
|
1388
|
+
metaError: meta.error,
|
|
1389
|
+
metaTouched: meta.touched,
|
|
1390
|
+
removeThumbText: removeThumbText,
|
|
1391
|
+
removeThumbTextColor: removeThumbTextColor,
|
|
1386
1392
|
removeThumbTextHoverColor: removeThumbTextHoverColor,
|
|
1387
|
-
|
|
1388
|
-
|
|
1393
|
+
removeThumbTextSize: removeThumbTextSize,
|
|
1394
|
+
removeThumbTextWeight: removeThumbTextWeight,
|
|
1395
|
+
shape: shape,
|
|
1396
|
+
showFilename: showFilename,
|
|
1397
|
+
size: size,
|
|
1398
|
+
thumbBorderColor: thumbBorderColor,
|
|
1399
|
+
thumbBorderColorHover: thumbBorderColorHover,
|
|
1400
|
+
thumbBorderType: thumbBorderType,
|
|
1401
|
+
thumbBorderWidth: thumbBorderWidth,
|
|
1389
1402
|
thumbColumn: thumbColumn,
|
|
1390
1403
|
thumbDirection: thumbDirection,
|
|
1391
|
-
inputName: input.name,
|
|
1392
|
-
inputValue: input.value,
|
|
1393
|
-
thumbNameTextSize: thumbNameTextSize,
|
|
1394
1404
|
thumbNameTextColor: thumbNameTextColor,
|
|
1395
|
-
|
|
1405
|
+
thumbNameTextSize: thumbNameTextSize,
|
|
1396
1406
|
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,
|
|
1407
|
+
thumbNameTextWrap: thumbNameTextWrap,
|
|
1411
1408
|
isPreviews: isPreviews,
|
|
1412
|
-
shape: shape,
|
|
1413
|
-
showFilename: showFilename,
|
|
1414
|
-
metaError: meta.error,
|
|
1415
|
-
metaTouched: meta.touched,
|
|
1416
|
-
removeThumbText: removeThumbText,
|
|
1417
1409
|
onAddFiles: onAddFiles,
|
|
1418
1410
|
onDeleteFile: onDeleteFile
|
|
1419
1411
|
}));
|
|
1420
1412
|
});
|
|
1421
1413
|
});
|
|
1422
|
-
FileInput.defaultProps = {
|
|
1423
|
-
errorMessageTextSize: 's',
|
|
1424
|
-
errorMessageTextColor: 'errorTextPrimary',
|
|
1425
|
-
thumbColumn: 1,
|
|
1426
|
-
thumbDirection: 'vertical'
|
|
1427
|
-
};
|
|
1428
1414
|
FileInput.propTypes = {
|
|
1429
|
-
|
|
1415
|
+
isPreviews: PropTypes.bool,
|
|
1416
|
+
isRequired: PropTypes.bool,
|
|
1430
1417
|
className: PropTypes.string,
|
|
1431
1418
|
classNameGroupItem: PropTypes.string,
|
|
1432
1419
|
classNameInput: PropTypes.string,
|
|
@@ -1435,11 +1422,10 @@ FileInput.propTypes = {
|
|
|
1435
1422
|
hintDescription: PropTypes.string,
|
|
1436
1423
|
hintTitle: PropTypes.string,
|
|
1437
1424
|
inputClass: PropTypes.string,
|
|
1438
|
-
isPreviews: PropTypes.bool,
|
|
1439
|
-
isRequired: PropTypes.bool,
|
|
1440
|
-
showFilename: PropTypes.bool,
|
|
1441
1425
|
label: PropTypes.any,
|
|
1426
|
+
name: PropTypes.string.isRequired,
|
|
1442
1427
|
removeThumbText: PropTypes.string,
|
|
1428
|
+
showFilename: PropTypes.bool,
|
|
1443
1429
|
onAddFiles: PropTypes.func,
|
|
1444
1430
|
onDeleteFile: PropTypes.func
|
|
1445
1431
|
};
|