-
Notifications
You must be signed in to change notification settings - Fork 829
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
8 changed files
with
107 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
documentation-site/examples/file-uploader/dynamic-loading.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import * as React from "react"; | ||
import { FileUploader, type FileRow } from "baseui/file-uploader"; | ||
|
||
export default function Example() { | ||
const [fileRows, setFileRows] = React.useState<Array<FileRow>>([]); | ||
|
||
// Fake an upload process and mock the amount of time it takes each file to load | ||
// For a real-world scenario, replace simulateFileProgressUpdates with application upload logic | ||
const simulateFileProgressUpdates = ( | ||
fileToProcess: File, | ||
fileToProcessId: string, | ||
fileRows: FileRow[], | ||
resolve: ({ | ||
errorMessage, | ||
fileInfo, | ||
}: { | ||
errorMessage: string | null; | ||
fileInfo?: any; | ||
}) => void, | ||
) => { | ||
const fileRowsCopy: FileRow[] = [...fileRows]; | ||
let indexOfFileToUpdate = fileRowsCopy.findIndex( | ||
(fileRow: FileRow) => fileRow.id === fileToProcessId, | ||
); | ||
let numberOfMockedLoadingSteps = 5 - (indexOfFileToUpdate % 3); | ||
let mockedTotalLoadingTime = indexOfFileToUpdate % 2 === 0 ? 10000 : 8000; | ||
for (let i = 0; i <= numberOfMockedLoadingSteps; i++) { | ||
if (i === numberOfMockedLoadingSteps) { | ||
// Simulates an onSuccess event | ||
setTimeout(() => { | ||
resolve({ errorMessage: null }); | ||
}, mockedTotalLoadingTime); | ||
} else { | ||
// Simulates an onLoading event | ||
setTimeout( | ||
() => { | ||
fileRowsCopy[indexOfFileToUpdate].progressAmount = | ||
(i / numberOfMockedLoadingSteps) * 100; | ||
setFileRows([...fileRowsCopy]); | ||
}, | ||
(i / numberOfMockedLoadingSteps) * mockedTotalLoadingTime, | ||
); | ||
} | ||
} | ||
}; | ||
|
||
return ( | ||
<FileUploader | ||
fileRows={fileRows} | ||
hint={ | ||
"Try uploading multiple files at once to see the progress bar upload independently for each file" | ||
} | ||
processFileOnDrop={(fileToProcess, fileToProcessId, newFileRows) => { | ||
return new Promise((resolve) => { | ||
simulateFileProgressUpdates( | ||
fileToProcess, | ||
fileToProcessId, | ||
newFileRows, | ||
resolve, | ||
); | ||
}); | ||
}} | ||
progressAmountStartValue={0} | ||
setFileRows={setFileRows} | ||
/> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters