forked from validatedpatterns/patterns-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileField.tsx
More file actions
80 lines (74 loc) · 2.22 KB
/
Copy pathFileField.tsx
File metadata and controls
80 lines (74 loc) · 2.22 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
import * as React from 'react';
import { useTranslation } from 'react-i18next';
import {
FileUpload,
FileUploadHelperText,
HelperText,
HelperTextItem,
} from '@patternfly/react-core';
interface FileFieldProps {
field: {
name: string;
description?: string;
path?: string;
base64?: boolean;
};
value: string | null;
onChange: (value: string | null) => void;
/** Shown under the control; also sets FileUpload `validated` to error when set. */
fieldError?: string;
}
export const FileField: React.FC<FileFieldProps> = ({ field, value, onChange, fieldError }) => {
const { t } = useTranslation('plugin__patterns-operator-console-plugin');
const [filename, setFilename] = React.useState('');
const handleFileInputChange = (_, file: File) => {
setFilename(file.name);
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = () => {
const base64filecontent = reader.result.toString().split(',')[1]; // Remove data:type;base64, prefix
onChange(base64filecontent);
};
};
const handleClear = () => {
setFilename('');
onChange(null);
};
return (
<>
<FileUpload
id={`file-${field.name}`}
value={value}
filename={filename}
type="text"
isRequired={true}
hideDefaultPreview
validated={fieldError ? 'error' : 'default'}
filenamePlaceholder={t('Drag and drop a file or upload one')}
onFileInputChange={handleFileInputChange}
onClearClick={handleClear}
dropzoneProps={{
accept: {
'text/*': ['.txt', '.pem', '.crt', '.key', '.ini', '.conf', '.config'],
'application/*': ['.json', '.yaml', '.yml'],
},
}}
>
<FileUploadHelperText>
<HelperText>
{fieldError && (
<HelperTextItem variant="error" id={`file-${field.name}-error`}>
{fieldError}
</HelperTextItem>
)}
{field.description && (
<HelperTextItem id={`file-${field.name}-helpertext`}>
{field.description}
</HelperTextItem>
)}
</HelperText>
</FileUploadHelperText>
</FileUpload>
</>
);
};