Skip to content

Commit 14ea08b

Browse files
committed
Upgrade 'react-dropzone' to latest version
1 parent a9f8ef0 commit 14ea08b

File tree

16 files changed

+144
-170
lines changed

16 files changed

+144
-170
lines changed

packages/react-code-editor/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"@patternfly/react-core": "^4.250.1",
3434
"@patternfly/react-icons": "^4.92.6",
3535
"@patternfly/react-styles": "^4.91.6",
36-
"react-dropzone": "9.0.0",
36+
"react-dropzone": "^14.2.3",
3737
"tslib": "^2.0.0"
3838
},
3939
"peerDependencies": {

packages/react-code-editor/src/components/CodeEditor/CodeEditor.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import UploadIcon from '@patternfly/react-icons/dist/esm/icons/upload-icon';
2323
import DownloadIcon from '@patternfly/react-icons/dist/esm/icons/download-icon';
2424
import CodeIcon from '@patternfly/react-icons/dist/esm/icons/code-icon';
2525
import HelpIcon from '@patternfly/react-icons/dist/esm/icons/help-icon';
26-
import Dropzone from 'react-dropzone';
26+
import Dropzone, { FileRejection } from 'react-dropzone';
2727
import { CodeEditorContext } from './CodeEditorUtils';
2828

2929
export interface Shortcut {
@@ -432,7 +432,7 @@ export class CodeEditor extends React.Component<CodeEditorProps, CodeEditorState
432432
}
433433
};
434434

435-
onDropRejected = (rejectedFiles: File[]) => {
435+
onDropRejected = (rejectedFiles: FileRejection[]) => {
436436
if (rejectedFiles.length > 0) {
437437
// eslint-disable-next-line no-console
438438
console.error('There was an error accepting that dropped file'); // TODO

packages/react-code-editor/src/components/CodeEditor/__test__/__snapshots__/CodeEditor.test.tsx.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ exports[`CodeEditor matches snapshot with all props 1`] = `
66
class="pf-c-code-editor pf-m-read-only"
77
>
88
<div
9-
class="pf-c-file-upload undefined false"
9+
class="pf-c-file-upload false false"
10+
role="presentation"
1011
tabindex="0"
1112
>
1213
<div
@@ -119,7 +120,6 @@ exports[`CodeEditor matches snapshot with all props 1`] = `
119120
class="pf-c-code-editor__main"
120121
>
121122
<input
122-
autocomplete="off"
123123
style="display: none;"
124124
tabindex="-1"
125125
type="file"

packages/react-core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"@patternfly/react-styles": "^4.91.6",
4949
"@patternfly/react-tokens": "^4.93.6",
5050
"focus-trap": "6.9.2",
51-
"react-dropzone": "9.0.0",
51+
"react-dropzone": "^14.2.3",
5252
"tippy.js": "5.1.2",
5353
"tslib": "^2.0.0"
5454
},

packages/react-core/src/components/FileUpload/FileUpload.tsx

Lines changed: 51 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
import * as React from 'react';
2-
import Dropzone, { DropzoneProps, DropzoneInputProps, DropFileEventHandler } from 'react-dropzone';
2+
import { DropEvent, DropzoneInputProps, DropzoneOptions, FileRejection, useDropzone } from 'react-dropzone';
33
import { FileUploadField, FileUploadFieldProps } from './FileUploadField';
44
import { readFile, fileReaderType } from '../../helpers/fileUtils';
55
import { fromEvent } from 'file-selector';
66

7-
interface DropzoneInputPropsWithRef extends DropzoneInputProps {
8-
ref: React.RefCallback<HTMLInputElement>; // Working around an issue in react-dropzone 9.0.0's types. Should not be necessary in later versions.
9-
}
10-
11-
/** The main file upload component with drag and drop functionality built in by default. */
12-
137
export interface FileUploadProps
148
extends Omit<
159
FileUploadFieldProps,
@@ -52,7 +46,7 @@ export interface FileUploadProps
5246
*/
5347
onClick?: (event: React.MouseEvent) => void;
5448
/** Change event emitted from the hidden \<input type="file" \> field associated with the component */
55-
onFileInputChange?: (event: React.ChangeEvent<HTMLInputElement> | React.DragEvent<HTMLElement>, file: File) => void;
49+
onFileInputChange?: (event: DropEvent, file: File) => void;
5650
/** Aria-valuetext for the loading spinner. */
5751
spinnerAriaValueText?: string;
5852
/** What type of file. Determines whether 'onDataChange` is called and what is
@@ -70,7 +64,7 @@ export interface FileUploadProps
7064
// Props available in FileUpload but not FileUploadField:
7165

7266
/** Optional extra props to customize react-dropzone. */
73-
dropzoneProps?: DropzoneProps;
67+
dropzoneProps?: DropzoneOptions;
7468
/** Clear button was clicked. */
7569
onClearClick?: React.MouseEventHandler<HTMLButtonElement>;
7670
/** On data changed - if type='text' or type='dataURL' and file was loaded it will call this method */
@@ -102,7 +96,7 @@ export const FileUpload: React.FunctionComponent<FileUploadProps> = ({
10296
dropzoneProps = {},
10397
...props
10498
}: FileUploadProps) => {
105-
const onDropAccepted: DropFileEventHandler = (acceptedFiles, event) => {
99+
const onDropAccepted = (acceptedFiles: File[], event: DropEvent) => {
106100
if (acceptedFiles.length > 0) {
107101
const fileHandle = acceptedFiles[0];
108102
if (event.type === 'drop') {
@@ -125,66 +119,62 @@ export const FileUpload: React.FunctionComponent<FileUploadProps> = ({
125119
dropzoneProps.onDropAccepted && dropzoneProps.onDropAccepted(acceptedFiles, event);
126120
};
127121

128-
const onDropRejected: DropFileEventHandler = (rejectedFiles, event) => {
122+
const onDropRejected = (rejectedFiles: FileRejection[], event: DropEvent) => {
129123
dropzoneProps.onDropRejected && dropzoneProps.onDropRejected(rejectedFiles, event);
130124
};
131125

132-
const fileInputRef = React.useRef<HTMLInputElement>();
133-
const setFileValue = (filename: string) => {
134-
fileInputRef.current.value = filename;
135-
};
136-
137126
const onClearButtonClick = (event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
138127
onClearClick?.(event);
139128
setFileValue(null);
140129
};
141130

142-
return (
143-
<Dropzone multiple={false} {...dropzoneProps} onDropAccepted={onDropAccepted} onDropRejected={onDropRejected}>
144-
{({ getRootProps, getInputProps, isDragActive, open }) => {
145-
const oldInputProps = getInputProps();
146-
const inputProps: DropzoneInputProps = {
147-
...oldInputProps,
148-
onChange: async (e: React.ChangeEvent<HTMLInputElement>) => {
149-
oldInputProps.onChange?.(e);
150-
const files = await fromEvent(e.nativeEvent);
151-
if (files.length === 1) {
152-
onFileInputChange?.(e, files[0] as File);
153-
}
154-
}
155-
};
131+
const { getRootProps, getInputProps, isDragActive, open, inputRef } = useDropzone({
132+
multiple: false,
133+
...dropzoneProps,
134+
onDropAccepted,
135+
onDropRejected
136+
});
137+
138+
const setFileValue = (filename: string) => {
139+
inputRef.current.value = filename;
140+
};
141+
142+
const oldInputProps = getInputProps();
143+
const inputProps: DropzoneInputProps = {
144+
...oldInputProps,
145+
onChange: async (e: React.ChangeEvent<HTMLInputElement>) => {
146+
oldInputProps.onChange?.(e);
147+
const files = await fromEvent(e.nativeEvent);
148+
if (files.length === 1) {
149+
onFileInputChange?.(e, files[0] as File);
150+
}
151+
}
152+
};
156153

157-
return (
158-
<FileUploadField
159-
{...getRootProps({
160-
...props,
161-
refKey: 'containerRef',
162-
onClick: event => event.preventDefault()
163-
})}
164-
tabIndex={null} // Omit the unwanted tabIndex from react-dropzone's getRootProps
165-
id={id}
166-
type={type}
167-
filename={filename}
168-
value={value}
169-
isDragActive={isDragActive}
170-
onBrowseButtonClick={open}
171-
onClearButtonClick={onClearButtonClick}
172-
onTextAreaClick={onClick}
173-
onTextChange={onTextChange}
174-
>
175-
<input
176-
/* hidden, necessary for react-dropzone */
177-
{...inputProps}
178-
ref={input => {
179-
fileInputRef.current = input;
180-
(inputProps as DropzoneInputPropsWithRef).ref(input);
181-
}}
182-
/>
183-
{children}
184-
</FileUploadField>
185-
);
186-
}}
187-
</Dropzone>
154+
const rootProps = getRootProps({
155+
...props,
156+
tabIndex: null, // Omit the unwanted tabIndex from react-dropzone's getRootProps
157+
id,
158+
type,
159+
filename,
160+
value,
161+
isDragActive,
162+
onBrowseButtonClick: open,
163+
onClearButtonClick,
164+
onTextAreaClick: onClick,
165+
onTextChange,
166+
onClick,
167+
refKey: 'containerRef'
168+
});
169+
170+
return (
171+
<FileUploadField {...rootProps}>
172+
<input
173+
/* hidden, necessary for react-dropzone */
174+
{...inputProps}
175+
/>
176+
{children}
177+
</FileUploadField>
188178
);
189179
};
190180
FileUpload.displayName = 'FileUpload';

packages/react-core/src/components/FileUpload/__tests__/__snapshots__/FileUpload.test.tsx.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ exports[`simple fileupload 1`] = `
44
<DocumentFragment>
55
<div
66
class="pf-c-file-upload"
7+
role="presentation"
78
>
89
<div
910
class="pf-c-file-upload__file-select"
@@ -63,7 +64,6 @@ exports[`simple fileupload 1`] = `
6364
/>
6465
</div>
6566
<input
66-
autocomplete="off"
6767
style="display: none;"
6868
tabindex="-1"
6969
type="file"

packages/react-core/src/components/FileUpload/examples/FileUploadCustomPreview.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,17 @@ import { FileUpload } from '@patternfly/react-core';
33
import FileUploadIcon from '@patternfly/react-icons/dist/esm/icons/file-upload-icon';
44

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

9-
const handleFileInputChange = (
10-
_event: React.ChangeEvent<HTMLInputElement> | React.DragEvent<HTMLElement>,
11-
file: File
12-
) => {
9+
const handleFileInputChange = (_, file: File) => {
1310
setValue(file);
1411
setFilename(file.name);
1512
};
1613

1714
const handleClear = (_event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
1815
setFilename('');
19-
setValue('');
16+
setValue(undefined);
2017
};
2118

2219
return (

packages/react-core/src/components/FileUpload/examples/FileUploadSimpleFile.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@ import React from 'react';
22
import { FileUpload } from '@patternfly/react-core';
33

44
export const SimpleFileUpload: React.FunctionComponent = () => {
5-
const [value, setValue] = React.useState(null);
5+
const [value, setValue] = React.useState('');
66
const [filename, setFilename] = React.useState('');
77

8-
const handleFileInputChange = (
9-
_event: React.ChangeEvent<HTMLInputElement> | React.DragEvent<HTMLElement>,
10-
file: File
11-
) => {
8+
const handleFileInputChange = (_, file: File) => {
129
setFilename(file.name);
1310
};
1411

packages/react-core/src/components/FileUpload/examples/FileUploadSimpleText.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ export const SimpleTextFileUpload: React.FunctionComponent = () => {
66
const [filename, setFilename] = React.useState('');
77
const [isLoading, setIsLoading] = React.useState(false);
88

9-
const handleFileInputChange = (
10-
_event: React.ChangeEvent<HTMLInputElement> | React.DragEvent<HTMLElement>,
11-
file: File
12-
) => {
9+
const handleFileInputChange = (_, file: File) => {
1310
setFilename(file.name);
1411
};
1512

packages/react-core/src/components/FileUpload/examples/FileUploadTextWithEdits.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ export const TextFileWithEditsAllowed: React.FunctionComponent = () => {
66
const [filename, setFilename] = React.useState('');
77
const [isLoading, setIsLoading] = React.useState(false);
88

9-
const handleFileInputChange = (
10-
_event: React.ChangeEvent<HTMLInputElement> | React.DragEvent<HTMLElement>,
11-
file: File
12-
) => {
9+
const handleFileInputChange = (_, file: File) => {
1310
setFilename(file.name);
1411
};
1512

0 commit comments

Comments
 (0)