| 1 |
import { useEffect, useState } from 'react'; |
| 2 |
import styles from '@patternfly/react-styles/css/components/MultipleFileUpload/multiple-file-upload'; |
| 3 |
import { css } from '@patternfly/react-styles'; |
| 4 |
import { Progress } from '../Progress'; |
| 5 |
import { Button } from '../Button'; |
| 6 |
import FileIcon from '@patternfly/react-icons/dist/esm/icons/file-icon'; |
| 7 |
import RhMicronsCloseIcon from '@patternfly/react-icons/dist/esm/icons/rh-microns-close-icon'; |
| 8 |
|
| 9 |
/** Automatically reads an uploaded file to render a visual representation of it, including |
| 10 |
* its name, size, and read status. This sub-component also allows custom reading of files |
| 11 |
* via various callbacks which will override the automatic reading behavior. |
| 12 |
*/ |
| 13 |
|
| 14 |
export interface MultipleFileUploadStatusItemProps extends React.HTMLProps<HTMLLIElement> { |
| 15 |
/** Class to add to outer div */ |
| 16 |
className?: string; |
| 17 |
/** Adds accessibility text to the status item deletion button */ |
| 18 |
buttonAriaLabel?: string; |
| 19 |
/** The file object being represented by the status item */ |
| 20 |
file?: File; |
| 21 |
/** A callback for when a selected file starts loading */ |
| 22 |
onReadStarted?: (fileHandle: File) => void; |
| 23 |
/** A callback for when a selected file finishes loading */ |
| 24 |
onReadFinished?: (fileHandle: File) => void; |
| 25 |
/** A callback for when the FileReader successfully reads the file */ |
| 26 |
onReadSuccess?: (data: string, file: File) => void; |
| 27 |
/** A callback for when the FileReader API fails */ |
| 28 |
onReadFail?: (error: DOMException, onReadFail: File) => void; |
| 29 |
/** Clear button was clicked */ |
| 30 |
onClearClick?: React.MouseEventHandler<HTMLButtonElement>; |
| 31 |
|
| 32 |
// Props to bypass built in behavior |
| 33 |
|
| 34 |
/** A callback to process file reading in a custom way */ |
| 35 |
customFileHandler?: (file: File) => void; |
| 36 |
/** A custom icon to show in place of the generic file icon */ |
| 37 |
fileIcon?: React.ReactNode; |
| 38 |
/** A custom name to display for the file rather than using built in functionality to auto-fill it */ |
| 39 |
fileName?: string; |
| 40 |
/** A custom file size to display for the file rather than using built in functionality to auto-fill it */ |
| 41 |
fileSize?: number; |
| 42 |
/** A custom value to display for the progress component rather than using built in functionality to auto-fill it */ |
| 43 |
progressValue?: number; |
| 44 |
/** A custom variant to apply to the progress component rather than using built in functionality to auto-fill it */ |
| 45 |
progressVariant?: 'danger' | 'success' | 'warning'; |
| 46 |
|
| 47 |
// Props passed through to the progress component |
| 48 |
|
| 49 |
/** Adds accessible text to the progress bar. Required when title not used and there is not any label associated with the progress bar */ |
| 50 |
progressAriaLabel?: string; |
| 51 |
/** Associates the progress bar with it's label for accessibility purposes. Required when title not used */ |
| 52 |
progressAriaLabelledBy?: string; |
| 53 |
/** Adds an accessible description to the ProgressBar via space separated list of ids. Required when helperText is passed in. */ |
| 54 |
progressAriaDescribedBy?: string; |
| 55 |
/** Modifies the text announced by assistive technologies when the progress bar updates. */ |
| 56 |
progressAriaLiveMessage?: string | ((loadPercentage: number) => string); |
| 57 |
/** Unique identifier for progress. Generated if not specified. */ |
| 58 |
progressId?: string; |
| 59 |
/** Additional content related to the status item. */ |
| 60 |
progressHelperText?: React.ReactNode; |
| 61 |
} |
| 62 |
|
| 63 |
export const MultipleFileUploadStatusItem: React.FunctionComponent<MultipleFileUploadStatusItemProps> = ({ |
| 64 |
className, |
| 65 |
file, |
| 66 |
fileIcon, |
| 67 |
onReadStarted = () => {}, |
| 68 |
onReadFinished = () => {}, |
| 69 |
onReadSuccess = () => {}, |
| 70 |
onReadFail = () => {}, |
| 71 |
onClearClick = () => {}, |
| 72 |
customFileHandler, |
| 73 |
fileName, |
| 74 |
fileSize, |
| 75 |
progressValue, |
| 76 |
progressVariant, |
| 77 |
progressAriaLabel, |
| 78 |
progressAriaLabelledBy, |
| 79 |
progressAriaDescribedBy, |
| 80 |
progressId, |
| 81 |
progressAriaLiveMessage, |
| 82 |
buttonAriaLabel = 'Remove from list', |
| 83 |
progressHelperText, |
| 84 |
...props |
| 85 |
}: MultipleFileUploadStatusItemProps) => { |
| 86 |
const [loadPercentage, setLoadPercentage] = useState(0); |
| 87 |
const [loadResult, setLoadResult] = useState<undefined | 'danger' | 'success'>(); |
| 88 |
|
| 89 |
function readFile(file: File) { |
| 90 |
return new Promise((resolve, reject) => { |
| 91 |
const reader = new FileReader(); |
| 92 |
reader.onload = () => resolve(reader.result); |
| 93 |
reader.onerror = () => reject(reader.error); |
| 94 |
reader.onprogress = (data) => { |
| 95 |
if (data.lengthComputable) { |
| 96 |
setLoadPercentage((data.loaded / data.total) * 100); |
| 97 |
} |
| 98 |
}; |
| 99 |
reader.readAsDataURL(file); |
| 100 |
}); |
| 101 |
} |
| 102 |
|
| 103 |
useEffect(() => { |
| 104 |
if (customFileHandler) { |
| 105 |
customFileHandler(file); |
| 106 |
} else { |
| 107 |
onReadStarted(file); |
| 108 |
readFile(file) |
| 109 |
.then((data) => { |
| 110 |
setLoadResult('success'); |
| 111 |
setLoadPercentage(100); |
| 112 |
onReadFinished(file); |
| 113 |
onReadSuccess(data as string, file); |
| 114 |
}) |
| 115 |
.catch((error: DOMException) => { |
| 116 |
onReadFinished(file); |
| 117 |
onReadFail(error, file); |
| 118 |
setLoadResult('danger'); |
| 119 |
}); |
| 120 |
} |
| 121 |
}, []); |
| 122 |
|
| 123 |
const getHumanReadableFileSize = (size: number) => { |
| 124 |
const prefixes = ['', 'K', 'M', 'G', 'T']; |
| 125 |
let prefixUnit = 0; |
| 126 |
while (size >= 1000) { |
| 127 |
prefixUnit += 1; |
| 128 |
size = size / 1000; |
| 129 |
} |
| 130 |
|
| 131 |
if (prefixUnit >= prefixes.length) { |
| 132 |
return 'File size too large'; |
| 133 |
} |
| 134 |
|
| 135 |
return `${Math.round(size)}${prefixes[prefixUnit]}B`; |
| 136 |
}; |
| 137 |
|
| 138 |
const value = progressValue || loadPercentage; |
| 139 |
const variant = progressVariant || loadResult; |
| 140 |
|
| 141 |
const title = ( |
| 142 |
<span className={styles.multipleFileUploadStatusItemProgress}> |
| 143 |
<span className={styles.multipleFileUploadStatusItemProgressText}>{fileName || file?.name || ''}</span> |
| 144 |
<span className={styles.multipleFileUploadStatusItemProgressSize}> |
| 145 |
{fileSize || getHumanReadableFileSize(file?.size || 0)} |
| 146 |
</span> |
| 147 |
</span> |
| 148 |
); |
| 149 |
|
| 150 |
return ( |
| 151 |
<li className={css(styles.multipleFileUploadStatusItem, className)} {...props}> |
| 152 |
<div className={styles.multipleFileUploadStatusItemIcon}>{fileIcon || <FileIcon />}</div> |
| 153 |
<div className={styles.multipleFileUploadStatusItemMain}> |
| 154 |
<div className="pf-v6-screen-reader" aria-live="polite"> |
| 155 |
{progressAriaLiveMessage && |
| 156 |
typeof progressAriaLiveMessage === 'function' && |
| 157 |
progressAriaLiveMessage(+loadPercentage.toFixed(2))} |
| 158 |
{progressAriaLiveMessage && typeof progressAriaLiveMessage === 'string' && progressAriaLiveMessage} |
| 159 |
{!progressAriaLiveMessage && `Progress value is ${progressValue || Math.floor(loadPercentage)}%.`} |
| 160 |
</div> |
| 161 |
<Progress |
| 162 |
title={title} |
| 163 |
value={value} |
| 164 |
variant={variant} |
| 165 |
aria-label={progressAriaLabel} |
| 166 |
aria-labelledby={progressAriaLabelledBy} |
| 167 |
aria-describedby={progressAriaDescribedBy} |
| 168 |
id={progressId} |
| 169 |
helperText={progressHelperText} |
| 170 |
/> |
| 171 |
</div> |
| 172 |
<div className={`${styles.multipleFileUploadStatusItem}-close`}> |
| 173 |
<Button variant="plain" aria-label={buttonAriaLabel} onClick={onClearClick} icon={<RhMicronsCloseIcon />} /> |
| 174 |
</div> |
| 175 |
</li> |
| 176 |
); |
| 177 |
}; |
| 178 |
|
| 179 |
MultipleFileUploadStatusItem.displayName = 'MultipleFileUploadStatusItem'; |