Skip to content

Commit 06dca5b

Browse files
authored
fix: call clearData in useDrag onDragStart (#8683)
* useDrag: clearData is onDragStart before writeToDataTransfer * optional chaining to avoid test failures without mock
1 parent f652637 commit 06dca5b

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

packages/@react-aria/dnd/src/useDrag.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ export function useDrag(options: DragOptions): DragResult {
116116
}
117117

118118
let items = options.getItems();
119+
// Clear existing data (e.g. selected text on the page would be included in some browsers)
120+
e.dataTransfer.clearData?.();
119121
writeToDataTransfer(e.dataTransfer, items);
120122

121123
let allowed = DROP_OPERATION.all;

packages/@react-aria/dnd/test/mocks.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,14 @@ export class DataTransfer {
125125
getData(type) {
126126
return this.items._items.find(item => item.kind === 'string' && item.type === type)?._data;
127127
}
128+
129+
clearData(type) {
130+
if (type) {
131+
this.items._items = this.items._items.filter(item => item.type !== type);
132+
} else {
133+
this.items._items = [];
134+
}
135+
}
128136
}
129137

130138
export class DragEvent extends MouseEvent {

0 commit comments

Comments
 (0)