@@ -14,11 +14,9 @@ type Position = {
1414 deltaY : number
1515} ;
1616type State = {
17- aspectRatio : number ,
18- bounds : Bounds ,
1917 resizing : boolean ,
20- height : number ,
21- width : number ,
18+ width : number , height : number ,
19+ initialWidth : number , initialHeight : number
2220} ;
2321type DragCallbackData = {
2422 node : HTMLElement ,
@@ -72,36 +70,42 @@ export default class Resizable extends React.Component {
7270 } ;
7371
7472 state : State = {
75- aspectRatio : this . props . width / this . props . height ,
76- bounds : this . constraintsToBounds ( ) ,
7773 resizing : false ,
74+ initialHeight : this . props . height ,
75+ initialWidth : this . props . width ,
7876 height : this . props . height ,
7977 width : this . props . width ,
8078 } ;
8179
8280 componentWillReceiveProps ( nextProps : Object ) {
81+ // If parent changes height/width, set that in our state.
8382 if ( ! this . state . resizing &&
8483 ( nextProps . width !== this . props . width || nextProps . height !== this . props . height ) ) {
8584 this . setState ( {
8685 width : nextProps . width ,
87- height : nextProps . height
86+ height : nextProps . height ,
8887 } ) ;
8988 }
9089 }
9190
91+ /**
92+ * Convert our constraints into bounds for the <Draggable>.
93+ * We have to use initialHeight/Width here because that's where the <Draggable>'s `0` is.
94+ */
9295 constraintsToBounds ( ) : Bounds {
93- const { minConstraints, maxConstraints, height, width} = this . props ;
96+ const { minConstraints, maxConstraints} = this . props ;
97+ const { initialHeight, initialWidth} = this . state ;
9498 return {
95- left : minConstraints [ 0 ] - width ,
96- top : minConstraints [ 1 ] - height ,
97- right : maxConstraints [ 0 ] - width ,
98- bottom : maxConstraints [ 1 ] - height
99+ left : minConstraints [ 0 ] - initialWidth ,
100+ top : minConstraints [ 1 ] - initialHeight ,
101+ right : maxConstraints [ 0 ] - initialWidth ,
102+ bottom : maxConstraints [ 1 ] - initialHeight
99103 } ;
100104 }
101105
102106 lockAspectRatio ( width : number , height : number , aspectRatio : number ) : [ number , number ] {
103- height = width / this . state . aspectRatio ;
104- width = height * this . state . aspectRatio ;
107+ height = width / aspectRatio ;
108+ width = height * aspectRatio ;
105109 return [ width , height ] ;
106110 }
107111
@@ -119,7 +123,7 @@ export default class Resizable extends React.Component {
119123 if ( handlerName === 'onResize' && ! widthChanged && ! heightChanged ) return ;
120124
121125 if ( this . props . lockAspectRatio ) {
122- [ width , height ] = this . lockAspectRatio ( width , height , this . state . aspectRatio ) ;
126+ [ width , height ] = this . lockAspectRatio ( width , height , this . state . width / this . state . height ) ;
123127 }
124128
125129 // Set the appropriate state for this handler.
@@ -162,7 +166,7 @@ export default class Resizable extends React.Component {
162166 onStop = { this . resizeHandler ( 'onResizeStop' ) }
163167 onStart = { this . resizeHandler ( 'onResizeStart' ) }
164168 onDrag = { this . resizeHandler ( 'onResize' ) }
165- bounds = { this . state . bounds }
169+ bounds = { this . constraintsToBounds ( ) }
166170 >
167171 < span className = "react-resizable-handle" />
168172 </ Draggable >
0 commit comments