forked from react-grid-layout/react-resizable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResizableBox.jsx
57 lines (50 loc) · 1.7 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
// @flow
import {default as React, PropTypes} from 'react';
import Resizable from './Resizable';
type State = {width: number, height: number, aspectRatio: number};
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 = {
height: PropTypes.number,
width: PropTypes.number
};
static defaultProps = {
handleSize: [20,20]
};
state: State = {
width: this.props.width,
height: this.props.height,
};
onResize = (event, {element, size}) => {
const {width, height} = size;
this.setState(size, () => {
this.props.onResize && this.props.onResize(event, {element, size});
});
};
onResize: (event: Event, data: ResizeData) => void;
render(): React.Element {
// Basic wrapper around a Resizable instance.
// If you use Resizable directly, you are responsible for updating the child component
// with a new width and height.
const {handleSize, onResizeStart, onResizeStop, draggableOpts,
minConstraints, maxConstraints, lockAspectRatio, width, height, ...props} = this.props;
return (
<Resizable
handleSize={handleSize}
width={this.state.width}
height={this.state.height}
onResizeStart={onResizeStart}
onResize={this.onResize}
onResizeStop={onResizeStop}
draggableOpts={draggableOpts}
minConstraints={minConstraints}
maxConstraints={maxConstraints}
lockAspectRatio={lockAspectRatio}
>
<div style={{width: this.state.width + 'px', height: this.state.height + 'px'}} {...props} />
</Resizable>
);
}
}