| 1 |
import { createContext } from 'react'; |
| 2 |
import { DropzoneOptions, useDropzone } from 'react-dropzone'; |
| 3 |
import { DropEvent } from '../../helpers/typeUtils'; |
| 4 |
import styles from '@patternfly/react-styles/css/components/MultipleFileUpload/multiple-file-upload'; |
| 5 |
import { css } from '@patternfly/react-styles'; |
| 6 |
|
| 7 |
/** Acts as a container for all other MultipleFileUpload sub-components. This sub-component |
| 8 |
* also provides the functionality for file uploads, and access to the uploaded files via |
| 9 |
* a callback. |
| 10 |
*/ |
| 11 |
|
| 12 |
export interface MultipleFileUploadProps extends Omit<React.HTMLProps<HTMLDivElement>, 'value'> { |
| 13 |
/** Content rendered inside the multi upload field */ |
| 14 |
children?: React.ReactNode; |
| 15 |
/** Class to add to outer div */ |
| 16 |
className?: string; |
| 17 |
/** Optional extra props to customize react-dropzone. */ |
| 18 |
dropzoneProps?: DropzoneOptions; |
| 19 |
/** Flag setting the component to horizontal styling mode */ |
| 20 |
isHorizontal?: boolean; |
| 21 |
/** When files are dropped or uploaded this callback will be called with all accepted files */ |
| 22 |
onFileDrop?: (event: DropEvent, data: File[]) => void; |
| 23 |
} |
| 24 |
|
| 25 |
export const MultipleFileUploadContext = createContext({ |
| 26 |
open: () => {} |
| 27 |
}); |
| 28 |
|
| 29 |
export const MultipleFileUpload: React.FunctionComponent<MultipleFileUploadProps> = ({ |
| 30 |
className, |
| 31 |
children, |
| 32 |
dropzoneProps = {}, |
| 33 |
isHorizontal, |
| 34 |
onFileDrop = () => {}, |
| 35 |
...props |
| 36 |
}: MultipleFileUploadProps) => { |
| 37 |
const onDropAccepted = (acceptedFiles: File[], event: DropEvent) => { |
| 38 |
onFileDrop(event, acceptedFiles); |
| 39 |
// allow users to set a custom drop accepted handler rather than using on data change |
| 40 |
dropzoneProps.onDropAccepted && dropzoneProps.onDropAccepted(acceptedFiles, event); |
| 41 |
}; |
| 42 |
|
| 43 |
const { getRootProps, getInputProps, isDragActive, open } = useDropzone({ |
| 44 |
multiple: true, |
| 45 |
...dropzoneProps, |
| 46 |
onDropAccepted |
| 47 |
}); |
| 48 |
|
| 49 |
const rootProps = getRootProps({ |
| 50 |
...props, |
| 51 |
onClick: (event) => event.stopPropagation() // Prevents clicking TextArea from opening file dialog |
| 52 |
}); |
| 53 |
|
| 54 |
return ( |
| 55 |
<MultipleFileUploadContext.Provider value={{ open }}> |
| 56 |
<div |
| 57 |
className={css( |
| 58 |
styles.multipleFileUpload, |
| 59 |
isDragActive && styles.modifiers.dragOver, |
| 60 |
isHorizontal && styles.modifiers.horizontal, |
| 61 |
className |
| 62 |
)} |
| 63 |
{...rootProps} |
| 64 |
{...props} |
| 65 |
> |
| 66 |
<input |
| 67 |
/* hidden, necessary for react-dropzone */ |
| 68 |
{...getInputProps()} |
| 69 |
hidden |
| 70 |
/> |
| 71 |
{children} |
| 72 |
</div> |
| 73 |
</MultipleFileUploadContext.Provider> |
| 74 |
); |
| 75 |
}; |
| 76 |
|
| 77 |
MultipleFileUpload.displayName = 'MultipleFileUpload'; |