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