react-use-file-upload
Advanced tools
Comparing version 0.4.0 to 0.4.1
@@ -46,3 +46,3 @@ "use strict"; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const setFiles = react_1.useCallback((e) => { | ||
const setFiles = react_1.useCallback((e, mode = 'w') => { | ||
var _a; | ||
@@ -59,5 +59,12 @@ let filesArr = []; | ||
} | ||
setFilesState(filesArr); | ||
handleSizes(filesArr); | ||
}, []); | ||
if (mode === 'w') { | ||
setFilesState(filesArr); | ||
handleSizes(filesArr); | ||
} | ||
else if (mode === 'a') { | ||
const _files = [...files, ...filesArr]; | ||
setFilesState(_files); | ||
handleSizes(_files); | ||
} | ||
}, [files]); | ||
/** @function handleSizes */ | ||
@@ -64,0 +71,0 @@ const handleSizes = react_1.useCallback((files) => { |
{ | ||
"name": "react-use-file-upload", | ||
"version": "0.4.0", | ||
"version": "0.4.1", | ||
"description": "A React Hook to make dealing with file uploading easier.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -73,6 +73,6 @@ # React useFileUpload | ||
A function that accepts the event emitted from the input field or drop zone, and creates an array of File objects. | ||
A function that accepts the event emitted from the input field or drop zone, and creates an array of File objects. The default mode is set to, `w`, which means it will write over previous files each time new ones are attached. If you want previously attached files to not be deleted each time a user attaches new files, then you can opt in by calling setFiles like this, `setFiles(e, 'a')`, where the `a` stands for append. If you want the default behavior, there is no need to pass a second argument to this function. | ||
``` | ||
(e: Event) => void | ||
(e: Event, mode = 'w') => void | ||
``` |
@@ -50,16 +50,25 @@ import { useState, useCallback, useEffect } from 'react'; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const setFiles = useCallback((e: any): void => { | ||
let filesArr: File[] = []; | ||
const setFiles = useCallback( | ||
(e: any, mode = 'w'): void => { | ||
let filesArr: File[] = []; | ||
if (e.currentTarget?.files) { | ||
filesArr = Array.from(e.currentTarget.files); | ||
} else if (e?.dataTransfer.files) { | ||
filesArr = Array.from(e.dataTransfer.files); | ||
} else { | ||
console.error('Argument not recognized. Are you sure your passing setFiles an event object?'); | ||
} | ||
if (e.currentTarget?.files) { | ||
filesArr = Array.from(e.currentTarget.files); | ||
} else if (e?.dataTransfer.files) { | ||
filesArr = Array.from(e.dataTransfer.files); | ||
} else { | ||
console.error('Argument not recognized. Are you sure your passing setFiles an event object?'); | ||
} | ||
setFilesState(filesArr); | ||
handleSizes(filesArr); | ||
}, []); | ||
if (mode === 'w') { | ||
setFilesState(filesArr); | ||
handleSizes(filesArr); | ||
} else if (mode === 'a') { | ||
const _files = [...files, ...filesArr]; | ||
setFilesState(_files); | ||
handleSizes(_files); | ||
} | ||
}, | ||
[files], | ||
); | ||
@@ -66,0 +75,0 @@ /** @function handleSizes */ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14125
276