@@ -118,6 +118,7 @@ export interface Props {
118
118
resizeGrid ?: Grid ;
119
119
bounds ?: string ;
120
120
onMouseDown ?: ( e : MouseEvent ) => void ;
121
+ onMouseUp ?: ( e : MouseEvent ) => void ;
121
122
onResizeStart ?: RndResizeStartCallback ;
122
123
onResize ?: RndResizeCallback ;
123
124
onResizeStop ?: RndResizeCallback ;
@@ -169,7 +170,7 @@ interface DefaultProps {
169
170
scale : number ;
170
171
}
171
172
172
- export class Rnd extends React . Component < Props , State > {
173
+ export class Rnd extends React . PureComponent < Props , State > {
173
174
public static defaultProps : DefaultProps = {
174
175
maxWidth : Number . MAX_SAFE_INTEGER ,
175
176
maxHeight : Number . MAX_SAFE_INTEGER ,
@@ -183,7 +184,8 @@ export class Rnd extends React.Component<Props, State> {
183
184
} ;
184
185
resizable ! : Resizable ;
185
186
draggable ! : $TODO ; // Draggable;
186
- isResizing = false ;
187
+ resizing = false ;
188
+ resizingPosition = { x : 0 , y : 0 } ;
187
189
188
190
constructor ( props : Props ) {
189
191
super ( props ) ;
@@ -346,7 +348,8 @@ export class Rnd extends React.Component<Props, State> {
346
348
elementRef : HTMLDivElement ,
347
349
) {
348
350
e . stopPropagation ( ) ;
349
- this . isResizing = true ;
351
+ this . resizing = true ;
352
+
350
353
const scale = this . props . scale as number ;
351
354
this . setState ( {
352
355
original : this . getDraggablePosition ( ) ,
@@ -445,32 +448,28 @@ export class Rnd extends React.Component<Props, State> {
445
448
const offset = this . getOffsetFromParent ( ) ;
446
449
if ( / l e f t / i. test ( direction ) ) {
447
450
x = this . state . original . x - delta . width ;
448
- // INFO: If uncontrolled component, apply x position by resize to draggable.
449
- if ( ! this . props . position ) {
450
- this . draggable . setState ( { x } ) ;
451
- }
451
+ // INFO: Apply x position by resize to draggable.
452
+ this . draggable . setState ( { x } ) ;
452
453
x += offset . left ;
453
454
}
454
455
if ( / t o p / i. test ( direction ) ) {
455
456
y = this . state . original . y - delta . height ;
456
- // INFO: If uncontrolled component, apply y position by resize to draggable.
457
- if ( ! this . props . position ) {
458
- this . draggable . setState ( { y } ) ;
459
- }
457
+ // INFO: Apply x position by resize to draggable.
458
+ this . draggable . setState ( { y } ) ;
460
459
y += offset . top ;
461
460
}
462
- if ( this . props . onResize ) {
463
- if ( typeof x === "undefined" ) {
464
- x = this . getDraggablePosition ( ) . x + offset . left ;
465
- }
466
- if ( typeof y === "undefined" ) {
467
- y = this . getDraggablePosition ( ) . y + offset . top ;
468
- }
469
- this . props . onResize ( e , direction , elementRef , delta , {
470
- x,
471
- y,
472
- } ) ;
461
+ if ( typeof x === "undefined" ) {
462
+ x = this . getDraggablePosition ( ) . x + offset . left ;
473
463
}
464
+ if ( typeof y === "undefined" ) {
465
+ y = this . getDraggablePosition ( ) . y + offset . top ;
466
+ }
467
+ this . resizingPosition = { x, y } ;
468
+ if ( ! this . props . onResize ) return ;
469
+ this . props . onResize ( e , direction , elementRef , delta , {
470
+ x,
471
+ y,
472
+ } ) ;
474
473
}
475
474
476
475
onResizeStop (
@@ -479,12 +478,11 @@ export class Rnd extends React.Component<Props, State> {
479
478
elementRef : HTMLDivElement ,
480
479
delta : { height : number ; width : number } ,
481
480
) {
482
- this . isResizing = false ;
481
+ this . resizing = false ;
483
482
const { maxWidth, maxHeight } = this . getMaxSizesFromProps ( ) ;
484
483
this . setState ( { maxWidth, maxHeight } ) ;
485
484
if ( this . props . onResizeStop ) {
486
- const position : Position = this . getDraggablePosition ( ) ;
487
- this . props . onResizeStop ( e , direction , elementRef , delta , position ) ;
485
+ this . props . onResizeStop ( e , direction , elementRef , delta , this . resizingPosition ) ;
488
486
}
489
487
}
490
488
@@ -518,13 +516,24 @@ export class Rnd extends React.Component<Props, State> {
518
516
} ;
519
517
}
520
518
519
+ refDraggable = ( c : $TODO ) => {
520
+ if ( ! c ) return ;
521
+ this . draggable = c ;
522
+ } ;
523
+
524
+ refResizable = ( c : Resizable | null ) => {
525
+ if ( ! c ) return ;
526
+ this . resizable = c ;
527
+ } ;
528
+
521
529
render ( ) {
522
530
const {
523
531
disableDragging,
524
532
style,
525
533
dragHandleClassName,
526
534
position,
527
535
onMouseDown,
536
+ onMouseUp,
528
537
dragAxis,
529
538
dragGrid,
530
539
bounds,
@@ -564,34 +573,30 @@ export class Rnd extends React.Component<Props, State> {
564
573
y : position . y - top ,
565
574
} ;
566
575
}
576
+ // INFO: Make uncontorolled component when resizing to control position by setPostion.
577
+ const pos = this . resizing ? undefined : draggablePosition
567
578
return (
568
579
< Draggable
569
- ref = { ( c : $TODO ) => {
570
- if ( ! c ) return ;
571
- this . draggable = c ;
572
- } }
580
+ ref = { this . refDraggable }
573
581
handle = { dragHandleClassName ? `.${ dragHandleClassName } ` : undefined }
574
582
defaultPosition = { defaultValue }
575
583
onMouseDown = { onMouseDown }
584
+ onMouseUp = { onMouseUp }
576
585
onStart = { this . onDragStart }
577
586
onDrag = { this . onDrag }
578
587
onStop = { this . onDragStop }
579
588
axis = { dragAxis }
580
589
disabled = { disableDragging }
581
590
grid = { dragGrid }
582
591
bounds = { bounds ? this . state . bounds : undefined }
583
- position = { draggablePosition }
592
+ position = { pos }
584
593
enableUserSelectHack = { enableUserSelectHack }
585
594
cancel = { cancel }
586
595
scale = { scale }
587
596
>
588
597
< Resizable
589
598
{ ...resizableProps }
590
- ref = { c => {
591
- if ( c ) {
592
- this . resizable = c ;
593
- }
594
- } }
599
+ ref = { this . refResizable }
595
600
defaultSize = { defaultValue }
596
601
size = { this . props . size }
597
602
enable = { enableResizing }
@@ -601,8 +606,8 @@ export class Rnd extends React.Component<Props, State> {
601
606
style = { innerStyle }
602
607
minWidth = { this . props . minWidth }
603
608
minHeight = { this . props . minHeight }
604
- maxWidth = { this . isResizing ? this . state . maxWidth : this . props . maxWidth }
605
- maxHeight = { this . isResizing ? this . state . maxHeight : this . props . maxHeight }
609
+ maxWidth = { this . resizing ? this . state . maxWidth : this . props . maxWidth }
610
+ maxHeight = { this . resizing ? this . state . maxHeight : this . props . maxHeight }
606
611
grid = { resizeGrid }
607
612
handleWrapperClass = { resizeHandleWrapperClass }
608
613
handleWrapperStyle = { resizeHandleWrapperStyle }
0 commit comments