Skip to content

Commit 2113e18

Browse files
authored
Add dynamic loading to file uploader documentation (#5357)
* Move file uploader basic stories to proper folder * Update file uploader code * Add file uploader stories * Add dynamic loading to file uploader documentation
1 parent 8c82af3 commit 2113e18

File tree

8 files changed

+107
-18
lines changed

8 files changed

+107
-18
lines changed

documentation-site/cheat-sheet.jsx

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1479,17 +1479,19 @@ const outlines = [
14791479
{ name: "id", lineStart: 46 },
14801480
{ name: "fileInfo", lineStart: 49 },
14811481
{ name: "imagePreviewThumbnail", lineStart: 50 },
1482-
{ name: "status", lineStart: 52 },
1482+
{ name: "progressAmount", lineStart: 51 },
1483+
{ name: "status", lineStart: 53 },
14831484
] },
1484-
{ name: "FileUploaderProps", lineStart: 55, children: [
1485-
{ name: "fileRows", lineStart: 59 },
1486-
{ name: "hint", lineStart: 60 },
1487-
{ name: "itemPreview", lineStart: 61 },
1488-
{ name: "label", lineStart: 62 },
1489-
{ name: "maxFiles", lineStart: 63 },
1490-
{ name: "overrides", lineStart: 64 },
1491-
{ name: "processFileOnDrop", lineStart: 66 },
1492-
{ name: "setFileRows", lineStart: 67 },
1485+
{ name: "FileUploaderProps", lineStart: 56, children: [
1486+
{ name: "fileRows", lineStart: 60 },
1487+
{ name: "hint", lineStart: 61 },
1488+
{ name: "itemPreview", lineStart: 62 },
1489+
{ name: "label", lineStart: 63 },
1490+
{ name: "maxFiles", lineStart: 64 },
1491+
{ name: "overrides", lineStart: 65 },
1492+
{ name: "processFileOnDrop", lineStart: 67 },
1493+
{ name: "progressAmountStartValue", lineStart: 72 },
1494+
{ name: "setFileRows", lineStart: 73 },
14931495
] },
14941496
],
14951497
},

documentation-site/components/yard/config/file-uploader.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,13 @@ const FileUploaderConfig: TConfig = {
135135
description: `Sets aria-describedby attribute.`,
136136
hidden: true,
137137
},
138+
progressAmountStartValue: {
139+
value: undefined,
140+
type: PropTypes.Number,
141+
description:
142+
"Sets the progress amount the loading bar begins at. It is a number between 1 and 100. It is particularly useful for the 'Dynamic loading' example.",
143+
hidden: true,
144+
},
138145
overrides: {
139146
value: undefined,
140147
type: PropTypes.Custom,

documentation-site/examples/file-uploader/basic.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import { FileUploader, type FileRow } from "baseui/file-uploader";
44
export default function Example() {
55
const [fileRows, setFileRows] = React.useState<Array<FileRow>>([
66
{
7+
errorMessage: null,
78
file: new File(["test file"], "file.txt"),
89
id: "0",
10+
progressAmount: 100,
911
status: "processed",
10-
errorMessage: null,
1112
},
1213
]);
1314

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import * as React from "react";
2+
import { FileUploader, type FileRow } from "baseui/file-uploader";
3+
4+
export default function Example() {
5+
const [fileRows, setFileRows] = React.useState<Array<FileRow>>([]);
6+
7+
// Fake an upload process and mock the amount of time it takes each file to load
8+
// For a real-world scenario, replace simulateFileProgressUpdates with application upload logic
9+
const simulateFileProgressUpdates = (
10+
fileToProcess: File,
11+
fileToProcessId: string,
12+
fileRows: FileRow[],
13+
resolve: ({
14+
errorMessage,
15+
fileInfo,
16+
}: {
17+
errorMessage: string | null;
18+
fileInfo?: any;
19+
}) => void,
20+
) => {
21+
const fileRowsCopy: FileRow[] = [...fileRows];
22+
let indexOfFileToUpdate = fileRowsCopy.findIndex(
23+
(fileRow: FileRow) => fileRow.id === fileToProcessId,
24+
);
25+
let numberOfMockedLoadingSteps = 5 - (indexOfFileToUpdate % 3);
26+
let mockedTotalLoadingTime = indexOfFileToUpdate % 2 === 0 ? 10000 : 8000;
27+
for (let i = 0; i <= numberOfMockedLoadingSteps; i++) {
28+
if (i === numberOfMockedLoadingSteps) {
29+
// Simulates an onSuccess event
30+
setTimeout(() => {
31+
resolve({ errorMessage: null });
32+
}, mockedTotalLoadingTime);
33+
} else {
34+
// Simulates an onLoading event
35+
setTimeout(
36+
() => {
37+
fileRowsCopy[indexOfFileToUpdate].progressAmount =
38+
(i / numberOfMockedLoadingSteps) * 100;
39+
setFileRows([...fileRowsCopy]);
40+
},
41+
(i / numberOfMockedLoadingSteps) * mockedTotalLoadingTime,
42+
);
43+
}
44+
}
45+
};
46+
47+
return (
48+
<FileUploader
49+
fileRows={fileRows}
50+
hint={
51+
"Try uploading multiple files at once to see the progress bar upload independently for each file"
52+
}
53+
processFileOnDrop={(fileToProcess, fileToProcessId, newFileRows) => {
54+
return new Promise((resolve) => {
55+
simulateFileProgressUpdates(
56+
fileToProcess,
57+
fileToProcessId,
58+
newFileRows,
59+
resolve,
60+
);
61+
});
62+
}}
63+
progressAmountStartValue={0}
64+
setFileRows={setFileRows}
65+
/>
66+
);
67+
}

documentation-site/examples/file-uploader/item-preview.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import { FileUploader, type FileRow } from "baseui/file-uploader";
44
export default function Example() {
55
const [fileRows, setFileRows] = React.useState<Array<FileRow>>([
66
{
7+
errorMessage: null,
78
file: new File(["test file"], "file.txt"),
89
id: "0",
10+
progressAmount: 100,
911
status: "processed",
10-
errorMessage: null,
1112
},
1213
]);
1314
return (

documentation-site/examples/file-uploader/overrides.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,18 @@ import { useStyletron } from "baseui";
55
export default function Example() {
66
const [fileRows, setFileRows] = React.useState<Array<FileRow>>([
77
{
8+
errorMessage: null,
89
file: new File(["test file 1"], "file-1.txt"),
910
id: "0",
11+
progressAmount: 100,
1012
status: "processed",
11-
errorMessage: null,
1213
},
1314
{
15+
errorMessage: "Failed to upload",
1416
file: new File(["test file 2"], "file-2.txt"),
1517
id: "1",
18+
progressAmount: 20,
1619
status: "error",
17-
errorMessage: "Failed to upload",
1820
},
1921
]);
2022
const [, theme] = useStyletron();

documentation-site/examples/file-uploader/upload-restrictions.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,32 @@ export default function Example() {
66
// artificially create an array of file rows with errors.
77
const [fileRows, setFileRows] = React.useState<Array<FileRow>>([
88
{
9+
errorMessage: "file type of img/jpeg is not accepted",
910
file: new File(["test file 1"], "unaccepted-file-type.jpeg"),
1011
id: "0",
12+
progressAmount: 20,
1113
status: "error",
12-
errorMessage: "file type of img/jpeg is not accepted",
1314
},
1415
{
16+
errorMessage: "file size must be greater than 20 KB",
1517
file: new File(["test file 2"], "file-too-small.png"),
1618
id: "1",
19+
progressAmount: 20,
1720
status: "error",
18-
errorMessage: "file size must be greater than 20 KB",
1921
},
2022
{
23+
errorMessage: "file size must be less than 100 KB",
2124
file: new File(["test file 3"], "file-too-big.png"),
2225
id: "2",
26+
progressAmount: 20,
2327
status: "error",
24-
errorMessage: "file size must be less than 100 KB",
2528
},
2629
{
30+
errorMessage: "cannot process more than 3 file(s)",
2731
file: new File(["test file 4"], "file-count-too-many.png"),
2832
id: "3",
33+
progressAmount: 20,
2934
status: "error",
30-
errorMessage: "cannot process more than 3 file(s)",
3135
},
3236
]);
3337

documentation-site/pages/components/file-uploader.mdx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import FileUploaderBasic from "examples/file-uploader/basic.tsx";
66
import FileUploaderItemPreview from "examples/file-uploader/item-preview.tsx";
77
import FileUploaderLabelHint from "examples/file-uploader/label-hint.tsx";
88
import FileUploaderUploadRestrictions from "examples/file-uploader/upload-restrictions.tsx";
9+
import FileUploaderDynamicLoading from "examples/file-uploader/dynamic-loading.tsx";
910
import FileUploaderOverrides from "examples/file-uploader/overrides.tsx";
1011

1112
import * as FileUploaderExports from "baseui/file-uploader";
@@ -64,6 +65,10 @@ To learn more, read the corresponding [OWASP article on file uploads](https://ww
6465
<FileUploaderUploadRestrictions />
6566
</Example>
6667

68+
<Example title="Dynamic loading" path="file-uploader/dynamic-loading.tsx">
69+
<FileUploaderDynamicLoading />
70+
</Example>
71+
6772
<Example title="Overrides" path="file-uploader/overrides.tsx">
6873
<FileUploaderOverrides />
6974
</Example>

0 commit comments

Comments
 (0)