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