Skip to content

Commit 370adbc

Browse files
committed
Add moveOnStartChange prop
1 parent 30a8f5e commit 370adbc

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ var App = React.createClass({
8484
// to use (you can use absolute or relative positioning of the child directly), but can be helpful
8585
// for uniformity in your callbacks and with css transforms.
8686
//
87+
// `moveOnStartChange`, if true (default false), will move the element if there is a change in `start`.
88+
// We set this by default to `false` because it can cause unwanted effects if you are not aware of it.
89+
//
8790
// `zIndex` specifies the zIndex to use while dragging.
8891
//
8992
// `onStart` is called when dragging starts.
@@ -95,6 +98,8 @@ var App = React.createClass({
9598
<Draggable
9699
axis="x"
97100
handle=".handle"
101+
start={{x: 0, y: 0}}
102+
moveOnStartChange={false}
98103
grid={[25, 25]}
99104
zIndex={100}
100105
onStart={this.handleStart}

lib/draggable.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,13 @@ module.exports = React.createClass({
386386
y: React.PropTypes.number
387387
}),
388388

389+
/**
390+
* `moveOnStartChange`, if true (default false) will move the element if the `start`
391+
* property changes.
392+
*/
393+
moveOnStartChange: React.PropTypes.bool,
394+
395+
389396
/**
390397
* `zIndex` specifies the zIndex to use while dragging.
391398
*
@@ -474,6 +481,14 @@ module.exports = React.createClass({
474481
onMouseDown: React.PropTypes.func,
475482
},
476483

484+
componentWillReceiveProps: function(newProps) {
485+
// React to changes in the 'start' param.
486+
if (newProps.moveOnStartChange && newProps.start &&
487+
(newProps.start.x !== this.state.initialStart.x || newProps.start.y !== this.state.initialStart.y)) {
488+
this.setState(this.getInitialState(newProps));
489+
}
490+
},
491+
477492
componentWillUnmount: function() {
478493
// Remove any leftover event handlers
479494
removeEvent(window, dragEventFor['move'], this.handleDrag);
@@ -488,6 +503,7 @@ module.exports = React.createClass({
488503
handle: null,
489504
cancel: null,
490505
grid: null,
506+
moveOnStartChange: false,
491507
start: {x: 0, y: 0},
492508
zIndex: NaN,
493509
enableUserSelectHack: true,
@@ -498,7 +514,9 @@ module.exports = React.createClass({
498514
};
499515
},
500516

501-
getInitialState: function () {
517+
getInitialState: function (props) {
518+
// Handle call from CWRP
519+
props = props || this.props;
502520
return {
503521
// Whether or not we are currently dragging.
504522
dragging: false,
@@ -507,7 +525,9 @@ module.exports = React.createClass({
507525
offsetX: 0, offsetY: 0,
508526

509527
// Current transform x and y.
510-
clientX: this.props.start.x, clientY: this.props.start.y
528+
clientX: props.start.x, clientY: props.start.y,
529+
530+
initialStart: props.start
511531
};
512532
},
513533

0 commit comments

Comments
 (0)