Skip to content

Commit 8c82af3

Browse files
authored
Add file uploader stories (#5356)
* Move file uploader basic stories to proper folder * Update file uploader code * Add file uploader stories
1 parent efde47a commit 8c82af3

9 files changed

+549
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
Copyright (c) Uber Technologies, Inc.
3+
4+
This source code is licensed under the MIT license found in the
5+
LICENSE file in the root directory of this source tree.
6+
*/
7+
import * as React from 'react';
8+
import { type FileRow, FileUploader } from '..';
9+
10+
export function Scenario() {
11+
const [fileRows, setFileRows] = React.useState<Array<FileRow>>([]);
12+
return (
13+
<FileUploader
14+
fileRows={fileRows}
15+
hint={'Try uploading a file to see the itemPreview'}
16+
itemPreview
17+
setFileRows={setFileRows}
18+
/>
19+
);
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
Copyright (c) Uber Technologies, Inc.
3+
4+
This source code is licensed under the MIT license found in the
5+
LICENSE file in the root directory of this source tree.
6+
*/
7+
import * as React from 'react';
8+
import { type FileRow, FileUploader } from '..';
9+
10+
export function Scenario() {
11+
const [fileRows, setFileRows] = React.useState<Array<FileRow>>([]);
12+
return (
13+
<FileUploader
14+
fileRows={fileRows}
15+
hint={'Test hint'}
16+
label={'Test label'}
17+
setFileRows={setFileRows}
18+
/>
19+
);
20+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
Copyright (c) Uber Technologies, Inc.
3+
4+
This source code is licensed under the MIT license found in the
5+
LICENSE file in the root directory of this source tree.
6+
*/
7+
import * as React from 'react';
8+
import { type FileRow, FileUploader } from '..';
9+
10+
export function Scenario() {
11+
const [fileRows, setFileRows] = React.useState<Array<FileRow>>([]);
12+
13+
const simulateFileProgressUpdates = (
14+
fileToProcess: File,
15+
fileToProcessId: string,
16+
fileRows: FileRow[],
17+
resolve: (file: File) => void
18+
) => {
19+
const fileRowsCopy: FileRow[] = [...fileRows];
20+
let indexOfFileToUpdate = fileRowsCopy.findIndex(
21+
(fileRow: FileRow) => fileRow.id === fileToProcessId
22+
);
23+
let numberOfMockedLoadingSteps = 5 - (indexOfFileToUpdate % 3);
24+
let mockedTotalLoadingTime = indexOfFileToUpdate % 2 === 0 ? 10000 : 8000;
25+
for (let i = 0; i <= numberOfMockedLoadingSteps; i++) {
26+
if (i === numberOfMockedLoadingSteps) {
27+
// Simulates an onSuccess event
28+
setTimeout(() => {
29+
resolve(fileToProcess);
30+
}, mockedTotalLoadingTime);
31+
} else {
32+
// Simulates an onLoading event
33+
setTimeout(() => {
34+
fileRowsCopy[indexOfFileToUpdate].progressAmount = (i / numberOfMockedLoadingSteps) * 100;
35+
setFileRows([...fileRowsCopy]);
36+
}, (i / numberOfMockedLoadingSteps) * mockedTotalLoadingTime);
37+
}
38+
}
39+
};
40+
41+
return (
42+
<FileUploader
43+
fileRows={fileRows}
44+
hint={
45+
'Try uploading multiple files at once to see the progress bar upload independently for each file'
46+
}
47+
processFileOnDrop={(fileToProcess: File, fileToProcessId: string, fileRows: FileRow[]) => {
48+
return new Promise((resolve) => {
49+
simulateFileProgressUpdates(fileToProcess, fileToProcessId, fileRows, resolve);
50+
});
51+
}}
52+
progressAmountStartValue={0}
53+
setFileRows={setFileRows}
54+
/>
55+
);
56+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
Copyright (c) Uber Technologies, Inc.
3+
4+
This source code is licensed under the MIT license found in the
5+
LICENSE file in the root directory of this source tree.
6+
*/
7+
import * as React from 'react';
8+
import { type FileRow, FileUploader } from '..';
9+
10+
export function Scenario() {
11+
const [fileRows, setFileRows] = React.useState<Array<FileRow>>([]);
12+
return (
13+
<FileUploader
14+
fileRows={fileRows}
15+
hint={'Try uploading a file, it will load for 10 seconds'}
16+
processFileOnDrop={(file) => {
17+
return new Promise((resolve) => {
18+
setTimeout(() => {
19+
resolve({ errorMessage: null });
20+
}, 10000);
21+
});
22+
}}
23+
setFileRows={setFileRows}
24+
/>
25+
);
26+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
Copyright (c) Uber Technologies, Inc.
3+
4+
This source code is licensed under the MIT license found in the
5+
LICENSE file in the root directory of this source tree.
6+
*/
7+
import * as React from 'react';
8+
import { type FileRow, FileUploader } from '..';
9+
import { useStyletron } from '../../styles'
10+
11+
export function Scenario() {
12+
const [fileRows, setFileRows] = React.useState<Array<FileRow>>([
13+
{
14+
errorMessage: null,
15+
file: new File(['test file 1'], 'file-1.txt'),
16+
id: '0',
17+
progressAmount: 100,
18+
status: 'processed',
19+
},
20+
{
21+
errorMessage: 'Failed to upload',
22+
file: new File(['test file 2'], 'file-2.txt'),
23+
id: '1',
24+
progressAmount: 20,
25+
status: 'error',
26+
},
27+
]);
28+
const [, theme] = useStyletron();
29+
return (
30+
<FileUploader
31+
fileRows={fileRows}
32+
setFileRows={setFileRows}
33+
overrides={{
34+
ButtonComponent: {
35+
props: {
36+
overrides: {
37+
BaseButton: {
38+
style: {
39+
outline: `${theme.colors.warning} solid`,
40+
},
41+
},
42+
},
43+
},
44+
},
45+
ContentMessage: {
46+
style: {
47+
color: theme.colors.warning,
48+
},
49+
},
50+
FileDragAndDrop: {
51+
style: {
52+
borderColor: theme.colors.warning,
53+
borderStyle: 'dashed',
54+
borderWidth: theme.sizing.scale0,
55+
},
56+
},
57+
FileRows: {
58+
style: {
59+
marginLeft: theme.sizing.scale0,
60+
marginRight: theme.sizing.scale0,
61+
outline: `${theme.colors.warning} dashed`,
62+
},
63+
},
64+
}}
65+
/>
66+
);
67+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
Copyright (c) Uber Technologies, Inc.
3+
4+
This source code is licensed under the MIT license found in the
5+
LICENSE file in the root directory of this source tree.
6+
*/
7+
import * as React from 'react';
8+
import { type FileRow, FileUploader } from '..';
9+
10+
export function Scenario() {
11+
const [fileRows, setFileRows] = React.useState<Array<FileRow>>([]);
12+
return (
13+
<FileUploader
14+
accept={['image/png', 'application/pdf']}
15+
fileRows={fileRows}
16+
hint={
17+
'Try uploading files that break these conditions: 1. accept set to ["image/png", "application/pdf"], 2. minSize set to 20000 bytes (20 KB), 3. maxSize set to 100000 bytes (100 KB), 4. maxFiles set to 3'
18+
}
19+
maxFiles={3}
20+
maxSize={100000}
21+
minSize={20000}
22+
setFileRows={setFileRows}
23+
/>
24+
);
25+
}

0 commit comments

Comments
 (0)