@@ -3,7 +3,7 @@ var React = require('react');
33var Draggable = require ( 'react-draggable' ) ;
44var PureRenderMixin = require ( 'react/lib/ReactComponentWithPureRenderMixin' ) ;
55var assign = require ( 'object-assign' ) ;
6- var cloneWithProps = require ( 'react/lib/cloneWithProps ' ) ;
6+ var cloneElement = require ( './cloneElement ' ) ;
77
88var Resizable = module . exports = React . createClass ( {
99 displayName : 'Resizable' ,
@@ -31,12 +31,24 @@ var Resizable = module.exports = React.createClass({
3131 } ;
3232 } ,
3333
34- minConstraints ( ) {
35- return parseConstraints ( this . props . minConstraints , this . props . handleSize ) || this . props . handleSize ;
34+ getInitialState : function ( ) {
35+ return {
36+ bounds : this . constraintsToBounds ( ) ,
37+ initialWidth : this . props . width ,
38+ initialHeight : this . props . height
39+ } ;
3640 } ,
3741
38- maxConstraints ( ) {
39- return parseConstraints ( this . props . maxConstraints , this . props . handleSize ) ;
42+ constraintsToBounds ( ) {
43+ var p = this . props ;
44+ var mins = p . minConstraints || p . handleSize ;
45+ var maxes = p . maxConstraints || [ Infinity , Infinity ] ;
46+ return {
47+ left : mins [ 0 ] - p . width ,
48+ top : mins [ 1 ] - p . height ,
49+ right : maxes [ 0 ] - p . width ,
50+ bottom : maxes [ 1 ] - p . height
51+ } ;
4052 } ,
4153
4254
@@ -48,8 +60,8 @@ var Resizable = module.exports = React.createClass({
4860 */
4961 resizeHandler ( handlerName ) {
5062 var me = this ;
51- return function ( e , { element , position} ) {
52- me . props [ handlerName ] && me . props [ handlerName ] ( e , { element , size : calcWH ( position , me . props . handleSize ) } ) ;
63+ return function ( e , { node , position} ) {
64+ me . props [ handlerName ] && me . props [ handlerName ] ( e , { node , size : calcWH ( me . state , position ) } ) ;
5365 } ;
5466 } ,
5567
@@ -60,18 +72,15 @@ var Resizable = module.exports = React.createClass({
6072 // We are then defining its children as:
6173 // Its original children (resizable's child's children), and
6274 // A draggable handle.
63- return cloneWithProps ( p . children , assign ( { } , p , {
75+ return cloneElement ( p . children , assign ( { } , p , {
6476 children : [
6577 p . children . props . children ,
6678 < Draggable
6779 { ...p . draggableOpts }
68- start = { { x : p . width - 20 , y : p . height - 20 } }
69- moveOnStartChange = { true }
7080 onStop = { this . resizeHandler ( 'onResizeStop' ) }
7181 onStart = { this . resizeHandler ( 'onResizeStart' ) }
7282 onDrag = { this . resizeHandler ( 'onResize' ) }
73- minConstraints = { this . minConstraints ( ) }
74- maxConstraints = { this . maxConstraints ( ) }
83+ bounds = { this . state . bounds }
7584 >
7685 < span className = "react-resizable-handle" />
7786 </ Draggable >
@@ -81,27 +90,11 @@ var Resizable = module.exports = React.createClass({
8190} ) ;
8291
8392/**
84- * Parse left and top coordinates; we have to add the handle size to get the full picture .
93+ * Parse left and top coordinates.
8594 * @param {Number } options.left Left coordinate.
8695 * @param {Number } options.top Top coordinate.
87- * @param {Array } handleSize Handle data.
8896 * @return {Object } Coordinates
8997 */
90- function calcWH ( { left, top} , handleSize ) {
91- return { width : left + handleSize [ 0 ] , height : top + handleSize [ 1 ] } ;
92- }
93-
94- /**
95- * Constraints must be subtracted by the size of the handle to work properly.
96- * This has a side-effect of effectively limiting the minimum size to the handleSize,
97- * which IMO is fine.
98- * @param {Array } constraints Constraints array.
99- * @param {Array } handleSize Handle size array.
100- * @return {Array } Transformed constraints.
101- */
102- function parseConstraints ( constraints , handleSize ) {
103- if ( ! constraints ) return ;
104- return constraints . map ( function ( c , i ) {
105- return c - handleSize [ i ] ;
106- } ) ;
98+ function calcWH ( { initialWidth, initialHeight} , { left, top} ) {
99+ return { width : initialWidth + left , height : initialHeight + top } ;
107100}
0 commit comments