forked from react-grid-layout/react-resizable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResizableBox.jsx
87 lines (76 loc) · 2.42 KB
/
ResizableBox.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
'use strict';
var React = require('react');
var Resizable = require('./Resizable');
var PureRenderMixin = require('react/lib/ReactComponentWithPureRenderMixin');
// An example use of Resizable.
var ResizableBox = module.exports = React.createClass({
displayName: 'ResizableBox',
mixins: [PureRenderMixin],
propTypes: {
lockAspectRatio: React.PropTypes.bool
},
getDefaultProps() {
return {
lockAspectRatio: false,
handleSize: [20,20]
};
},
getInitialState() {
return {
width: this.props.width,
height: this.props.height,
aspectRatio: this.props.width / this.props.height
};
},
onResize(event, {element, size}) {
var {width, height} = size;
var widthChanged = width !== this.state.width, heightChanged = height !== this.state.height;
if (!widthChanged && !heightChanged) return;
if (this.props.lockAspectRatio) {
[width, height] = this.preserveAspectRatio(width, height);
}
this.setState({width, height}, () => {
if (this.props.onResize) {
this.props.onResize(event, {element, size: {width, height}});
}
});
},
// If you do this, be careful of constraints
preserveAspectRatio(width, height) {
var [min, max] = [this.props.minConstraints, this.props.maxConstraints];
height = width / this.state.aspectRatio;
width = height * this.state.aspectRatio;
if (min) {
width = Math.max(min[0], width);
height = Math.max(min[1], height);
}
if (max) {
width = Math.min(max[0], width);
height = Math.min(max[1], height);
}
return [width, height];
},
render() {
// Basic wrapper around a Resizable instance.
// If you use Resizable directly, you are responsible for updating the component
// with a new width and height.
var {handleSize, minConstraints, maxConstraints, ...props} = this.props;
return (
<Resizable
minConstraints={minConstraints}
maxConstraints={maxConstraints}
handleSize={handleSize}
width={this.state.width}
height={this.state.height}
onResizeStart={this.props.onResizeStart}
onResize={this.onResize}
onResizeStop={this.props.onResizeStop}
draggableOpts={this.props.draggableOpts}
>
<div style={{width: this.state.width + 'px', height: this.state.height + 'px'}} {...props}>
{this.props.children}
</div>
</Resizable>
);
}
});