1- 'use strict' ;
2- var React = require ( 'react' ) ;
3- var DraggableCore = require ( 'react-draggable' ) . DraggableCore ;
4- var assign = require ( 'object-assign' ) ;
5- var cloneElement = require ( './cloneElement' ) ;
1+ import { default as React , PropTypes } from 'react' ;
2+ import { DraggableCore } from 'react-draggable' ;
3+ import assign from 'object-assign' ;
4+ import cloneElement from './cloneElement' ;
65
7- var Resizable = module . exports = React . createClass ( {
8- displayName : 'Resizable' ,
6+ export default class Resizable extends React . Component {
7+ static propTypes = {
8+ //
9+ // Required Props
10+ //
911
10- propTypes : {
1112 // Require that one and only one child be present.
12- children : React . PropTypes . element . isRequired ,
13- // Functions
14- onResizeStop : React . PropTypes . func ,
15- onResizeStart : React . PropTypes . func ,
16- onResize : React . PropTypes . func ,
17-
18- width : React . PropTypes . number . isRequired ,
19- height : React . PropTypes . number . isRequired ,
13+ children : PropTypes . element . isRequired ,
14+
15+ // Initial w/h
16+ width : PropTypes . number . isRequired ,
17+ height : PropTypes . number . isRequired ,
18+
19+ //
20+ // Optional props
21+ //
22+
2023 // If you change this, be sure to update your css
21- handleSize : React . PropTypes . array ,
22- // These will be passed wholesale to react-draggable
23- draggableOpts : React . PropTypes . object
24- } ,
24+ handleSize : PropTypes . array ,
2525
26- getDefaultProps ( ) {
27- return {
28- handleSize : [ 20 , 20 ]
29- } ;
30- } ,
26+ // Min/max size
27+ minConstraints : PropTypes . arrayOf ( PropTypes . number ) ,
28+ maxConstraints : PropTypes . arrayOf ( PropTypes . number ) ,
3129
32- getInitialState ( ) {
33- return {
34- bounds : this . constraintsToBounds ( ) ,
35- width : this . props . width ,
36- height : this . props . height
37- } ;
38- } ,
30+ // Callbacks
31+ onResizeStop : PropTypes . func ,
32+ onResizeStart : PropTypes . func ,
33+ onResize : PropTypes . func ,
34+
35+ // These will be passed wholesale to react-draggable's DraggableCore
36+ draggableOpts : PropTypes . object
37+ } ;
38+
39+ static defaultProps = {
40+ handleSize : [ 20 , 20 ]
41+ } ;
42+
43+ state = {
44+ bounds : this . constraintsToBounds ( ) ,
45+ width : this . props . width ,
46+ height : this . props . height
47+ } ;
3948
4049 componentWillReceiveProps ( props : Object ) {
4150 if ( ! this . state . resizing ) {
@@ -45,19 +54,19 @@ var Resizable = module.exports = React.createClass({
4554 bounds : this . constraintsToBounds ( )
4655 } ) ;
4756 }
48- } ,
57+ }
4958
5059 constraintsToBounds ( ) {
51- var p = this . props ;
52- var mins = p . minConstraints || p . handleSize ;
53- var maxes = p . maxConstraints || [ Infinity , Infinity ] ;
60+ let p = this . props ;
61+ let mins = p . minConstraints || p . handleSize ;
62+ let maxes = p . maxConstraints || [ Infinity , Infinity ] ;
5463 return {
5564 left : mins [ 0 ] - p . width ,
5665 top : mins [ 1 ] - p . height ,
5766 right : maxes [ 0 ] - p . width ,
5867 bottom : maxes [ 1 ] - p . height
5968 } ;
60- } ,
69+ }
6170
6271 /**
6372 * Wrapper around drag events to provide more useful data.
@@ -66,23 +75,22 @@ var Resizable = module.exports = React.createClass({
6675 * @return {Function } Handler function.
6776 */
6877 resizeHandler ( handlerName : string ) {
69- var me = this ;
70- return function ( e , { node, position} ) {
71- let width = me . state . width + position . deltaX , height = me . state . height + position . deltaY ;
72- me . props [ handlerName ] && me . props [ handlerName ] ( e , { node, size : { width, height} } ) ;
78+ return ( e , { node, position} ) => {
79+ let width = this . state . width + position . deltaX , height = this . state . height + position . deltaY ;
80+ this . props [ handlerName ] && this . props [ handlerName ] ( e , { node, size : { width, height} } ) ;
7381
7482 if ( handlerName === 'onResizeStart' ) {
75- me . setState ( { resizing : true } ) ;
83+ this . setState ( { resizing : true } ) ;
7684 } else if ( handlerName === 'onResizeStop' ) {
77- me . setState ( { resizing : false } ) ;
85+ this . setState ( { resizing : false } ) ;
7886 } else {
79- me . setState ( { width, height} ) ;
87+ this . setState ( { width, height} ) ;
8088 }
8189 } ;
82- } ,
90+ }
8391
8492 render ( ) {
85- var p = this . props , s = this . state ;
93+ let p = this . props ;
8694
8795 // What we're doing here is getting the child of this element, and cloning it with this element's props.
8896 // We are then defining its children as:
@@ -104,4 +112,4 @@ var Resizable = module.exports = React.createClass({
104112 ]
105113 } ) ) ;
106114 }
107- } ) ;
115+ }
0 commit comments