Debian Codegraph @

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

raw
1 import styles from '@patternfly/react-styles/css/components/FileUpload/file-upload';
2 import { css } from '@patternfly/react-styles';
3 import { InputGroup, InputGroupItem } from '../InputGroup';
4 import { TextInput } from '../TextInput';
5 import { Button, ButtonVariant } from '../Button';
6 import { TextArea, TextAreResizeOrientation } from '../TextArea';
7 import { Spinner, spinnerSize } from '../Spinner';
8 import { fileReaderType } from '../../helpers/fileUtils';
9
10 /** A more customizable file upload component for implementing custom logic. Drag and drop
11 * functionality is not built in by default.
12 */
13
14 export interface FileUploadFieldProps extends Omit<React.HTMLProps<HTMLDivElement>, 'value'> {
15 /** Flag to allow editing of a text file's contents after it is selected from disk. */
16 allowEditingUploadedText?: boolean;
17 /** Aria-label for the text area. */
18 'aria-label'?: string;
19 /** Text for the browse button. */
20 browseButtonText?: string;
21 /** ID or ID's of elements that describe the browse button. Typically this should refer
22 * to elements such as helper text when there are file restrictions.
23 */
24 browseButtonAriaDescribedby?: string;
25 /** Additional children to render after (or instead of) the file preview. */
26 children?: React.ReactNode;
27 /** Additional classes added to the file upload field container element. */
28 className?: string;
29 /** Text for the clear button. */
30 clearButtonText?: string;
31 /** Value to be shown in the read-only filename field. */
32 filename?: string;
33 /** Aria-label for the read-only filename field. */
34 filenameAriaLabel?: string;
35 /** Placeholder string to display in the empty filename field. */
36 filenamePlaceholder?: string;
37 /** Flag to hide the built-in preview of the file (where available). If true, you can use
38 * the children property to render an alternate preview.
39 */
40 hideDefaultPreview?: boolean;
41 /** Unique id for the text area. Also used to generate ids for accessible labels. */
42 id: string;
43 /** Name property for the text input. */
44 name?: string;
45 /** Flag to disable the browse button. */
46 isBrowseButtonDisabled?: boolean;
47 /** Flag to disable the clear button. */
48 isClearButtonDisabled?: boolean;
49 /** Flag to show if the field is disabled. */
50 isDisabled?: boolean;
51 /** Flag to show if a file is being loaded. */
52 isLoading?: boolean;
53 /** Flag to show if the field is read only. */
54 isReadOnly?: boolean;
55 /** Flag to show if the field is required. */
56 isRequired?: boolean;
57 /** Aria-valuetext for the loading spinner. */
58 spinnerAriaValueText?: string;
59 /** What type of file. Determines what is is expected by the value property (a string for
60 * 'text' and 'dataURL', or a File object otherwise).
61 */
62 type?: 'text' | 'dataURL';
63 /** Value to indicate if the field is modified to show that validation state.
64 * If set to success, field will be modified to indicate valid state.
65 * If set to error, field will be modified to indicate error state.
66 */
67 validated?: 'success' | 'error' | 'default';
68 /** Value of the file's contents (string if text file, File object otherwise). */
69 value?: string | File;
70
71 // Props available in FileUploadField but not FileUpload:
72
73 /** A reference object to attach to the file upload field container element. */
74 containerRef?: React.Ref<HTMLDivElement>;
75 /** Flag to show if a file is being dragged over the file upload field. */
76 isDragActive?: boolean;
77 /** A callback for when the browse button is clicked. */
78 onBrowseButtonClick?: (event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => void;
79 /** A callback for when the clear button is clicked. */
80 onClearButtonClick?: (event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => void;
81 /** Callback for when focus is lost on the text area field. */
82 onTextAreaBlur?: (event?: any) => void;
83 /** A callback for when the text area is clicked. Can also be set via the onClick property
84 * of the file upload component. */
85 onTextAreaClick?: (event: React.MouseEvent<HTMLTextAreaElement, MouseEvent>) => void;
86 /** Text area text changed. */
87 onTextChange?: (event: React.ChangeEvent<HTMLTextAreaElement>, text: string) => void;
88 /** Placeholder string to display in the empty text area field. */
89 textAreaPlaceholder?: string;
90 }
91
92 export const FileUploadField: React.FunctionComponent<FileUploadFieldProps> = ({
93 id,
94 name,
95 type,
96 value = '',
97 filename = '',
98 onBrowseButtonClick = () => {},
99 onClearButtonClick = () => {},
100 onTextAreaClick,
101 onTextChange,
102 onTextAreaBlur,
103 textAreaPlaceholder = '',
104 className = '',
105 isDisabled = false,
106 isReadOnly = false,
107 isLoading = false,
108 spinnerAriaValueText,
109 isRequired = false,
110 isDragActive = false,
111 validated = 'default' as 'success' | 'error' | 'default',
112 'aria-label': ariaLabel = 'File upload',
113 filenamePlaceholder = 'Drag a file here or browse to upload',
114 filenameAriaLabel = filename ? 'Read only filename' : filenamePlaceholder,
115 browseButtonText = 'Browse...',
116 browseButtonAriaDescribedby,
117 clearButtonText = 'Clear',
118 isBrowseButtonDisabled = false,
119 isClearButtonDisabled = !filename && !value,
120 containerRef = null as React.Ref<HTMLDivElement>,
121 allowEditingUploadedText = false,
122 hideDefaultPreview = false,
123 children = null,
124
125 ...props
126 }: FileUploadFieldProps) => {
127 const onTextAreaChange = (event: React.ChangeEvent<HTMLTextAreaElement>, newValue: string) => {
128 onTextChange?.(event, newValue);
129 };
130 return (
131 <div
132 className={css(
133 styles.fileUpload,
134 isDragActive && styles.modifiers.dragHover,
135 isLoading && styles.modifiers.loading,
136 className
137 )}
138 ref={containerRef}
139 {...props}
140 >
141 <div className={styles.fileUploadFileSelect}>
142 <InputGroup>
143 <InputGroupItem isFill>
144 <TextInput
145 readOnlyVariant="default" // Always read-only regardless of isReadOnly prop (which is just for the TextArea)
146 isDisabled={isDisabled}
147 id={`${id}-filename`}
148 name={name || `${id}-filename`}
149 aria-label={filenameAriaLabel}
150 placeholder={filenamePlaceholder}
151 value={filename}
152 />
153 </InputGroupItem>
154 <InputGroupItem>
155 <Button
156 variant={ButtonVariant.control}
157 onClick={onBrowseButtonClick}
158 isDisabled={isDisabled || isBrowseButtonDisabled}
159 aria-describedby={browseButtonAriaDescribedby}
160 >
161 {browseButtonText}
162 </Button>
163 </InputGroupItem>
164 <InputGroupItem>
165 <Button
166 variant={ButtonVariant.control}
167 isDisabled={isDisabled || isClearButtonDisabled}
168 onClick={onClearButtonClick}
169 >
170 {clearButtonText}
171 </Button>
172 </InputGroupItem>
173 </InputGroup>
174 </div>
175 {(isLoading || (!hideDefaultPreview && type === fileReaderType.text)) && (
176 <div className={styles.fileUploadFileDetails}>
177 {!hideDefaultPreview && type === fileReaderType.text && (
178 <TextArea
179 readOnly={isReadOnly || (!!filename && !allowEditingUploadedText)}
180 disabled={isDisabled}
181 isRequired={isRequired}
182 resizeOrientation={TextAreResizeOrientation.vertical}
183 validated={validated}
184 id={id}
185 aria-label={ariaLabel}
186 value={value as string}
187 onChange={onTextAreaChange}
188 onClick={onTextAreaClick}
189 onBlur={onTextAreaBlur}
190 placeholder={textAreaPlaceholder}
191 />
192 )}
193 {isLoading && (
194 <div className={styles.fileUploadFileDetailsSpinner}>
195 <Spinner size={spinnerSize.lg} aria-valuetext={spinnerAriaValueText} />
196 </div>
197 )}
198 </div>
199 )}
200 {children}
201 </div>
202 );
203 };
204 FileUploadField.displayName = 'FileUploadField';