Debian Codegraph @

node/@patternfly/react-core/src/components/FileUpload/examples/FileUploadTextWithRestrictions.tsx

raw
1 import { useState } from 'react';
2 import {
3 FileUpload,
4 DropzoneErrorCode,
5 FileUploadHelperText,
6 Form,
7 FormGroup,
8 HelperText,
9 HelperTextItem,
10 DropEvent
11 } from '@patternfly/react-core';
12
13 export const TextFileUploadWithRestrictions: React.FunctionComponent = () => {
14 const [value, setValue] = useState('');
15 const [filename, setFilename] = useState('');
16 const [isLoading, setIsLoading] = useState(false);
17 const [isRejected, setIsRejected] = useState(false);
18 const [message, setMessage] = useState('Must be a CSV file no larger than 1 KB');
19
20 const handleFileInputChange = (_, file: File) => {
21 setFilename(file.name);
22 };
23
24 const handleTextChange = (_event: React.ChangeEvent<HTMLTextAreaElement>, value: string) => {
25 setValue(value);
26 };
27
28 const handleDataChange = (_event: DropEvent, value: string) => {
29 setValue(value);
30 };
31
32 const reset = () => {
33 setFilename('');
34 setValue('');
35 };
36
37 const handleClear = (_event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
38 reset();
39 setIsRejected(false);
40 };
41
42 const handleFileRejected = () => {
43 reset();
44 setIsRejected(true);
45 };
46
47 const handleFileAccepted = () => {
48 setIsRejected(false);
49 };
50
51 const handleFileReadStarted = (_event: DropEvent, _fileHandle: File) => {
52 setIsLoading(true);
53 };
54
55 const handleFileReadFinished = (_event: DropEvent, _fileHandle: File) => {
56 setIsLoading(false);
57 };
58
59 return (
60 <Form>
61 <FormGroup fieldId="text-file-with-restrictions-example">
62 <FileUpload
63 id="text-file-with-restrictions-example"
64 type="text"
65 value={value}
66 filename={filename}
67 filenamePlaceholder="Drag and drop a file or upload one"
68 onFileInputChange={handleFileInputChange}
69 onDataChange={handleDataChange}
70 onTextChange={handleTextChange}
71 onReadStarted={handleFileReadStarted}
72 onReadFinished={handleFileReadFinished}
73 onClearClick={handleClear}
74 isLoading={isLoading}
75 dropzoneProps={{
76 accept: { 'text/csv': ['.csv'] },
77 maxSize: 1024,
78 onDropRejected: (rejections) => {
79 const error = rejections[0].errors[0];
80 if (error.code === DropzoneErrorCode.FileTooLarge) {
81 setMessage('File is too big');
82 } else if (error.code === DropzoneErrorCode.FileInvalidType) {
83 setMessage('File is not a CSV file');
84 }
85 handleFileRejected();
86 },
87 onDropAccepted: handleFileAccepted
88 }}
89 validated={isRejected ? 'error' : 'default'}
90 browseButtonText="Upload"
91 browseButtonAriaDescribedby="restricted-file-example-helpText"
92 >
93 <FileUploadHelperText>
94 <HelperText isLiveRegion>
95 <HelperTextItem id="restricted-file-example-helpText" variant={isRejected ? 'error' : 'default'}>
96 {isRejected ? message : 'Upload a CSV file'}
97 </HelperTextItem>
98 </HelperText>
99 </FileUploadHelperText>
100 </FileUpload>
101 </FormGroup>
102 </Form>
103 );
104 };