Skip to content

Commit b28566a

Browse files
committed
Cancel dragging if callbacks return boolean false.
1 parent 968354b commit b28566a

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

lib/draggable.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ module.exports = React.createClass({
276276

277277
/**
278278
* Called when dragging starts.
279+
* If this function returns the boolean false, dragging will be canceled.
279280
*
280281
* Example:
281282
*
@@ -296,6 +297,7 @@ module.exports = React.createClass({
296297

297298
/**
298299
* Called while dragging.
300+
* If this function returns the boolean false, dragging will be canceled.
299301
*
300302
* Example:
301303
*
@@ -393,12 +395,16 @@ module.exports = React.createClass({
393395
return;
394396
}
395397

398+
// Call event handler. If it returns explicit false, cancel.
399+
var shouldStart = this.props.onStart(e, createUIEvent(this));
400+
if (shouldStart === false) return;
401+
402+
var dragPoint = getControlPosition(e);
403+
396404
// Add a style to the body to disable user-select. This prevents text from
397405
// being selected all over the page.
398406
addUserSelectStyles(this);
399407

400-
var dragPoint = getControlPosition(e);
401-
402408
// Initiate dragging. Set the current x and y as offsets
403409
// so we know how much we've moved during the drag. This allows us
404410
// to drag elements around even if they have been moved, without issue.
@@ -408,8 +414,6 @@ module.exports = React.createClass({
408414
offsetY: dragPoint.clientY - this.state.clientY
409415
});
410416

411-
// Call event handler
412-
this.props.onStart(e, createUIEvent(this));
413417

414418
// Add event handlers
415419
addEvent(window, dragEventFor['move'], this.handleDrag);
@@ -450,14 +454,15 @@ module.exports = React.createClass({
450454
clientX = coords[0], clientY = coords[1];
451455
}
452456

457+
// Call event handler. If it returns explicit false, cancel.
458+
var shouldUpdate = this.props.onDrag(e, createUIEvent(this));
459+
if (shouldUpdate === false) return this.handleDragEnd();
460+
453461
// Update transform
454462
this.setState({
455463
clientX: clientX,
456464
clientY: clientY
457465
});
458-
459-
// Call event handler
460-
this.props.onDrag(e, createUIEvent(this));
461466
},
462467

463468
render: function () {

0 commit comments

Comments
 (0)