@patternfly/react-core 4.169.4 → 4.170.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +11 -0
- package/dist/esm/components/FileUpload/FileUpload.d.ts +11 -3
- package/dist/esm/components/FileUpload/FileUpload.d.ts.map +1 -1
- package/dist/esm/components/FileUpload/FileUpload.js +28 -5
- package/dist/esm/components/FileUpload/FileUpload.js.map +1 -1
- package/dist/esm/components/FileUpload/FileUploadField.d.ts +2 -0
- package/dist/esm/components/FileUpload/FileUploadField.d.ts.map +1 -1
- package/dist/esm/components/FileUpload/FileUploadField.js +2 -1
- package/dist/esm/components/FileUpload/FileUploadField.js.map +1 -1
- package/dist/js/components/FileUpload/FileUpload.d.ts +11 -3
- package/dist/js/components/FileUpload/FileUpload.d.ts.map +1 -1
- package/dist/js/components/FileUpload/FileUpload.js +27 -4
- package/dist/js/components/FileUpload/FileUpload.js.map +1 -1
- package/dist/js/components/FileUpload/FileUploadField.d.ts +2 -0
- package/dist/js/components/FileUpload/FileUploadField.d.ts.map +1 -1
- package/dist/js/components/FileUpload/FileUploadField.js +2 -1
- package/dist/js/components/FileUpload/FileUploadField.js.map +1 -1
- package/dist/umd/react-core.css +12 -12
- package/dist/umd/react-core.js +1567 -1293
- package/dist/umd/react-core.min.js +5 -6
- package/package.json +6 -6
- package/src/components/FileUpload/FileUpload.tsx +77 -30
- package/src/components/FileUpload/FileUploadField.tsx +5 -0
- package/src/components/FileUpload/__tests__/__snapshots__/FileUpload.test.tsx.snap +3 -7
- package/src/components/FileUpload/examples/FileUpload.md +54 -17
- package/src/components/Modal/examples/Modal.md +203 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@patternfly/react-core",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.170.0",
|
|
4
4
|
"description": "This library provides a set of common React components for use with the PatternFly reference implementation.",
|
|
5
5
|
"main": "dist/js/index.js",
|
|
6
6
|
"module": "dist/esm/index.js",
|
|
@@ -34,11 +34,11 @@
|
|
|
34
34
|
"generate": "node scripts/copyStyles.js"
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"@patternfly/react-icons": "^4.
|
|
38
|
-
"@patternfly/react-styles": "^4.
|
|
39
|
-
"@patternfly/react-tokens": "^4.
|
|
37
|
+
"@patternfly/react-icons": "^4.21.0",
|
|
38
|
+
"@patternfly/react-styles": "^4.20.0",
|
|
39
|
+
"@patternfly/react-tokens": "^4.22.0",
|
|
40
40
|
"focus-trap": "6.2.2",
|
|
41
|
-
"react-dropzone": "
|
|
41
|
+
"react-dropzone": "10.2.2",
|
|
42
42
|
"tippy.js": "5.1.2",
|
|
43
43
|
"tslib": "^2.0.0"
|
|
44
44
|
},
|
|
@@ -60,5 +60,5 @@
|
|
|
60
60
|
"react": "^16.8.0 || ^17.0.0",
|
|
61
61
|
"react-dom": "^16.8.0 || ^17.0.0"
|
|
62
62
|
},
|
|
63
|
-
"gitHead": "
|
|
63
|
+
"gitHead": "320fde1581d401ba386289ac2b6893d567a079b8"
|
|
64
64
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
|
-
import Dropzone, { DropzoneProps,
|
|
2
|
+
import Dropzone, { DropzoneProps, DropEvent, DropzoneInputProps } from 'react-dropzone';
|
|
3
3
|
import { FileUploadField, FileUploadFieldProps } from './FileUploadField';
|
|
4
4
|
import { readFile, fileReaderType } from '../../helpers/fileUtils';
|
|
5
|
-
|
|
5
|
+
import { fromEvent } from 'file-selector';
|
|
6
6
|
export interface FileUploadProps
|
|
7
7
|
extends Omit<
|
|
8
8
|
FileUploadFieldProps,
|
|
@@ -18,15 +18,19 @@ export interface FileUploadProps
|
|
|
18
18
|
value?: string | File;
|
|
19
19
|
/** Value to be shown in the read-only filename field. */
|
|
20
20
|
filename?: string;
|
|
21
|
-
/** A callback for when the file contents change. */
|
|
21
|
+
/** *(deprecated)* A callback for when the file contents change. Please instead use onFileInputChange, onTextChange, onDataChange, onClearClick individually. */
|
|
22
22
|
onChange?: (
|
|
23
23
|
value: string | File,
|
|
24
24
|
filename: string,
|
|
25
25
|
event:
|
|
26
|
+
| React.MouseEvent<HTMLButtonElement, MouseEvent> // Clear button was clicked
|
|
26
27
|
| React.DragEvent<HTMLElement> // User dragged/dropped a file
|
|
27
|
-
| React.ChangeEvent<
|
|
28
|
-
|
|
|
28
|
+
| React.ChangeEvent<HTMLElement> // User typed in the TextArea
|
|
29
|
+
| DragEvent
|
|
30
|
+
| Event
|
|
29
31
|
) => void;
|
|
32
|
+
/** Change event emitted from the hidden \<input type="file" \> field associated with the component */
|
|
33
|
+
onFileInputChange?: (event: React.ChangeEvent<HTMLInputElement> | DropEvent, file: File) => void;
|
|
30
34
|
/** Callback for clicking on the FileUploadField text area. By default, prevents a click in the text area from opening file dialog. */
|
|
31
35
|
onClick?: (event: React.MouseEvent) => void;
|
|
32
36
|
/** Additional classes added to the FileUpload container element. */
|
|
@@ -74,6 +78,12 @@ export interface FileUploadProps
|
|
|
74
78
|
onReadFailed?: (error: DOMException, fileHandle: File) => void;
|
|
75
79
|
/** Optional extra props to customize react-dropzone. */
|
|
76
80
|
dropzoneProps?: DropzoneProps;
|
|
81
|
+
/** Clear button was clicked */
|
|
82
|
+
onClearClick?: React.MouseEventHandler<HTMLButtonElement>;
|
|
83
|
+
/** Text area text changed */
|
|
84
|
+
onTextChange?: (text: string) => void;
|
|
85
|
+
/** On data changed - if type='text' or type='dataURL' and file was loaded it will call this method */
|
|
86
|
+
onDataChange?: (data: string) => void;
|
|
77
87
|
}
|
|
78
88
|
|
|
79
89
|
export const FileUpload: React.FunctionComponent<FileUploadProps> = ({
|
|
@@ -83,16 +93,23 @@ export const FileUpload: React.FunctionComponent<FileUploadProps> = ({
|
|
|
83
93
|
filename = '',
|
|
84
94
|
children = null,
|
|
85
95
|
onChange = () => {},
|
|
96
|
+
onFileInputChange = null,
|
|
86
97
|
onReadStarted = () => {},
|
|
87
98
|
onReadFinished = () => {},
|
|
88
99
|
onReadFailed = () => {},
|
|
100
|
+
onClearClick,
|
|
89
101
|
onClick = event => event.preventDefault(),
|
|
102
|
+
onTextChange,
|
|
103
|
+
onDataChange,
|
|
90
104
|
dropzoneProps = {},
|
|
91
105
|
...props
|
|
92
106
|
}: FileUploadProps) => {
|
|
93
|
-
const onDropAccepted
|
|
107
|
+
const onDropAccepted = (acceptedFiles: File[], event: DropEvent) => {
|
|
94
108
|
if (acceptedFiles.length > 0) {
|
|
95
109
|
const fileHandle = acceptedFiles[0];
|
|
110
|
+
if (event.type === 'drop') {
|
|
111
|
+
onFileInputChange?.(event, fileHandle);
|
|
112
|
+
}
|
|
96
113
|
if (type === fileReaderType.text || type === fileReaderType.dataURL) {
|
|
97
114
|
onChange('', fileHandle.name, event); // Show the filename while reading
|
|
98
115
|
onReadStarted(fileHandle);
|
|
@@ -100,11 +117,13 @@ export const FileUpload: React.FunctionComponent<FileUploadProps> = ({
|
|
|
100
117
|
.then(data => {
|
|
101
118
|
onReadFinished(fileHandle);
|
|
102
119
|
onChange(data as string, fileHandle.name, event);
|
|
120
|
+
onDataChange?.(data as string);
|
|
103
121
|
})
|
|
104
122
|
.catch((error: DOMException) => {
|
|
105
123
|
onReadFailed(error, fileHandle);
|
|
106
124
|
onReadFinished(fileHandle);
|
|
107
125
|
onChange('', '', event); // Clear the filename field on a failure
|
|
126
|
+
onDataChange?.('');
|
|
108
127
|
});
|
|
109
128
|
} else {
|
|
110
129
|
onChange(fileHandle, fileHandle.name, event);
|
|
@@ -113,41 +132,69 @@ export const FileUpload: React.FunctionComponent<FileUploadProps> = ({
|
|
|
113
132
|
dropzoneProps.onDropAccepted && dropzoneProps.onDropAccepted(acceptedFiles, event);
|
|
114
133
|
};
|
|
115
134
|
|
|
116
|
-
const onDropRejected
|
|
135
|
+
const onDropRejected = (rejectedFiles: File[], event: DropEvent) => {
|
|
117
136
|
if (rejectedFiles.length > 0) {
|
|
118
137
|
onChange('', rejectedFiles[0].name, event);
|
|
119
138
|
}
|
|
120
139
|
dropzoneProps.onDropRejected && dropzoneProps.onDropRejected(rejectedFiles, event);
|
|
121
140
|
};
|
|
122
141
|
|
|
142
|
+
const fileInputRef = React.useRef<HTMLInputElement>();
|
|
143
|
+
const setFileValue = (filename: string) => {
|
|
144
|
+
fileInputRef.current.value = filename;
|
|
145
|
+
};
|
|
146
|
+
|
|
123
147
|
const onClearButtonClick = (event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
|
|
124
148
|
onChange('', '', event);
|
|
149
|
+
onClearClick?.(event);
|
|
150
|
+
setFileValue(null);
|
|
125
151
|
};
|
|
126
152
|
|
|
127
153
|
return (
|
|
128
|
-
<Dropzone
|
|
129
|
-
{
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
154
|
+
<Dropzone
|
|
155
|
+
multiple={false}
|
|
156
|
+
{...dropzoneProps}
|
|
157
|
+
onDropAccepted={onDropAccepted}
|
|
158
|
+
onDropRejected={onDropRejected}
|
|
159
|
+
noClick={true}
|
|
160
|
+
>
|
|
161
|
+
{({ getRootProps, getInputProps, isDragActive, open }) => {
|
|
162
|
+
const oldInputProps = getInputProps();
|
|
163
|
+
const inputProps: DropzoneInputProps = {
|
|
164
|
+
...oldInputProps,
|
|
165
|
+
onChange: async (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
166
|
+
oldInputProps.onChange?.(e);
|
|
167
|
+
const files = await fromEvent(e.nativeEvent);
|
|
168
|
+
if (files.length === 1) {
|
|
169
|
+
onFileInputChange?.(e, files[0] as File);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
return (
|
|
175
|
+
<FileUploadField
|
|
176
|
+
{...getRootProps({
|
|
177
|
+
...props,
|
|
178
|
+
refKey: 'containerRef',
|
|
179
|
+
onClick: event => event.preventDefault()
|
|
180
|
+
})}
|
|
181
|
+
tabIndex={null} // Omit the unwanted tabIndex from react-dropzone's getRootProps
|
|
182
|
+
id={id}
|
|
183
|
+
type={type}
|
|
184
|
+
filename={filename}
|
|
185
|
+
value={value}
|
|
186
|
+
onChange={onChange}
|
|
187
|
+
isDragActive={isDragActive}
|
|
188
|
+
onBrowseButtonClick={open}
|
|
189
|
+
onClearButtonClick={onClearButtonClick}
|
|
190
|
+
onTextAreaClick={onClick}
|
|
191
|
+
onTextChange={onTextChange}
|
|
192
|
+
>
|
|
193
|
+
<input {...inputProps} ref={fileInputRef} /* hidden, necessary for react-dropzone */ />
|
|
194
|
+
{children}
|
|
195
|
+
</FileUploadField>
|
|
196
|
+
);
|
|
197
|
+
}}
|
|
151
198
|
</Dropzone>
|
|
152
199
|
);
|
|
153
200
|
};
|
|
@@ -76,6 +76,8 @@ export interface FileUploadFieldProps extends Omit<React.HTMLProps<HTMLDivElemen
|
|
|
76
76
|
isDragActive?: boolean;
|
|
77
77
|
/** A reference object to attach to the FileUploadField container element. */
|
|
78
78
|
containerRef?: React.Ref<HTMLDivElement>;
|
|
79
|
+
/** Text area text changed */
|
|
80
|
+
onTextChange?: (text: string) => void;
|
|
79
81
|
}
|
|
80
82
|
|
|
81
83
|
export const FileUploadField: React.FunctionComponent<FileUploadFieldProps> = ({
|
|
@@ -87,6 +89,7 @@ export const FileUploadField: React.FunctionComponent<FileUploadFieldProps> = ({
|
|
|
87
89
|
onBrowseButtonClick = () => {},
|
|
88
90
|
onClearButtonClick = () => {},
|
|
89
91
|
onTextAreaClick,
|
|
92
|
+
onTextChange,
|
|
90
93
|
className = '',
|
|
91
94
|
isDisabled = false,
|
|
92
95
|
isReadOnly = false,
|
|
@@ -105,10 +108,12 @@ export const FileUploadField: React.FunctionComponent<FileUploadFieldProps> = ({
|
|
|
105
108
|
allowEditingUploadedText = false,
|
|
106
109
|
hideDefaultPreview = false,
|
|
107
110
|
children = null,
|
|
111
|
+
|
|
108
112
|
...props
|
|
109
113
|
}: FileUploadFieldProps) => {
|
|
110
114
|
const onTextAreaChange = (newValue: string, event: React.ChangeEvent<HTMLTextAreaElement>) => {
|
|
111
115
|
onChange(newValue, filename, event);
|
|
116
|
+
onTextChange?.(newValue);
|
|
112
117
|
};
|
|
113
118
|
return (
|
|
114
119
|
<div
|
|
@@ -1,16 +1,12 @@
|
|
|
1
1
|
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
|
2
2
|
|
|
3
3
|
exports[`simple fileupload 1`] = `
|
|
4
|
-
<
|
|
5
|
-
disabled={false}
|
|
6
|
-
getDataTransferItems={[Function]}
|
|
7
|
-
maxSize={Infinity}
|
|
8
|
-
minSize={0}
|
|
4
|
+
<Dropzone
|
|
9
5
|
multiple={false}
|
|
6
|
+
noClick={true}
|
|
10
7
|
onDropAccepted={[Function]}
|
|
11
8
|
onDropRejected={[Function]}
|
|
12
|
-
preventDropOnDocument={true}
|
|
13
9
|
>
|
|
14
10
|
<Component />
|
|
15
|
-
</
|
|
11
|
+
</Dropzone>
|
|
16
12
|
`;
|
|
@@ -9,13 +9,15 @@ import FileUploadIcon from '@patternfly/react-icons/dist/esm/icons/file-upload-i
|
|
|
9
9
|
|
|
10
10
|
## Examples
|
|
11
11
|
|
|
12
|
-
The basic `FileUpload` component can accept a file via browse or drag-and-drop, and behaves like a standard form field with its `value` and `
|
|
12
|
+
The basic `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.
|
|
13
13
|
|
|
14
14
|
### Text files
|
|
15
15
|
|
|
16
|
-
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 `
|
|
16
|
+
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.
|
|
17
|
+
Pressing _Clear_ button triggers `onClearClick` event.
|
|
17
18
|
|
|
18
19
|
### Simple text file
|
|
20
|
+
|
|
19
21
|
```js
|
|
20
22
|
import React from 'react';
|
|
21
23
|
import { FileUpload } from '@patternfly/react-core';
|
|
@@ -24,7 +26,9 @@ class SimpleTextFileUpload extends React.Component {
|
|
|
24
26
|
constructor(props) {
|
|
25
27
|
super(props);
|
|
26
28
|
this.state = { value: '', filename: '', isLoading: false };
|
|
27
|
-
this.
|
|
29
|
+
this.handleFileInputChange = (event, file) => this.setState({ filename: file.name });
|
|
30
|
+
this.handleDataChange = value => this.setState({ value });
|
|
31
|
+
this.handleClear = event => this.setState({ filename: '', value: '' });
|
|
28
32
|
this.handleFileReadStarted = fileHandle => this.setState({ isLoading: true });
|
|
29
33
|
this.handleFileReadFinished = fileHandle => this.setState({ isLoading: false });
|
|
30
34
|
}
|
|
@@ -38,9 +42,11 @@ class SimpleTextFileUpload extends React.Component {
|
|
|
38
42
|
value={value}
|
|
39
43
|
filename={filename}
|
|
40
44
|
filenamePlaceholder="Drag and drop a file or upload one"
|
|
41
|
-
|
|
45
|
+
onFileInputChange={this.handleFileInputChange}
|
|
46
|
+
onDataChange={this.handleDataChange}
|
|
42
47
|
onReadStarted={this.handleFileReadStarted}
|
|
43
48
|
onReadFinished={this.handleFileReadFinished}
|
|
49
|
+
onClearClick={this.handleClear}
|
|
44
50
|
isLoading={isLoading}
|
|
45
51
|
browseButtonText="Upload"
|
|
46
52
|
/>
|
|
@@ -49,9 +55,11 @@ class SimpleTextFileUpload extends React.Component {
|
|
|
49
55
|
}
|
|
50
56
|
```
|
|
51
57
|
|
|
52
|
-
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
|
|
58
|
+
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.
|
|
59
|
+
Typing/pasting text in the box will call `onTextChange` with a string, and a string value is expected for the `value` prop. :
|
|
53
60
|
|
|
54
61
|
### Text file with edits allowed
|
|
62
|
+
|
|
55
63
|
```js
|
|
56
64
|
import React from 'react';
|
|
57
65
|
import { FileUpload } from '@patternfly/react-core';
|
|
@@ -60,7 +68,9 @@ class TextFileWithEditsAllowed extends React.Component {
|
|
|
60
68
|
constructor(props) {
|
|
61
69
|
super(props);
|
|
62
70
|
this.state = { value: '', filename: '', isLoading: false };
|
|
63
|
-
this.
|
|
71
|
+
this.handleFileInputChange = (event, file) => this.setState({ filename: file.name });
|
|
72
|
+
this.handleTextOrDataChange = value => this.setState({ value });
|
|
73
|
+
this.handleClear = event => this.setState({ filename: '', value: '' });
|
|
64
74
|
this.handleFileReadStarted = fileHandle => this.setState({ isLoading: true });
|
|
65
75
|
this.handleFileReadFinished = fileHandle => this.setState({ isLoading: false });
|
|
66
76
|
}
|
|
@@ -74,7 +84,10 @@ class TextFileWithEditsAllowed extends React.Component {
|
|
|
74
84
|
value={value}
|
|
75
85
|
filename={filename}
|
|
76
86
|
filenamePlaceholder="Drag and drop a file or upload one"
|
|
77
|
-
|
|
87
|
+
onFileInputChange={this.handleFileInputChange}
|
|
88
|
+
onDataChange={this.handleTextOrDataChange}
|
|
89
|
+
onClearClick={this.handleClear}
|
|
90
|
+
onTextChange={this.handleTextOrDataChange}
|
|
78
91
|
onReadStarted={this.handleFileReadStarted}
|
|
79
92
|
onReadFinished={this.handleFileReadFinished}
|
|
80
93
|
isLoading={isLoading}
|
|
@@ -95,6 +108,7 @@ Any [props accepted by `react-dropzone`'s `Dropzone` component](https://react-dr
|
|
|
95
108
|
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.
|
|
96
109
|
|
|
97
110
|
### Text file with restrictions
|
|
111
|
+
|
|
98
112
|
```js
|
|
99
113
|
import React from 'react';
|
|
100
114
|
import { FileUpload, Form, FormGroup } from '@patternfly/react-core';
|
|
@@ -103,9 +117,9 @@ class TextFileUploadWithRestrictions extends React.Component {
|
|
|
103
117
|
constructor(props) {
|
|
104
118
|
super(props);
|
|
105
119
|
this.state = { value: '', filename: '', isLoading: false, isRejected: false };
|
|
106
|
-
this.
|
|
107
|
-
|
|
108
|
-
};
|
|
120
|
+
this.handleFileInputChange = (event, file) => this.setState({ filename: file.name });
|
|
121
|
+
this.handleTextOrDataChange = value => this.setState({ value });
|
|
122
|
+
this.handleClear = event => this.setState({ filename: '', value: '', isRejected: false });
|
|
109
123
|
this.handleFileRejected = (rejectedFiles, event) => this.setState({ isRejected: true });
|
|
110
124
|
this.handleFileReadStarted = fileHandle => this.setState({ isLoading: true });
|
|
111
125
|
this.handleFileReadFinished = fileHandle => this.setState({ isLoading: false });
|
|
@@ -127,7 +141,10 @@ class TextFileUploadWithRestrictions extends React.Component {
|
|
|
127
141
|
value={value}
|
|
128
142
|
filename={filename}
|
|
129
143
|
filenamePlaceholder="Drag and drop a file or upload one"
|
|
130
|
-
|
|
144
|
+
onFileInputChange={this.handleFileInputChange}
|
|
145
|
+
onDataChange={this.handleTextOrDataChange}
|
|
146
|
+
onTextChange={this.handleTextOrDataChange}
|
|
147
|
+
onClearClick={this.handleClear}
|
|
131
148
|
onReadStarted={this.handleFileReadStarted}
|
|
132
149
|
onReadFinished={this.handleFileReadFinished}
|
|
133
150
|
isLoading={isLoading}
|
|
@@ -148,9 +165,10 @@ class TextFileUploadWithRestrictions extends React.Component {
|
|
|
148
165
|
|
|
149
166
|
### Other file types
|
|
150
167
|
|
|
151
|
-
If no `type` prop is specified, the component will not read files directly. When a file is selected, a [`File` object](https://developer.mozilla.org/en-US/docs/Web/API/File) will be passed to `
|
|
168
|
+
If no `type` prop is specified, the component will not read files directly. When a file is selected, a [`File` object](https://developer.mozilla.org/en-US/docs/Web/API/File) 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](https://developer.mozilla.org/en-US/docs/Web/API/FileReader) or attaching it to a [FormData object](https://developer.mozilla.org/en-US/docs/Web/API/FormData/Using_FormData_Objects)). 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.
|
|
152
169
|
|
|
153
170
|
### Simple file of any format
|
|
171
|
+
|
|
154
172
|
```js
|
|
155
173
|
import React from 'react';
|
|
156
174
|
import { FileUpload } from '@patternfly/react-core';
|
|
@@ -159,12 +177,25 @@ class SimpleFileUpload extends React.Component {
|
|
|
159
177
|
constructor(props) {
|
|
160
178
|
super(props);
|
|
161
179
|
this.state = { value: null, filename: '' };
|
|
162
|
-
this.
|
|
180
|
+
this.handleFileInputChange = (event, file) => {
|
|
181
|
+
this.setState({ filename: file.name });
|
|
182
|
+
};
|
|
183
|
+
this.handleClear = event => this.setState({ filename: '', value: '' });
|
|
163
184
|
}
|
|
164
185
|
|
|
165
186
|
render() {
|
|
166
187
|
const { value, filename } = this.state;
|
|
167
|
-
return
|
|
188
|
+
return (
|
|
189
|
+
<FileUpload
|
|
190
|
+
id="simple-file"
|
|
191
|
+
value={value}
|
|
192
|
+
filename={filename}
|
|
193
|
+
filenamePlaceholder="Drag and drop a file or upload one"
|
|
194
|
+
browseButtonText="Upload"
|
|
195
|
+
onFileInputChange={this.handleFileInputChange}
|
|
196
|
+
onClearClick={this.handleClear}
|
|
197
|
+
/>
|
|
198
|
+
);
|
|
168
199
|
}
|
|
169
200
|
}
|
|
170
201
|
```
|
|
@@ -174,6 +205,7 @@ class SimpleFileUpload extends React.Component {
|
|
|
174
205
|
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`.
|
|
175
206
|
|
|
176
207
|
### Custom file preview
|
|
208
|
+
|
|
177
209
|
```js
|
|
178
210
|
import React from 'react';
|
|
179
211
|
import { FileUpload } from '@patternfly/react-core';
|
|
@@ -183,7 +215,10 @@ class CustomPreviewFileUpload extends React.Component {
|
|
|
183
215
|
constructor(props) {
|
|
184
216
|
super(props);
|
|
185
217
|
this.state = { value: null, filename: '' };
|
|
186
|
-
this.
|
|
218
|
+
this.handleFileInputChange = (event, file) => {
|
|
219
|
+
this.setState({ value: file, filename: file.name });
|
|
220
|
+
};
|
|
221
|
+
this.handleClear = event => this.setState({ filename: '', value: '' });
|
|
187
222
|
}
|
|
188
223
|
|
|
189
224
|
render() {
|
|
@@ -194,7 +229,8 @@ class CustomPreviewFileUpload extends React.Component {
|
|
|
194
229
|
value={value}
|
|
195
230
|
filename={filename}
|
|
196
231
|
filenamePlaceholder="Drag and drop a file or upload one"
|
|
197
|
-
|
|
232
|
+
onFileInputChange={this.handleFileInputChange}
|
|
233
|
+
onClearClick={this.handleClear}
|
|
198
234
|
hideDefaultPreview
|
|
199
235
|
browseButtonText="Upload"
|
|
200
236
|
>
|
|
@@ -216,6 +252,7 @@ class CustomPreviewFileUpload extends React.Component {
|
|
|
216
252
|
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`!
|
|
217
253
|
|
|
218
254
|
### Custom file upload
|
|
255
|
+
|
|
219
256
|
```js
|
|
220
257
|
import React from 'react';
|
|
221
258
|
import { FileUploadField, Checkbox } from '@patternfly/react-core';
|
|
@@ -274,7 +311,7 @@ class CustomFileUpload extends React.Component {
|
|
|
274
311
|
type="text"
|
|
275
312
|
value={value}
|
|
276
313
|
filename={filename ? 'example-filename.txt' : ''}
|
|
277
|
-
|
|
314
|
+
onTextChange={this.handleTextAreaChange}
|
|
278
315
|
filenamePlaceholder="Do something custom with this!"
|
|
279
316
|
onBrowseButtonClick={() => alert('Browse button clicked!')}
|
|
280
317
|
onClearButtonClick={() => alert('Clear button clicked!')}
|
|
@@ -863,3 +863,206 @@ class HelpModal extends React.Component {
|
|
|
863
863
|
}
|
|
864
864
|
}
|
|
865
865
|
```
|
|
866
|
+
|
|
867
|
+
### With form
|
|
868
|
+
|
|
869
|
+
```js
|
|
870
|
+
import React from 'react';
|
|
871
|
+
import { Modal, ModalVariant, Button, Form, FormGroup, HelpIcon, Popover, TextInput } from '@patternfly/react-core';
|
|
872
|
+
|
|
873
|
+
FormModal = () => {
|
|
874
|
+
const [isModalOpen, setModalOpen] = React.useState(false);
|
|
875
|
+
const [nameValue, setNameValue] = React.useState("");
|
|
876
|
+
const [emailValue, setEmailValue] = React.useState("");
|
|
877
|
+
const [addressValue, setAddressValue] = React.useState("");
|
|
878
|
+
|
|
879
|
+
handleModalToggle = () => {
|
|
880
|
+
setModalOpen(!isModalOpen);
|
|
881
|
+
};
|
|
882
|
+
|
|
883
|
+
handleNameInputChange = value => {
|
|
884
|
+
setNameValue(value);
|
|
885
|
+
};
|
|
886
|
+
|
|
887
|
+
handleEmailInputChange = value => {
|
|
888
|
+
setEmailValue(value);
|
|
889
|
+
};
|
|
890
|
+
handleAddressInputChange = value => {
|
|
891
|
+
setAddressValue(value);
|
|
892
|
+
};
|
|
893
|
+
|
|
894
|
+
|
|
895
|
+
return (
|
|
896
|
+
<React.Fragment>
|
|
897
|
+
<Button variant="primary" onClick={handleModalToggle}>
|
|
898
|
+
Show modal
|
|
899
|
+
</Button>
|
|
900
|
+
<Modal
|
|
901
|
+
variant={ModalVariant.small}
|
|
902
|
+
title="Create account"
|
|
903
|
+
description="Enter your personal information below to create an account."
|
|
904
|
+
isOpen={isModalOpen}
|
|
905
|
+
onClose={handleModalToggle}
|
|
906
|
+
actions={[
|
|
907
|
+
<Button key="create" variant="primary" form="modal-with-form-form" onClick={this.handleModalToggle}>
|
|
908
|
+
Confirm
|
|
909
|
+
</Button>,
|
|
910
|
+
<Button key="cancel" variant="link" onClick={this.handleModalToggle}>
|
|
911
|
+
Cancel
|
|
912
|
+
</Button>
|
|
913
|
+
]}
|
|
914
|
+
>
|
|
915
|
+
<Form id="modal-with-form-form">
|
|
916
|
+
<FormGroup
|
|
917
|
+
label="Name"
|
|
918
|
+
labelIcon={
|
|
919
|
+
<Popover
|
|
920
|
+
headerContent={
|
|
921
|
+
<div>
|
|
922
|
+
The
|
|
923
|
+
<a href="https://schema.org/name" target="_blank">
|
|
924
|
+
name
|
|
925
|
+
</a>
|
|
926
|
+
of a
|
|
927
|
+
<a href="https://schema.org/Person" target="_blank">
|
|
928
|
+
Person
|
|
929
|
+
</a>
|
|
930
|
+
</div>
|
|
931
|
+
}
|
|
932
|
+
bodyContent={
|
|
933
|
+
<div>
|
|
934
|
+
Often composed of
|
|
935
|
+
<a href="https://schema.org/givenName" target="_blank">
|
|
936
|
+
givenName
|
|
937
|
+
</a>
|
|
938
|
+
and
|
|
939
|
+
<a href="https://schema.org/familyName" target="_blank">
|
|
940
|
+
familyName
|
|
941
|
+
</a>
|
|
942
|
+
.
|
|
943
|
+
</div>
|
|
944
|
+
}
|
|
945
|
+
>
|
|
946
|
+
<button
|
|
947
|
+
type="button"
|
|
948
|
+
aria-label="More info for name field"
|
|
949
|
+
onClick={e => e.preventDefault()}
|
|
950
|
+
aria-describedby="modal-with-form-form-name"
|
|
951
|
+
className="pf-c-form__group-label-help"
|
|
952
|
+
>
|
|
953
|
+
<HelpIcon noVerticalAlign />
|
|
954
|
+
</button>
|
|
955
|
+
</Popover>
|
|
956
|
+
}
|
|
957
|
+
isRequired
|
|
958
|
+
fieldId="modal-with-form-form-name">
|
|
959
|
+
<TextInput
|
|
960
|
+
isRequired
|
|
961
|
+
type="email"
|
|
962
|
+
id="modal-with-form-form-name"
|
|
963
|
+
name="modal-with-form-form-name"
|
|
964
|
+
value={nameValue}
|
|
965
|
+
onChange={handleNameInputChange}
|
|
966
|
+
/>
|
|
967
|
+
</FormGroup>
|
|
968
|
+
<FormGroup
|
|
969
|
+
label="E-mail"
|
|
970
|
+
labelIcon={
|
|
971
|
+
<Popover
|
|
972
|
+
headerContent={
|
|
973
|
+
<div>
|
|
974
|
+
The
|
|
975
|
+
<a href="https://schema.org/email" target="_blank">
|
|
976
|
+
e-mail
|
|
977
|
+
</a>
|
|
978
|
+
of a
|
|
979
|
+
<a href="https://schema.org/Person" target="_blank">
|
|
980
|
+
person
|
|
981
|
+
</a>
|
|
982
|
+
</div>
|
|
983
|
+
}
|
|
984
|
+
bodyContent={
|
|
985
|
+
<div>
|
|
986
|
+
Valid
|
|
987
|
+
<a href="https://schema.org/email" target="_blank">
|
|
988
|
+
e-mail
|
|
989
|
+
</a>
|
|
990
|
+
address.
|
|
991
|
+
</div>
|
|
992
|
+
}
|
|
993
|
+
>
|
|
994
|
+
<button
|
|
995
|
+
type="button"
|
|
996
|
+
aria-label="More info for e-mail field"
|
|
997
|
+
onClick={e => e.preventDefault()}
|
|
998
|
+
aria-describedby="modal-with-form-form-email"
|
|
999
|
+
className="pf-c-form__group-label-help"
|
|
1000
|
+
>
|
|
1001
|
+
<HelpIcon noVerticalAlign />
|
|
1002
|
+
</button>
|
|
1003
|
+
</Popover>
|
|
1004
|
+
}
|
|
1005
|
+
isRequired
|
|
1006
|
+
fieldId="modal-with-form-form-email">
|
|
1007
|
+
<TextInput
|
|
1008
|
+
isRequired
|
|
1009
|
+
type="email"
|
|
1010
|
+
id="modal-with-form-form-email"
|
|
1011
|
+
name="modal-with-form-form-email"
|
|
1012
|
+
value={emailValue}
|
|
1013
|
+
onChange={handleEmailInputChange}
|
|
1014
|
+
/>
|
|
1015
|
+
</FormGroup>
|
|
1016
|
+
<FormGroup
|
|
1017
|
+
label="Address"
|
|
1018
|
+
labelIcon={
|
|
1019
|
+
<Popover
|
|
1020
|
+
headerContent={
|
|
1021
|
+
<div>
|
|
1022
|
+
The
|
|
1023
|
+
<a href="https://schema.org/address" target="_blank">
|
|
1024
|
+
adress
|
|
1025
|
+
</a>
|
|
1026
|
+
of a
|
|
1027
|
+
<a href="https://schema.org/Person" target="_blank">
|
|
1028
|
+
person
|
|
1029
|
+
</a>
|
|
1030
|
+
</div>
|
|
1031
|
+
}
|
|
1032
|
+
bodyContent={
|
|
1033
|
+
<div>
|
|
1034
|
+
Valid
|
|
1035
|
+
<a href="https://schema.org/PostalAddress" target="_blank">
|
|
1036
|
+
postal address.
|
|
1037
|
+
</a>
|
|
1038
|
+
</div>
|
|
1039
|
+
}
|
|
1040
|
+
>
|
|
1041
|
+
<button
|
|
1042
|
+
type="button"
|
|
1043
|
+
aria-label="More info for address field"
|
|
1044
|
+
onClick={e => e.preventDefault()}
|
|
1045
|
+
aria-describedby="modal-with-form-form-address"
|
|
1046
|
+
className="pf-c-form__group-label-help"
|
|
1047
|
+
>
|
|
1048
|
+
<HelpIcon noVerticalAlign />
|
|
1049
|
+
</button>
|
|
1050
|
+
</Popover>
|
|
1051
|
+
}
|
|
1052
|
+
isRequired
|
|
1053
|
+
fieldId="modal-with-form-form-address">
|
|
1054
|
+
<TextInput
|
|
1055
|
+
isRequired
|
|
1056
|
+
type="email"
|
|
1057
|
+
id="modal-with-form-form-address"
|
|
1058
|
+
name="modal-with-form-form-address"
|
|
1059
|
+
value={addressValue}
|
|
1060
|
+
onChange={handleAddressInputChange}
|
|
1061
|
+
/>
|
|
1062
|
+
</FormGroup>
|
|
1063
|
+
</Form>
|
|
1064
|
+
</Modal>
|
|
1065
|
+
</React.Fragment>
|
|
1066
|
+
);
|
|
1067
|
+
};
|
|
1068
|
+
```
|