You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

пре 3 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. function OuterDropzone(props) {
  6. const {getRootProps} = useDropzone({
  7. // Note how this callback is never invoked if drop occurs on the inner dropzone
  8. onDrop: files => console.log(files)
  9. });
  10. return (
  11. <div className="container">
  12. <div {...getRootProps({className: 'dropzone'})}>
  13. <InnerDropzone />
  14. <p>Outer dropzone</p>
  15. </div>
  16. </div>
  17. );
  18. }
  19. function InnerDropzone(props) {
  20. const {getRootProps} = useDropzone({noDragEventsBubbling: true});
  21. return (
  22. <div {...getRootProps({className: 'dropzone'})}>
  23. <p>Inner dropzone</p>
  24. </div>
  25. );
  26. }
  27. <OuterDropzone />
  28. ```
  29. 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).
  30. If you'd like to selectively turn off the default dropzone behavior for `onClick`, use the `{noClick}` property:
  31. ```jsx harmony
  32. import React from 'react';
  33. import {useDropzone} from 'react-dropzone';
  34. function DropzoneWithoutClick(props) {
  35. const {getRootProps, getInputProps, acceptedFiles} = useDropzone({noClick: true});
  36. const files = acceptedFiles.map(file => <li key={file.path}>{file.path}</li>);
  37. return (
  38. <section className="container">
  39. <div {...getRootProps({className: 'dropzone'})}>
  40. <input {...getInputProps()} />
  41. <p>Dropzone without click events</p>
  42. </div>
  43. <aside>
  44. <h4>Files</h4>
  45. <ul>{files}</ul>
  46. </aside>
  47. </section>
  48. );
  49. }
  50. <DropzoneWithoutClick />
  51. ```
  52. If you'd like to selectively turn off the default dropzone behavior for `onKeyDown`, `onFocus` and `onBlur`, use the `{noKeyboard}` property:
  53. ```jsx harmony
  54. import React from 'react';
  55. import {useDropzone} from 'react-dropzone';
  56. function DropzoneWithoutKeyboard(props) {
  57. const {getRootProps, getInputProps, acceptedFiles} = useDropzone({noKeyboard: true});
  58. const files = acceptedFiles.map(file => <li key={file.path}>{file.path}</li>);
  59. return (
  60. <section className="container">
  61. <div {...getRootProps({className: 'dropzone'})}>
  62. <input {...getInputProps()} />
  63. <p>Dropzone without keyboard events</p>
  64. <em>(SPACE/ENTER and focus events are disabled)</em>
  65. </div>
  66. <aside>
  67. <h4>Files</h4>
  68. <ul>{files}</ul>
  69. </aside>
  70. </section>
  71. );
  72. }
  73. <DropzoneWithoutKeyboard />
  74. ```
  75. Note that you can prevent the default behavior for click and keyboard events if you ommit the input:
  76. ```jsx harmony
  77. import React from 'react';
  78. import {useDropzone} from 'react-dropzone';
  79. function DropzoneWithoutClick(props) {
  80. const {getRootProps, acceptedFiles} = useDropzone();
  81. const files = acceptedFiles.map(file => <li key={file.path}>{file.path}</li>);
  82. return (
  83. <section className="container">
  84. <div {...getRootProps({className: 'dropzone'})}>
  85. <p>Dropzone without click events</p>
  86. </div>
  87. <aside>
  88. <h4>Files</h4>
  89. <ul>{files}</ul>
  90. </aside>
  91. </section>
  92. );
  93. }
  94. <DropzoneWithoutClick />
  95. ```
  96. If you'd like to selectively turn off the default dropzone behavior for drag events, use the `{noDrag}` property:
  97. ```jsx harmony
  98. import React from 'react';
  99. import {useDropzone} from 'react-dropzone';
  100. function DropzoneWithoutDrag(props) {
  101. const {getRootProps, getInputProps, acceptedFiles} = useDropzone({noDrag: true});
  102. const files = acceptedFiles.map(file => <li key={file.path}>{file.path}</li>);
  103. return (
  104. <section className="container">
  105. <div {...getRootProps({className: 'dropzone'})}>
  106. <input {...getInputProps()} />
  107. <p>Dropzone with no drag events</p>
  108. <em>(Drag 'n' drop is disabled)</em>
  109. </div>
  110. <aside>
  111. <h4>Files</h4>
  112. <ul>{files}</ul>
  113. </aside>
  114. </section>
  115. );
  116. }
  117. <DropzoneWithoutDrag />
  118. ```
  119. Keep in mind that if you provide your own callback handlers as well and use `event.stopPropagation()`, it will prevent the default dropzone behavior:
  120. ```jsx harmony
  121. import React from 'react';
  122. import Dropzone from 'react-dropzone';
  123. // Note that there will be nothing logged when files are dropped
  124. <Dropzone onDrop={files => console.log(files)}>
  125. {({getRootProps, getInputProps}) => (
  126. <div className="container">
  127. <div
  128. {...getRootProps({
  129. className: 'dropzone',
  130. onDrop: event => event.stopPropagation()
  131. })}
  132. >
  133. <input {...getInputProps()} />
  134. <p>Drag 'n' drop some files here, or click to select files</p>
  135. </div>
  136. </div>
  137. )}
  138. </Dropzone>
  139. ```