Skip to content

Commit 31798e9

Browse files
authored
grid prop unused in handleDragStop (#621)
* fix: `grid` prop unused in `handleDragStop` * call `onStop` even if `deltaX = 0` and `deltaY = 0` * fix `const {x, y}` in `handleDragStop`; add test case for `grid` prop
1 parent ffb4766 commit 31798e9

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

lib/DraggableCore.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,15 @@ export default class DraggableCore extends React.Component<DraggableCoreProps, D
383383

384384
const position = getControlPosition(e, this.state.touchIdentifier, this);
385385
if (position == null) return;
386-
const {x, y} = position;
386+
let {x, y} = position;
387+
388+
// Snap to grid if prop has been provided
389+
if (Array.isArray(this.props.grid)) {
390+
let deltaX = x - this.state.lastX, deltaY = y - this.state.lastY;
391+
[deltaX, deltaY] = snapToGrid(this.props.grid, deltaX, deltaY);
392+
x = this.state.lastX + deltaX, y = this.state.lastY + deltaY;
393+
}
394+
387395
const coreEvent = createCoreData(this, x, y);
388396

389397
// Call event handler

specs/draggable.spec.jsx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,33 @@ describe('react-draggable', function () {
942942
// (element, fromX, fromY, toX, toY)
943943
simulateMovementFromTo(drag, 0, 0, 100, 100);
944944
});
945+
946+
it('should call back with snapped data output when grid prop is provided', function(done) {
947+
function onDrag(event, data) {
948+
assert(data.x === 99);
949+
assert(data.y === 96);
950+
assert(data.deltaX === 99);
951+
assert(data.deltaY === 96);
952+
assert(data.node === ReactDOM.findDOMNode(drag));
953+
}
954+
function onStop(event, data) {
955+
assert(data.x === 99);
956+
assert(data.y === 96);
957+
// Single drag-and-stop so stop {x, y} is same as drag {x, y}.
958+
assert(data.deltaX === 0);
959+
assert(data.deltaY === 0);
960+
assert(data.node === ReactDOM.findDOMNode(drag));
961+
done();
962+
}
963+
drag = TestUtils.renderIntoDocument(
964+
<DraggableCore onDrag={onDrag} onStop={onStop} grid={[9, 16]}>
965+
<div />
966+
</DraggableCore>
967+
);
968+
969+
// (element, fromX, fromY, toX, toY)
970+
simulateMovementFromTo(drag, 0, 0, 100, 100);
971+
});
945972
});
946973

947974

0 commit comments

Comments
 (0)