forked from react-grid-layout/react-resizable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResizableBox.js
82 lines (74 loc) · 2.43 KB
/
ResizableBox.js
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
// @flow
import React from 'react';
import PropTypes from 'prop-types';
import Resizable from './Resizable';
import type {Props as ResizableProps, ResizeCallbackData} from './Resizable';
import type {Node as ReactNode} from 'react';
type State = {
width: number, height: number,
propsWidth: number, propsHeight: number,
};
// An example use of Resizable.
export default class ResizableBox extends React.Component<ResizableProps, State> {
static propTypes = {
height: PropTypes.number,
width: PropTypes.number
};
static defaultProps = {
handleSize: [20,20]
};
state: State = {
width: this.props.width,
height: this.props.height,
propsWidth: this.props.width,
propsHeight: this.props.height,
};
static getDerivedStateFromProps(props: ResizableProps, state: State) {
// If parent changes height/width, set that in our state.
if (state.propsWidth !== props.width || state.propsHeight !== props.height) {
return {
width: props.width,
height: props.height,
propsWidth: props.width,
propsHeight: props.height,
};
}
return null;
}
onResize = (e: SyntheticEvent<>, data: ResizeCallbackData) => {
const {size} = data;
const {width, height} = size;
if (this.props.onResize) {
e.persist && e.persist();
this.setState(size, () => this.props.onResize && this.props.onResize(e, data));
} else {
this.setState(size);
}
};
render(): ReactNode {
// 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 {handle, handleSize, onResize, onResizeStart, onResizeStop, draggableOpts, minConstraints,
maxConstraints, lockAspectRatio, axis, width, height, resizeHandles, ...props} = this.props;
return (
<Resizable
handle={handle}
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}
axis={axis}
resizeHandles={resizeHandles}
>
<div style={{width: this.state.width + 'px', height: this.state.height + 'px'}} {...props} />
</Resizable>
);
}
}