Skip to content

Commit 83f12b4

Browse files
committed
Merge pull request #8 from cjblomqvist/enhancements
Adding start position and onMouseDown support.
2 parents cc6b2f5 + 087b07a commit 83f12b4

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

lib/draggable.js

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
/** @jsx React.DOM */
44
var React = require('react/addons');
5-
var invariant = require('react/lib/invariant');
65
var emptyFunction = require('react/lib/emptyFunction');
76

87
function createUIEvent(draggable) {
@@ -144,6 +143,25 @@ module.exports = React.createClass({
144143
*/
145144
grid: React.PropTypes.arrayOf(React.PropTypes.number),
146145

146+
/**
147+
* `start` specifies the x and y that the dragged item should start at
148+
*
149+
* Example:
150+
*
151+
* ```jsx
152+
* var App = React.createClass({
153+
* render: function () {
154+
* return (
155+
* <Draggable start={{x: 25, y: 25}}>
156+
* <div>I start with left: 25px; top: 25px;</div>
157+
* </Draggable>
158+
* );
159+
* }
160+
* });
161+
* ```
162+
*/
163+
start: React.PropTypes.object,
164+
147165
/**
148166
* `zIndex` specifies the zIndex to use while dragging.
149167
*
@@ -221,7 +239,13 @@ module.exports = React.createClass({
221239
* }
222240
* ```
223241
*/
224-
onStop: React.PropTypes.func
242+
onStop: React.PropTypes.func,
243+
244+
/**
245+
* A workaround option which can be passed if onMouseDown needs to be accessed, since it'll always be blocked (due to that there's internal use of onMouseDown)
246+
*
247+
*/
248+
onMouseDown: React.PropTypes.func
225249
},
226250

227251
componentWillUnmount: function() {
@@ -236,10 +260,15 @@ module.exports = React.createClass({
236260
handle: null,
237261
cancel: null,
238262
grid: null,
263+
start: {
264+
x: 0,
265+
y: 0
266+
},
239267
zIndex: NaN,
240268
onStart: emptyFunction,
241269
onDrag: emptyFunction,
242-
onStop: emptyFunction
270+
onStop: emptyFunction,
271+
onMouseDown: emptyFunction
243272
};
244273
},
245274

@@ -255,11 +284,14 @@ module.exports = React.createClass({
255284
offsetX: 0, offsetY: 0,
256285

257286
// Current top/left of this.getDOMNode()
258-
clientX: 0, clientY: 0
287+
clientX: this.props.start.x, clientY: this.props.start.y
259288
};
260289
},
261290

262291
handleMouseDown: function (e) {
292+
// Make it possible to attach event handlers on top of this one
293+
this.props.onMouseDown(e);
294+
263295
var node = this.getDOMNode();
264296

265297
// Short circuit if handle or cancel prop was provided and selector doesn't match

0 commit comments

Comments
 (0)