react-dropzone 7.0.1 → 8.0.3

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/README.md CHANGED
@@ -33,36 +33,87 @@ yarn add react-dropzone
33
33
 
34
34
  ## Usage
35
35
 
36
- Import `Dropzone` in your React component:
37
-
38
36
  ```javascript static
37
+ import React from 'react'
38
+ import classNames from 'classnames'
39
39
  import Dropzone from 'react-dropzone'
40
+
41
+ class MyDropzone extends React.Component {
42
+ onDrop = (acceptedFiles, rejectedFiles) => {
43
+ // Do something with files
44
+ }
45
+
46
+ render() {
47
+ return (
48
+ <Dropzone onDrop={this.onDrop}>
49
+ {({getRootProps, getInputProps, isDragActive}) => {
50
+ return (
51
+ <div
52
+ {...getRootProps()}
53
+ className={classNames('dropzone', {'dropzone--isActive': isDragActive})}
54
+ >
55
+ <input {...getInputProps()} />
56
+ {
57
+ isDragActive ?
58
+ <p>Drop files here...</p> :
59
+ <p>Try dropping some files here, or click to select files to upload.</p>
60
+ }
61
+ </div>
62
+ )
63
+ }}
64
+ </Dropzone>
65
+ );
66
+ }
67
+ }
40
68
  ```
41
69
 
42
- and specify the `onDrop` method that accepts two arguments. The first argument represents the accepted files and the second argument the rejected files.
70
+ ## Render Prop Function
43
71
 
44
- ```javascript static
45
- function onDrop(acceptedFiles, rejectedFiles) {
46
- // do stuff with files...
47
- }
72
+ The render property function is what you use to render whatever you want to based on the state of `Dropzone`:
73
+ ```jsx static
74
+ <Dropzone>
75
+ {({getRootProps}) => <div {...getRootProps()} />}
76
+ </Dropzone>
48
77
  ```
49
78
 
50
- Files accepted or rejected based on `accept` prop. This must be a valid [MIME type](http://www.iana.org/assignments/media-types/media-types.xhtml) according to [input element specification](https://www.w3.org/wiki/HTML/Elements/input/file).
79
+ ### Prop Getters
80
+ See https://react-dropzone.netlify.com/#proptypes `{children}` for more info.
81
+
82
+ These functions are used to apply props to the elements that you render.
51
83
 
52
- Please note that `onDrop` method will always be called regardless if dropped file was accepted or rejected. The `onDropAccepted` method will be called if all dropped files were accepted and the `onDropRejected` method will be called if any of the dropped files was rejected.
84
+ This gives you maximum flexibility to render what, when, and wherever you like. You call these on the element in question (for example: `<div {...getRootProps()} />`).
53
85
 
54
- Using `react-dropzone` is similar to using a file form field, but instead of getting the `files` property from the field, you listen to the `onDrop` callback to handle the files. Simple explanation here: http://abandon.ie/notebook/simple-file-uploads-using-jquery-ajax
86
+ You should pass all your props to that function rather than applying them on the element yourself to avoid your props being overridden (or overriding the props returned).
87
+ E.g.
88
+ ```jsx static
89
+ <div
90
+ {...getRootProps({
91
+ onClick: evt => console.log(event)
92
+ })}
93
+ />
94
+ ```
55
95
 
56
- Specifying the `onDrop` method, provides you with an array of [Files](https://developer.mozilla.org/en-US/docs/Web/API/File) which you can then send to a server. For example, with [SuperAgent](https://github.com/visionmedia/superagent) as a http/ajax library:
96
+ ### State
97
+ See https://react-dropzone.netlify.com/#proptypes `{children}` for more info.
98
+
99
+ ### Custom refKey
100
+
101
+ Both `getRootProps` and `getInputProps` accept custom `refKey` (defaulted to `ref`) as one of the attributes passed down in the parameter.
57
102
 
58
103
  ```javascript static
59
- onDrop: acceptedFiles => {
60
- const req = request.post('/upload');
61
- acceptedFiles.forEach(file => {
62
- req.attach(file.name, file);
63
- });
64
- req.end(callback);
65
- }
104
+ const StyledDropArea = styled.div`
105
+ // Some styling here
106
+ `
107
+ const Example = () => (
108
+ <Dropzone>
109
+ {({ getRootProps, getInputProps }) => (
110
+ <StyledDropArea {...getRootProps({ refKey: 'innerRef' })}>
111
+ <input {...getInputProps()} />
112
+ <p>Drop some files here</p>
113
+ </StyledDropArea>
114
+ )}
115
+ </Dropzone>
116
+ );
66
117
  ```
67
118
 
68
119
  **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).
@@ -109,14 +160,14 @@ it('tests drag state', async () => {
109
160
  await flushPromises(dropzone)
110
161
  dropzone.update()
111
162
 
112
- const child = updatedDropzone.find(DummyChildComponent)
163
+ const child = dropzone.find(DummyChildComponent)
113
164
  expect(child).toHaveProp('isDragActive', true)
114
165
  expect(child).toHaveProp('isDragAccept', false)
115
166
  expect(child).toHaveProp('isDragReject', true)
116
167
  })
117
168
  ```
118
169
 
119
- Remember to update your mounted component before asserting any props. A complete example for this can be found in `react-dropzone`s own test suite.
170
+ Remember to update your mounted component before asserting any props. A complete example for this can be found in `react-dropzone`s own [test suite](https://github.com/react-dropzone/react-dropzone/blob/master/src/index.spec.js).
120
171
 
121
172
  ## Support
122
173