Skip to content

Commit 36a0087

Browse files
committed
Fix inconsistencies caused by external mutations to element size.
Added example. TODO test suite
1 parent 18b9157 commit 36a0087

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

lib/Resizable.jsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ var Resizable = module.exports = React.createClass({
3939
};
4040
},
4141

42+
componentWillReceiveProps: function(props) {
43+
if (!this.state.resizing) {
44+
this.setState({
45+
initialWidth: props.width,
46+
initialHeight: props.height
47+
});
48+
this.refs.draggable.resetState();
49+
}
50+
},
51+
4252
constraintsToBounds() {
4353
var p = this.props;
4454
var mins = p.minConstraints || p.handleSize;
@@ -62,11 +72,17 @@ var Resizable = module.exports = React.createClass({
6272
var me = this;
6373
return function(e, {node, position}) {
6474
me.props[handlerName] && me.props[handlerName](e, {node, size: calcWH(me.state, position)});
75+
76+
if (handlerName === 'onResizeStart') {
77+
me.setState({resizing: true});
78+
} else if (handlerName === 'onResizeStop') {
79+
me.setState({resizing: false});
80+
}
6581
};
6682
},
6783

6884
render() {
69-
var p = this.props;
85+
var p = this.props, s = this.state;
7086

7187
// What we're doing here is getting the child of this element, and cloning it with this element's props.
7288
// We are then defining its children as:
@@ -77,6 +93,7 @@ var Resizable = module.exports = React.createClass({
7793
p.children.props.children,
7894
<Draggable
7995
{...p.draggableOpts}
96+
ref="draggable"
8097
onStop={this.resizeHandler('onResizeStop')}
8198
onStart={this.resizeHandler('onResizeStart')}
8299
onDrag={this.resizeHandler('onResize')}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
},
4444
"dependencies": {
4545
"object-assign": "^2.0.0",
46-
"react-draggable": "^0.8.0"
46+
"react-draggable": "^0.8.1"
4747
},
4848
"publishConfig": {
4949
"registry": "https://registry.npmjs.org"

test/TestLayout.jsx

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,35 @@ typeof window !== "undefined" && (window.React = React); // for devtools
44
typeof window !== "undefined" && (window.Perf = React.addons.Perf); // for devtools
55
var _ = require('lodash');
66
var ResizableBox = require('../lib/ResizableBox.jsx');
7+
var Resizable = require('../lib/Resizable.jsx');
78
require('style!css!../css/styles.css');
89

910
var TestLayout = module.exports = React.createClass({
1011
displayName: 'TestLayout',
1112

13+
getInitialState() {
14+
return {width: 200, height: 200};
15+
},
16+
17+
onClick() {
18+
this.setState({width: 200, height: 200})
19+
},
20+
21+
onResize(event, {element, size}) {
22+
this.setState({width: size.width, height: size.height});
23+
},
24+
1225
render() {
1326
return (
1427
<div>
28+
<button onClick={this.onClick} style={{'marginBottom': '10px'}}>Reset first element's width/height</button>
29+
<Resizable className="box" height={this.state.height} width={this.state.width} onResize={this.onResize}>
30+
<div className="box" style={{width: this.state.width + 'px', height: this.state.height + 'px'}}>
31+
<span className="text">{"Raw use of <Resizable> element. 200x200, no constraints."}</span>
32+
</div>
33+
</Resizable>
1534
<ResizableBox className="box" width={200} height={200}>
16-
<span className="text">Resizable box, starting at 200x200, no constraints</span>
35+
<span className="text">{"<ResizableBox>, same as above."}</span>
1736
</ResizableBox>
1837
<ResizableBox className="box" width={200} height={200} draggableOpts={{grid: [25, 25]}}>
1938
<span className="text">Resizable box that snaps to even intervals of 25px.</span>
@@ -30,6 +49,7 @@ var TestLayout = module.exports = React.createClass({
3049
<ResizableBox className="box" width={200} height={120} lockAspectRatio={true}>
3150
<span className="text">Resizable rectangle with a locked aspect ratio.</span>
3251
</ResizableBox>
52+
3353
</div>
3454
);
3555
}

0 commit comments

Comments
 (0)