| 1 |
The hook accepts a `getFilesFromEvent` prop that enhances the handling of dropped file system objects and allows more flexible use of them e.g. passing a function that accepts drop event of a folder and resolves it to an array of files adds plug-in functionality of folders drag-and-drop. |
| 2 |
|
| 3 |
Though, note that the provided `getFilesFromEvent` fn must return a `Promise` with a list of `File` objects (or `DataTransferItem` of `{kind: 'file'}` when data cannot be accessed). |
| 4 |
Otherwise, the results will be discarded and none of the drag events callbacks will be invoked. |
| 5 |
|
| 6 |
In case you need to extend the `File` with some additional properties, you should use [Object.defineProperty()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty) so that the result will still pass through our filter: |
| 7 |
|
| 8 |
```jsx harmony |
| 9 |
import React from 'react'; |
| 10 |
import {useDropzone} from 'react-dropzone'; |
| 11 |
|
| 12 |
function Plugin(props) { |
| 13 |
const {acceptedFiles, getRootProps, getInputProps} = useDropzone({ |
| 14 |
getFilesFromEvent: event => myCustomFileGetter(event) |
| 15 |
}); |
| 16 |
|
| 17 |
const files = acceptedFiles.map(f => ( |
| 18 |
<li key={f.name}> |
| 19 |
{f.name} has <strong>myProps</strong>: {f.myProp === true ? 'YES' : ''} |
| 20 |
</li> |
| 21 |
)); |
| 22 |
|
| 23 |
return ( |
| 24 |
<section className="container"> |
| 25 |
<div {...getRootProps({className: 'dropzone'})}> |
| 26 |
<input {...getInputProps()} /> |
| 27 |
<p>Drag 'n' drop some files here, or click to select files</p> |
| 28 |
</div> |
| 29 |
<aside> |
| 30 |
<h4>Files</h4> |
| 31 |
<ul>{files}</ul> |
| 32 |
</aside> |
| 33 |
</section> |
| 34 |
); |
| 35 |
} |
| 36 |
|
| 37 |
async function myCustomFileGetter(event) { |
| 38 |
const files = []; |
| 39 |
const fileList = event.dataTransfer ? event.dataTransfer.files : event.target.files; |
| 40 |
|
| 41 |
for (var i = 0; i < fileList.length; i++) { |
| 42 |
const file = fileList.item(i); |
| 43 |
|
| 44 |
Object.defineProperty(file, 'myProp', { |
| 45 |
value: true |
| 46 |
}); |
| 47 |
|
| 48 |
files.push(file); |
| 49 |
} |
| 50 |
|
| 51 |
return files; |
| 52 |
} |
| 53 |
|
| 54 |
<Plugin /> |
| 55 |
``` |