Skip to content

Commit

Permalink
grid prop unused in handleDragStop (#621)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
BrianHung committed Jan 27, 2022
1 parent ffb4766 commit 31798e9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/DraggableCore.js
Expand Up @@ -383,7 +383,15 @@ export default class DraggableCore extends React.Component<DraggableCoreProps, D

const position = getControlPosition(e, this.state.touchIdentifier, this);
if (position == null) return;
const {x, y} = position;
let {x, y} = position;

// Snap to grid if prop has been provided
if (Array.isArray(this.props.grid)) {
let deltaX = x - this.state.lastX, deltaY = y - this.state.lastY;
[deltaX, deltaY] = snapToGrid(this.props.grid, deltaX, deltaY);
x = this.state.lastX + deltaX, y = this.state.lastY + deltaY;
}

const coreEvent = createCoreData(this, x, y);

// Call event handler
Expand Down
27 changes: 27 additions & 0 deletions specs/draggable.spec.jsx
Expand Up @@ -942,6 +942,33 @@ describe('react-draggable', function () {
// (element, fromX, fromY, toX, toY)
simulateMovementFromTo(drag, 0, 0, 100, 100);
});

it('should call back with snapped data output when grid prop is provided', function(done) {
function onDrag(event, data) {
assert(data.x === 99);
assert(data.y === 96);
assert(data.deltaX === 99);
assert(data.deltaY === 96);
assert(data.node === ReactDOM.findDOMNode(drag));
}
function onStop(event, data) {
assert(data.x === 99);
assert(data.y === 96);
// Single drag-and-stop so stop {x, y} is same as drag {x, y}.
assert(data.deltaX === 0);
assert(data.deltaY === 0);
assert(data.node === ReactDOM.findDOMNode(drag));
done();
}
drag = TestUtils.renderIntoDocument(
<DraggableCore onDrag={onDrag} onStop={onStop} grid={[9, 16]}>
<div />
</DraggableCore>
);

// (element, fromX, fromY, toX, toY)
simulateMovementFromTo(drag, 0, 0, 100, 100);
});
});


Expand Down

0 comments on commit 31798e9

Please sign in to comment.