react-dropzone 11.5.3 → 12.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintignore +1 -3
- package/.eslintrc +2 -4
- package/.husky/commit-msg +5 -0
- package/.husky/pre-commit +5 -0
- package/.nvmrc +1 -1
- package/README.md +78 -29
- package/commitlint.config.js +1 -1
- package/dist/es/index.js +195 -151
- package/dist/es/utils/index.js +53 -17
- package/dist/index.js +16 -2
- package/examples/events/README.md +3 -1
- package/examples/no-jsx/README.md +32 -0
- package/package.json +80 -76
- package/rollup.config.js +22 -19
- package/src/.eslintrc +7 -4
- package/src/__snapshots__/index.spec.js.snap +1 -1
- package/src/index.js +372 -288
- package/src/index.spec.js +1837 -1369
- package/src/utils/index.js +117 -56
- package/src/utils/index.spec.js +414 -254
- package/styleguide.config.js +57 -50
- package/testSetup.js +1 -1
- package/typings/.eslintrc +28 -0
- package/typings/react-dropzone.d.ts +24 -14
- package/typings/tests/accept.tsx +10 -9
- package/typings/tests/all.tsx +6 -5
- package/typings/tests/basic.tsx +8 -7
- package/typings/tests/events.tsx +8 -6
- package/typings/tests/file-dialog.tsx +4 -3
- package/typings/tests/hook.tsx +6 -6
- package/typings/tests/plugin.tsx +11 -22
- package/typings/tests/refs.tsx +4 -4
- package/tslint.json +0 -101
package/.eslintignore
CHANGED
package/.eslintrc
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"parser": "babel-
|
|
2
|
+
"parser": "@babel/eslint-parser",
|
|
3
3
|
"parserOptions": {
|
|
4
4
|
"ecmaVersion": 2017,
|
|
5
5
|
"sourceType": "module"
|
|
@@ -15,10 +15,8 @@
|
|
|
15
15
|
"prettier"
|
|
16
16
|
],
|
|
17
17
|
"extends": [
|
|
18
|
-
// TODO: Fix this so we can use
|
|
19
|
-
// "okonet/node"
|
|
20
18
|
"eslint:recommended",
|
|
21
|
-
"prettier"
|
|
19
|
+
"plugin:prettier/recommended"
|
|
22
20
|
],
|
|
23
21
|
"rules": {
|
|
24
22
|
"strict": 0,
|
package/.nvmrc
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
14.17.0
|
package/README.md
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|

|
|
2
2
|
|
|
3
3
|
# react-dropzone
|
|
4
|
-
|
|
5
4
|
[](https://www.npmjs.com/package/react-dropzone)
|
|
6
|
-
[](https://github.com/react-dropzone/react-dropzone/actions?query=workflow%3ATest)
|
|
7
6
|
[](https://codecov.io/gh/react-dropzone/react-dropzone)
|
|
8
|
-
[](#backers)
|
|
9
|
-
[](#sponsors)
|
|
10
|
-
[](#backers)
|
|
8
|
+
[](#sponsors)
|
|
9
|
+
[](https://gitpod.io/#https://github.com/react-dropzone/react-dropzone)
|
|
10
|
+
[](https://github.com/react-dropzone/.github/blob/main/CODE_OF_CONDUCT.md)
|
|
11
11
|
|
|
12
12
|
Simple React hook to create a HTML5-compliant drag'n'drop zone for files.
|
|
13
13
|
|
|
@@ -15,7 +15,6 @@ Documentation and examples at https://react-dropzone.js.org. Source code at http
|
|
|
15
15
|
|
|
16
16
|
|
|
17
17
|
## Installation
|
|
18
|
-
|
|
19
18
|
Install it from npm and include it in your React build process (using [Webpack](http://webpack.github.io/), [Browserify](http://browserify.org/), etc).
|
|
20
19
|
|
|
21
20
|
```bash
|
|
@@ -53,8 +52,6 @@ function MyDropzone() {
|
|
|
53
52
|
}
|
|
54
53
|
```
|
|
55
54
|
|
|
56
|
-
**IMPORTANT**: Under the hood, this lib makes use of [hooks](https://reactjs.org/docs/hooks-intro.html), therefore, using it requires React `>= 16.8`.
|
|
57
|
-
|
|
58
55
|
Or the wrapper component for the hook:
|
|
59
56
|
```jsx static
|
|
60
57
|
import React from 'react'
|
|
@@ -72,10 +69,7 @@ import Dropzone from 'react-dropzone'
|
|
|
72
69
|
</Dropzone>
|
|
73
70
|
```
|
|
74
71
|
|
|
75
|
-
|
|
76
|
-
**Warning**: On most recent browsers versions, the files given by `onDrop` won't have properties `path` or `fullPath`, see [this SO question](https://stackoverflow.com/a/23005925/2275818) and [this issue](https://github.com/react-dropzone/react-dropzone/issues/477).
|
|
77
|
-
|
|
78
|
-
Furthermore, if you want to access file contents you have to use the [FileReader API](https://developer.mozilla.org/en-US/docs/Web/API/FileReader):
|
|
72
|
+
If you want to access file contents you have to use the [FileReader API](https://developer.mozilla.org/en-US/docs/Web/API/FileReader):
|
|
79
73
|
|
|
80
74
|
```jsx static
|
|
81
75
|
import React, {useCallback} from 'react'
|
|
@@ -110,7 +104,6 @@ function MyDropzone() {
|
|
|
110
104
|
|
|
111
105
|
|
|
112
106
|
## Dropzone Props Getters
|
|
113
|
-
|
|
114
107
|
The dropzone property getters are just two functions that return objects with properties which you need to use to create the drag 'n' drop zone.
|
|
115
108
|
The root properties can be applied to whatever element you want, whereas the input properties must be applied to an `<input>`:
|
|
116
109
|
```jsx static
|
|
@@ -134,11 +127,13 @@ This is in order to avoid your props being overridden (or overriding the props r
|
|
|
134
127
|
```jsx static
|
|
135
128
|
<div
|
|
136
129
|
{...getRootProps({
|
|
137
|
-
onClick: event => console.log(event)
|
|
130
|
+
onClick: event => console.log(event),
|
|
131
|
+
role: 'button',
|
|
132
|
+
'aria-label': 'drag and drop area',
|
|
133
|
+
...
|
|
138
134
|
})}
|
|
139
135
|
/>
|
|
140
136
|
```
|
|
141
|
-
> ♿ this is also where you pass accessibility props like `role`, `aria-labelledby` ...etc.
|
|
142
137
|
|
|
143
138
|
In the example above, the provided `{onClick}` handler will be invoked before the internal one, therefore, internal callbacks can be prevented by simply using [stopPropagation](https://developer.mozilla.org/en-US/docs/Web/API/Event/stopPropagation).
|
|
144
139
|
See [Events](https://react-dropzone.js.org#events) for more examples.
|
|
@@ -146,10 +141,9 @@ See [Events](https://react-dropzone.js.org#events) for more examples.
|
|
|
146
141
|
*Important*: if you omit rendering an `<input>` and/or binding the props from `getInputProps()`, opening a file dialog will not be possible.
|
|
147
142
|
|
|
148
143
|
## Refs
|
|
149
|
-
|
|
150
144
|
Both `getRootProps` and `getInputProps` accept a custom `refKey` (defaults to `ref`) as one of the attributes passed down in the parameter.
|
|
151
145
|
|
|
152
|
-
This can be useful when the element you're trying to apply the props from either one of those fns does not expose a reference to the element, e.g
|
|
146
|
+
This can be useful when the element you're trying to apply the props from either one of those fns does not expose a reference to the element, e.g:
|
|
153
147
|
|
|
154
148
|
```jsx static
|
|
155
149
|
import React from 'react'
|
|
@@ -170,7 +164,7 @@ function Example() {
|
|
|
170
164
|
}
|
|
171
165
|
```
|
|
172
166
|
|
|
173
|
-
If you're working with [Material UI](https://
|
|
167
|
+
If you're working with [Material UI v4](https://v4.mui.com/) and would like to apply the root props on some component that does not expose a ref, use [RootRef](https://v4.mui.com/api/root-ref/):
|
|
174
168
|
|
|
175
169
|
```jsx static
|
|
176
170
|
import React from 'react'
|
|
@@ -190,7 +184,7 @@ function PaperDropzone() {
|
|
|
190
184
|
}
|
|
191
185
|
```
|
|
192
186
|
|
|
193
|
-
|
|
187
|
+
**IMPORTANT**: do not set the `ref` prop on the elements where `getRootProps()`/`getInputProps()` props are set, instead, get the refs from the hook itself:
|
|
194
188
|
|
|
195
189
|
```jsx static
|
|
196
190
|
import React from 'react'
|
|
@@ -232,12 +226,11 @@ dropzoneRef.open()
|
|
|
232
226
|
|
|
233
227
|
|
|
234
228
|
## Testing
|
|
235
|
-
|
|
236
|
-
*Important*: `react-dropzone` makes some of its drag 'n' drop callbacks asynchronous to enable promise based `getFilesFromEvent()` functions. In order to test components that use this library, you may want to use the [react-testing-library](https://github.com/testing-library/react-testing-library):
|
|
229
|
+
`react-dropzone` makes some of its drag 'n' drop callbacks asynchronous to enable promise based `getFilesFromEvent()` functions. In order to test components that use this library, you need to use the [react-testing-library](https://github.com/testing-library/react-testing-library):
|
|
237
230
|
```js static
|
|
238
231
|
import React from 'react'
|
|
239
232
|
import Dropzone from 'react-dropzone'
|
|
240
|
-
import {
|
|
233
|
+
import {act, fireEvent, render, waitFor} from '@testing-library/react'
|
|
241
234
|
|
|
242
235
|
test('invoke onDragEnter when dragenter event occurs', async () => {
|
|
243
236
|
const file = new File([
|
|
@@ -289,22 +282,79 @@ function mockData(files) {
|
|
|
289
282
|
}
|
|
290
283
|
```
|
|
291
284
|
|
|
292
|
-
|
|
285
|
+
**NOTE**: using [Enzyme](https://airbnb.io/enzyme) for testing is not supported at the moment, see [#2011](https://github.com/airbnb/enzyme/issues/2011).
|
|
293
286
|
|
|
294
|
-
More examples for this can be found in `react-dropzone`s own [test suites](https://github.com/react-dropzone/react-dropzone/blob/master/src/index.spec.js).
|
|
287
|
+
More examples for this can be found in `react-dropzone`'s own [test suites](https://github.com/react-dropzone/react-dropzone/blob/master/src/index.spec.js).
|
|
295
288
|
|
|
296
|
-
##
|
|
289
|
+
## Caveats
|
|
290
|
+
### Required React Version
|
|
291
|
+
React [16.8](https://reactjs.org/blog/2019/02/06/react-v16.8.0.html) or above is required because we use [hooks](https://reactjs.org/docs/hooks-intro.html) (the lib itself is a hook).
|
|
297
292
|
|
|
293
|
+
### File Paths
|
|
294
|
+
Files returned by the hook or passed as arg to the `onDrop` cb won't have the properties `path` or `fullPath`.
|
|
295
|
+
For more inf check [this SO question](https://stackoverflow.com/a/23005925/2275818) and [this issue](https://github.com/react-dropzone/react-dropzone/issues/477).
|
|
298
296
|
|
|
297
|
+
### Not a File Uploader
|
|
298
|
+
This lib is not a file uploader; as such, it does not process files or provide any way to make HTTP requests to some server; if you're looking for that, checkout [filepond](https://pqina.nl/filepond) or [uppy.io](https://uppy.io/).
|
|
299
299
|
|
|
300
|
-
|
|
300
|
+
### Using \<label\> as Root
|
|
301
|
+
If you use [\<label\>](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/label) as the root element, the file dialog will be opened twice; see [#1107](https://github.com/react-dropzone/react-dropzone/issues/1107) why. To avoid this, use `noClick`:
|
|
302
|
+
```jsx static
|
|
303
|
+
import React, {useCallback} from 'react'
|
|
304
|
+
import {useDropzone} from 'react-dropzone'
|
|
301
305
|
|
|
302
|
-
|
|
306
|
+
function MyDropzone() {
|
|
307
|
+
const {getRootProps, getInputProps} = useDropzone({noClick: true})
|
|
303
308
|
|
|
304
|
-
|
|
309
|
+
return (
|
|
310
|
+
<label {...getRootProps()}>
|
|
311
|
+
<input {...getInputProps()} />
|
|
312
|
+
</label>
|
|
313
|
+
)
|
|
314
|
+
}
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
### Using open() on Click
|
|
318
|
+
If you bind a click event on an inner element and use `open()`, it will trigger a click on the root element too, resulting in the file dialog opening twice. To prevent this, use the `noClick` on the root:
|
|
319
|
+
```jsx static
|
|
320
|
+
import React, {useCallback} from 'react'
|
|
321
|
+
import {useDropzone} from 'react-dropzone'
|
|
305
322
|
|
|
323
|
+
function MyDropzone() {
|
|
324
|
+
const {getRootProps, getInputProps, open} = useDropzone({noClick: true})
|
|
325
|
+
|
|
326
|
+
return (
|
|
327
|
+
<div {...getRootProps()}>
|
|
328
|
+
<input {...getInputProps()} />
|
|
329
|
+
<button type="button" onClick={open}>
|
|
330
|
+
Open
|
|
331
|
+
</button>
|
|
332
|
+
</div>
|
|
333
|
+
)
|
|
334
|
+
}
|
|
335
|
+
```
|
|
336
|
+
|
|
337
|
+
### File Dialog Cancel Callback
|
|
338
|
+
The `onFileDialogCancel()` cb is unstable in most browsers, meaning, there's a good chance of it being triggered even though you have selected files.
|
|
339
|
+
|
|
340
|
+
We rely on using a timeout of `300ms` after the window is focused (the window `onfocus` event is triggered when the file select dialog is closed) to check if any files were selected and trigger `onFileDialogCancel` if none were selected.
|
|
341
|
+
|
|
342
|
+
As one can imagine, this doesn't really work if there's a lot of files or large files as by the time we trigger the check, the browser is still processing the files and no `onchange` events are triggered yet on the input. Check [#1031](https://github.com/react-dropzone/react-dropzone/issues/1031) for more info.
|
|
343
|
+
|
|
344
|
+
Fortunately, there's the [File System Access API](https://developer.mozilla.org/en-US/docs/Web/API/File_System_Access_API), which is currently a working draft and some browsers support it (see [browser compatibility](https://developer.mozilla.org/en-US/docs/Web/API/window/showOpenFilePicker#browser_compatibility)), that provides a reliable way to prompt the user for file selection and capture cancellation.
|
|
345
|
+
|
|
346
|
+
And this lib makes use of it if available. Though, there's a small catch: using file extensions for the `accept` property is not supported; you must use MIME types as described in [common MIME types](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types). Also check [accepting specific file types](https://react-dropzone.js.org/#section-accepting-specific-file-types) for more info on the subject of `accept` limitations.
|
|
347
|
+
|
|
348
|
+
## Supported Browsers
|
|
306
349
|
We use [browserslist](https://github.com/browserslist/browserslist) config to state the browser support for this lib, so check it out on [browserslist.dev](https://browserslist.dev/?q=ZGVmYXVsdHM%3D).
|
|
307
350
|
|
|
351
|
+
|
|
352
|
+
## Need image editing?
|
|
353
|
+
React Dropzone integrates perfectly with [Pintura Image Editor](https://pqina.nl/pintura/?ref=react-dropzone), creating a modern image editing experience. Pintura supports crop aspect ratios, resizing, rotating, cropping, annotating, filtering, and much more.
|
|
354
|
+
|
|
355
|
+
Checkout the [Pintura integration example](https://codesandbox.io/s/react-dropzone-pintura-40xh4?file=/src/App.js).
|
|
356
|
+
|
|
357
|
+
|
|
308
358
|
## Support
|
|
309
359
|
|
|
310
360
|
### Backers
|
|
@@ -378,5 +428,4 @@ Become a sponsor and get your logo on our README on Github with a link to your s
|
|
|
378
428
|
|
|
379
429
|
|
|
380
430
|
## License
|
|
381
|
-
|
|
382
431
|
MIT
|
package/commitlint.config.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
module.exports = { extends: [
|
|
1
|
+
module.exports = { extends: ["@commitlint/config-angular"] };
|