| 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 { ExpandableSection } from '../ExpandableSection'; |
| 5 |
import { useSSRSafeId } from '../../helpers'; |
| 6 |
import RhUiInProgressIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-in-progress-icon'; |
| 7 |
import RhUiCheckCircleFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-check-circle-fill-icon'; |
| 8 |
import TimesCircleIcon from '@patternfly/react-icons/dist/esm/icons/times-circle-icon'; |
| 9 |
|
| 10 |
/** Acts as an expandable container for all uploaded file statuses. |
| 11 |
* An optional text and/or icon can also be passed into this sub-component. |
| 12 |
* This sub-component can be conditionally rendered when at least 1 file has been |
| 13 |
* attempted to be uploaded. |
| 14 |
*/ |
| 15 |
|
| 16 |
export interface MultipleFileUploadStatusProps extends React.HTMLProps<HTMLDivElement> { |
| 17 |
/** Content rendered inside multi file upload status list */ |
| 18 |
children?: React.ReactNode; |
| 19 |
/** Class to add to outer div */ |
| 20 |
className?: string; |
| 21 |
/** String to show in the status toggle */ |
| 22 |
statusToggleText?: string; |
| 23 |
/** Icon to show in the status toggle */ |
| 24 |
statusToggleIcon?: 'danger' | 'success' | 'inProgress' | React.ReactNode; |
| 25 |
/** Adds an accessible label to the list of status items. */ |
| 26 |
'aria-label'?: string; |
| 27 |
} |
| 28 |
|
| 29 |
export const MultipleFileUploadStatus: React.FunctionComponent<MultipleFileUploadStatusProps> = ({ |
| 30 |
children, |
| 31 |
className, |
| 32 |
statusToggleText, |
| 33 |
statusToggleIcon, |
| 34 |
'aria-label': ariaLabel, |
| 35 |
...props |
| 36 |
}: MultipleFileUploadStatusProps) => { |
| 37 |
const expandableSectionId = useSSRSafeId('pf-expandable-section-'); |
| 38 |
const [icon, setIcon] = useState<React.ReactNode>(); |
| 39 |
const [isOpen, setIsOpen] = useState(true); |
| 40 |
|
| 41 |
useEffect(() => { |
| 42 |
switch (statusToggleIcon) { |
| 43 |
case 'danger': |
| 44 |
setIcon(<TimesCircleIcon />); |
| 45 |
break; |
| 46 |
case 'success': |
| 47 |
setIcon(<RhUiCheckCircleFillIcon />); |
| 48 |
break; |
| 49 |
case 'inProgress': |
| 50 |
setIcon(<RhUiInProgressIcon />); |
| 51 |
break; |
| 52 |
default: |
| 53 |
setIcon(statusToggleIcon); |
| 54 |
} |
| 55 |
}, [statusToggleIcon]); |
| 56 |
|
| 57 |
const toggle = ( |
| 58 |
<div className={styles.multipleFileUploadStatusProgress}> |
| 59 |
<div className={`${styles.multipleFileUploadStatusProgress}-icon`}>{icon}</div> |
| 60 |
<div className={`${styles.multipleFileUploadStatusProgress}-text`}>{statusToggleText}</div> |
| 61 |
</div> |
| 62 |
); |
| 63 |
|
| 64 |
const toggleExpandableSection = () => { |
| 65 |
setIsOpen(!isOpen); |
| 66 |
}; |
| 67 |
|
| 68 |
return ( |
| 69 |
<div className={css(styles.multipleFileUploadStatus, className)} {...props}> |
| 70 |
<ExpandableSection |
| 71 |
contentId={`${expandableSectionId}-content`} |
| 72 |
toggleId={`${expandableSectionId}-toggle`} |
| 73 |
toggleContent={toggle} |
| 74 |
isExpanded={isOpen} |
| 75 |
onToggle={toggleExpandableSection} |
| 76 |
> |
| 77 |
<ul className={`${styles.multipleFileUploadStatus}-list`} role="list" aria-label={ariaLabel}> |
| 78 |
{children} |
| 79 |
</ul> |
| 80 |
</ExpandableSection> |
| 81 |
</div> |
| 82 |
); |
| 83 |
}; |
| 84 |
|
| 85 |
MultipleFileUploadStatus.displayName = 'MultipleFileUploadStatus'; |