| 1 |
import { DropzoneInputProps, DropzoneOptions, FileRejection, useDropzone, ErrorCode } from 'react-dropzone'; |
| 2 |
import { FileUploadField, FileUploadFieldProps } from './FileUploadField'; |
| 3 |
import { readFile, fileReaderType } from '../../helpers/fileUtils'; |
| 4 |
import { DropEvent } from '../../helpers/typeUtils'; |
| 5 |
import { fromEvent } from 'file-selector'; |
| 6 |
|
| 7 |
export interface FileUploadProps extends Omit< |
| 8 |
FileUploadFieldProps, |
| 9 |
'children' | 'onBrowseButtonClick' | 'onClearButtonClick' | 'isDragActive' | 'containerRef' |
| 10 |
> { |
| 11 |
/** Flag to allow editing of a text file's contents after it is selected from disk. */ |
| 12 |
allowEditingUploadedText?: boolean; |
| 13 |
/** Aria-label for the text area. */ |
| 14 |
'aria-label'?: string; |
| 15 |
/** Text for the browse button. */ |
| 16 |
browseButtonText?: string; |
| 17 |
/** ID or ID's of elements that describe the browse button. Typically this should refer |
| 18 |
* to elements such as helper text when there are file restrictions. |
| 19 |
*/ |
| 20 |
browseButtonAriaDescribedby?: string; |
| 21 |
/** Additional children to render after (or instead of) the file preview. */ |
| 22 |
children?: React.ReactNode; |
| 23 |
/** Additional classes added to the file upload container element. */ |
| 24 |
className?: string; |
| 25 |
/** Text for the clear button. */ |
| 26 |
clearButtonText?: string; |
| 27 |
/** Value to be shown in the read-only filename field. */ |
| 28 |
filename?: string; |
| 29 |
/** Aria-label for the read-only filename field. */ |
| 30 |
filenameAriaLabel?: string; |
| 31 |
/** Placeholder string to display in the empty filename field. */ |
| 32 |
filenamePlaceholder?: string; |
| 33 |
/** Flag to hide the built-in preview of the file (where available). If true, you can use |
| 34 |
* the children property to render an alternate preview. |
| 35 |
*/ |
| 36 |
hideDefaultPreview?: boolean; |
| 37 |
/** Unique id for the text area. Also used to generate ids for accessible labels. */ |
| 38 |
id: string; |
| 39 |
/** Flag to show if the field is disabled. */ |
| 40 |
isDisabled?: boolean; |
| 41 |
/** Flag to show if a file is being loaded. */ |
| 42 |
isLoading?: boolean; |
| 43 |
/** Flag to show if the field is read only. */ |
| 44 |
isReadOnly?: boolean; |
| 45 |
/** Flag to show if the field is required. */ |
| 46 |
isRequired?: boolean; |
| 47 |
/** Callback for clicking on the file upload field text area. By default, prevents a click |
| 48 |
* in the text area from opening file dialog. |
| 49 |
*/ |
| 50 |
onClick?: (event: React.MouseEvent) => void; |
| 51 |
/** Change event emitted from the hidden \<input type="file" \> field associated with the component */ |
| 52 |
onFileInputChange?: (event: DropEvent, file: File) => void; |
| 53 |
/** Aria-valuetext for the loading spinner. */ |
| 54 |
spinnerAriaValueText?: string; |
| 55 |
/** What type of file. Determines whether 'onDataChange` is called and what is |
| 56 |
* expected by the value property (a string for 'text' and 'dataURL', or a File object otherwise. |
| 57 |
*/ |
| 58 |
type?: 'text' | 'dataURL'; |
| 59 |
/** Value to indicate if the field is modified to show that validation state. |
| 60 |
* If set to success, field will be modified to indicate valid state. |
| 61 |
* If set to error, field will be modified to indicate error state. |
| 62 |
*/ |
| 63 |
validated?: 'success' | 'error' | 'default'; |
| 64 |
/** Value of the file's contents (string if text file, File object otherwise). */ |
| 65 |
value?: string | File; |
| 66 |
|
| 67 |
// Props available in FileUpload but not FileUploadField: |
| 68 |
|
| 69 |
/** Optional extra props to customize react-dropzone. */ |
| 70 |
dropzoneProps?: Partial<DropzoneOptions>; |
| 71 |
/** Clear button was clicked. */ |
| 72 |
onClearClick?: React.MouseEventHandler<HTMLButtonElement>; |
| 73 |
/** On data changed - if type='text' or type='dataURL' and file was loaded it will call this method */ |
| 74 |
onDataChange?: (event: DropEvent, data: string) => void; |
| 75 |
/** A callback for when the FileReader API fails. */ |
| 76 |
onReadFailed?: (event: DropEvent, error: DOMException, fileHandle: File) => void; |
| 77 |
/** A callback for when a selected file finishes loading. */ |
| 78 |
onReadFinished?: (event: DropEvent, fileHandle: File) => void; |
| 79 |
/** A callback for when a selected file starts loading. */ |
| 80 |
onReadStarted?: (event: DropEvent, fileHandle: File) => void; |
| 81 |
/** Text area text changed. */ |
| 82 |
onTextChange?: (event: React.ChangeEvent<HTMLTextAreaElement>, text: string) => void; |
| 83 |
} |
| 84 |
|
| 85 |
export { ErrorCode as DropzoneErrorCode }; // FileInvalidType, FileTooLarge, FileTooSmall, TooManyFiles |
| 86 |
|
| 87 |
export const FileUpload: React.FunctionComponent<FileUploadProps> = ({ |
| 88 |
id, |
| 89 |
type, |
| 90 |
value = type === fileReaderType.text || type === fileReaderType.dataURL ? '' : null, |
| 91 |
filename = '', |
| 92 |
children = null, |
| 93 |
onFileInputChange = null, |
| 94 |
onReadStarted = () => {}, |
| 95 |
onReadFinished = () => {}, |
| 96 |
onReadFailed = () => {}, |
| 97 |
onClearClick, |
| 98 |
onClick = (event) => event.preventDefault(), |
| 99 |
onTextChange, |
| 100 |
onDataChange, |
| 101 |
dropzoneProps = {}, |
| 102 |
...props |
| 103 |
}: FileUploadProps) => { |
| 104 |
const onDropAccepted = (acceptedFiles: File[], event: DropEvent) => { |
| 105 |
if (acceptedFiles.length > 0) { |
| 106 |
const fileHandle = acceptedFiles[0]; |
| 107 |
onFileInputChange?.(event, fileHandle); |
| 108 |
|
| 109 |
if (type === fileReaderType.text || type === fileReaderType.dataURL) { |
| 110 |
onReadStarted(event, fileHandle); |
| 111 |
readFile(fileHandle, type as fileReaderType) |
| 112 |
.then((data) => { |
| 113 |
onReadFinished(event, fileHandle); |
| 114 |
onDataChange?.(event, data as string); |
| 115 |
}) |
| 116 |
.catch((error: DOMException) => { |
| 117 |
onReadFailed(event, error, fileHandle); |
| 118 |
onReadFinished(event, fileHandle); |
| 119 |
onDataChange?.(event, ''); |
| 120 |
}); |
| 121 |
} |
| 122 |
} |
| 123 |
dropzoneProps.onDropAccepted && dropzoneProps.onDropAccepted(acceptedFiles, event); |
| 124 |
}; |
| 125 |
|
| 126 |
const onDropRejected = (rejectedFiles: FileRejection[], event: DropEvent) => { |
| 127 |
dropzoneProps.onDropRejected && dropzoneProps.onDropRejected(rejectedFiles, event); |
| 128 |
}; |
| 129 |
|
| 130 |
const onClearButtonClick = (event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => { |
| 131 |
onClearClick?.(event); |
| 132 |
setFileValue(null); |
| 133 |
}; |
| 134 |
|
| 135 |
const { getRootProps, getInputProps, isDragActive, open, inputRef } = useDropzone({ |
| 136 |
noClick: true, |
| 137 |
multiple: false, |
| 138 |
...dropzoneProps, |
| 139 |
onDropAccepted, |
| 140 |
onDropRejected |
| 141 |
}); |
| 142 |
|
| 143 |
const setFileValue = (filename: string) => { |
| 144 |
inputRef.current.value = filename; |
| 145 |
}; |
| 146 |
|
| 147 |
const oldInputProps = getInputProps(); |
| 148 |
const inputProps: DropzoneInputProps = { |
| 149 |
...oldInputProps, |
| 150 |
onChange: async (e: React.ChangeEvent<HTMLInputElement>) => { |
| 151 |
oldInputProps.onChange?.(e); |
| 152 |
const files = await fromEvent(e.nativeEvent); |
| 153 |
if (files.length === 1) { |
| 154 |
onFileInputChange?.(e, files[0] as File); |
| 155 |
} |
| 156 |
} |
| 157 |
}; |
| 158 |
|
| 159 |
const rootProps = getRootProps({ |
| 160 |
...props, |
| 161 |
tabIndex: null, // Omit the unwanted tabIndex from react-dropzone's getRootProps |
| 162 |
id, |
| 163 |
type, |
| 164 |
filename, |
| 165 |
value, |
| 166 |
isDragActive, |
| 167 |
onBrowseButtonClick: open, |
| 168 |
onClearButtonClick, |
| 169 |
onTextAreaClick: onClick, |
| 170 |
onTextChange, |
| 171 |
onClick, |
| 172 |
refKey: 'containerRef' |
| 173 |
}); |
| 174 |
|
| 175 |
return ( |
| 176 |
<FileUploadField {...rootProps}> |
| 177 |
<input |
| 178 |
/* hidden, necessary for react-dropzone */ |
| 179 |
{...inputProps} |
| 180 |
hidden |
| 181 |
/> |
| 182 |
{children} |
| 183 |
</FileUploadField> |
| 184 |
); |
| 185 |
}; |
| 186 |
FileUpload.displayName = 'FileUpload'; |