@@ -55,7 +55,6 @@ type Size = {
5555} ;
5656
5757type State = {
58- original : Position ;
5958 bounds : {
6059 top : number ;
6160 right : number ;
@@ -214,14 +213,11 @@ export class Rnd extends React.PureComponent<Props, State> {
214213 resizingPosition = { x : 0 , y : 0 } ;
215214 offsetFromParent = { left : 0 , top : 0 } ;
216215 resizableElement : { current : HTMLElement | null } = { current : null } ;
216+ originalPosition = { x : 0 , y : 0 } ;
217217
218218 constructor ( props : Props ) {
219219 super ( props ) ;
220220 this . state = {
221- original : {
222- x : 0 ,
223- y : 0 ,
224- } ,
225221 bounds : {
226222 top : 0 ,
227223 right : 0 ,
@@ -305,6 +301,8 @@ export class Rnd extends React.PureComponent<Props, State> {
305301 if ( this . props . onDragStart ) {
306302 this . props . onDragStart ( e , data ) ;
307303 }
304+ const pos = this . getDraggablePosition ( ) ;
305+ this . originalPosition = pos ;
308306 if ( ! this . props . bounds ) return ;
309307 const parent = this . getParent ( ) ;
310308 const scale = this . props . scale as number ;
@@ -359,16 +357,26 @@ export class Rnd extends React.PureComponent<Props, State> {
359357 }
360358
361359 onDrag ( e : RndDragEvent , data : DraggableData ) {
362- if ( this . props . onDrag ) {
363- const offset = this . offsetFromParent ;
364- return this . props . onDrag ( e , { ...data , x : data . x - offset . left , y : data . y - offset . top } ) ;
360+ if ( ! this . props . onDrag ) return ;
361+ const { left, top } = this . offsetFromParent ;
362+ if ( ! this . props . dragAxis || this . props . dragAxis === "both" ) {
363+ return this . props . onDrag ( e , { ...data , x : data . x - left , y : data . y - top } ) ;
364+ } else if ( this . props . dragAxis === "x" ) {
365+ return this . props . onDrag ( e , { ...data , x : data . x + left , y : this . originalPosition . y + top , deltaY : 0 } ) ;
366+ } else if ( this . props . dragAxis === "y" ) {
367+ return this . props . onDrag ( e , { ...data , x : this . originalPosition . x + left , y : data . y + top , deltaX : 0 } ) ;
365368 }
366369 }
367370
368371 onDragStop ( e : RndDragEvent , data : DraggableData ) {
369- if ( this . props . onDragStop ) {
370- const { left, top } = this . offsetFromParent ;
372+ if ( ! this . props . onDragStop ) return ;
373+ const { left, top } = this . offsetFromParent ;
374+ if ( ! this . props . dragAxis || this . props . dragAxis === "both" ) {
371375 return this . props . onDragStop ( e , { ...data , x : data . x + left , y : data . y + top } ) ;
376+ } else if ( this . props . dragAxis === "x" ) {
377+ return this . props . onDragStop ( e , { ...data , x : data . x + left , y : this . originalPosition . y + top , deltaY : 0 } ) ;
378+ } else if ( this . props . dragAxis === "y" ) {
379+ return this . props . onDragStop ( e , { ...data , x : this . originalPosition . x + left , y : data . y + top , deltaX : 0 } ) ;
372380 }
373381 }
374382
@@ -384,9 +392,7 @@ export class Rnd extends React.PureComponent<Props, State> {
384392 const offset = this . offsetFromParent ;
385393 const pos = this . getDraggablePosition ( ) ;
386394 this . resizingPosition = { x : pos . x + offset . left , y : pos . y + offset . top } ;
387- this . setState ( {
388- original : pos ,
389- } ) ;
395+ this . originalPosition = pos ;
390396 if ( this . props . bounds ) {
391397 const parent = this . getParent ( ) ;
392398 let boundary ;
@@ -477,7 +483,7 @@ export class Rnd extends React.PureComponent<Props, State> {
477483 delta : { height : number ; width : number } ,
478484 ) {
479485 // INFO: Apply x and y position adjustments caused by resizing to draggable
480- const newPos = { x : this . state . original . x , y : this . state . original . y } ;
486+ const newPos = { x : this . originalPosition . x , y : this . originalPosition . y } ;
481487 const left = - delta . width ;
482488 const top = - delta . height ;
483489 const directions = [ "top" , "left" , "topLeft" , "bottomLeft" , "topRight" ] ;
0 commit comments