-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add draggable file upload component (#690)
* feat: add draggable file upload component Signed-off-by: 4nalog <[email protected]> * chore: add support for listening to file changes Signed-off-by: 4nalog <[email protected]> * refactor: extensible file upload input Signed-off-by: 4nalog <[email protected]> * refactor: rename component Signed-off-by: 4nalog <[email protected]> * chore: fix file upload props Signed-off-by: 4nalog <[email protected]> * chore: add comments for file upload component options Signed-off-by: 4nalog <[email protected]> --------- Signed-off-by: 4nalog <[email protected]>
- Loading branch information
Showing
4 changed files
with
185 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
packages/console/src/components/common/FileUpload/FileItem.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { makeStyles, Theme } from '@material-ui/core'; | ||
import { Clear, Done } from '@material-ui/icons'; | ||
import React from 'react'; | ||
|
||
const useStyles = makeStyles((theme: Theme) => ({ | ||
container: { | ||
background: theme.palette.grey[100], | ||
display: 'flex', | ||
flexDirection: 'row', | ||
justifyContent: 'space-between', | ||
gap: '8px', | ||
alignItems: 'center', | ||
width: '100%', | ||
padding: theme.spacing(1, 0.5), | ||
fontSize: 12, | ||
}, | ||
icon: { | ||
color: theme.palette.success.main, | ||
}, | ||
fileName: { | ||
flex: 1, | ||
textAlign: 'left', | ||
}, | ||
})); | ||
|
||
interface Props { | ||
file: File; | ||
remove: () => void; | ||
} | ||
|
||
function FileItem({ file, remove }: Props) { | ||
const styles = useStyles(); | ||
|
||
return ( | ||
<div className={styles.container}> | ||
<Done className={styles.icon} /> | ||
<div className={styles.fileName}>{file.name}</div> | ||
<Clear | ||
onClick={e => { | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
|
||
remove(); | ||
}} | ||
/> | ||
</div> | ||
); | ||
} | ||
|
||
export default FileItem; |
85 changes: 85 additions & 0 deletions
85
packages/console/src/components/common/FileUpload/FileUpload.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { makeStyles, Theme } from '@material-ui/core'; | ||
import React from 'react'; | ||
import { DropzoneProps, useDropzone } from 'react-dropzone'; | ||
import FileItem from './FileItem'; | ||
|
||
const useStyles = makeStyles((theme: Theme) => ({ | ||
container: { | ||
margin: 'auto', | ||
maxWidth: '536px', | ||
color: theme.palette.grey[400], | ||
cursor: 'pointer', | ||
}, | ||
uploadContainer: { | ||
width: '100%', | ||
display: 'flex', | ||
flexDirection: 'column', | ||
justifyContent: 'center', | ||
alignItems: 'center', | ||
gap: theme.spacing(0.5), | ||
padding: theme.spacing(4, 3), | ||
border: `0.5px dashed ${theme.palette.divider}`, | ||
borderRadius: '4px', | ||
}, | ||
filesContainer: { | ||
width: '100%', | ||
}, | ||
highlight: { | ||
color: theme.palette.primary.main, | ||
}, | ||
})); | ||
|
||
/** | ||
* For a list of available options, see https://react-dropzone.js.org/ | ||
*/ | ||
export interface FileUploadProps extends DropzoneProps { | ||
files: File[]; | ||
setFiles: React.Dispatch<React.SetStateAction<File[]>>; | ||
helpText?: React.ReactNode; | ||
} | ||
|
||
export function FileUpload({ | ||
files, | ||
setFiles, | ||
helpText, | ||
...options | ||
}: FileUploadProps) { | ||
const styles = useStyles(); | ||
const { getRootProps, getInputProps } = useDropzone({ | ||
...options, | ||
onDrop: (acceptedFiles, fileRejections, event) => { | ||
setFiles(acceptedFiles); | ||
options?.onDrop?.(acceptedFiles, fileRejections, event); | ||
}, | ||
}); | ||
|
||
const removeFile = (fileIdx: number) => { | ||
setFiles(files => files.filter((_, idx) => idx !== fileIdx)); | ||
}; | ||
|
||
const ctaText = files.length ? 'Replace file' : 'Upload a file'; | ||
|
||
return ( | ||
<div className={styles.container}> | ||
<div {...getRootProps({ className: styles.uploadContainer })}> | ||
<div className={styles.filesContainer}> | ||
{files.map((file, idx) => ( | ||
<FileItem | ||
file={file} | ||
remove={() => removeFile(idx)} | ||
key={file.name} | ||
/> | ||
))} | ||
</div> | ||
<div> | ||
<span className={styles.highlight}>{ctaText}</span> or drag and drop | ||
here | ||
</div> | ||
{helpText} | ||
<input {...getInputProps()} /> | ||
</div> | ||
</div> | ||
); | ||
} | ||
|
||
export default FileUpload; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters