Skip to content
Merged
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: 9 additions & 1 deletion lib/DraggableCore.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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