From 3aae4a92eab7bb07a2fdf639d7273e7812225421 Mon Sep 17 00:00:00 2001 From: Idan Entin Date: Mon, 2 Dec 2019 17:28:16 +0200 Subject: [PATCH] fix(reset-drag): `resetState` and then dragging positions wrong When using `resetState` and then dragging the position was off. --- src/lib/Draggable.jsx | 3 ++- src/lib/draggable.test.js | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/lib/Draggable.jsx b/src/lib/Draggable.jsx index c445a25..bf9556c 100644 --- a/src/lib/Draggable.jsx +++ b/src/lib/Draggable.jsx @@ -148,7 +148,8 @@ export function useDraggable({ const resetState = useCallback(() => { setDelta({ x: 0, y: 0 }); - }, [setDelta]); + setPrev({ x: 0, y: 0 }); + }, [setDelta, setPrev]); return { targetRef, handleRef, getTargetProps, dragging, delta, resetState }; } diff --git a/src/lib/draggable.test.js b/src/lib/draggable.test.js index af8e380..381579a 100644 --- a/src/lib/draggable.test.js +++ b/src/lib/draggable.test.js @@ -285,6 +285,18 @@ describe('draggable', () => { 'translate(0px, 0px)' ); }); + + describe('after reset', () => { + it('should start dragging from the original position', () => { + const { getByText, getByTestId, drag } = utils; + drag({ start: { clientX: 3, clientY: 5 }, delta: { x: 15, y: 20 } }); + fireEvent.click(getByText(/reset/i)); + drag({ start: { clientX: 3, clientY: 5 }, delta: { x: 15, y: 20 } }); + expect(getByTestId('main').style.transform).to.equal( + 'translate(15px, 20px)' + ); + }); + }); }); function Consumer(props) {