Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 1 addition & 9 deletions lib/DraggableCore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -402,15 +402,7 @@ export default class DraggableCore extends React.Component<Partial<DraggableCore
// Call event handler. If it returns explicit false, trigger end.
const shouldUpdate = this.props.onDrag(e, coreEvent);
if (shouldUpdate === false || this.mounted === false) {
try {
this.handleDragStop(new MouseEvent('mouseup') as MouseTouchEvent);
} catch {
// Old browsers
const event = document.createEvent('MouseEvents') as unknown as MouseTouchEvent;
// I see why this insanity was deprecated
event.initMouseEvent('mouseup', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
this.handleDragStop(event);
}
this.handleDragStop(e);
return;
}

Expand Down
30 changes: 30 additions & 0 deletions test/DraggableCore.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,36 @@ describe('DraggableCore', () => {
});

describe('cancellation', () => {
it('preserves mouse coordinates when onDrag cancels', () => {
const onStop = vi.fn();
const { container } = render(
<DraggableCoreWrapper onDrag={() => false} onStop={onStop}>
<div />
</DraggableCoreWrapper>
);
startDrag(container.firstChild, { x: 10, y: 20 });
moveDrag({ x: 30, y: 50 });
expect(onStop.mock.calls[0][1]).toMatchObject({ x: 30, y: 50, deltaX: 20, deltaY: 30 });
});

it('stops touch movement when onDrag cancels', () => {
const onDrag = vi.fn(() => false);
const onStop = vi.fn();
const { container } = render(
<DraggableCoreWrapper onDrag={onDrag} onStop={onStop}>
<div />
</DraggableCoreWrapper>
);
container.firstChild.dispatchEvent(createTouchEvent('touchstart', { clientX: 10, clientY: 20 }));
document.dispatchEvent(createTouchEvent('touchmove', { clientX: 30, clientY: 50 }));
expect(onStop).toHaveBeenCalledTimes(1);
expect(onStop.mock.calls[0][1]).toMatchObject({ x: 30, y: 50, deltaX: 20, deltaY: 30 });
document.dispatchEvent(createTouchEvent('touchmove', { clientX: 40, clientY: 60 }));
expect(onDrag).toHaveBeenCalledTimes(1);
document.dispatchEvent(createTouchEvent('touchend', { clientX: 40, clientY: 60 }));
expect(onStop).toHaveBeenCalledTimes(1);
});

it('should cancel drag when onStart returns false', () => {
const onStart = vi.fn(() => false);
const onDrag = vi.fn();
Expand Down