Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missing file info when dragging native files #3262

Merged
merged 5 commits into from Feb 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions .yarn/versions/dba13c30.yml
@@ -0,0 +1,7 @@
releases:
react-dnd-examples: patch
react-dnd-html5-backend: patch
react-dnd-test-utils: patch

declined:
- react-dnd-documentation
22 changes: 19 additions & 3 deletions packages/backend-html5/src/HTML5BackendImpl.ts
Expand Up @@ -205,7 +205,11 @@ export class HTML5BackendImpl implements Backend {
true,
)
target.addEventListener('dragover', this.handleTopDragOver as EventListener)
target.addEventListener('dragover', this.handleTopDragOverCapture, true)
target.addEventListener(
'dragover',
this.handleTopDragOverCapture as EventListener,
true,
)
target.addEventListener('drop', this.handleTopDrop as EventListener)
target.addEventListener(
'drop',
Expand Down Expand Up @@ -244,7 +248,11 @@ export class HTML5BackendImpl implements Backend {
'dragover',
this.handleTopDragOver as EventListener,
)
target.removeEventListener('dragover', this.handleTopDragOverCapture, true)
target.removeEventListener(
'dragover',
this.handleTopDragOverCapture as EventListener,
true,
)
target.removeEventListener('drop', this.handleTopDrop as EventListener)
target.removeEventListener(
'drop',
Expand Down Expand Up @@ -527,6 +535,10 @@ export class HTML5BackendImpl implements Backend {
public handleTopDragEnterCapture = (e: DragEvent): void => {
this.dragEnterTargetIds = []

if (this.isDraggingNativeItem()) {
this.currentNativeSource?.loadDataTransfer(e.dataTransfer)
}

const isFirstEnter = this.enterLeaveCounter.enter(e.target)
if (!isFirstEnter || this.monitor.isDragging()) {
return
Expand Down Expand Up @@ -578,8 +590,12 @@ export class HTML5BackendImpl implements Backend {
}
}

public handleTopDragOverCapture = (): void => {
public handleTopDragOverCapture = (e: DragEvent): void => {
this.dragOverTargetIds = []

if (this.isDraggingNativeItem()) {
this.currentNativeSource?.loadDataTransfer(e.dataTransfer)
}
}

public handleDragOver(e: DragEvent, targetId: string): void {
Expand Down
22 changes: 18 additions & 4 deletions packages/examples/src/06-other/native-files/TargetBox.tsx
Expand Up @@ -24,10 +24,24 @@ export const TargetBox: FC<TargetBoxProps> = (props) => {
onDrop(item)
}
},
collect: (monitor: DropTargetMonitor) => ({
isOver: monitor.isOver(),
canDrop: monitor.canDrop(),
}),
canDrop(item: any) {
console.log('canDrop', item.files[0], item.items[0])
return true
},
hover(item: any) {
console.log('hover', item.files[0], item.items[0])
},
collect: (monitor: DropTargetMonitor) => {
const item = monitor.getItem() as any
if (item) {
console.log('collect', item.files[0], item.items[0])
}

return {
isOver: monitor.isOver(),
canDrop: monitor.canDrop(),
}
},
}),
[props],
)
Expand Down