1- 'use strict' ;
2-
31import React from 'react' ;
42import classNames from 'classnames' ;
53import assign from 'object-assign' ;
64import { autobind } from './utils/shims' ;
75import { createUIEvent , createCSSTransform } from './utils/domFns' ;
86import { canDragX , canDragY , getBoundPosition , snapToGrid } from './utils/positionFns' ;
97import DraggableCore from './DraggableCore' ;
8+ import log from './utils/log' ;
109
1110//
1211// Define <Draggable>
@@ -27,10 +26,10 @@ export default class Draggable extends DraggableCore {
2726 }
2827
2928 onDragStart ( e , coreEvent ) {
30- console . log ( 'Draggable: onDragStart: %j' , coreEvent . position ) ;
29+ log ( 'Draggable: onDragStart: %j' , coreEvent . position ) ;
3130
3231 // Short-circuit if user's callback killed it.
33- let shouldStart = this . props . onStart ( e , coreEvent ) ;
32+ let shouldStart = this . props . onStart ( e , createUIEvent ( this , coreEvent ) ) ;
3433 // Kills start event on core as well, so move handlers are never bound.
3534 if ( shouldStart === false ) return false ;
3635
@@ -41,39 +40,42 @@ export default class Draggable extends DraggableCore {
4140
4241 onDrag ( e , coreEvent ) {
4342 if ( ! this . state . dragging ) return false ;
44- console . log ( 'Draggable: onDrag: %j' , coreEvent . position ) ;
43+ log ( 'Draggable: onDrag: %j' , coreEvent . position ) ;
4544
4645 // Short-circuit if user's callback killed it.
47- let shouldUpdate = this . props . onDrag ( e , coreEvent ) ;
46+ let shouldUpdate = this . props . onDrag ( e , createUIEvent ( this , coreEvent ) ) ;
4847 if ( shouldUpdate === false ) return false ;
4948
50- var clientX = this . state . clientX + coreEvent . position . deltaX ;
51- var clientY = this . state . clientY + coreEvent . position . deltaY ;
49+ let newState = {
50+ clientX : this . state . clientX + coreEvent . position . deltaX ,
51+ clientY : this . state . clientY + coreEvent . position . deltaY
52+ } ;
5253
5354 // Snap to grid if prop has been provided
5455 if ( Array . isArray ( this . props . grid ) ) {
55- [ clientX , clientY ] = snapToGrid ( this . props . grid , clientX , clientY ) ;
56+ newState . lastX = ( this . state . lastX || newState . clientX ) + coreEvent . position . deltaX ;
57+ newState . lastY = ( this . state . lastY || newState . clientY ) + coreEvent . position . deltaY ;
58+ // Eslint bug, it thinks newState.clientY is undefined
59+ /*eslint no-undef:0*/
60+ [ newState . clientX , newState . clientY ] = snapToGrid ( this . props . grid , newState . lastX , newState . lastY ) ;
5661 }
5762
5863 // Keep within bounds.
5964 if ( this . props . bounds ) {
60- [ clientX , clientY ] = getBoundPosition ( this , clientX , clientY ) ;
65+ [ newState . clientX , newState . clientY ] = getBoundPosition ( this , newState . clientX , newState . clientY ) ;
6166 }
6267
63- // TODO create drag event using createUIEvent and call back with it
64- // this.props.onDrag(createUIEvent(e));
65-
66- this . setState ( { clientX, clientY} ) ;
68+ this . setState ( newState ) ;
6769 }
6870
69- onDragEnd ( e , coreEvent ) {
71+ onDragStop ( e , coreEvent ) {
7072 if ( ! this . state . dragging ) return false ;
7173
7274 // Short-circuit if user's callback killed it.
73- let shouldStop = this . props . onStop ( e , coreEvent ) ;
75+ let shouldStop = this . props . onStop ( e , createUIEvent ( this , coreEvent ) ) ;
7476 if ( shouldStop === false ) return false ;
7577
76- console . log ( 'Draggable: onDragEnd : %j' , coreEvent . position ) ;
78+ log ( 'Draggable: onDragStop : %j' , coreEvent . position ) ;
7779
7880 this . setState ( {
7981 dragging : false
@@ -86,7 +88,7 @@ export default class Draggable extends DraggableCore {
8688 // without worrying about whether or not it is relatively or absolutely positioned.
8789 // If the item you are dragging already has a transform set, wrap it in a <span> so <Draggable>
8890 // has a clean slate.
89- var style = createCSSTransform ( {
91+ let style = createCSSTransform ( {
9092 // Set left if horizontal drag is enabled
9193 x : canDragX ( this ) ?
9294 this . state . clientX :
@@ -104,7 +106,7 @@ export default class Draggable extends DraggableCore {
104106 }
105107
106108 // Mark with class while dragging
107- var className = classNames ( ( this . props . children . props . className || '' ) , 'react-draggable' , {
109+ let className = classNames ( ( this . props . children . props . className || '' ) , 'react-draggable' , {
108110 'react-draggable-dragging' : this . state . dragging ,
109111 'react-draggable-dragged' : this . state . dragged
110112 } ) ;
@@ -113,7 +115,7 @@ export default class Draggable extends DraggableCore {
113115 // This makes it flexible to use whatever element is wanted (div, ul, etc)
114116 return (
115117 < DraggableCore { ...this . props } style = { style } className = { className }
116- onStart = { this . onDragStart } onDrag = { this . onDrag } onStop = { this . onDragEnd } >
118+ onStart = { this . onDragStart } onDrag = { this . onDrag } onStop = { this . onDragStop } >
117119 { React . Children . only ( this . props . children ) }
118120 </ DraggableCore >
119121 ) ;
@@ -147,7 +149,7 @@ Draggable.propTypes = assign({}, DraggableCore.propTypes, {
147149 * Example:
148150 *
149151 * ```jsx
150- * var App = React.createClass({
152+ * let App = React.createClass({
151153 * render: function () {
152154 * return (
153155 * <Draggable bounds={{right: 300, bottom: 300}}>
@@ -174,7 +176,7 @@ Draggable.propTypes = assign({}, DraggableCore.propTypes, {
174176 * Example:
175177 *
176178 * ```jsx
177- * var App = React.createClass({
179+ * let App = React.createClass({
178180 * render: function () {
179181 * return (
180182 * <Draggable grid={[25, 25]}>
@@ -193,7 +195,7 @@ Draggable.propTypes = assign({}, DraggableCore.propTypes, {
193195 * Example:
194196 *
195197 * ```jsx
196- * var App = React.createClass({
198+ * let App = React.createClass({
197199 * render: function () {
198200 * return (
199201 * <Draggable start={{x: 25, y: 25}}>
@@ -215,7 +217,7 @@ Draggable.propTypes = assign({}, DraggableCore.propTypes, {
215217 * Example:
216218 *
217219 * ```jsx
218- * var App = React.createClass({
220+ * let App = React.createClass({
219221 * render: function () {
220222 * return (
221223 * <Draggable zIndex={100}>
0 commit comments