Examples
The FileUpload
component can accept a file via browse or drag-and-drop, and behaves like a standard form field with its value
and onFileInputChange
event that is similar to <input onChange="...">
prop. The type
prop determines how the FileUpload
component behaves upon accepting a file, what type of value it passes to its onDataChange
event.
Text files
If type="text"
is passed (and hideDefaultPreview
is not), a TextArea
preview will be rendered underneath the filename bar. When a file is selected, its contents will be read into memory and passed to the onDataChange
event as a string. Every filename change is passed to onFileInputChange
same as it would do with the <input>
element.
Pressing Clear button triggers onClearClick
event.
With helper text
You can pass in the <FileUploadHelperText
sub-component via the children
property to <FileUpload>
.
Text file with edits allowed
A user can always type instead of selecting a file, but by default, once a user selects a text file from their disk they are not allowed to edit it (to prevent unintended changes to a format-sensitive file). This behavior can be changed with the allowEditingUploadedText
prop.
Typing/pasting text in the box will call onTextChange
with a string, and a string value is expected for the value
prop. :
Restricting file size and type
Any props accepted by react-dropzone
's Dropzone
component 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.
IMPORTANT: A note about security
Restricting file sizes and types in this way is for user convenience only, and it cannot prevent a malicious user from submitting anything to your server. As with any user input, your application should also validate, sanitize and/or reject restricted files on the server side.
Other file types
If no type
prop is specified, the component will not read files directly. When a file is selected, a File
object will be passed as a second argument to onFileInputChange
and your application will be responsible for reading from it (e.g. by using the FileReader API or attaching it to a FormData object). A File
object will also be expected for the value
prop instead of a string, and no preview of the file contents will be shown by default. The onReadStarted
and onReadFinished
callbacks will also not be called since the component is not reading the file.
Customizing the file preview
Regardless of type
, the preview area (the TextArea, or any future implementations of default previews for other types) can be removed by passing hideDefaultPreview
, and a custom one can be rendered by passing children
.
Bringing your own file browse logic
FileUpload
is a thin wrapper around the FileUploadField
presentational component. If you need to implement your own logic for accepting files, you can instead render a FileUploadField
directly, which does not include react-dropzone
and requires additional props (e.g. onBrowseButtonClick
, onClearButtonClick
, isDragActive
).
Note that the isLoading
prop is styled to position the spinner dead center above the entire component, so it should not be used with hideDefaultPreview
unless a custom empty-state preview is provided via children
. The below example prevents isLoading
and hideDefaultPreview
from being used at the same time. You can always provide your own spinner as part of the children
!
Types
File upload uses the DropzoneOptions
type from react-dropzone. It is comprised of additional props with their own types. For more information on using DropzoneOptions
visit react-dropzone props and methods.
Additionally, it calls the onDataChange
, onReadFailed
, onReadFinished
, and onReadStarted
callbacks with an event of type DropEvent
. DropEvent
is a union comprised of the following types:
React.DragEvent<HTMLElement>
| React.ChangeEvent<HTMLInputElement>
| DragEvent
| Event
Props
FileUpload
Name | Type | Default | Description |
---|---|---|---|
idrequired | string | Unique id for the text area. Also used to generate ids for accessible labels. | |
allowEditingUploadedText | boolean | Flag to allow editing of a text file's contents after it is selected from disk. | |
aria-label | string | Aria-label for the text area. | |
browseButtonAriaDescribedby | string | ID or ID's of elements that describe the browse button. Typically this should refer to elements such as helper text when there are file restrictions. | |
browseButtonText | string | Text for the browse button. | |
children | React.ReactNode | null | Additional children to render after (or instead of) the file preview. |
className | string | Additional classes added to the file upload container element. | |
clearButtonText | string | Text for the clear button. | |
dropzoneProps | Partial<DropzoneOptions> | {} | Optional extra props to customize react-dropzone. |
filename | string | '' | Value to be shown in the read-only filename field. |
filenameAriaLabel | string | Aria-label for the read-only filename field. | |
filenamePlaceholder | string | Placeholder string to display in the empty filename field. | |
hideDefaultPreview | boolean | Flag to hide the built-in preview of the file (where available). If true, you can use the children property to render an alternate preview. | |
isDisabled | boolean | Flag to show if the field is disabled. | |
isLoading | boolean | Flag to show if a file is being loaded. | |
isReadOnly | boolean | Flag to show if the field is read only. | |
isRequired | boolean | Flag to show if the field is required. | |
onClearClick | React.MouseEventHandler<HTMLButtonElement> | Clear button was clicked. | |
onClick | (event: React.MouseEvent) => void | (event) => event.preventDefault() | Callback for clicking on the file upload field text area. By default, prevents a click in the text area from opening file dialog. |
onDataChange | (event: DropEvent, data: string) => void | On data changed - if type='text' or type='dataURL' and file was loaded it will call this method | |
onFileInputChange | (event: DropEvent, file: File) => void | null | Change event emitted from the hidden \<input type="file" \> field associated with the component |
onReadFailed | (event: DropEvent, error: DOMException, fileHandle: File) => void | () => {} | A callback for when the FileReader API fails. |
onReadFinished | (event: DropEvent, fileHandle: File) => void | () => {} | A callback for when a selected file finishes loading. |
onReadStarted | (event: DropEvent, fileHandle: File) => void | () => {} | A callback for when a selected file starts loading. |
onTextChange | (event: React.ChangeEvent<HTMLTextAreaElement>, text: string) => void | Text area text changed. | |
spinnerAriaValueText | string | Aria-valuetext for the loading spinner. | |
type | 'text' | 'dataURL' | What type of file. Determines whether 'onDataChange` is called and what is expected by the value property (a string for 'text' and 'dataURL', or a File object otherwise. | |
validated | 'success' | 'error' | 'default' | Value to indicate if the field is modified to show that validation state. If set to success, field will be modified to indicate valid state. If set to error, field will be modified to indicate error state. | |
value | string | File | type === fileReaderType.text || type === fileReaderType.dataURL ? '' : null | Value of the file's contents (string if text file, File object otherwise). |
FileUploadField
Name | Type | Default | Description |
---|---|---|---|
idrequired | string | Unique id for the text area. Also used to generate ids for accessible labels. | |
allowEditingUploadedText | boolean | false | Flag to allow editing of a text file's contents after it is selected from disk. |
aria-label | string | 'File upload' | Aria-label for the text area. |
browseButtonAriaDescribedby | string | ID or ID's of elements that describe the browse button. Typically this should refer to elements such as helper text when there are file restrictions. | |
browseButtonText | string | 'Browse...' | Text for the browse button. |
children | React.ReactNode | null | Additional children to render after (or instead of) the file preview. |
className | string | '' | Additional classes added to the file upload field container element. |
clearButtonText | string | 'Clear' | Text for the clear button. |
containerRef | React.Ref<HTMLDivElement> | null | A reference object to attach to the file upload field container element. |
filename | string | '' | Value to be shown in the read-only filename field. |
filenameAriaLabel | string | filename ? 'Read only filename' : filenamePlaceholder | Aria-label for the read-only filename field. |
filenamePlaceholder | string | 'Drag a file here or browse to upload' | Placeholder string to display in the empty filename field. |
hideDefaultPreview | boolean | false | Flag to hide the built-in preview of the file (where available). If true, you can use the children property to render an alternate preview. |
isClearButtonDisabled | boolean | !filename && !value | Flag to disable the clear button. |
isDisabled | boolean | false | Flag to show if the field is disabled. |
isDragActive | boolean | false | Flag to show if a file is being dragged over the file upload field. |
isLoading | boolean | false | Flag to show if a file is being loaded. |
isReadOnly | boolean | false | Flag to show if the field is read only. |
isRequired | boolean | false | Flag to show if the field is required. |
name | string | Name property for the text input. | |
onBrowseButtonClick | (event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => void | () => {} | A callback for when the browse button is clicked. |
onClearButtonClick | (event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => void | () => {} | A callback for when the clear button is clicked. |
onTextAreaBlur | (event?: any) => void | Callback for when focus is lost on the text area field. | |
onTextAreaClick | (event: React.MouseEvent<HTMLTextAreaElement, MouseEvent>) => void | A callback for when the text area is clicked. Can also be set via the onClick property of the file upload component. | |
onTextChange | (event: React.ChangeEvent<HTMLTextAreaElement>, text: string) => void | Text area text changed. | |
spinnerAriaValueText | string | Aria-valuetext for the loading spinner. | |
textAreaPlaceholder | string | '' | Placeholder string to display in the empty text area field. |
type | 'text' | 'dataURL' | What type of file. Determines what is is expected by the value property (a string for 'text' and 'dataURL', or a File object otherwise). | |
validated | 'success' | 'error' | 'default' | 'default' | Value to indicate if the field is modified to show that validation state. If set to success, field will be modified to indicate valid state. If set to error, field will be modified to indicate error state. |
value | string | File | '' | Value of the file's contents (string if text file, File object otherwise). |
CSS variables
Expand or collapse column | Selector | Variable | Value | |
---|---|---|---|---|
.pf-v6-c-file-upload | --pf-v6-c-file-upload--RowGap | 0.5rem | ||
| ||||
.pf-v6-c-file-upload | --pf-v6-c-file-upload--PaddingBlockStart | 0.5rem | ||
| ||||
.pf-v6-c-file-upload | --pf-v6-c-file-upload--PaddingBlockEnd | 0.5rem | ||
| ||||
.pf-v6-c-file-upload | --pf-v6-c-file-upload--PaddingInlineStart | 0.5rem | ||
| ||||
.pf-v6-c-file-upload | --pf-v6-c-file-upload--PaddingInlineEnd | 0.5rem | ||
| ||||
.pf-v6-c-file-upload | --pf-v6-c-file-upload--BorderRadius | 6px | ||
| ||||
.pf-v6-c-file-upload | --pf-v6-c-file-upload--BorderWidth | 1px | ||
| ||||
.pf-v6-c-file-upload | --pf-v6-c-file-upload--BorderColor | transparent | ||
.pf-v6-c-file-upload | --pf-v6-c-file-upload--BorderStyle | solid | ||
.pf-v6-c-file-upload | --pf-v6-c-file-upload--m-drag-hover--BorderColor | (In light theme) #0066cc | ||
| ||||
.pf-v6-c-file-upload | --pf-v6-c-file-upload--m-drag-hover--BorderStyle | dashed | ||
.pf-v6-c-file-upload | --pf-v6-c-file-upload__file-select__c-button--m-control--OutlineOffset | calc(-1 * 0.25rem) | ||
| ||||
.pf-v6-c-file-upload | --pf-v6-c-file-upload__file-details__c-form-control--MinHeight | calc(4rem * 2) | ||
| ||||
.pf-v6-c-file-upload.pf-m-drag-hover | --pf-v6-c-file-upload--BorderColor | (In light theme) #0066cc | ||
| ||||
.pf-v6-c-file-upload.pf-m-drag-hover | --pf-v6-c-file-upload--BorderStyle | dashed | ||
|