-
Notifications
You must be signed in to change notification settings - Fork 372
/
Copy pathResizable.js
234 lines (200 loc) · 7.17 KB
/
Resizable.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// @flow
import React from 'react';
import PropTypes from 'prop-types';
import {DraggableCore} from 'react-draggable';
import cloneElement from './cloneElement';
import type {Element as ReactElement, Node as ReactNode} from 'react';
type Axis = 'both' | 'x' | 'y' | 'none';
type State = {
resizing: boolean,
width: number, height: number,
slackW: number, slackH: number
};
type DragCallbackData = {
node: HTMLElement,
x: number, y: number,
deltaX: number, deltaY: number,
lastX: number, lastY: number
};
export type ResizeCallbackData = {
node: HTMLElement,
size: {width: number, height: number}
};
export type Props = {
children: ReactElement<any>,
className?: ?string,
width: number,
height: number,
handleSize: [number, number],
lockAspectRatio: boolean,
axis: Axis,
minConstraints: [number, number],
maxConstraints: [number, number],
onResizeStop?: ?(e: SyntheticEvent<>, data: ResizeCallbackData) => any,
onResizeStart?: ?(e: SyntheticEvent<>, data: ResizeCallbackData) => any,
onResize?: ?(e: SyntheticEvent<>, data: ResizeCallbackData) => any,
draggableOpts?: ?Object
};
export default class Resizable extends React.Component<Props, State> {
static propTypes = {
//
// Required Props
//
// Require that one and only one child be present.
children: PropTypes.element.isRequired,
// Initial w/h
width: PropTypes.number.isRequired,
height: PropTypes.number.isRequired,
//
// Optional props
//
// If you change this, be sure to update your css
handleSize: PropTypes.array,
// If true, will only allow width/height to move in lockstep
lockAspectRatio: PropTypes.bool,
// Restricts resizing to a particular axis (default: 'both')
// 'both' - allows resizing by width or height
// 'x' - only allows the width to be changed
// 'y' - only allows the height to be changed
// 'none' - disables resizing altogether
axis: PropTypes.oneOf(['both', 'x', 'y', 'none']),
// Min/max size
minConstraints: PropTypes.arrayOf(PropTypes.number),
maxConstraints: PropTypes.arrayOf(PropTypes.number),
// Callbacks
onResizeStop: PropTypes.func,
onResizeStart: PropTypes.func,
onResize: PropTypes.func,
// These will be passed wholesale to react-draggable's DraggableCore
draggableOpts: PropTypes.object
};
static defaultProps = {
handleSize: [20, 20],
lockAspectRatio: false,
axis: 'both',
minConstraints: [20, 20],
maxConstraints: [Infinity, Infinity]
};
state: State = {
resizing: false,
width: this.props.width, height: this.props.height,
slackW: 0, slackH: 0
};
componentWillReceiveProps(nextProps: Object) {
// If parent changes height/width, set that in our state.
if (!this.state.resizing &&
(nextProps.width !== this.props.width || nextProps.height !== this.props.height)) {
this.setState({
width: nextProps.width,
height: nextProps.height
});
}
}
lockAspectRatio(width: number, height: number, aspectRatio: number): [number, number] {
height = width / aspectRatio;
width = height * aspectRatio;
return [width, height];
}
// If you do this, be careful of constraints
runConstraints(width: number, height: number): [number, number] {
const [min, max] = [this.props.minConstraints, this.props.maxConstraints];
if (this.props.lockAspectRatio) {
const ratio = this.state.width / this.state.height;
height = width / ratio;
width = height * ratio;
}
if (!min && !max) return [width, height];
const [oldW, oldH] = [width, height];
// Add slack to the values used to calculate bound position. This will ensure that if
// we start removing slack, the element won't react to it right away until it's been
// completely removed.
let {slackW, slackH} = this.state;
width += slackW;
height += slackH;
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);
}
// If the numbers changed, we must have introduced some slack. Record it for the next iteration.
slackW += (oldW - width);
slackH += (oldH - height);
if (slackW !== this.state.slackW || slackH !== this.state.slackH) {
this.setState({slackW, slackH});
}
return [width, height];
}
/**
* Wrapper around drag events to provide more useful data.
*
* @param {String} handlerName Handler name to wrap.
* @return {Function} Handler function.
*/
resizeHandler(handlerName: string): Function {
return (e: SyntheticEvent<> | MouseEvent, {node, deltaX, deltaY}: DragCallbackData) => {
// Axis restrictions
const canDragX = this.props.axis === 'both' || this.props.axis === 'x';
const canDragY = this.props.axis === 'both' || this.props.axis === 'y';
// Update w/h
let width = this.state.width + (canDragX ? deltaX : 0);
let height = this.state.height + (canDragY ? deltaY : 0);
// Early return if no change
const widthChanged = width !== this.state.width, heightChanged = height !== this.state.height;
if (handlerName === 'onResize' && !widthChanged && !heightChanged) return;
[width, height] = this.runConstraints(width, height);
// Set the appropriate state for this handler.
const newState = {};
if (handlerName === 'onResizeStart') {
newState.resizing = true;
} else if (handlerName === 'onResizeStop') {
newState.resizing = false;
newState.slackW = newState.slackH = 0;
} else {
// Early return if no change after constraints
if (width === this.state.width && height === this.state.height) return;
newState.width = width;
newState.height = height;
}
const hasCb = typeof this.props[handlerName] === 'function';
if (hasCb) {
if (typeof e.persist === 'function') e.persist();
this.setState(newState, () => this.props[handlerName](e, {node, size: {width, height}}));
} else {
this.setState(newState);
}
e.stopPropagation();
};
}
render(): ReactNode {
// eslint-disable-next-line no-unused-vars
const {children, draggableOpts, width, height, handleSize,
lockAspectRatio, axis, minConstraints, maxConstraints, onResize,
onResizeStop, onResizeStart, ...p} = this.props;
const className = p.className ?
`${p.className} react-resizable`:
'react-resizable';
// What we're doing here is getting the child of this element, and cloning it with this element's props.
// We are then defining its children as:
// Its original children (resizable's child's children), and
// A draggable handle.
return cloneElement(children, {
...p,
className,
children: [
children.props.children,
<DraggableCore
{...draggableOpts}
key="resizableHandle"
onStop={this.resizeHandler('onResizeStop')}
onStart={this.resizeHandler('onResizeStart')}
onDrag={this.resizeHandler('onResize')}
>
<span className="react-resizable-handle" />
</DraggableCore>
]
});
}
}