浏览代码

Merge pull request #284 from liqun98/feature/drag_import

import file by drag and drop
Aykut Saraç 2 年之前
父节点
当前提交
b8a86b1479
共有 1 个文件被更改,包括 16 次插入1 次删除
  1. 16 1
      src/containers/Modals/ImportModal/index.tsx

+ 16 - 1
src/containers/Modals/ImportModal/index.tsx

@@ -51,6 +51,18 @@ export const ImportModal: React.FC<ModalProps> = ({ visible, setVisible }) => {
     if (e.target.files) setJsonFile(e.target.files?.item(0));
     if (e.target.files) setJsonFile(e.target.files?.item(0));
   };
   };
 
 
+  const handleFileDrag = (e: React.DragEvent<HTMLLabelElement>) => {
+    e.preventDefault();
+
+    if (e.type === 'drop' && e.dataTransfer.files.length) {
+      if (e.dataTransfer.files[0].type === 'application/json') {
+        setJsonFile(e.dataTransfer.files[0])
+      } else {
+        toast.error('Please upload JSON file!')
+      }
+    }
+  }
+
   const handleImportFile = () => {
   const handleImportFile = () => {
     if (url) {
     if (url) {
       setJsonFile(null);
       setJsonFile(null);
@@ -88,7 +100,10 @@ export const ImportModal: React.FC<ModalProps> = ({ visible, setVisible }) => {
           placeholder="URL of JSON to fetch"
           placeholder="URL of JSON to fetch"
           autoFocus
           autoFocus
         />
         />
-        <StyledUploadWrapper>
+        <StyledUploadWrapper
+          onDrop={handleFileDrag}
+          onDragOver={handleFileDrag}
+        >
           <input
           <input
             key={jsonFile?.name}
             key={jsonFile?.name}
             onChange={handleFileChange}
             onChange={handleFileChange}