-
Notifications
You must be signed in to change notification settings - Fork 3
Implement FileUpload component #269
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
emir89
wants to merge
5
commits into
feature/createFileUploadComponentForBetterUppyUsage-CMEM-5925
Choose a base branch
from
feature/file-upload-CMEM-5925
base: feature/createFileUploadComponentForBetterUppyUsage-CMEM-5925
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,2 +1,4 @@ | ||
| export * from "./codemirror/CodeMirror"; | ||
| export * from "./react-flow"; | ||
| export * from "./uppy/FileUpload"; | ||
| export * from "@uppy/core"; |
This file contains hidden or 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,33 @@ | ||
| import React from "react"; | ||
| import { Meta, StoryFn } from "@storybook/react"; | ||
|
|
||
| import { FileUpload } from "./FileUpload"; | ||
|
|
||
| export default { | ||
| title: "Extensions/FileUpload", | ||
| component: FileUpload, | ||
| } as Meta<typeof FileUpload>; | ||
|
|
||
| const TemplateFull: StoryFn<typeof FileUpload> = (args) => <FileUpload {...args} />; | ||
|
|
||
| export const BasicExample = TemplateFull.bind({}); | ||
| BasicExample.args = { | ||
| id: "fileupload", | ||
| onError: null, | ||
| onSuccess: null, | ||
| handleResponseData: null, | ||
| localeOptions: { | ||
| strings: { | ||
| dropHereOr: "Drop here or %{browse}", | ||
| browse: "browse", | ||
| }, | ||
| }, | ||
| resetForm: true, | ||
| autoProceed: true, | ||
| restrictions: { | ||
| maxFileSize: 1000000, | ||
| minNumberOfFiles: 1, | ||
| maxNumberOfFiles: 5, | ||
| allowedFileTypes: [".jpg", ".png"], | ||
| }, | ||
| }; |
This file contains hidden or 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,169 @@ | ||
| import React from "react"; | ||
| import Uppy, { Locale, Restrictions, UppyEventMap, UppyFile } from "@uppy/core"; | ||
| import { DragDrop, ProgressBar, useUppy } from "@uppy/react"; | ||
| import XHRUpload, { XHRUploadOptions } from "@uppy/xhr-upload"; | ||
|
|
||
| import { TestableComponent } from "../../components/interfaces"; | ||
|
|
||
| import "@uppy/core/dist/style.css"; | ||
| import "@uppy/drag-drop/dist/style.css"; | ||
| import "@uppy/progress-bar/dist/style.css"; | ||
|
|
||
| type eventNames = keyof UppyEventMap; | ||
|
|
||
| export interface FileUploadProps extends TestableComponent { | ||
| /** | ||
| * upload instance id | ||
| */ | ||
| id?: string; | ||
| /** | ||
| * upload error callback function | ||
| */ | ||
| onError?: (responseText: string) => void; | ||
| /** | ||
| * upload success callback function | ||
| */ | ||
| onSuccess?: (responseText: string) => void; | ||
| /** | ||
| * upload response callback function | ||
| */ | ||
| handleResponseData?: (responseText: string) => void; | ||
| /** | ||
| * locale options, like button labels etc. | ||
| */ | ||
| localeOptions?: Locale; | ||
| /** | ||
| * reset form after upload or in case of error/navigation steps | ||
| */ | ||
| resetForm?: boolean; | ||
| /** | ||
| * auto proceed bolean | ||
| */ | ||
| autoProceed?: boolean; | ||
| /** | ||
| * upload restrictions | ||
| */ | ||
| restrictions?: Restrictions; | ||
| /** | ||
| * additional upload files | ||
| */ | ||
| additionalFiles?: { | ||
| id: string; | ||
| name: string; | ||
| size: number; | ||
| data: Blob; | ||
| }[]; | ||
| /** | ||
| * xhr upload options | ||
| */ | ||
| xhrUploadOptions: XHRUploadOptions; | ||
| /** | ||
| * uppy events to register and unregister to | ||
| */ | ||
| events?: { [key in eventNames]?: any }; | ||
| /** | ||
| * set uppy instance | ||
| */ | ||
| setInstance?: (instance: Uppy) => void; | ||
| } | ||
|
|
||
| export { ProgressBar as UppyProgressBar, DragDrop } from "@uppy/react"; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be exported as |
||
|
|
||
| export const FileUpload: React.FC<FileUploadProps> = ({ | ||
| id, | ||
| onError, | ||
| onSuccess, | ||
| handleResponseData, | ||
| resetForm, | ||
| autoProceed = false, | ||
| restrictions, | ||
| additionalFiles, | ||
| xhrUploadOptions, | ||
| localeOptions, | ||
| events, | ||
| setInstance, | ||
| children, | ||
| ...rest | ||
| }) => { | ||
| const uppy = useUppy(() => { | ||
| return new Uppy({ | ||
| id, | ||
| autoProceed, | ||
| restrictions, | ||
| locale: localeOptions, | ||
| }) | ||
| .on("upload-success", (_, response) => { | ||
| onSuccess?.(response.body); | ||
| }) | ||
| .use(XHRUpload, { | ||
| ...xhrUploadOptions, | ||
| getResponseData: (responseText) => { | ||
| handleResponseData?.(responseText); | ||
|
|
||
| return responseText; | ||
| }, | ||
| getResponseError: (responseText) => { | ||
| uppy.reset(); | ||
|
|
||
| onError?.(responseText); | ||
| return new Error(responseText); | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| React.useEffect(() => { | ||
| uppy && setInstance && setInstance(uppy); | ||
| const eventEntries = Object.entries(events ?? {}) as [keyof UppyEventMap, (file: UppyFile) => Promise<void>][]; | ||
|
|
||
| const unregisterEvents = () => { | ||
| eventEntries.length && eventEntries.forEach(([event, handler]) => uppy.off(event, handler)); | ||
| }; | ||
|
|
||
| unregisterEvents(); | ||
|
|
||
| eventEntries.length && | ||
| eventEntries.forEach(([event, handler]) => { | ||
| uppy.on(event, handler); | ||
| }); | ||
|
|
||
| return () => unregisterEvents(); | ||
| }, [uppy, events]); | ||
|
|
||
| React.useEffect(() => { | ||
| if (resetForm) { | ||
| uppy.reset(); | ||
| } | ||
|
|
||
| return () => { | ||
| uppy.close(); | ||
| }; | ||
| }, [resetForm, uppy]); | ||
|
|
||
| React.useEffect(() => { | ||
| if (additionalFiles) { | ||
| additionalFiles.forEach((file) => { | ||
| uppy.addFile({ | ||
| name: file.name, | ||
| data: file.data, | ||
| size: file.size, | ||
| id: file.id, | ||
| }); | ||
| }); | ||
| } | ||
| }, [additionalFiles, uppy]); | ||
|
|
||
| if (!uppy) return null; | ||
|
|
||
| return ( | ||
| <div {...rest}> | ||
| {children ? ( | ||
| children | ||
| ) : ( | ||
| <> | ||
| <DragDrop uppy={uppy} /> | ||
| <ProgressBar uppy={uppy} /> | ||
| </> | ||
| )} | ||
| </div> | ||
| ); | ||
| }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FileUploadPropsprobaby should extend also Uppy and HTMLDivElement. Or is there a reason not to do so?Properties forwarded to Uppy could also be included by the
uppyPropspattern that we use also in other components. This way it is more distinct what are our properties, what properties come from Uppy.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't notice the particular reason for this, but if it makes more sense according to the certain pattern, then sure it should be adjusted.