11/** @jsx React.DOM */
2- var React = require ( 'react' ) ;
2+ var React = require ( 'react' ) ,
3+ emptyFunction = require ( 'react/lib/emptyFunction' ) ;
4+
5+ function createUIEvent ( draggable ) {
6+ return {
7+ position : {
8+ top : draggable . state . clientY ,
9+ left : draggable . state . clientX
10+ }
11+ } ;
12+ }
13+
14+ function canDragY ( draggable ) {
15+ return draggable . props . axis === 'both' ||
16+ draggable . props . axis === 'y' ;
17+ }
18+
19+ function canDragX ( draggable ) {
20+ return draggable . props . axis === 'both' ||
21+ draggable . props . axis === 'x' ;
22+ }
323
424module . exports = React . createClass ( {
25+ propTypes : {
26+ /**
27+ * `axis` determines which axis the draggable can move.
28+ *
29+ * 'both' allows movement horizontally and vertically.
30+ * 'x' limits movement to horizontal axis.
31+ * 'y' limits movement to vertical axis.
32+ *
33+ * Defaults to 'both'.
34+ */
35+ axis : React . PropTypes . oneOf ( [ 'both' , 'x' , 'y' ] ) ,
36+
37+ /**
38+ * Called when dragging starts.
39+ *
40+ * Example:
41+ *
42+ * ```js
43+ * function (event, ui) {}
44+ * ```
45+ *
46+ * `event` is the Event that was triggered.
47+ * `ui` is an object:
48+ *
49+ * ```js
50+ * {
51+ * position: {top: 0, left: 0}
52+ * }
53+ * ```
54+ */
55+ onStart : React . PropTypes . func ,
56+
57+ /**
58+ * Called while dragging.
59+ *
60+ * Example:
61+ *
62+ * ```js
63+ * function (event, ui) {}
64+ * ```
65+ *
66+ * `event` is the Event that was triggered.
67+ * `ui` is an object:
68+ *
69+ * ```js
70+ * {
71+ * position: {top: 0, left: 0}
72+ * }
73+ * ```
74+ */
75+ onDrag : React . PropTypes . func ,
76+
77+ /**
78+ * Called when dragging stops.
79+ *
80+ * Example:
81+ *
82+ * ```js
83+ * function (event, ui) {}
84+ * ```
85+ *
86+ * `event` is the Event that was triggered.
87+ * `ui` is an object:
88+ *
89+ * ```js
90+ * {
91+ * position: {top: 0, left: 0}
92+ * }
93+ * ```
94+ */
95+ onStop : React . PropTypes . func
96+ } ,
97+
98+ getDefaultProps : function ( ) {
99+ return {
100+ axis : 'both' ,
101+ onStart : emptyFunction ,
102+ onDrag : emptyFunction ,
103+ onStop : emptyFunction
104+ } ;
105+ } ,
106+
5107 getInitialState : function ( ) {
6108 return {
7109 startX : 0 , startY : 0 ,
@@ -19,10 +121,14 @@ module.exports = React.createClass({
19121 startY : parseInt ( node . style . top , 10 ) || 0
20122 } ) ;
21123
124+ this . props . onStart ( e , createUIEvent ( this ) ) ;
125+
22126 window . addEventListener ( 'mousemove' , this . handleMouseMove ) ;
23127 } ,
24128
25129 handleMouseUp : function ( e ) {
130+ this . props . onStop ( e , createUIEvent ( this ) ) ;
131+
26132 window . removeEventListener ( 'mousemove' , this . handleMouseMove ) ;
27133 } ,
28134
@@ -31,12 +137,14 @@ module.exports = React.createClass({
31137 clientX : ( this . state . startX + ( e . clientX - this . state . offsetX ) ) ,
32138 clientY : ( this . state . startY + ( e . clientY - this . state . offsetY ) )
33139 } ) ;
140+
141+ this . props . onDrag ( e , createUIEvent ( this ) ) ;
34142 } ,
35143
36144 render : function ( ) {
37145 var style = {
38- top : this . state . clientY ,
39- left : this . state . clientX
146+ top : canDragY ( this ) ? this . state . clientY : this . state . startY ,
147+ left : canDragX ( this ) ? this . state . clientX : this . state . startX
40148 } ;
41149
42150 return (
0 commit comments