Debian Codegraph @

node/react-dropzone/examples/events/README.md

raw
1 If you'd like to prevent drag events propagation from the child to parent, you can use the `{noDragEventsBubbling}` property on the child:
2 ```jsx harmony
3 import React from 'react';
4 import {useDropzone} from 'react-dropzone';
5
6 function OuterDropzone(props) {
7 const {getRootProps} = useDropzone({
8 // Note how this callback is never invoked if drop occurs on the inner dropzone
9 onDrop: files => console.log(files)
10 });
11
12 return (
13 <div className="container">
14 <div {...getRootProps({className: 'dropzone'})}>
15 <InnerDropzone />
16 <p>Outer dropzone</p>
17 </div>
18 </div>
19 );
20 }
21
22 function InnerDropzone(props) {
23 const {getRootProps} = useDropzone({noDragEventsBubbling: true});
24 return (
25 <div {...getRootProps({className: 'dropzone'})}>
26 <p>Inner dropzone</p>
27 </div>
28 );
29 }
30
31 <OuterDropzone />
32 ```
33
34 Note that internally we use `event.stopPropagation()` to achieve the behavior illustrated above, but this comes with its own [drawbacks](https://javascript.info/bubbling-and-capturing#stopping-bubbling).
35
36 If you'd like to selectively turn off the default dropzone behavior for `onClick`, use the `{noClick}` property:
37 ```jsx harmony
38 import React from 'react';
39 import {useDropzone} from 'react-dropzone';
40
41 function DropzoneWithoutClick(props) {
42 const {getRootProps, getInputProps, acceptedFiles} = useDropzone({noClick: true});
43 const files = acceptedFiles.map(file => <li key={file.path}>{file.path}</li>);
44
45 return (
46 <section className="container">
47 <div {...getRootProps({className: 'dropzone'})}>
48 <input {...getInputProps()} />
49 <p>Dropzone without click events</p>
50 </div>
51 <aside>
52 <h4>Files</h4>
53 <ul>{files}</ul>
54 </aside>
55 </section>
56 );
57 }
58
59 <DropzoneWithoutClick />
60 ```
61
62 If you'd like to selectively turn off the default dropzone behavior for `onKeyDown`, `onFocus` and `onBlur`, use the `{noKeyboard}` property:
63 ```jsx harmony
64 import React from 'react';
65 import {useDropzone} from 'react-dropzone';
66
67 function DropzoneWithoutKeyboard(props) {
68 const {getRootProps, getInputProps, acceptedFiles} = useDropzone({noKeyboard: true});
69 const files = acceptedFiles.map(file => <li key={file.path}>{file.path}</li>);
70
71 return (
72 <section className="container">
73 <div {...getRootProps({className: 'dropzone'})}>
74 <input {...getInputProps()} />
75 <p>Dropzone without keyboard events</p>
76 <em>(SPACE/ENTER and focus events are disabled)</em>
77 </div>
78 <aside>
79 <h4>Files</h4>
80 <ul>{files}</ul>
81 </aside>
82 </section>
83 );
84 }
85
86 <DropzoneWithoutKeyboard />
87 ```
88
89 Or you can prevent the default behavior for both click and keyboard events if you omit the input:
90 ```jsx harmony
91 import React from 'react';
92 import {useDropzone} from 'react-dropzone';
93
94 function DropzoneWithoutClick(props) {
95 const {getRootProps, acceptedFiles} = useDropzone();
96 const files = acceptedFiles.map(file => <li key={file.path}>{file.path}</li>);
97
98 return (
99 <section className="container">
100 <div {...getRootProps({className: 'dropzone'})}>
101 <p>Dropzone without click events</p>
102 </div>
103 <aside>
104 <h4>Files</h4>
105 <ul>{files}</ul>
106 </aside>
107 </section>
108 );
109 }
110
111 <DropzoneWithoutClick />
112 ```
113
114 **NOTE** If the browser supports the [File System Access API](https://developer.mozilla.org/en-US/docs/Web/API/File_System_Access_API) and you've set the `useFsAccessApi` to true, removing the `<input>` has no effect.
115
116 If you'd like to selectively turn off the default dropzone behavior for drag events, use the `{noDrag}` property:
117 ```jsx harmony
118 import React from 'react';
119 import {useDropzone} from 'react-dropzone';
120
121 function DropzoneWithoutDrag(props) {
122 const {getRootProps, getInputProps, acceptedFiles} = useDropzone({noDrag: true});
123 const files = acceptedFiles.map(file => <li key={file.path}>{file.path}</li>);
124
125 return (
126 <section className="container">
127 <div {...getRootProps({className: 'dropzone'})}>
128 <input {...getInputProps()} />
129 <p>Dropzone with no drag events</p>
130 <em>(Drag 'n' drop is disabled)</em>
131 </div>
132 <aside>
133 <h4>Files</h4>
134 <ul>{files}</ul>
135 </aside>
136 </section>
137 );
138 }
139
140 <DropzoneWithoutDrag />
141 ```
142
143 Keep in mind that if you provide your own callback handlers as well and use `event.stopPropagation()`, it will prevent the default dropzone behavior:
144 ```jsx harmony
145 import React from 'react';
146 import Dropzone from 'react-dropzone';
147
148 // Note that there will be nothing logged when files are dropped
149 <Dropzone onDrop={files => console.log(files)}>
150 {({getRootProps, getInputProps}) => (
151 <div className="container">
152 <div
153 {...getRootProps({
154 className: 'dropzone',
155 onDrop: event => event.stopPropagation()
156 })}
157 >
158 <input {...getInputProps()} />
159 <p>Drag 'n' drop some files here, or click to select files</p>
160 </div>
161 </div>
162 )}
163 </Dropzone>
164 ```