@react-magma/dropzone 0.1.1 → 0.1.2
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/fileuploader.js +1 -1
- package/dist/fileuploader.js.map +1 -1
- package/dist/fileuploader.modern.js +1 -1
- package/dist/fileuploader.modern.js.map +1 -1
- package/dist/fileuploader.modern.module.js +1 -1
- package/dist/fileuploader.modern.module.js.map +1 -1
- package/dist/fileuploader.umd.js +1 -1
- package/dist/fileuploader.umd.js.map +1 -1
- package/dist/src/components/dropzone/Dropzone.stories.d.ts +3 -3
- package/package.json +3 -3
- package/src/components/dropzone/Dropzone.stories.tsx +142 -84
- package/src/components/dropzone/Preview.tsx +7 -7
|
@@ -2,44 +2,49 @@ import { useState } from 'react';
|
|
|
2
2
|
import { Dropzone, DropzoneProps, OnSendFileProps } from './Dropzone';
|
|
3
3
|
import { Textarea, Datagrid } from 'react-magma-dom';
|
|
4
4
|
|
|
5
|
-
function csvJSON(csv: string){
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
function csvJSON(csv: string) {
|
|
6
|
+
const lines = csv.split('\n');
|
|
7
|
+
const result = [];
|
|
8
|
+
const headers: Array<string> = lines[0].split(',');
|
|
9
9
|
|
|
10
|
-
for(
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
for (let i = 1; i < lines.length; i++) {
|
|
11
|
+
const obj: Record<string, string> = {};
|
|
12
|
+
const currentline = lines[i].split(',');
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
for (let j = 0; j < headers.length; j++) {
|
|
15
|
+
obj[headers[j]] = currentline[j];
|
|
16
|
+
}
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
result.push({ id: i, ...obj });
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
return [
|
|
21
|
+
return [
|
|
22
|
+
headers.map((header: string) => {
|
|
23
|
+
return { field: header, header };
|
|
24
|
+
}),
|
|
25
|
+
result,
|
|
26
|
+
]; //JavaScript object
|
|
22
27
|
// return JSON.stringify(result); //JSON
|
|
23
28
|
}
|
|
24
29
|
|
|
25
30
|
const onSendFile = (props: OnSendFileProps) => {
|
|
26
|
-
const {onProgress, onError, onFinish, file} = props;
|
|
31
|
+
const { onProgress, onError, onFinish, file } = props;
|
|
27
32
|
let percent: number = 1;
|
|
28
33
|
|
|
29
34
|
const interval = setInterval(() => {
|
|
30
35
|
percent++;
|
|
31
|
-
onProgress && onProgress({percent, file})
|
|
36
|
+
onProgress && onProgress({ percent, file });
|
|
32
37
|
|
|
33
|
-
if(percent >= 100) {
|
|
38
|
+
if (percent >= 100) {
|
|
34
39
|
clearInterval(interval);
|
|
35
|
-
onFinish && onFinish({file})
|
|
40
|
+
onFinish && onFinish({ file });
|
|
36
41
|
}
|
|
37
42
|
|
|
38
43
|
// if(Math.random() * 100 > 90) {
|
|
39
44
|
// clearInterval(interval)
|
|
40
45
|
// onError && onError({errors:[{code: 'upload-err', header: 'story error', message: "The destination server has returned an error."}], file})
|
|
41
46
|
// }
|
|
42
|
-
}, 100 * Math.random())
|
|
47
|
+
}, 100 * Math.random());
|
|
43
48
|
};
|
|
44
49
|
|
|
45
50
|
export default {
|
|
@@ -74,102 +79,155 @@ export default {
|
|
|
74
79
|
};
|
|
75
80
|
|
|
76
81
|
export const Default = (args: DropzoneProps) => {
|
|
77
|
-
return
|
|
78
|
-
<
|
|
79
|
-
{
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
82
|
+
return (
|
|
83
|
+
<div
|
|
84
|
+
style={{
|
|
85
|
+
background: args.isInverse ? '#003865' : '#fff',
|
|
86
|
+
padding: '50px',
|
|
87
|
+
}}
|
|
88
|
+
>
|
|
89
|
+
<Dropzone
|
|
90
|
+
{...args}
|
|
91
|
+
accept={['.png', '.jpg', '.svg']}
|
|
92
|
+
maxFiles={5}
|
|
93
|
+
maxSize={1024 * 1024}
|
|
94
|
+
onSendFile={onSendFile}
|
|
95
|
+
labelText="Upload files"
|
|
96
|
+
helperMessage="Only PNG, JPG, and SVG files with a max size of 1MB"
|
|
97
|
+
/>
|
|
98
|
+
</div>
|
|
99
|
+
);
|
|
88
100
|
};
|
|
89
101
|
|
|
90
102
|
export const NoLimits = (args: DropzoneProps) => {
|
|
91
|
-
return
|
|
92
|
-
<
|
|
93
|
-
{
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
103
|
+
return (
|
|
104
|
+
<div
|
|
105
|
+
style={{
|
|
106
|
+
background: args.isInverse ? '#003865' : '#fff',
|
|
107
|
+
padding: '50px',
|
|
108
|
+
}}
|
|
109
|
+
>
|
|
110
|
+
<Dropzone
|
|
111
|
+
{...args}
|
|
112
|
+
onSendFile={onSendFile}
|
|
113
|
+
labelText="Upload files"
|
|
114
|
+
helperMessage="It's a free for all, upload anything."
|
|
115
|
+
/>
|
|
116
|
+
</div>
|
|
117
|
+
);
|
|
99
118
|
};
|
|
100
119
|
|
|
101
|
-
|
|
102
|
-
export const Image = () => {
|
|
120
|
+
export const Image = (args: DropzoneProps) => {
|
|
103
121
|
const [file, setFile] = useState<string>();
|
|
104
122
|
|
|
105
|
-
|
|
106
|
-
const {file, onFinish} = props;
|
|
123
|
+
const onSendFile = (props: OnSendFileProps) => {
|
|
124
|
+
const { file, onFinish } = props;
|
|
107
125
|
const reader = new FileReader();
|
|
108
|
-
reader.onload = function(evt) {
|
|
109
|
-
setFile(
|
|
110
|
-
|
|
126
|
+
reader.onload = function (evt) {
|
|
127
|
+
setFile(
|
|
128
|
+
(evt &&
|
|
129
|
+
evt.target &&
|
|
130
|
+
evt.target.result &&
|
|
131
|
+
evt.target.result.toString()) ||
|
|
132
|
+
''
|
|
133
|
+
);
|
|
134
|
+
onFinish && onFinish({ file });
|
|
111
135
|
};
|
|
112
136
|
reader.readAsDataURL(file);
|
|
113
137
|
};
|
|
114
138
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
}
|
|
139
|
+
const onDeleteFile = () => {
|
|
140
|
+
setFile(undefined);
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
return (
|
|
144
|
+
<div>
|
|
145
|
+
<Dropzone
|
|
146
|
+
{...args}
|
|
147
|
+
onSendFile={onSendFile}
|
|
148
|
+
accept={['image/*']}
|
|
149
|
+
helperMessage="Only PNG files"
|
|
150
|
+
sendFiles
|
|
151
|
+
onDeleteFile={onDeleteFile}
|
|
152
|
+
/>
|
|
153
|
+
{file && <img src={file} />}
|
|
154
|
+
</div>
|
|
155
|
+
);
|
|
156
|
+
};
|
|
125
157
|
|
|
126
|
-
export const Text = () => {
|
|
158
|
+
export const Text = (args: DropzoneProps) => {
|
|
127
159
|
const [file, setFile] = useState<string>();
|
|
128
160
|
|
|
129
|
-
|
|
130
|
-
const {file, onFinish} = props;
|
|
161
|
+
const onSendFile = (props: OnSendFileProps) => {
|
|
162
|
+
const { file, onFinish } = props;
|
|
131
163
|
const reader = new FileReader();
|
|
132
|
-
reader.onload = function(evt) {
|
|
133
|
-
setFile(
|
|
134
|
-
|
|
164
|
+
reader.onload = function (evt) {
|
|
165
|
+
setFile(
|
|
166
|
+
(evt &&
|
|
167
|
+
evt.target &&
|
|
168
|
+
evt.target.result &&
|
|
169
|
+
evt.target.result.toString()) ||
|
|
170
|
+
''
|
|
171
|
+
);
|
|
172
|
+
onFinish && onFinish({ file });
|
|
135
173
|
};
|
|
136
174
|
reader.readAsText(file);
|
|
137
175
|
};
|
|
138
176
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
}
|
|
177
|
+
const onDeleteFile = () => {
|
|
178
|
+
setFile(undefined);
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
return (
|
|
182
|
+
<div>
|
|
183
|
+
<Dropzone
|
|
184
|
+
{...args}
|
|
185
|
+
onSendFile={onSendFile}
|
|
186
|
+
accept={['.txt', '.csv']}
|
|
187
|
+
helperMessage="Only TXT or CSV files"
|
|
188
|
+
sendFiles
|
|
189
|
+
onDeleteFile={onDeleteFile}
|
|
190
|
+
/>
|
|
191
|
+
{file && <Textarea textareaStyle={{ height: '250px' }} value={file} />}
|
|
192
|
+
</div>
|
|
193
|
+
);
|
|
194
|
+
};
|
|
149
195
|
|
|
150
|
-
export const Csv = () => {
|
|
196
|
+
export const Csv = (args: DropzoneProps) => {
|
|
151
197
|
const [file, setFile] = useState<any>();
|
|
152
198
|
const [columns, setColumns] = useState<any>();
|
|
153
199
|
|
|
154
200
|
const onSendFile = (props: OnSendFileProps) => {
|
|
155
|
-
const {file, onFinish} = props;
|
|
201
|
+
const { file, onFinish } = props;
|
|
156
202
|
const reader = new FileReader();
|
|
157
|
-
reader.onload = function(evt) {
|
|
158
|
-
const [columns, rows] =
|
|
203
|
+
reader.onload = function (evt) {
|
|
204
|
+
const [columns, rows] =
|
|
205
|
+
(evt &&
|
|
206
|
+
evt.target &&
|
|
207
|
+
evt.target.result &&
|
|
208
|
+
csvJSON(evt.target.result.toString())) ||
|
|
209
|
+
[];
|
|
159
210
|
setColumns(columns);
|
|
160
211
|
setFile(rows);
|
|
161
|
-
onFinish && onFinish({file})
|
|
212
|
+
onFinish && onFinish({ file });
|
|
162
213
|
};
|
|
163
214
|
reader.readAsText(file);
|
|
164
215
|
};
|
|
216
|
+
const onDeleteFile = () => {
|
|
217
|
+
setFile(undefined);
|
|
218
|
+
};
|
|
165
219
|
|
|
166
|
-
return
|
|
167
|
-
<
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
220
|
+
return (
|
|
221
|
+
<div>
|
|
222
|
+
<Dropzone
|
|
223
|
+
{...args}
|
|
224
|
+
onSendFile={onSendFile}
|
|
225
|
+
accept={['.csv']}
|
|
226
|
+
helperMessage="Only CSV files"
|
|
227
|
+
sendFiles
|
|
228
|
+
onDeleteFile={onDeleteFile}
|
|
229
|
+
/>
|
|
230
|
+
{file && <Datagrid columns={columns} rows={file} />}
|
|
231
|
+
</div>
|
|
232
|
+
);
|
|
233
|
+
};
|
|
@@ -138,6 +138,13 @@ const formatError = (
|
|
|
138
138
|
},
|
|
139
139
|
byteLabel: string
|
|
140
140
|
) => {
|
|
141
|
+
const accept =
|
|
142
|
+
Array.isArray(constraints.accept) && constraints.accept.length === 1
|
|
143
|
+
? constraints.accept[0]
|
|
144
|
+
: constraints.accept;
|
|
145
|
+
const messageSuffix = Array.isArray(accept)
|
|
146
|
+
? `one of ${accept.join(', ')}`
|
|
147
|
+
: accept;
|
|
141
148
|
switch (error.code) {
|
|
142
149
|
case 'file-too-large':
|
|
143
150
|
return {
|
|
@@ -150,13 +157,6 @@ const formatError = (
|
|
|
150
157
|
message: `${error.message} ${formatFileSize(constraints.minSize, 2, byteLabel)}.`,
|
|
151
158
|
};
|
|
152
159
|
case 'file-invalid-type':
|
|
153
|
-
const accept =
|
|
154
|
-
Array.isArray(constraints.accept) && constraints.accept.length === 1
|
|
155
|
-
? constraints.accept[0]
|
|
156
|
-
: constraints.accept;
|
|
157
|
-
const messageSuffix = Array.isArray(accept)
|
|
158
|
-
? `one of ${accept.join(', ')}`
|
|
159
|
-
: accept;
|
|
160
160
|
return { ...error, message: `${error.message}: ${messageSuffix}` };
|
|
161
161
|
default:
|
|
162
162
|
return error;
|