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.63 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
import {default as React, PropTypes} from 'react';
import Resizable from './Resizable';
type Size = {width: number, height: number};
type ResizeData = {element: Element, size: Size};
// An example use of Resizable.
export default class ResizableBox extends React.Component {
static propTypes = {
lockAspectRatio: PropTypes.bool,
minConstraints: PropTypes.arrayOf(PropTypes.number),
maxConstraints: PropTypes.arrayOf(PropTypes.number),
height: PropTypes.number,
width: PropTypes.number
};
static defaultProps = {
lockAspectRatio: false,
handleSize: [20,20]
};
state = {
width: this.props.width,
height: this.props.height,
aspectRatio: this.props.width / this.props.height
};
// TODO data is ResizeData type, but that doesn't work in babel-typecheck pre-babel6
onResize = (event: Event, data: Object) => {
let {element, size} = data;
let {width, height} = size;
let widthChanged = width !== this.state.width, heightChanged = height !== this.state.height;
if (!widthChanged && !heightChanged) return;
[width, height] = this.runConstraints(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
runConstraints(width: number, height: number) {
let [min, max] = [this.props.minConstraints, this.props.maxConstraints];
if (this.props.lockAspectRatio) {
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.
let {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>
);
}
}