Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import Dropzone from 'react-dropzone';
  4.  
  5. export default class DragDrop extends React.Component {
  6. dropRef = React.createRef()
  7.  
  8. state = {
  9. event: '',
  10. styleInEvent: { border: '1px solid red' },
  11. }
  12.  
  13. comaponentDidMount() {
  14. let div = this.dropRef.current;
  15. div.addEventListener('dragenter', this.handleDragIn);
  16. div.addEventListener('dragleave', this.handleDragOut);
  17. div.addEventListener('dragover', this.handleDrag);
  18. div.addEventListener('drop', this.handleDrop);
  19.  
  20. this.setState({ styleInEvent: { border: '1px solid blue' } });
  21. }
  22.  
  23. componAentWillUnmount() {
  24. let div = this.dropRef.current;
  25. div.removeEventListener('dragenter', this.handleDragIn);
  26. div.removeEventListener('dragleave', this.handleDragOut);
  27. div.removeEventListener('dragover', this.handleDrag);
  28. div.removeEventListener('drop', this.handleDrop);
  29.  
  30. this.setState({ styleInEvent: { border: '1px solid green' } });
  31. }
  32.  
  33. handleDrag = e => {
  34. e.preventDefault();
  35. e.stopPropagation();
  36. this.setState({ event: 'handleDrag' });
  37. console.log('handleDrag');
  38.  
  39. this.setState({ styleInEvent: { border: '1px solid yellow' } });
  40. }
  41.  
  42. handleDragIn = e => {
  43. e.preventDefault();
  44. e.stopPropagation();
  45.  
  46. this.setState({ event: 'handleDragIn' });
  47. console.log('handleDragIn');
  48.  
  49. this.setState({ styleInEvent: { border: '1px solid black' } });
  50. }
  51.  
  52. handleDragOut = e => {
  53. e.preventDefault();
  54. e.stopPropagation();
  55.  
  56. this.setState({ event: 'handleDragOut' });
  57. console.log('handleDragOut');
  58.  
  59. this.setState({ styleInEvent: { border: '1px solid red' } });
  60. }
  61.  
  62. handleDrop = e => {
  63. e.preventDefault();
  64. e.stopPropagation();
  65. console.log('handleDrop', e);
  66. if (e.dataTransfer.files && e.dataTransfer.files.length > 0) {
  67. console.log(e.dataTransfer.files);
  68. e.dataTransfer.clearData();
  69. this.dragCounter = 0;
  70. }
  71. this.setState({
  72. event: 'handleDrop',
  73. styleInEvent: { border: '1px solid #fff', color: 'blue' },
  74. });
  75. }
  76.  
  77. render() {
  78. const { event, styleInEvent } = this.state;
  79. const { children } = this.props;
  80. return (
  81. <div style={{ width: '900px', ...styleInEvent }}>
  82. <p>Media {event}</p>
  83. {children};
  84.  
  85. <Dropzone
  86. onDropAccepted={e => console.log(e)}
  87. multiple
  88. onDrop={this.onDrop}
  89. >
  90. {({ getRootProps, getInputProps, isDragActive }) => (
  91. <p></p>
  92. )}
  93. </Dropzone>
  94. </div>
  95. );
  96. }
  97. }
  98.  
  99. DragDrop.propTypes = {
  100. handleDrag: PropTypes.func,
  101. handleDragIn: PropTypes.func,
  102. handleDragOut: PropTypes.func,
  103. handleDrop: PropTypes.func,
  104. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement