Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: fix event propagation and close #686
- persist event in callbacks where the event is passed down the custom callbacks - do not call onDrop, onDragEnter callbacks if event propagation was stopped from a child
- Loading branch information
1 parent
14e6b67
commit fd65863
Showing
4 changed files
with
153 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
In case you need to nest dropzone components and prevent any drag events from the child propagate to the parent, it can easily be achieved by using `.stopPropagation()` in the child dropzone. | ||
|
||
```jsx harmony | ||
const parentStyle = { | ||
width: 200, | ||
height: 200, | ||
border: '2px dashed #888' | ||
} | ||
|
||
const childStyle = { | ||
width: 160, | ||
height: 160, | ||
margin: 20, | ||
border: '2px dashed #ccc' | ||
} | ||
|
||
class NestedDropzone extends React.Component { | ||
constructor() { | ||
super() | ||
this.state = { | ||
parent: {}, | ||
child: {} | ||
} | ||
} | ||
|
||
createDragHandler(eventType, node) { | ||
const updater = this.createStateUpdater(eventType, node) | ||
return (evt) => { | ||
evt.preventDefault(); | ||
if (node === 'child') { | ||
evt.stopPropagation() | ||
} | ||
this.setState(updater) | ||
} | ||
} | ||
|
||
createDropHandler(node) { | ||
const updater = this.createStateUpdater('drop', node) | ||
return (accepted, rejected, evt) => { | ||
evt.preventDefault(); | ||
if (node === 'child') { | ||
evt.stopPropagation() | ||
} | ||
this.setState(updater) | ||
} | ||
} | ||
|
||
createStateUpdater(eventType, node) { | ||
return state => { | ||
const events = {...state[node]}; | ||
if (eventType !== events.current) { | ||
events.previous = events.current; | ||
} | ||
events.current = eventType; | ||
return { | ||
[node]: events | ||
} | ||
} | ||
} | ||
|
||
render() { | ||
return ( | ||
<section> | ||
<div className="dropzone"> | ||
<Dropzone | ||
onDragStart={this.createDragHandler('dragstart', 'parent')} | ||
onDragEnter={this.createDragHandler('dragenter', 'parent')} | ||
onDragOver={this.createDragHandler('dragover', 'parent')} | ||
onDragLeave={this.createDragHandler('dragleave', 'parent')} | ||
onDrop={this.createDropHandler('parent')} | ||
style={parentStyle} | ||
> | ||
<Dropzone | ||
onDragStart={this.createDragHandler('dragstart', 'child')} | ||
onDragEnter={this.createDragHandler('dragenter', 'child')} | ||
onDragOver={this.createDragHandler('dragover', 'child')} | ||
onDragLeave={this.createDragHandler('dragleave', 'child')} | ||
onDrop={this.createDropHandler('child')} | ||
style={childStyle} | ||
/> | ||
</Dropzone> | ||
</div> | ||
<aside> | ||
<p>Parent: {JSON.stringify(this.state.parent)}</p> | ||
<p>Child: {JSON.stringify(this.state.child)}</p> | ||
</aside> | ||
</section> | ||
); | ||
} | ||
} | ||
|
||
<NestedDropzone /> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters