Skip to content

Commit 469ea9d

Browse files
authored
feat(FileUpload): expose dropdown error types (#11289)
* feat(FileUpload): expose dropdown error types * updated due to dropzone changes and fixed legacy bug in example
1 parent 22ff75e commit 469ea9d

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from 'react';
2-
import { DropzoneInputProps, DropzoneOptions, FileRejection, useDropzone } from 'react-dropzone';
2+
import { DropzoneInputProps, DropzoneOptions, FileRejection, useDropzone, ErrorCode } from 'react-dropzone';
33
import { FileUploadField, FileUploadFieldProps } from './FileUploadField';
44
import { readFile, fileReaderType } from '../../helpers/fileUtils';
55
import { DropEvent } from '../../helpers/typeUtils';
@@ -84,6 +84,8 @@ export interface FileUploadProps
8484
onTextChange?: (event: React.ChangeEvent<HTMLTextAreaElement>, text: string) => void;
8585
}
8686

87+
export { ErrorCode as DropzoneErrorCode }; // FileInvalidType, FileTooLarge, FileTooSmall, TooManyFiles
88+
8789
export const FileUpload: React.FunctionComponent<FileUploadProps> = ({
8890
id,
8991
type,

packages/react-core/src/components/FileUpload/examples/FileUpload.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Typing/pasting text in the box will call `onTextChange` with a string, and a str
4040

4141
### Restricting file size and type
4242

43-
Any [props accepted by `react-dropzone`'s `Dropzone` component](https://react-dropzone.js.org/#!/Dropzone) can be passed as a `dropzoneProps` object in order to customize the behavior of the Dropzone, such as restricting the size and type of files allowed. The following example will only accept CSV files smaller than 1 KB. Note that file type determination is not reliable across platforms (see the note on react-dropzone's docs about the `accept` prop), so be sure to test the behavior of your file upload restriction on all browsers and operating systems targeted by your application.
43+
Any [props accepted by `react-dropzone`'s `Dropzone` component](https://react-dropzone.js.org/#!/Dropzone) can be passed as a `dropzoneProps` object in order to customize the behavior of the Dropzone, such as restricting the size and type of files allowed. You can also capture and act upon native `react-dropzone` errors using the exposed `DropzoneErrorCode` enum. The following example will only accept CSV files smaller than 1 KB. Note that file type determination is not reliable across platforms (see the note on react-dropzone's docs about the `accept` prop), so be sure to test the behavior of your file upload restriction on all browsers and operating systems targeted by your application.
4444

4545
#### IMPORTANT: A note about security
4646

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

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react';
22
import {
33
FileUpload,
4+
DropzoneErrorCode,
45
FileUploadHelperText,
56
Form,
67
FormGroup,
@@ -9,13 +10,13 @@ import {
910
DropEvent,
1011
Icon
1112
} from '@patternfly/react-core';
12-
import ExclamationCircleIcon from '@patternfly/react-icons/dist/esm/icons/exclamation-circle-icon';
1313

1414
export const TextFileUploadWithRestrictions: React.FunctionComponent = () => {
1515
const [value, setValue] = React.useState('');
1616
const [filename, setFilename] = React.useState('');
1717
const [isLoading, setIsLoading] = React.useState(false);
1818
const [isRejected, setIsRejected] = React.useState(false);
19+
const [message, setMessage] = React.useState('Must be a CSV file no larger than 1 KB');
1920

2021
const handleFileInputChange = (_, file: File) => {
2122
setFilename(file.name);
@@ -29,16 +30,25 @@ export const TextFileUploadWithRestrictions: React.FunctionComponent = () => {
2930
setValue(value);
3031
};
3132

32-
const handleClear = (_event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
33+
const reset = () => {
3334
setFilename('');
3435
setValue('');
36+
};
37+
38+
const handleClear = (_event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
39+
reset();
3540
setIsRejected(false);
3641
};
3742

3843
const handleFileRejected = () => {
44+
reset();
3945
setIsRejected(true);
4046
};
4147

48+
const handleFileAccepted = () => {
49+
setIsRejected(false);
50+
};
51+
4252
const handleFileReadStarted = (_event: DropEvent, _fileHandle: File) => {
4353
setIsLoading(true);
4454
};
@@ -66,7 +76,16 @@ export const TextFileUploadWithRestrictions: React.FunctionComponent = () => {
6676
dropzoneProps={{
6777
accept: { 'text/csv': ['.csv'] },
6878
maxSize: 1024,
69-
onDropRejected: handleFileRejected
79+
onDropRejected: (rejections) => {
80+
const error = rejections[0].errors[0];
81+
if (error.code === DropzoneErrorCode.FileTooLarge) {
82+
setMessage('File is too big');
83+
} else if (error.code === DropzoneErrorCode.FileInvalidType) {
84+
setMessage('File is not a CSV file');
85+
}
86+
handleFileRejected();
87+
},
88+
onDropAccepted: handleFileAccepted
7089
}}
7190
validated={isRejected ? 'error' : 'default'}
7291
browseButtonText="Upload"
@@ -77,10 +96,8 @@ export const TextFileUploadWithRestrictions: React.FunctionComponent = () => {
7796
<HelperTextItem id="restricted-file-example-helpText" variant={isRejected ? 'error' : 'default'}>
7897
{isRejected ? (
7998
<>
80-
<Icon status="danger">
81-
<ExclamationCircleIcon />
82-
</Icon>
83-
Must be a CSV file no larger than 1 KB
99+
<Icon status="danger" />
100+
{message}
84101
</>
85102
) : (
86103
'Upload a CSV file'

0 commit comments

Comments
 (0)