Skip to content

Commit 0f78734

Browse files
committed
Move grid into <DraggableCore>, it's more useful there
1 parent 6c902d3 commit 0f78734

File tree

3 files changed

+31
-32
lines changed

3 files changed

+31
-32
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,6 @@ on itself.
185185

186186
* `axis`
187187
* `bounds`
188-
* `grid`
189188
* `start`
190189
* `zIndex`
191190

lib/Draggable.es6

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import ReactDOM from 'react-dom';
33
import classNames from 'classnames';
44
import assign from 'object-assign';
55
import {createUIEvent, createTransform} from './utils/domFns';
6-
import {canDragX, canDragY, getBoundPosition, snapToGrid} from './utils/positionFns';
6+
import {canDragX, canDragY, getBoundPosition} from './utils/positionFns';
77
import {dontSetMe} from './utils/shims';
88
import DraggableCore from './DraggableCore';
99
import log from './utils/log';
@@ -64,25 +64,6 @@ export default class Draggable extends DraggableCore {
6464
PropTypes.oneOf(['parent', false])
6565
]),
6666

67-
/**
68-
* `grid` specifies the x and y that dragging should snap to.
69-
*
70-
* Example:
71-
*
72-
* ```jsx
73-
* let App = React.createClass({
74-
* render: function () {
75-
* return (
76-
* <Draggable grid={[25, 25]}>
77-
* <div>I snap to a 25 x 25 grid</div>
78-
* </Draggable>
79-
* );
80-
* }
81-
* });
82-
* ```
83-
*/
84-
grid: PropTypes.arrayOf(PropTypes.number),
85-
8667
/**
8768
* `start` specifies the x and y that the dragged item should start at
8869
*
@@ -135,7 +116,6 @@ export default class Draggable extends DraggableCore {
135116
static defaultProps = assign({}, DraggableCore.defaultProps, {
136117
axis: 'both',
137118
bounds: false,
138-
grid: null,
139119
start: {x: 0, y: 0},
140120
zIndex: NaN
141121
});
@@ -184,15 +164,6 @@ export default class Draggable extends DraggableCore {
184164
clientY: this.state.clientY + coreEvent.position.deltaY
185165
};
186166

187-
// Snap to grid if prop has been provided
188-
if (Array.isArray(this.props.grid)) {
189-
newState.lastX = (this.state.lastX || newState.clientX) + coreEvent.position.deltaX;
190-
newState.lastY = (this.state.lastY || newState.clientY) + coreEvent.position.deltaY;
191-
// Eslint bug, it thinks newState.clientY is undefined
192-
/*eslint no-undef:0*/
193-
[newState.clientX, newState.clientY] = snapToGrid(this.props.grid, newState.lastX, newState.lastY);
194-
}
195-
196167
// Keep within bounds.
197168
if (this.props.bounds) {
198169
[newState.clientX, newState.clientY] = getBoundPosition(this, newState.clientX, newState.clientY);

lib/DraggableCore.es6

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {default as React, PropTypes} from 'react';
22
import {matchesSelector, createCoreEvent, addEvent, removeEvent, addUserSelectStyles,
33
removeUserSelectStyles, styleHacks} from './utils/domFns';
4-
import {getControlPosition} from './utils/positionFns';
4+
import {getControlPosition, snapToGrid} from './utils/positionFns';
55
import {dontSetMe} from './utils/shims';
66
import log from './utils/log';
77

@@ -69,6 +69,25 @@ export default class DraggableCore extends React.Component {
6969
*/
7070
enableUserSelectHack: PropTypes.bool,
7171

72+
/**
73+
* `grid` specifies the x and y that dragging should snap to.
74+
*
75+
* Example:
76+
*
77+
* ```jsx
78+
* let App = React.createClass({
79+
* render: function () {
80+
* return (
81+
* <Draggable grid={[25, 25]}>
82+
* <div>I snap to a 25 x 25 grid</div>
83+
* </Draggable>
84+
* );
85+
* }
86+
* });
87+
* ```
88+
*/
89+
grid: PropTypes.arrayOf(PropTypes.number),
90+
7291
/**
7392
* `handle` specifies a selector to be used as the handle that initiates drag.
7493
*
@@ -195,6 +214,7 @@ export default class DraggableCore extends React.Component {
195214
disabled: false,
196215
enableUserSelectHack: true,
197216
handle: null,
217+
grid: null,
198218
transform: null,
199219
onStart: function(){},
200220
onDrag: function(){},
@@ -285,10 +305,19 @@ export default class DraggableCore extends React.Component {
285305

286306
let {clientX, clientY} = getControlPosition(e);
287307

308+
// Snap to grid if prop has been provided
309+
if (Array.isArray(this.props.grid)) {
310+
let deltaX = clientX - this.state.lastX, deltaY = clientY - this.state.lastY;
311+
[deltaX, deltaY] = snapToGrid(this.props.grid, deltaX, deltaY);
312+
if (!deltaX && !deltaY) return; // skip useless drag
313+
clientX = this.state.lastX + deltaX, clientY = this.state.lastY + deltaY;
314+
}
315+
288316
let coreEvent = createCoreEvent(this, clientX, clientY);
289317

290318
log('DraggableCore: handleDrag: %j', coreEvent.position);
291319

320+
292321
// Call event handler. If it returns explicit false, trigger end.
293322
let shouldUpdate = this.props.onDrag(e, coreEvent);
294323
if (shouldUpdate === false) {

0 commit comments

Comments
 (0)