react-resizable 1.10.1 → 1.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/.eslintrc +10 -6
  2. package/.flowconfig +8 -8
  3. package/CHANGELOG.md +48 -37
  4. package/README.md +13 -3
  5. package/__tests__/Resizable.test.js +245 -0
  6. package/__tests__/ResizableBox.test.js +99 -0
  7. package/__tests__/__snapshots__/Resizable.test.js.snap +29 -0
  8. package/__tests__/__snapshots__/ResizableBox.test.js.snap +23 -0
  9. package/build/Resizable.js +100 -134
  10. package/build/Resizable.js.flow +87 -161
  11. package/build/ResizableBox.js +32 -29
  12. package/build/ResizableBox.js.flow +44 -28
  13. package/build/propTypes.js +112 -0
  14. package/build/propTypes.js.flow +135 -0
  15. package/build/utils.js +3 -3
  16. package/coverage/clover.xml +107 -0
  17. package/coverage/coverage-final.json +5 -0
  18. package/coverage/lcov-report/Resizable.js.html +665 -0
  19. package/coverage/lcov-report/ResizableBox.js.html +374 -0
  20. package/coverage/lcov-report/base.css +224 -0
  21. package/coverage/lcov-report/block-navigation.js +79 -0
  22. package/coverage/lcov-report/favicon.png +0 -0
  23. package/coverage/lcov-report/flow-typed/npm/index.html +111 -0
  24. package/coverage/lcov-report/flow-typed/npm/jest_v26.x.x.js.html +3734 -0
  25. package/coverage/lcov-report/index.html +156 -0
  26. package/coverage/lcov-report/prettify.css +1 -0
  27. package/coverage/lcov-report/prettify.js +2 -0
  28. package/coverage/lcov-report/propTypes.js.html +485 -0
  29. package/coverage/lcov-report/react-resizable/dist/bundle.js.html +95 -0
  30. package/coverage/lcov-report/react-resizable/dist/index.html +111 -0
  31. package/coverage/lcov-report/react-resizable/flow-typed/npm/index.html +111 -0
  32. package/coverage/lcov-report/react-resizable/flow-typed/npm/jest_v26.x.x.js.html +3734 -0
  33. package/coverage/lcov-report/react-resizable/index.html +111 -0
  34. package/coverage/lcov-report/react-resizable/index.js.html +101 -0
  35. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  36. package/coverage/lcov-report/sorter.js +170 -0
  37. package/coverage/lcov-report/utils.js.html +122 -0
  38. package/coverage/lcov.info +233 -0
  39. package/dist/bundle.js +6 -0
  40. package/flow-typed/npm/jest_v26.x.x.js +1218 -0
  41. package/package.json +26 -20
  42. package/setupTests/enzyme.js +4 -0
  43. package/index.html +0 -15
@@ -1,103 +1,13 @@
1
1
  // @flow
2
2
  import React from 'react';
3
- import PropTypes from 'prop-types';
3
+ import type {Node as ReactNode} from 'react';
4
4
  import {DraggableCore} from 'react-draggable';
5
5
  import {cloneElement} from './utils';
6
- import type {Element as ReactElement, Node as ReactNode} from 'react';
6
+ import {resizableProps} from "./propTypes";
7
+ import type {ResizeHandleAxis, Props, ResizableState, DragCallbackData} from './propTypes';
7
8
 
8
- type Axis = 'both' | 'x' | 'y' | 'none';
9
- type ResizeHandle = 's' | 'w' | 'e' | 'n' | 'sw' | 'nw' | 'se' | 'ne';
10
- type State = {
11
- slackW: number, slackH: number,
12
- };
13
- type DragCallbackData = {
14
- node: HTMLElement,
15
- x: number, y: number,
16
- deltaX: number, deltaY: number,
17
- lastX: number, lastY: number
18
- };
19
- export type ResizeCallbackData = {
20
- node: HTMLElement,
21
- size: {width: number, height: number},
22
- handle: ResizeHandle
23
- };
24
- export type Props = {
25
- children: ReactElement<any>,
26
- className?: ?string,
27
- width: number,
28
- height: number,
29
- handle: ReactElement<any> | (resizeHandle: ResizeHandle) => ReactElement<any>,
30
- handleSize: [number, number],
31
- lockAspectRatio: boolean,
32
- axis: Axis,
33
- minConstraints: [number, number],
34
- maxConstraints: [number, number],
35
- onResizeStop?: ?(e: SyntheticEvent<>, data: ResizeCallbackData) => any,
36
- onResizeStart?: ?(e: SyntheticEvent<>, data: ResizeCallbackData) => any,
37
- onResize?: ?(e: SyntheticEvent<>, data: ResizeCallbackData) => any,
38
- draggableOpts?: ?Object,
39
- resizeHandles: ResizeHandle[],
40
- transformScale: number,
41
- };
42
-
43
- export default class Resizable extends React.Component<Props, State> {
44
- static propTypes = {
45
- //
46
- // Required Props
47
- //
48
-
49
- // Require that one and only one child be present.
50
- children: PropTypes.element.isRequired,
51
-
52
- // Initial w/h
53
- width: PropTypes.number.isRequired,
54
- height: PropTypes.number.isRequired,
55
-
56
- //
57
- // Optional props
58
- //
59
-
60
- // Custom resize handle
61
- handle: PropTypes.element,
62
-
63
- // If you change this, be sure to update your css
64
- handleSize: PropTypes.array,
65
-
66
- // Defines which resize handles should be rendered (default: 'se')
67
- // Allows for any combination of:
68
- // 's' - South handle (bottom-center)
69
- // 'w' - West handle (left-center)
70
- // 'e' - East handle (right-center)
71
- // 'n' - North handle (top-center)
72
- // 'sw' - Southwest handle (bottom-left)
73
- // 'nw' - Northwest handle (top-left)
74
- // 'se' - Southeast handle (bottom-right)
75
- // 'ne' - Northeast handle (top-center)
76
- resizeHandles: PropTypes.arrayOf(PropTypes.oneOf(['s', 'w', 'e', 'n', 'sw', 'nw', 'se', 'ne'])),
77
- transformScale: PropTypes.number,
78
-
79
- // If true, will only allow width/height to move in lockstep
80
- lockAspectRatio: PropTypes.bool,
81
-
82
- // Restricts resizing to a particular axis (default: 'both')
83
- // 'both' - allows resizing by width or height
84
- // 'x' - only allows the width to be changed
85
- // 'y' - only allows the height to be changed
86
- // 'none' - disables resizing altogether
87
- axis: PropTypes.oneOf(['both', 'x', 'y', 'none']),
88
-
89
- // Min/max size
90
- minConstraints: PropTypes.arrayOf(PropTypes.number),
91
- maxConstraints: PropTypes.arrayOf(PropTypes.number),
92
-
93
- // Callbacks
94
- onResizeStop: PropTypes.func,
95
- onResizeStart: PropTypes.func,
96
- onResize: PropTypes.func,
97
-
98
- // These will be passed wholesale to react-draggable's DraggableCore
99
- draggableOpts: PropTypes.object
100
- };
9
+ export default class Resizable extends React.Component<Props, ResizableState> {
10
+ static propTypes = resizableProps;
101
11
 
102
12
  static defaultProps = {
103
13
  handleSize: [20, 20],
@@ -109,9 +19,14 @@ export default class Resizable extends React.Component<Props, State> {
109
19
  transformScale: 1
110
20
  };
111
21
 
112
- state: State = {
113
- slackW: 0, slackH: 0
114
- };
22
+ state: ResizableState = undefined;
23
+
24
+ lastHandleRect: ?ClientRect = null;
25
+ slack: ?[number, number] = null;
26
+
27
+ componentWillUnmount() {
28
+ this.resetData();
29
+ }
115
30
 
116
31
  lockAspectRatio(width: number, height: number, aspectRatio: number): [number, number] {
117
32
  height = width / aspectRatio;
@@ -119,14 +34,19 @@ export default class Resizable extends React.Component<Props, State> {
119
34
  return [width, height];
120
35
  }
121
36
 
122
- // If you do this, be careful of constraints
37
+ resetData() {
38
+ this.lastHandleRect = this.slack = null;
39
+ }
40
+
41
+ // Clamp width and height within provided constraints
123
42
  runConstraints(width: number, height: number): [number, number] {
124
43
  const [min, max] = [this.props.minConstraints, this.props.maxConstraints];
125
44
  if (!min && !max) return [width, height];
126
45
 
127
- // Fit width & height to aspect ratio
46
+ // If constraining to min and max, we need to also fit width and height to aspect ratio.
128
47
  if (this.props.lockAspectRatio) {
129
- if (height === this.props.height) {
48
+ const resizingHorizontally = height === this.props.height;
49
+ if (resizingHorizontally) {
130
50
  const ratio = this.props.width / this.props.height;
131
51
  height = width / ratio;
132
52
  width = height * ratio;
@@ -144,7 +64,7 @@ export default class Resizable extends React.Component<Props, State> {
144
64
  // Add slack to the values used to calculate bound position. This will ensure that if
145
65
  // we start removing slack, the element won't react to it right away until it's been
146
66
  // completely removed.
147
- let {slackW, slackH} = this.state;
67
+ let [slackW, slackH] = this.slack || [0, 0];
148
68
  width += slackW;
149
69
  height += slackH;
150
70
 
@@ -157,12 +77,8 @@ export default class Resizable extends React.Component<Props, State> {
157
77
  height = Math.min(max[1], height);
158
78
  }
159
79
 
160
- // If the numbers changed, we must have introduced some slack. Record it for the next iteration.
161
- slackW += (oldW - width);
162
- slackH += (oldH - height);
163
- if (slackW !== this.state.slackW || slackH !== this.state.slackH) {
164
- this.setState({slackW, slackH});
165
- }
80
+ // If the width or height changed, we must have introduced some slack. Record it for the next iteration.
81
+ this.slack = [slackW + (oldW - width), slackH + (oldH - height)];
166
82
 
167
83
  return [width, height];
168
84
  }
@@ -173,75 +89,85 @@ export default class Resizable extends React.Component<Props, State> {
173
89
  * @param {String} handlerName Handler name to wrap.
174
90
  * @return {Function} Handler function.
175
91
  */
176
- resizeHandler(handlerName: string, axis: ResizeHandle): Function {
177
- return (e: SyntheticEvent<> | MouseEvent, {node, deltaX, deltaY}: DragCallbackData) => {
178
- deltaX /= this.props.transformScale;
179
- deltaY /= this.props.transformScale;
92
+ resizeHandler(handlerName: 'onResize' | 'onResizeStart' | 'onResizeStop', axis: ResizeHandleAxis): Function {
93
+ return (e: SyntheticEvent<>, {node, deltaX, deltaY}: DragCallbackData) => {
94
+ // Reset data in case it was left over somehow (should not be possible)
95
+ if (handlerName === 'onResizeStart') this.resetData();
180
96
 
181
97
  // Axis restrictions
182
- const canDragX = (this.props.axis === 'both' || this.props.axis === 'x') && ['n', 's'].indexOf(axis) === -1;
183
- const canDragY = (this.props.axis === 'both' || this.props.axis === 'y') && ['e', 'w'].indexOf(axis) === -1;
184
-
185
- // reverse delta if using top or left drag handles
186
- if (canDragX && axis[axis.length - 1] === 'w') {
187
- deltaX = -deltaX;
188
- }
189
- if (canDragY && axis[0] === 'n') {
190
- deltaY = -deltaY;
98
+ const canDragX = (this.props.axis === 'both' || this.props.axis === 'x') && axis !== 'n' && axis !== 's';
99
+ const canDragY = (this.props.axis === 'both' || this.props.axis === 'y') && axis !== 'e' && axis !== 'w';
100
+ // No dragging possible.
101
+ if (!canDragX && !canDragY) return;
102
+
103
+ // Decompose axis for later use
104
+ const axisV = axis[0];
105
+ const axisH = axis[axis.length - 1]; // intentionally not axis[1], so that this catches axis === 'w' for example
106
+
107
+ // Track the element being dragged to account for changes in position.
108
+ // If a handle's position is changed between callbacks, we need to factor this in to the next callback.
109
+ // Failure to do so will cause the element to "skip" when resized upwards or leftwards.
110
+ const handleRect = node.getBoundingClientRect();
111
+ if (this.lastHandleRect != null) {
112
+ // If the handle has repositioned on either axis since last render,
113
+ // we need to increase our callback values by this much.
114
+ // Only checking 'n', 'w' since resizing by 's', 'w' won't affect the overall position on page,
115
+ if (axisH === 'w') {
116
+ const deltaLeftSinceLast = handleRect.left - this.lastHandleRect.left;
117
+ deltaX += deltaLeftSinceLast;
118
+ }
119
+ if (axisV === 'n') {
120
+ const deltaTopSinceLast = handleRect.top - this.lastHandleRect.top;
121
+ deltaY += deltaTopSinceLast;
122
+ }
191
123
  }
124
+ // Storage of last rect so we know how much it has really moved.
125
+ this.lastHandleRect = handleRect;
192
126
 
193
- // Update w/h
194
- let width = this.props.width + (canDragX ? deltaX : 0);
195
- let height = this.props.height + (canDragY ? deltaY : 0);
127
+ // Reverse delta if using top or left drag handles.
128
+ if (axisH === 'w') deltaX = -deltaX;
129
+ if (axisV === 'n') deltaY = -deltaY;
196
130
 
197
- // Early return if no change
198
- const widthChanged = width !== this.props.width, heightChanged = height !== this.props.height;
199
- if (handlerName === 'onResize' && !widthChanged && !heightChanged) return;
131
+ // Update w/h by the deltas. Also factor in transformScale.
132
+ let width = this.props.width + (canDragX ? deltaX / this.props.transformScale : 0);
133
+ let height = this.props.height + (canDragY ? deltaY / this.props.transformScale : 0);
200
134
 
135
+ // Run user-provided constraints.
201
136
  [width, height] = this.runConstraints(width, height);
202
137
 
203
- // Set the appropriate state for this handler.
204
- const newState = {};
205
- if (handlerName === 'onResizeStart') {
206
- // nothing
207
- } else if (handlerName === 'onResizeStop') {
208
- newState.slackW = newState.slackH = 0;
209
- } else {
210
- // Early return if no change after constraints
211
- if (width === this.props.width && height === this.props.height) return;
212
- }
138
+ const dimensionsChanged = width !== this.props.width || height !== this.props.height;
213
139
 
214
- const hasCb = typeof this.props[handlerName] === 'function';
215
- if (hasCb) {
216
- // $FlowIgnore isn't refining this correctly to SyntheticEvent
140
+ // Call user-supplied callback if present.
141
+ const cb = typeof this.props[handlerName] === 'function' ? this.props[handlerName] : null;
142
+ // Don't call 'onResize' if dimensions haven't changed.
143
+ const shouldSkipCb = handlerName === 'onResize' && !dimensionsChanged;
144
+ if (cb && !shouldSkipCb) {
217
145
  if (typeof e.persist === 'function') e.persist();
218
- this.setState(newState, () => this.props[handlerName](e, {node, size: {width, height}, handle: axis}));
219
- } else {
220
- this.setState(newState);
146
+ cb(e, {node, size: {width, height}, handle: axis});
221
147
  }
148
+
149
+ // Reset internal data
150
+ if (handlerName === 'onResizeStop') this.resetData();
222
151
  };
223
152
  }
224
153
 
225
- renderResizeHandle(resizeHandle: ResizeHandle): ReactNode {
154
+ renderResizeHandle(resizeHandleAxis: ResizeHandleAxis): ReactNode {
226
155
  const {handle} = this.props;
227
156
  if (handle) {
228
157
  if (typeof handle === 'function') {
229
- return handle(resizeHandle);
158
+ return handle(resizeHandleAxis);
230
159
  }
231
160
  return handle;
232
161
  }
233
- return <span className={`react-resizable-handle react-resizable-handle-${resizeHandle}`} />;
162
+ return <span className={`react-resizable-handle react-resizable-handle-${resizeHandleAxis}`} />;
234
163
  }
235
164
 
236
165
  render(): ReactNode {
166
+ // Pass along only props not meant for the `<Resizable>`.`
237
167
  // eslint-disable-next-line no-unused-vars
238
- const {children, draggableOpts, width, height, handleSize,
239
- lockAspectRatio, axis, minConstraints, maxConstraints, onResize,
240
- onResizeStop, onResizeStart, resizeHandles, transformScale, ...p} = this.props;
241
-
242
- const className = p.className ?
243
- `${p.className} react-resizable`:
244
- 'react-resizable';
168
+ const {children, className, draggableOpts, width, height, handle, handleSize,
169
+ lockAspectRatio, axis, minConstraints, maxConstraints, onResize,
170
+ onResizeStop, onResizeStart, resizeHandles, transformScale, ...p} = this.props;
245
171
 
246
172
  // What we're doing here is getting the child of this element, and cloning it with this element's props.
247
173
  // We are then defining its children as:
@@ -249,18 +175,18 @@ export default class Resizable extends React.Component<Props, State> {
249
175
  // One or more draggable handles.
250
176
  return cloneElement(children, {
251
177
  ...p,
252
- className,
178
+ className: `${className ? `${className} ` : ''}react-resizable`,
253
179
  children: [
254
- children.props.children,
255
- resizeHandles.map(h => (
180
+ ...children.props.children,
181
+ ...resizeHandles.map((handleAxis) => (
256
182
  <DraggableCore
257
183
  {...draggableOpts}
258
- key={`resizableHandle-${h}`}
259
- onStop={this.resizeHandler('onResizeStop', h)}
260
- onStart={this.resizeHandler('onResizeStart', h)}
261
- onDrag={this.resizeHandler('onResize', h)}
184
+ key={`resizableHandle-${handleAxis}`}
185
+ onStop={this.resizeHandler('onResizeStop', handleAxis)}
186
+ onStart={this.resizeHandler('onResizeStart', handleAxis)}
187
+ onDrag={this.resizeHandler('onResize', handleAxis)}
262
188
  >
263
- {this.renderResizeHandle(h)}
189
+ {this.renderResizeHandle(handleAxis)}
264
190
  </DraggableCore>
265
191
  ))
266
192
  ]
@@ -3,16 +3,26 @@
3
3
  exports.__esModule = true;
4
4
  exports.default = void 0;
5
5
 
6
- var _react = _interopRequireDefault(require("react"));
6
+ var React = _interopRequireWildcard(require("react"));
7
7
 
8
8
  var _propTypes = _interopRequireDefault(require("prop-types"));
9
9
 
10
10
  var _Resizable = _interopRequireDefault(require("./Resizable"));
11
11
 
12
+ var _propTypes2 = require("./propTypes");
13
+
12
14
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
15
 
16
+ function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
17
+
18
+ function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
19
+
14
20
  function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
15
21
 
22
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
23
+
24
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
25
+
16
26
  function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
17
27
 
18
28
  function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
@@ -21,10 +31,7 @@ function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.crea
21
31
 
22
32
  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
23
33
 
24
- // An example use of Resizable.
25
- var ResizableBox =
26
- /*#__PURE__*/
27
- function (_React$Component) {
34
+ var ResizableBox = /*#__PURE__*/function (_React$Component) {
28
35
  _inheritsLoose(ResizableBox, _React$Component);
29
36
 
30
37
  function ResizableBox() {
@@ -45,8 +52,6 @@ function (_React$Component) {
45
52
 
46
53
  _defineProperty(_assertThisInitialized(_this), "onResize", function (e, data) {
47
54
  var size = data.size;
48
- var width = size.width,
49
- height = size.height;
50
55
 
51
56
  if (_this.props.onResize) {
52
57
  e.persist && e.persist();
@@ -96,40 +101,38 @@ function (_React$Component) {
96
101
  width = _this$props.width,
97
102
  height = _this$props.height,
98
103
  resizeHandles = _this$props.resizeHandles,
99
- props = _objectWithoutPropertiesLoose(_this$props, ["handle", "handleSize", "onResize", "onResizeStart", "onResizeStop", "draggableOpts", "minConstraints", "maxConstraints", "lockAspectRatio", "axis", "width", "height", "resizeHandles"]);
104
+ style = _this$props.style,
105
+ transformScale = _this$props.transformScale,
106
+ props = _objectWithoutPropertiesLoose(_this$props, ["handle", "handleSize", "onResize", "onResizeStart", "onResizeStop", "draggableOpts", "minConstraints", "maxConstraints", "lockAspectRatio", "axis", "width", "height", "resizeHandles", "style", "transformScale"]);
100
107
 
101
- return _react.default.createElement(_Resizable.default, {
108
+ return /*#__PURE__*/React.createElement(_Resizable.default, {
109
+ axis: axis,
110
+ draggableOpts: draggableOpts,
102
111
  handle: handle,
103
112
  handleSize: handleSize,
104
- width: this.state.width,
105
113
  height: this.state.height,
114
+ lockAspectRatio: lockAspectRatio,
115
+ maxConstraints: maxConstraints,
116
+ minConstraints: minConstraints,
106
117
  onResizeStart: onResizeStart,
107
118
  onResize: this.onResize,
108
119
  onResizeStop: onResizeStop,
109
- draggableOpts: draggableOpts,
110
- minConstraints: minConstraints,
111
- maxConstraints: maxConstraints,
112
- lockAspectRatio: lockAspectRatio,
113
- axis: axis,
114
- resizeHandles: resizeHandles
115
- }, _react.default.createElement("div", _extends({
116
- style: {
120
+ resizeHandles: resizeHandles,
121
+ transformScale: transformScale,
122
+ width: this.state.width
123
+ }, /*#__PURE__*/React.createElement("div", _extends({}, props, {
124
+ style: _objectSpread(_objectSpread({}, style), {}, {
117
125
  width: this.state.width + 'px',
118
126
  height: this.state.height + 'px'
119
- }
120
- }, props)));
127
+ })
128
+ })));
121
129
  };
122
130
 
123
131
  return ResizableBox;
124
- }(_react.default.Component);
132
+ }(React.Component);
125
133
 
126
134
  exports.default = ResizableBox;
127
135
 
128
- _defineProperty(ResizableBox, "propTypes", {
129
- height: _propTypes.default.number,
130
- width: _propTypes.default.number
131
- });
132
-
133
- _defineProperty(ResizableBox, "defaultProps", {
134
- handleSize: [20, 20]
135
- });
136
+ _defineProperty(ResizableBox, "propTypes", _objectSpread(_objectSpread({}, _propTypes2.resizableProps), {}, {
137
+ children: _propTypes.default.element
138
+ }));
@@ -1,34 +1,34 @@
1
1
  // @flow
2
- import React from 'react';
2
+ import * as React from 'react';
3
+ import type {Node as ReactNode, Element as ReactElement} from 'react';
3
4
  import PropTypes from 'prop-types';
5
+
4
6
  import Resizable from './Resizable';
5
- import type {Props as ResizableProps, ResizeCallbackData} from './Resizable';
6
- import type {Node as ReactNode} from 'react';
7
+ import {resizableProps} from "./propTypes";
8
+ import type {ResizeCallbackData, ResizableBoxState} from './propTypes';
7
9
 
8
- type State = {
9
- width: number, height: number,
10
- propsWidth: number, propsHeight: number,
11
- };
10
+ // ElementConfig gives us an object type where all items present in `defaultProps` are made optional.
11
+ // <ResizableBox> does not have defaultProps, so we can use this type to tell Flow that we don't
12
+ // care about that and will handle it in <Resizable> instead.
13
+ // A <ResizableBox> can also have a `style` property.
14
+ type ResizableBoxProps = {|...React.ElementConfig<typeof Resizable>, style?: Object, children?: ReactElement<any>|};
12
15
 
13
- // An example use of Resizable.
14
- export default class ResizableBox extends React.Component<ResizableProps, State> {
15
- static propTypes = {
16
- height: PropTypes.number,
17
- width: PropTypes.number
18
- };
16
+ export default class ResizableBox extends React.Component<ResizableBoxProps, ResizableBoxState> {
19
17
 
20
- static defaultProps = {
21
- handleSize: [20,20]
18
+ // PropTypes are identical to <Resizable>, except that children are not strictly required to be present.
19
+ static propTypes = {
20
+ ...resizableProps,
21
+ children: PropTypes.element,
22
22
  };
23
23
 
24
- state: State = {
24
+ state: ResizableBoxState = {
25
25
  width: this.props.width,
26
26
  height: this.props.height,
27
27
  propsWidth: this.props.width,
28
28
  propsHeight: this.props.height,
29
29
  };
30
30
 
31
- static getDerivedStateFromProps(props: ResizableProps, state: State) {
31
+ static getDerivedStateFromProps(props: ResizableBoxProps, state: ResizableBoxState) {
32
32
  // If parent changes height/width, set that in our state.
33
33
  if (state.propsWidth !== props.width || state.propsHeight !== props.height) {
34
34
  return {
@@ -43,8 +43,6 @@ export default class ResizableBox extends React.Component<ResizableProps, State>
43
43
 
44
44
  onResize = (e: SyntheticEvent<>, data: ResizeCallbackData) => {
45
45
  const {size} = data;
46
- const {width, height} = size;
47
-
48
46
  if (this.props.onResize) {
49
47
  e.persist && e.persist();
50
48
  this.setState(size, () => this.props.onResize && this.props.onResize(e, data));
@@ -57,25 +55,43 @@ export default class ResizableBox extends React.Component<ResizableProps, State>
57
55
  // Basic wrapper around a Resizable instance.
58
56
  // If you use Resizable directly, you are responsible for updating the child component
59
57
  // with a new width and height.
60
- const {handle, handleSize, onResize, onResizeStart, onResizeStop, draggableOpts, minConstraints,
61
- maxConstraints, lockAspectRatio, axis, width, height, resizeHandles, ...props} = this.props;
58
+ const {
59
+ handle,
60
+ handleSize,
61
+ onResize,
62
+ onResizeStart,
63
+ onResizeStop,
64
+ draggableOpts,
65
+ minConstraints,
66
+ maxConstraints,
67
+ lockAspectRatio,
68
+ axis,
69
+ width,
70
+ height,
71
+ resizeHandles,
72
+ style,
73
+ transformScale,
74
+ ...props
75
+ } = this.props;
76
+
62
77
  return (
63
78
  <Resizable
79
+ axis={axis}
80
+ draggableOpts={draggableOpts}
64
81
  handle={handle}
65
82
  handleSize={handleSize}
66
- width={this.state.width}
67
83
  height={this.state.height}
84
+ lockAspectRatio={lockAspectRatio}
85
+ maxConstraints={maxConstraints}
86
+ minConstraints={minConstraints}
68
87
  onResizeStart={onResizeStart}
69
88
  onResize={this.onResize}
70
89
  onResizeStop={onResizeStop}
71
- draggableOpts={draggableOpts}
72
- minConstraints={minConstraints}
73
- maxConstraints={maxConstraints}
74
- lockAspectRatio={lockAspectRatio}
75
- axis={axis}
76
90
  resizeHandles={resizeHandles}
91
+ transformScale={transformScale}
92
+ width={this.state.width}
77
93
  >
78
- <div style={{width: this.state.width + 'px', height: this.state.height + 'px'}} {...props} />
94
+ <div {...props} style={{...style, width: this.state.width + 'px', height: this.state.height + 'px'}} />
79
95
  </Resizable>
80
96
  );
81
97
  }
@@ -0,0 +1,112 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.resizableProps = void 0;
5
+
6
+ var _propTypes = _interopRequireDefault(require("prop-types"));
7
+
8
+ var _reactDraggable = require("react-draggable");
9
+
10
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
+
12
+ var resizableProps = {
13
+ /*
14
+ * Restricts resizing to a particular axis (default: 'both')
15
+ * 'both' - allows resizing by width or height
16
+ * 'x' - only allows the width to be changed
17
+ * 'y' - only allows the height to be changed
18
+ * 'none' - disables resizing altogether
19
+ * */
20
+ axis: _propTypes.default.oneOf(['both', 'x', 'y', 'none']),
21
+ className: _propTypes.default.string,
22
+
23
+ /*
24
+ * Require that one and only one child be present.
25
+ * */
26
+ children: _propTypes.default.element.isRequired,
27
+
28
+ /*
29
+ * These will be passed wholesale to react-draggable's DraggableCore
30
+ * */
31
+ draggableOpts: _propTypes.default.shape({
32
+ allowAnyClick: _propTypes.default.bool,
33
+ cancel: _propTypes.default.string,
34
+ children: _propTypes.default.node,
35
+ disabled: _propTypes.default.bool,
36
+ enableUserSelectHack: _propTypes.default.bool,
37
+ offsetParent: _propTypes.default.node,
38
+ grid: _propTypes.default.arrayOf(_propTypes.default.number),
39
+ handle: _propTypes.default.string,
40
+ nodeRef: _propTypes.default.object,
41
+ onStart: _propTypes.default.func,
42
+ onDrag: _propTypes.default.func,
43
+ onStop: _propTypes.default.func,
44
+ onMouseDown: _propTypes.default.func,
45
+ scale: _propTypes.default.number
46
+ }),
47
+
48
+ /*
49
+ * Initial height
50
+ * */
51
+ height: _propTypes.default.number.isRequired,
52
+
53
+ /*
54
+ * Customize cursor resize handle
55
+ * */
56
+ handle: _propTypes.default.oneOfType([_propTypes.default.node, _propTypes.default.func]),
57
+
58
+ /*
59
+ * If you change this, be sure to update your css
60
+ * */
61
+ handleSize: _propTypes.default.arrayOf(_propTypes.default.number),
62
+ lockAspectRatio: _propTypes.default.bool,
63
+
64
+ /*
65
+ * Max X & Y measure
66
+ * */
67
+ maxConstraints: _propTypes.default.arrayOf(_propTypes.default.number),
68
+
69
+ /*
70
+ * Min X & Y measure
71
+ * */
72
+ minConstraints: _propTypes.default.arrayOf(_propTypes.default.number),
73
+
74
+ /*
75
+ * Called on stop resize event
76
+ * */
77
+ onResizeStop: _propTypes.default.func,
78
+
79
+ /*
80
+ * Called on start resize event
81
+ * */
82
+ onResizeStart: _propTypes.default.func,
83
+
84
+ /*
85
+ * Called on resize event
86
+ * */
87
+ onResize: _propTypes.default.func,
88
+
89
+ /*
90
+ * Defines which resize handles should be rendered (default: 'se')
91
+ * 's' - South handle (bottom-center)
92
+ * 'w' - West handle (left-center)
93
+ * 'e' - East handle (right-center)
94
+ * 'n' - North handle (top-center)
95
+ * 'sw' - Southwest handle (bottom-left)
96
+ * 'nw' - Northwest handle (top-left)
97
+ * 'se' - Southeast handle (bottom-right)
98
+ * 'ne' - Northeast handle (top-center)
99
+ * */
100
+ resizeHandles: _propTypes.default.arrayOf(_propTypes.default.oneOf(['s', 'w', 'e', 'n', 'sw', 'nw', 'se', 'ne'])),
101
+
102
+ /*
103
+ * If `transform: scale(n)` is set on the parent, this should be set to `n`.
104
+ * */
105
+ transformScale: _propTypes.default.number,
106
+
107
+ /*
108
+ * Initial width
109
+ */
110
+ width: _propTypes.default.number.isRequired
111
+ };
112
+ exports.resizableProps = resizableProps;