From 4119eec796794d6a026f34f8b097b379eb9895a0 Mon Sep 17 00:00:00 2001 From: James Date: Wed, 29 Dec 2021 10:56:36 -0500 Subject: [PATCH] fix: cross-browser upload drag and drop --- .../views/collections/Edit/Upload/index.tsx | 25 +++++++------------ 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/src/admin/components/views/collections/Edit/Upload/index.tsx b/src/admin/components/views/collections/Edit/Upload/index.tsx index ca8f5ddf44..3ae1e9204b 100644 --- a/src/admin/components/views/collections/Edit/Upload/index.tsx +++ b/src/admin/components/views/collections/Edit/Upload/index.tsx @@ -27,7 +27,6 @@ const validate = (value) => { const Upload: React.FC = (props) => { const inputRef = useRef(null); const dropRef = useRef(null); - const [fileList, setFileList] = useState(undefined); const [selectingFile, setSelectingFile] = useState(false); const [dragging, setDragging] = useState(false); const [dragCounter, setDragCounter] = useState(0); @@ -53,16 +52,16 @@ const Upload: React.FC = (props) => { const handleDragIn = useCallback((e) => { e.preventDefault(); e.stopPropagation(); - setDragCounter(dragCounter + 1); + setDragCounter((count) => count + 1); if (e.dataTransfer.items && e.dataTransfer.items.length > 0) { setDragging(true); } - }, [dragCounter]); + }, []); const handleDragOut = useCallback((e) => { e.preventDefault(); e.stopPropagation(); - setDragCounter(dragCounter - 1); + setDragCounter((count) => count - 1); if (dragCounter > 1) return; setDragging(false); }, [dragCounter]); @@ -73,7 +72,7 @@ const Upload: React.FC = (props) => { setDragging(false); if (e.dataTransfer.files && e.dataTransfer.files.length > 0) { - setFileList(e.dataTransfer.files); + setValue(e.dataTransfer.files[0]); setDragging(false); e.dataTransfer.clearData(); @@ -81,11 +80,13 @@ const Upload: React.FC = (props) => { } else { setDragging(false); } - }, []); + }, [setValue]); + // Only called when input is interacted with directly + // Not called when drag + drop is used + // Or when input is cleared const handleInputChange = useCallback(() => { setSelectingFile(false); - setFileList(inputRef?.current?.files || null); setValue(inputRef?.current?.files?.[0] || null); }, [inputRef, setValue]); @@ -113,13 +114,7 @@ const Upload: React.FC = (props) => { } return () => null; - }, [handleDragIn, handleDragOut, handleDrop, dropRef]); - - useEffect(() => { - if (inputRef.current && fileList !== undefined) { - inputRef.current.files = fileList; - } - }, [fileList]); + }, [handleDragIn, handleDragOut, handleDrop, value]); useEffect(() => { setReplacingFile(false); @@ -143,7 +138,6 @@ const Upload: React.FC = (props) => { collection={collection} handleRemove={() => { setReplacingFile(true); - setFileList(null); setValue(null); }} /> @@ -163,7 +157,6 @@ const Upload: React.FC = (props) => { buttonStyle="icon-label" iconStyle="with-border" onClick={() => { - setFileList(null); setValue(null); }} />