@reykjavik/hanna-react 0.10.61 → 0.10.62
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/CHANGELOG.md +6 -0
- package/FileInput.d.ts +16 -1
- package/FileInput.js +19 -20
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
package/FileInput.d.ts
CHANGED
|
@@ -9,7 +9,22 @@ export declare type FileInputProps = {
|
|
|
9
9
|
showFileSize?: boolean;
|
|
10
10
|
showImagePreviews?: boolean;
|
|
11
11
|
removeFileText: string;
|
|
12
|
-
onFilesUpdated?: (
|
|
12
|
+
onFilesUpdated?: (
|
|
13
|
+
/** Updated, full list of Files. */
|
|
14
|
+
files: Array<File>,
|
|
15
|
+
/** Information about which Files were added or removed during with this update.
|
|
16
|
+
*
|
|
17
|
+
* NOTE: When a diff contains both added and deleted files, this indicates a
|
|
18
|
+
* name-conflict occurred — i.e. one of the added files has a name that
|
|
19
|
+
* existed in the old file list.
|
|
20
|
+
* In such cases the deletion is more implicit than explicit, and depending
|
|
21
|
+
* on the circumstances, you MIGHT wish to either warn the user, rename
|
|
22
|
+
* one of the files, instead of overwriting/deleting the older file, etc.
|
|
23
|
+
*/
|
|
24
|
+
diff: {
|
|
25
|
+
deleted?: Array<File>;
|
|
26
|
+
added?: Array<File>;
|
|
27
|
+
}) => void;
|
|
13
28
|
name?: string;
|
|
14
29
|
value?: ReadonlyArray<File>;
|
|
15
30
|
} & FormFieldWrappingProps;
|
package/FileInput.js
CHANGED
|
@@ -32,20 +32,6 @@ const arrayToFileList = (arr) => {
|
|
|
32
32
|
});
|
|
33
33
|
return fileList.files;
|
|
34
34
|
};
|
|
35
|
-
const dedupeFilesArray = (files) => {
|
|
36
|
-
const newArray = [];
|
|
37
|
-
const found = {};
|
|
38
|
-
files.forEach((file) => {
|
|
39
|
-
if (!(file.name in found)) {
|
|
40
|
-
newArray.push(file);
|
|
41
|
-
found[file.name] = true;
|
|
42
|
-
}
|
|
43
|
-
else {
|
|
44
|
-
releasePreview(file);
|
|
45
|
-
}
|
|
46
|
-
});
|
|
47
|
-
return newArray;
|
|
48
|
-
};
|
|
49
35
|
const formatBytes = (bytes, decimals = 2) => {
|
|
50
36
|
if (bytes === 0) {
|
|
51
37
|
return '0 Bytes';
|
|
@@ -97,23 +83,36 @@ const FileInput = (props) => {
|
|
|
97
83
|
}, [files]);
|
|
98
84
|
const removeFile = (name) => {
|
|
99
85
|
if (fileInput.current) {
|
|
86
|
+
const deleted = [];
|
|
100
87
|
const newFileList = files.filter((file) => {
|
|
101
88
|
if (file.name !== name) {
|
|
102
89
|
return true;
|
|
103
90
|
}
|
|
91
|
+
deleted.push(file);
|
|
104
92
|
releasePreview(file);
|
|
93
|
+
return false;
|
|
105
94
|
});
|
|
106
95
|
fileInput.current.files = arrayToFileList(newFileList);
|
|
107
|
-
onFilesUpdated(newFileList);
|
|
96
|
+
onFilesUpdated(newFileList, { deleted });
|
|
108
97
|
}
|
|
109
98
|
};
|
|
110
|
-
const addFiles = (
|
|
99
|
+
const addFiles = (added) => {
|
|
111
100
|
if (fileInput.current) {
|
|
112
|
-
const
|
|
113
|
-
|
|
114
|
-
|
|
101
|
+
const deleted = [];
|
|
102
|
+
const retained = [];
|
|
103
|
+
const oldFiles = dropzoneProps.multiple ? files : [];
|
|
104
|
+
oldFiles.forEach((oldFile) => {
|
|
105
|
+
if (added.find(({ name }) => name === oldFile.name)) {
|
|
106
|
+
deleted.push(oldFile);
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
retained.push(oldFile);
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
const newFileList = retained.concat(added);
|
|
115
113
|
fileInput.current.files = arrayToFileList(newFileList);
|
|
116
|
-
|
|
114
|
+
const diff = deleted.length ? { added, deleted } : { added };
|
|
115
|
+
onFilesUpdated(newFileList, diff);
|
|
117
116
|
}
|
|
118
117
|
if (inputRef.current) {
|
|
119
118
|
// Empty on every add
|