From bf6166c6988cc9dc5d32390008f5ba21ba85fcdb Mon Sep 17 00:00:00 2001 From: Dan Head Date: Wed, 3 Feb 2021 20:17:05 +0000 Subject: [PATCH] feat(custom-dropzone): add file list and restrictions --- src/components/custom-dropzone.js | 64 ++++++++++++++++++++++++++----- 1 file changed, 54 insertions(+), 10 deletions(-) diff --git a/src/components/custom-dropzone.js b/src/components/custom-dropzone.js index def95f0..1161f4c 100644 --- a/src/components/custom-dropzone.js +++ b/src/components/custom-dropzone.js @@ -1,4 +1,4 @@ -import React, { useCallback } from "react"; +import React, { useCallback, useState } from "react"; import { useDropzone } from "react-dropzone"; import JSZip from "jszip"; import styled from "@emotion/styled"; @@ -7,8 +7,7 @@ import { Text } from "."; const Container = styled.div` border: 1px solid var(--color-dark); margin-bottom: var(--spacing-y-sm); - padding: var(--spacing-y-lg) var(--spacing-x-sm); - text-align: center; + padding: var(--spacing-y-sm) var(--spacing-x-sm) 0; &:focus { outline: 0; @@ -20,8 +19,26 @@ const Container = styled.div` } `; +const Message = styled.div` + border: 3px dashed var(--color-mid-grey); + padding: var(--spacing-y-md) var(--spacing-x-sm); + margin-bottom: var(--spacing-y-sm); + text-align: center; +`; + +const Files = styled.div` + margin-bottom: var(--spacing-y-sm); +`; + +const List = styled.ul` + margin: 0 0 var(--spacing-y-sm); + list-style: circle; +`; + export default function CustomDropzone({ name, files, setFiles, ...other }) { + const [fileList, setFileList] = useState([]); const onDrop = useCallback(acceptedFiles => { + setFileList(acceptedFiles.map(file => file.path)); if (acceptedFiles.length > 1) { const zip = new JSZip(); acceptedFiles.forEach(file => zip.file(file.name, file)); @@ -36,16 +53,43 @@ export default function CustomDropzone({ name, files, setFiles, ...other }) { setFiles(acceptedFiles[0]); } }, []); - const { getRootProps, getInputProps, isDragActive } = useDropzone({ onDrop }); + const { getRootProps, getInputProps, isDragActive } = useDropzone({ + accept: "image/jpeg, image/png", + maxFiles: 5, + onDrop + }); return ( - {isDragActive ? ( - Drop the images here - ) : ( - - Drop your images here, or click to select the files - + + {isDragActive ? ( + Drop the images here + ) : ( + <> + + Drop your images here, or click to select the files + + + (5 files maximum, *.jpg and *.png only) + + + )} + + {fileList.length > 0 && ( + + + Files added: + + + {fileList.map(file => ( +
  • + + {file} + +
  • + ))} +
    +
    )}
    );