Skip to content

Commit

Permalink
Upgrade 'react-dropzone' to latest version
Browse files Browse the repository at this point in the history
  • Loading branch information
jonkoops committed Sep 2, 2022
1 parent 07d9fc8 commit 2ca13c0
Show file tree
Hide file tree
Showing 16 changed files with 148 additions and 171 deletions.
2 changes: 1 addition & 1 deletion packages/react-code-editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"@patternfly/react-core": "^4.236.3",
"@patternfly/react-icons": "^4.87.3",
"@patternfly/react-styles": "^4.86.3",
"react-dropzone": "9.0.0",
"react-dropzone": "^14.2.2",
"tslib": "^2.0.0"
},
"peerDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import UploadIcon from '@patternfly/react-icons/dist/esm/icons/upload-icon';
import DownloadIcon from '@patternfly/react-icons/dist/esm/icons/download-icon';
import CodeIcon from '@patternfly/react-icons/dist/esm/icons/code-icon';
import HelpIcon from '@patternfly/react-icons/dist/esm/icons/help-icon';
import Dropzone from 'react-dropzone';
import Dropzone, { FileRejection } from 'react-dropzone';
import { CodeEditorContext } from './CodeEditorUtils';

export interface Shortcut {
Expand Down Expand Up @@ -426,7 +426,7 @@ export class CodeEditor extends React.Component<CodeEditorProps, CodeEditorState
}
};

onDropRejected = (rejectedFiles: File[]) => {
onDropRejected = (rejectedFiles: FileRejection[]) => {
if (rejectedFiles.length > 0) {
// eslint-disable-next-line no-console
console.error('There was an error accepting that dropped file'); // TODO
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ exports[`CodeEditor matches snapshot with all props 1`] = `
class="pf-c-code-editor pf-m-read-only"
>
<div
class="pf-c-file-upload undefined false"
class="pf-c-file-upload false false"
role="presentation"
tabindex="0"
>
<div
Expand Down Expand Up @@ -119,7 +120,6 @@ exports[`CodeEditor matches snapshot with all props 1`] = `
class="pf-c-code-editor__main"
>
<input
autocomplete="off"
style="display: none;"
tabindex="-1"
type="file"
Expand Down
2 changes: 1 addition & 1 deletion packages/react-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"@patternfly/react-styles": "^4.86.3",
"@patternfly/react-tokens": "^4.88.3",
"focus-trap": "6.9.2",
"react-dropzone": "9.0.0",
"react-dropzone": "^14.2.2",
"tippy.js": "5.1.2",
"tslib": "^2.0.0"
},
Expand Down
117 changes: 55 additions & 62 deletions packages/react-core/src/components/FileUpload/FileUpload.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import * as React from 'react';
import Dropzone, { DropzoneProps, DropzoneInputProps, DropFileEventHandler } from 'react-dropzone';
import { DropEvent, DropzoneInputProps, DropzoneOptions, FileRejection, useDropzone } from 'react-dropzone';
import { FileUploadField, FileUploadFieldProps } from './FileUploadField';
import { readFile, fileReaderType } from '../../helpers/fileUtils';
import { fromEvent } from 'file-selector';

interface DropzoneInputPropsWithRef extends DropzoneInputProps {
ref: React.RefCallback<HTMLInputElement>; // Working around an issue in react-dropzone 9.0.0's types. Should not be necessary in later versions.
}

export interface FileUploadProps
extends Omit<
FileUploadFieldProps,
Expand All @@ -29,11 +25,11 @@ export interface FileUploadProps
filename: string,
event:
| React.MouseEvent<HTMLButtonElement, MouseEvent> // Clear button was clicked
| React.DragEvent<HTMLElement> // User dragged/dropped a file
| React.ChangeEvent<HTMLElement> // User typed in the TextArea
| DropEvent
) => void;
/** Change event emitted from the hidden \<input type="file" \> field associated with the component */
onFileInputChange?: (event: React.ChangeEvent<HTMLInputElement> | React.DragEvent<HTMLElement>, file: File) => void;
onFileInputChange?: (event: DropEvent, file: File) => void;
/** Callback for clicking on the FileUploadField text area. By default, prevents a click in the text area from opening file dialog. */
onClick?: (event: React.MouseEvent) => void;
/** Additional classes added to the FileUpload container element. */
Expand Down Expand Up @@ -80,7 +76,7 @@ export interface FileUploadProps
/** A callback for when the FileReader API fails */
onReadFailed?: (error: DOMException, fileHandle: File) => void;
/** Optional extra props to customize react-dropzone. */
dropzoneProps?: DropzoneProps;
dropzoneProps?: DropzoneOptions;
/** Clear button was clicked */
onClearClick?: React.MouseEventHandler<HTMLButtonElement>;
/** Text area text changed */
Expand All @@ -107,7 +103,7 @@ export const FileUpload: React.FunctionComponent<FileUploadProps> = ({
dropzoneProps = {},
...props
}: FileUploadProps) => {
const onDropAccepted: DropFileEventHandler = (acceptedFiles, event) => {
const onDropAccepted = (acceptedFiles: File[], event: DropEvent) => {
if (acceptedFiles.length > 0) {
const fileHandle = acceptedFiles[0];
if (event.type === 'drop') {
Expand Down Expand Up @@ -135,71 +131,68 @@ export const FileUpload: React.FunctionComponent<FileUploadProps> = ({
dropzoneProps.onDropAccepted && dropzoneProps.onDropAccepted(acceptedFiles, event);
};

const onDropRejected: DropFileEventHandler = (rejectedFiles, event) => {
const onDropRejected = (rejectedFiles: FileRejection[], event: DropEvent) => {
if (rejectedFiles.length > 0) {
onChange('', rejectedFiles[0].name, event);
onChange('', rejectedFiles[0].file.name, event);
}
dropzoneProps.onDropRejected && dropzoneProps.onDropRejected(rejectedFiles, event);
};

const fileInputRef = React.useRef<HTMLInputElement>();
const setFileValue = (filename: string) => {
fileInputRef.current.value = filename;
};

const onClearButtonClick = (event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
onChange('', '', event);
onClearClick?.(event);
setFileValue(null);
};

return (
<Dropzone multiple={false} {...dropzoneProps} onDropAccepted={onDropAccepted} onDropRejected={onDropRejected}>
{({ getRootProps, getInputProps, isDragActive, open }) => {
const oldInputProps = getInputProps();
const inputProps: DropzoneInputProps = {
...oldInputProps,
onChange: async (e: React.ChangeEvent<HTMLInputElement>) => {
oldInputProps.onChange?.(e);
const files = await fromEvent(e.nativeEvent);
if (files.length === 1) {
onFileInputChange?.(e, files[0] as File);
}
}
};
const { getRootProps, getInputProps, isDragActive, open, inputRef } = useDropzone({
multiple: false,
...dropzoneProps,
onDropAccepted,
onDropRejected
});

const setFileValue = (filename: string) => {
inputRef.current.value = filename;
};

return (
<FileUploadField
{...getRootProps({
...props,
refKey: 'containerRef',
onClick: event => event.preventDefault()
})}
tabIndex={null} // Omit the unwanted tabIndex from react-dropzone's getRootProps
id={id}
type={type}
filename={filename}
value={value}
onChange={onChange}
isDragActive={isDragActive}
onBrowseButtonClick={open}
onClearButtonClick={onClearButtonClick}
onTextAreaClick={onClick}
onTextChange={onTextChange}
>
<input
/* hidden, necessary for react-dropzone */
{...inputProps}
ref={input => {
fileInputRef.current = input;
(inputProps as DropzoneInputPropsWithRef).ref(input);
}}
/>
{children}
</FileUploadField>
);
}}
</Dropzone>
const oldInputProps = getInputProps();
const inputProps: DropzoneInputProps = {
...oldInputProps,
onChange: async (e: React.ChangeEvent<HTMLInputElement>) => {
oldInputProps.onChange?.(e);
const files = await fromEvent(e.nativeEvent);
if (files.length === 1) {
onFileInputChange?.(e, files[0] as File);
}
}
};

return (
<FileUploadField
{...getRootProps({
...props,
refKey: 'containerRef',
onClick: event => event.preventDefault()
})}
tabIndex={null} // Omit the unwanted tabIndex from react-dropzone's getRootProps
id={id}
type={type}
filename={filename}
value={value}
onChange={onChange}
isDragActive={isDragActive}
onBrowseButtonClick={open}
onClearButtonClick={onClearButtonClick}
onTextAreaClick={onClick}
onTextChange={onTextChange}
>
<input
/* hidden, necessary for react-dropzone */
{...inputProps}
ref={inputRef}
/>
{children}
</FileUploadField>
);
};
FileUpload.displayName = 'FileUpload';
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ exports[`simple fileupload 1`] = `
<DocumentFragment>
<div
class="pf-c-file-upload"
role="presentation"
>
<div
class="pf-c-file-upload__file-select"
Expand Down Expand Up @@ -63,7 +64,6 @@ exports[`simple fileupload 1`] = `
/>
</div>
<input
autocomplete="off"
style="display: none;"
tabindex="-1"
type="file"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,17 @@ import { FileUpload } from '@patternfly/react-core';
import FileUploadIcon from '@patternfly/react-icons/dist/esm/icons/file-upload-icon';

export const CustomPreviewFileUpload: React.FunctionComponent = () => {
const [value, setValue] = React.useState(null);
const [value, setValue] = React.useState<File>();
const [filename, setFilename] = React.useState('');

const handleFileInputChange = (
_event: React.ChangeEvent<HTMLInputElement> | React.DragEvent<HTMLElement>,
file: File
) => {
const handleFileInputChange = (_, file: File) => {
setValue(file);
setFilename(file.name);
};

const handleClear = (_event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
setFilename('');
setValue('');
setValue(undefined);
};

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ import React from 'react';
import { FileUpload } from '@patternfly/react-core';

export const SimpleFileUpload: React.FunctionComponent = () => {
const [value, setValue] = React.useState(null);
const [value, setValue] = React.useState('');
const [filename, setFilename] = React.useState('');

const handleFileInputChange = (
_event: React.ChangeEvent<HTMLInputElement> | React.DragEvent<HTMLElement>,
file: File
) => {
const handleFileInputChange = (_, file: File) => {
setFilename(file.name);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ export const SimpleTextFileUpload: React.FunctionComponent = () => {
const [filename, setFilename] = React.useState('');
const [isLoading, setIsLoading] = React.useState(false);

const handleFileInputChange = (
_event: React.ChangeEvent<HTMLInputElement> | React.DragEvent<HTMLElement>,
file: File
) => {
const handleFileInputChange = (_, file: File) => {
setFilename(file.name);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ export const TextFileWithEditsAllowed: React.FunctionComponent = () => {
const [filename, setFilename] = React.useState('');
const [isLoading, setIsLoading] = React.useState(false);

const handleFileInputChange = (
_event: React.ChangeEvent<HTMLInputElement> | React.DragEvent<HTMLElement>,
file: File
) => {
const handleFileInputChange = (_, file: File) => {
setFilename(file.name);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ export const TextFileUploadWithRestrictions: React.FunctionComponent = () => {
const [isLoading, setIsLoading] = React.useState(false);
const [isRejected, setIsRejected] = React.useState(false);

const handleFileInputChange = (
_event: React.ChangeEvent<HTMLInputElement> | React.DragEvent<HTMLElement>,
file: File
) => {
const handleFileInputChange = (_, file: File) => {
setFilename(file.name);
};

Expand All @@ -24,7 +21,7 @@ export const TextFileUploadWithRestrictions: React.FunctionComponent = () => {
setIsRejected(false);
};

const handleFileRejected = (_rejectedFiles: File[], _event: React.DragEvent<HTMLElement>) => {
const handleFileRejected = () => {
setIsRejected(true);
};

Expand Down Expand Up @@ -58,7 +55,7 @@ export const TextFileUploadWithRestrictions: React.FunctionComponent = () => {
onClearClick={handleClear}
isLoading={isLoading}
dropzoneProps={{
accept: '.csv',
accept: { 'text/csv': ['.csv'] },
maxSize: 1024,
onDropRejected: handleFileRejected
}}
Expand Down

0 comments on commit 2ca13c0

Please sign in to comment.