Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/extensions/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./codemirror/CodeMirror";
export * from "./react-flow";
export * from "./uppy/FileUpload";
export * from "@uppy/core";
55 changes: 48 additions & 7 deletions src/extensions/uppy/FileUpload.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { FC, useEffect } from "react";
import Uppy, { Locale, Restrictions } from "@uppy/core";
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";

Expand All @@ -9,6 +9,8 @@ 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 {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FileUploadProps probaby 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 uppyProps pattern that we use also in other components. This way it is more distinct what are our properties, what properties come from Uppy.

Copy link
Copy Markdown
Contributor Author

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.

/**
* upload instance id
Expand Down Expand Up @@ -55,9 +57,19 @@ export interface FileUploadProps extends TestableComponent {
* 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 const FileUpload: FC<FileUploadProps> = ({
export { ProgressBar as UppyProgressBar, DragDrop } from "@uppy/react";
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be exported as FileUploadProgressBar and FileUploadDragDrop.


export const FileUpload: React.FC<FileUploadProps> = ({
id,
onError,
onSuccess,
Expand All @@ -68,6 +80,9 @@ export const FileUpload: FC<FileUploadProps> = ({
additionalFiles,
xhrUploadOptions,
localeOptions,
events,
setInstance,
children,
...rest
}) => {
const uppy = useUppy(() => {
Expand Down Expand Up @@ -96,7 +111,25 @@ export const FileUpload: FC<FileUploadProps> = ({
});
});

useEffect(() => {
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();
}
Expand All @@ -106,7 +139,7 @@ export const FileUpload: FC<FileUploadProps> = ({
};
}, [resetForm, uppy]);

useEffect(() => {
React.useEffect(() => {
if (additionalFiles) {
additionalFiles.forEach((file) => {
uppy.addFile({
Expand All @@ -119,10 +152,18 @@ export const FileUpload: FC<FileUploadProps> = ({
}
}, [additionalFiles, uppy]);

if (!uppy) return null;

return (
<div {...rest}>
<DragDrop uppy={uppy} />
<ProgressBar uppy={uppy} />
{children ? (
children
) : (
<>
<DragDrop uppy={uppy} />
<ProgressBar uppy={uppy} />
</>
)}
</div>
);
};