react-grid-layout 1.3.4 → 1.4.1

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.
package/build/GridItem.js CHANGED
@@ -1,542 +1,549 @@
1
1
  "use strict";
2
2
 
3
- function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
4
-
5
3
  Object.defineProperty(exports, "__esModule", {
6
4
  value: true
7
5
  });
8
6
  exports.default = void 0;
9
-
10
7
  var _react = _interopRequireDefault(require("react"));
11
-
12
8
  var _propTypes = _interopRequireDefault(require("prop-types"));
13
-
14
9
  var _reactDraggable = require("react-draggable");
15
-
16
10
  var _reactResizable = require("react-resizable");
17
-
18
11
  var _utils = require("./utils");
19
-
20
12
  var _calculateUtils = require("./calculateUtils");
21
-
22
13
  var _ReactGridLayoutPropTypes = require("./ReactGridLayoutPropTypes");
23
-
24
14
  var _clsx = _interopRequireDefault(require("clsx"));
25
-
26
15
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
27
-
28
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
29
-
30
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
31
-
32
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
33
-
34
- function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
35
-
36
- function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
37
-
38
- function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
39
-
40
- function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
41
-
42
- function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
43
-
44
- function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
45
-
46
- function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
47
-
48
- function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
49
-
50
- function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
51
-
52
- 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; }
53
-
16
+ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
17
+ function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
18
+ function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
19
+ /*:: import type { Element as ReactElement, Node as ReactNode } from "react";*/
20
+ /*:: import type {
21
+ ReactDraggableCallbackData,
22
+ GridDragEvent,
23
+ GridResizeEvent,
24
+ DroppingPosition,
25
+ Position,
26
+ ResizeHandleAxis
27
+ } from "./utils";*/
28
+ /*:: import type { PositionParams } from "./calculateUtils";*/
29
+ /*:: import type { ResizeHandle, ReactRef } from "./ReactGridLayoutPropTypes";*/
30
+ /*:: type PartialPosition = { top: number, left: number };*/
31
+ /*:: type GridItemCallback<Data: GridDragEvent | GridResizeEvent> = (
32
+ i: string,
33
+ w: number,
34
+ h: number,
35
+ Data
36
+ ) => void;*/
37
+ /*:: type ResizeCallbackData = {
38
+ node: HTMLElement,
39
+ size: Position,
40
+ handle: ResizeHandleAxis
41
+ };*/
42
+ /*:: type GridItemResizeCallback = (
43
+ e: Event,
44
+ data: ResizeCallbackData,
45
+ position: Position
46
+ ) => void;*/
47
+ /*:: type State = {
48
+ resizing: ?{ top: number, left: number, width: number, height: number },
49
+ dragging: ?{ top: number, left: number },
50
+ className: string
51
+ };*/
52
+ /*:: type Props = {
53
+ children: ReactElement<any>,
54
+ cols: number,
55
+ containerWidth: number,
56
+ margin: [number, number],
57
+ containerPadding: [number, number],
58
+ rowHeight: number,
59
+ maxRows: number,
60
+ isDraggable: boolean,
61
+ isResizable: boolean,
62
+ isBounded: boolean,
63
+ static?: boolean,
64
+ useCSSTransforms?: boolean,
65
+ usePercentages?: boolean,
66
+ transformScale: number,
67
+ droppingPosition?: DroppingPosition,
68
+
69
+ className: string,
70
+ style?: Object,
71
+ // Draggability
72
+ cancel: string,
73
+ handle: string,
74
+
75
+ x: number,
76
+ y: number,
77
+ w: number,
78
+ h: number,
79
+
80
+ minW: number,
81
+ maxW: number,
82
+ minH: number,
83
+ maxH: number,
84
+ i: string,
85
+
86
+ resizeHandles?: ResizeHandleAxis[],
87
+ resizeHandle?: ResizeHandle,
88
+
89
+ onDrag?: GridItemCallback<GridDragEvent>,
90
+ onDragStart?: GridItemCallback<GridDragEvent>,
91
+ onDragStop?: GridItemCallback<GridDragEvent>,
92
+ onResize?: GridItemCallback<GridResizeEvent>,
93
+ onResizeStart?: GridItemCallback<GridResizeEvent>,
94
+ onResizeStop?: GridItemCallback<GridResizeEvent>
95
+ };*/
96
+ /*:: type DefaultProps = {
97
+ className: string,
98
+ cancel: string,
99
+ handle: string,
100
+ minH: number,
101
+ minW: number,
102
+ maxH: number,
103
+ maxW: number,
104
+ transformScale: number
105
+ };*/
54
106
  /**
55
107
  * An individual item within a ReactGridLayout.
56
108
  */
57
- var GridItem = /*#__PURE__*/function (_React$Component) {
58
- _inherits(GridItem, _React$Component);
59
-
60
- var _super = _createSuper(GridItem);
61
-
62
- function GridItem() {
63
- var _this;
64
-
65
- _classCallCheck(this, GridItem);
66
-
67
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
68
- args[_key] = arguments[_key];
69
- }
70
-
71
- _this = _super.call.apply(_super, [this].concat(args));
72
-
73
- _defineProperty(_assertThisInitialized(_this), "state", {
109
+ class GridItem extends _react.default.Component /*:: <Props, State>*/{
110
+ constructor() {
111
+ super(...arguments);
112
+ _defineProperty(this, "state", {
74
113
  resizing: null,
75
114
  dragging: null,
76
115
  className: ""
77
116
  });
78
-
79
- _defineProperty(_assertThisInitialized(_this), "elementRef", /*#__PURE__*/_react.default.createRef());
80
-
81
- _defineProperty(_assertThisInitialized(_this), "onDragStart", function (e, _ref) {
82
- var node = _ref.node;
83
- var _this$props = _this.props,
84
- onDragStart = _this$props.onDragStart,
85
- transformScale = _this$props.transformScale;
117
+ _defineProperty(this, "elementRef", /*#__PURE__*/_react.default.createRef());
118
+ /**
119
+ * onDragStart event handler
120
+ * @param {Event} e event data
121
+ * @param {Object} callbackData an object with node, delta and position information
122
+ */
123
+ _defineProperty(this, "onDragStart", (e, _ref) => {
124
+ let {
125
+ node
126
+ } = _ref;
127
+ const {
128
+ onDragStart,
129
+ transformScale
130
+ } = this.props;
86
131
  if (!onDragStart) return;
87
- var newPosition
88
- /*: PartialPosition*/
89
- = {
132
+ const newPosition /*: PartialPosition*/ = {
90
133
  top: 0,
91
134
  left: 0
92
- }; // TODO: this wont work on nested parents
135
+ };
93
136
 
94
- var offsetParent = node.offsetParent;
137
+ // TODO: this wont work on nested parents
138
+ const {
139
+ offsetParent
140
+ } = node;
95
141
  if (!offsetParent) return;
96
- var parentRect = offsetParent.getBoundingClientRect();
97
- var clientRect = node.getBoundingClientRect();
98
- var cLeft = clientRect.left / transformScale;
99
- var pLeft = parentRect.left / transformScale;
100
- var cTop = clientRect.top / transformScale;
101
- var pTop = parentRect.top / transformScale;
142
+ const parentRect = offsetParent.getBoundingClientRect();
143
+ const clientRect = node.getBoundingClientRect();
144
+ const cLeft = clientRect.left / transformScale;
145
+ const pLeft = parentRect.left / transformScale;
146
+ const cTop = clientRect.top / transformScale;
147
+ const pTop = parentRect.top / transformScale;
102
148
  newPosition.left = cLeft - pLeft + offsetParent.scrollLeft;
103
149
  newPosition.top = cTop - pTop + offsetParent.scrollTop;
104
-
105
- _this.setState({
150
+ this.setState({
106
151
  dragging: newPosition
107
- }); // Call callback with this data
108
-
109
-
110
- var _calcXY = (0, _calculateUtils.calcXY)(_this.getPositionParams(), newPosition.top, newPosition.left, _this.props.w, _this.props.h),
111
- x = _calcXY.x,
112
- y = _calcXY.y;
152
+ });
113
153
 
114
- return onDragStart.call(_assertThisInitialized(_this), _this.props.i, x, y, {
115
- e: e,
116
- node: node,
117
- newPosition: newPosition
154
+ // Call callback with this data
155
+ const {
156
+ x,
157
+ y
158
+ } = (0, _calculateUtils.calcXY)(this.getPositionParams(), newPosition.top, newPosition.left, this.props.w, this.props.h);
159
+ return onDragStart.call(this, this.props.i, x, y, {
160
+ e,
161
+ node,
162
+ newPosition
118
163
  });
119
164
  });
120
-
121
- _defineProperty(_assertThisInitialized(_this), "onDrag", function (e, _ref2) {
122
- var node = _ref2.node,
123
- deltaX = _ref2.deltaX,
124
- deltaY = _ref2.deltaY;
125
- var onDrag = _this.props.onDrag;
165
+ /**
166
+ * onDrag event handler
167
+ * @param {Event} e event data
168
+ * @param {Object} callbackData an object with node, delta and position information
169
+ */
170
+ _defineProperty(this, "onDrag", (e, _ref2) => {
171
+ let {
172
+ node,
173
+ deltaX,
174
+ deltaY
175
+ } = _ref2;
176
+ const {
177
+ onDrag
178
+ } = this.props;
126
179
  if (!onDrag) return;
127
-
128
- if (!_this.state.dragging) {
180
+ if (!this.state.dragging) {
129
181
  throw new Error("onDrag called before onDragStart.");
130
182
  }
131
-
132
- var top = _this.state.dragging.top + deltaY;
133
- var left = _this.state.dragging.left + deltaX;
134
- var _this$props2 = _this.props,
135
- isBounded = _this$props2.isBounded,
136
- i = _this$props2.i,
137
- w = _this$props2.w,
138
- h = _this$props2.h,
139
- containerWidth = _this$props2.containerWidth;
140
-
141
- var positionParams = _this.getPositionParams(); // Boundary calculations; keeps items within the grid
142
-
143
-
183
+ let top = this.state.dragging.top + deltaY;
184
+ let left = this.state.dragging.left + deltaX;
185
+ const {
186
+ isBounded,
187
+ i,
188
+ w,
189
+ h,
190
+ containerWidth
191
+ } = this.props;
192
+ const positionParams = this.getPositionParams();
193
+
194
+ // Boundary calculations; keeps items within the grid
144
195
  if (isBounded) {
145
- var offsetParent = node.offsetParent;
146
-
196
+ const {
197
+ offsetParent
198
+ } = node;
147
199
  if (offsetParent) {
148
- var _this$props3 = _this.props,
149
- margin = _this$props3.margin,
150
- rowHeight = _this$props3.rowHeight;
151
- var bottomBoundary = offsetParent.clientHeight - (0, _calculateUtils.calcGridItemWHPx)(h, rowHeight, margin[1]);
200
+ const {
201
+ margin,
202
+ rowHeight
203
+ } = this.props;
204
+ const bottomBoundary = offsetParent.clientHeight - (0, _calculateUtils.calcGridItemWHPx)(h, rowHeight, margin[1]);
152
205
  top = (0, _calculateUtils.clamp)(top, 0, bottomBoundary);
153
- var colWidth = (0, _calculateUtils.calcGridColWidth)(positionParams);
154
- var rightBoundary = containerWidth - (0, _calculateUtils.calcGridItemWHPx)(w, colWidth, margin[0]);
206
+ const colWidth = (0, _calculateUtils.calcGridColWidth)(positionParams);
207
+ const rightBoundary = containerWidth - (0, _calculateUtils.calcGridItemWHPx)(w, colWidth, margin[0]);
155
208
  left = (0, _calculateUtils.clamp)(left, 0, rightBoundary);
156
209
  }
157
210
  }
158
-
159
- var newPosition
160
- /*: PartialPosition*/
161
- = {
162
- top: top,
163
- left: left
211
+ const newPosition /*: PartialPosition*/ = {
212
+ top,
213
+ left
164
214
  };
165
-
166
- _this.setState({
215
+ this.setState({
167
216
  dragging: newPosition
168
- }); // Call callback with this data
169
-
170
-
171
- var _calcXY2 = (0, _calculateUtils.calcXY)(positionParams, top, left, w, h),
172
- x = _calcXY2.x,
173
- y = _calcXY2.y;
217
+ });
174
218
 
175
- return onDrag.call(_assertThisInitialized(_this), i, x, y, {
176
- e: e,
177
- node: node,
178
- newPosition: newPosition
219
+ // Call callback with this data
220
+ const {
221
+ x,
222
+ y
223
+ } = (0, _calculateUtils.calcXY)(positionParams, top, left, w, h);
224
+ return onDrag.call(this, i, x, y, {
225
+ e,
226
+ node,
227
+ newPosition
179
228
  });
180
229
  });
181
-
182
- _defineProperty(_assertThisInitialized(_this), "onDragStop", function (e, _ref3) {
183
- var node = _ref3.node;
184
- var onDragStop = _this.props.onDragStop;
230
+ /**
231
+ * onDragStop event handler
232
+ * @param {Event} e event data
233
+ * @param {Object} callbackData an object with node, delta and position information
234
+ */
235
+ _defineProperty(this, "onDragStop", (e, _ref3) => {
236
+ let {
237
+ node
238
+ } = _ref3;
239
+ const {
240
+ onDragStop
241
+ } = this.props;
185
242
  if (!onDragStop) return;
186
-
187
- if (!_this.state.dragging) {
243
+ if (!this.state.dragging) {
188
244
  throw new Error("onDragEnd called before onDragStart.");
189
245
  }
190
-
191
- var _this$props4 = _this.props,
192
- w = _this$props4.w,
193
- h = _this$props4.h,
194
- i = _this$props4.i;
195
- var _this$state$dragging = _this.state.dragging,
196
- left = _this$state$dragging.left,
197
- top = _this$state$dragging.top;
198
- var newPosition
199
- /*: PartialPosition*/
200
- = {
201
- top: top,
202
- left: left
246
+ const {
247
+ w,
248
+ h,
249
+ i
250
+ } = this.props;
251
+ const {
252
+ left,
253
+ top
254
+ } = this.state.dragging;
255
+ const newPosition /*: PartialPosition*/ = {
256
+ top,
257
+ left
203
258
  };
204
-
205
- _this.setState({
259
+ this.setState({
206
260
  dragging: null
207
261
  });
208
-
209
- var _calcXY3 = (0, _calculateUtils.calcXY)(_this.getPositionParams(), top, left, w, h),
210
- x = _calcXY3.x,
211
- y = _calcXY3.y;
212
-
213
- return onDragStop.call(_assertThisInitialized(_this), i, x, y, {
214
- e: e,
215
- node: node,
216
- newPosition: newPosition
262
+ const {
263
+ x,
264
+ y
265
+ } = (0, _calculateUtils.calcXY)(this.getPositionParams(), top, left, w, h);
266
+ return onDragStop.call(this, i, x, y, {
267
+ e,
268
+ node,
269
+ newPosition
217
270
  });
218
271
  });
219
-
220
- _defineProperty(_assertThisInitialized(_this), "onResizeStop", function (e, callbackData) {
221
- _this.onResizeHandler(e, callbackData, "onResizeStop");
222
- });
223
-
224
- _defineProperty(_assertThisInitialized(_this), "onResizeStart", function (e, callbackData) {
225
- _this.onResizeHandler(e, callbackData, "onResizeStart");
226
- });
227
-
228
- _defineProperty(_assertThisInitialized(_this), "onResize", function (e, callbackData) {
229
- _this.onResizeHandler(e, callbackData, "onResize");
230
- });
231
-
232
- return _this;
233
- }
234
-
235
- _createClass(GridItem, [{
236
- key: "shouldComponentUpdate",
237
- value: function shouldComponentUpdate(nextProps
238
- /*: Props*/
239
- , nextState
240
- /*: State*/
241
- )
242
- /*: boolean*/
243
- {
244
- // We can't deeply compare children. If the developer memoizes them, we can
245
- // use this optimization.
246
- if (this.props.children !== nextProps.children) return true;
247
- if (this.props.droppingPosition !== nextProps.droppingPosition) return true; // TODO memoize these calculations so they don't take so long?
248
-
249
- var oldPosition = (0, _calculateUtils.calcGridItemPosition)(this.getPositionParams(this.props), this.props.x, this.props.y, this.props.w, this.props.h, this.state);
250
- var newPosition = (0, _calculateUtils.calcGridItemPosition)(this.getPositionParams(nextProps), nextProps.x, nextProps.y, nextProps.w, nextProps.h, nextState);
251
- return !(0, _utils.fastPositionEqual)(oldPosition, newPosition) || this.props.useCSSTransforms !== nextProps.useCSSTransforms;
252
- }
253
- }, {
254
- key: "componentDidMount",
255
- value: function componentDidMount() {
256
- this.moveDroppingItem({});
257
- }
258
- }, {
259
- key: "componentDidUpdate",
260
- value: function componentDidUpdate(prevProps
261
- /*: Props*/
262
- ) {
263
- this.moveDroppingItem(prevProps);
264
- } // When a droppingPosition is present, this means we should fire a move event, as if we had moved
265
- // this element by `x, y` pixels.
266
-
267
- }, {
268
- key: "moveDroppingItem",
269
- value: function moveDroppingItem(prevProps
270
- /*: Props*/
271
- ) {
272
- var droppingPosition = this.props.droppingPosition;
273
- if (!droppingPosition) return;
274
- var node = this.elementRef.current; // Can't find DOM node (are we unmounted?)
275
-
276
- if (!node) return;
277
- var prevDroppingPosition = prevProps.droppingPosition || {
278
- left: 0,
279
- top: 0
280
- };
281
- var dragging = this.state.dragging;
282
- var shouldDrag = dragging && droppingPosition.left !== prevDroppingPosition.left || droppingPosition.top !== prevDroppingPosition.top;
283
-
284
- if (!dragging) {
285
- this.onDragStart(droppingPosition.e, {
286
- node: node,
287
- deltaX: droppingPosition.left,
288
- deltaY: droppingPosition.top
289
- });
290
- } else if (shouldDrag) {
291
- var deltaX = droppingPosition.left - dragging.left;
292
- var deltaY = droppingPosition.top - dragging.top;
293
- this.onDrag(droppingPosition.e, {
294
- node: node,
295
- deltaX: deltaX,
296
- deltaY: deltaY
297
- });
298
- }
299
- }
300
- }, {
301
- key: "getPositionParams",
302
- value: function getPositionParams()
303
- /*: PositionParams*/
304
- {
305
- var props
306
- /*: Props*/
307
- = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.props;
308
- return {
309
- cols: props.cols,
310
- containerPadding: props.containerPadding,
311
- containerWidth: props.containerWidth,
312
- margin: props.margin,
313
- maxRows: props.maxRows,
314
- rowHeight: props.rowHeight
315
- };
316
- }
317
- /**
318
- * This is where we set the grid item's absolute placement. It gets a little tricky because we want to do it
319
- * well when server rendering, and the only way to do that properly is to use percentage width/left because
320
- * we don't know exactly what the browser viewport is.
321
- * Unfortunately, CSS Transforms, which are great for performance, break in this instance because a percentage
322
- * left is relative to the item itself, not its container! So we cannot use them on the server rendering pass.
323
- *
324
- * @param {Object} pos Position object with width, height, left, top.
325
- * @return {Object} Style object.
326
- */
327
-
328
- }, {
329
- key: "createStyle",
330
- value: function createStyle(pos
331
- /*: Position*/
332
- )
333
- /*: { [key: string]: ?string }*/
334
- {
335
- var _this$props5 = this.props,
336
- usePercentages = _this$props5.usePercentages,
337
- containerWidth = _this$props5.containerWidth,
338
- useCSSTransforms = _this$props5.useCSSTransforms;
339
- var style; // CSS Transforms support (default)
340
-
341
- if (useCSSTransforms) {
342
- style = (0, _utils.setTransform)(pos);
343
- } else {
344
- // top,left (slow)
345
- style = (0, _utils.setTopLeft)(pos); // This is used for server rendering.
346
-
347
- if (usePercentages) {
348
- style.left = (0, _utils.perc)(pos.left / containerWidth);
349
- style.width = (0, _utils.perc)(pos.width / containerWidth);
350
- }
351
- }
352
-
353
- return style;
354
- }
355
272
  /**
356
- * Mix a Draggable instance into a child.
357
- * @param {Element} child Child element.
358
- * @return {Element} Child wrapped in Draggable.
273
+ * onResizeStop event handler
274
+ * @param {Event} e event data
275
+ * @param {Object} callbackData an object with node and size information
359
276
  */
277
+ _defineProperty(this, "onResizeStop", (e, callbackData, position) => this.onResizeHandler(e, callbackData, position, "onResizeStop"));
278
+ // onResizeStart event handler
279
+ _defineProperty(this, "onResizeStart", (e, callbackData, position) => this.onResizeHandler(e, callbackData, position, "onResizeStart"));
280
+ // onResize event handler
281
+ _defineProperty(this, "onResize", (e, callbackData, position) => this.onResizeHandler(e, callbackData, position, "onResize"));
282
+ }
283
+ shouldComponentUpdate(nextProps /*: Props*/, nextState /*: State*/) /*: boolean*/{
284
+ // We can't deeply compare children. If the developer memoizes them, we can
285
+ // use this optimization.
286
+ if (this.props.children !== nextProps.children) return true;
287
+ if (this.props.droppingPosition !== nextProps.droppingPosition) return true;
288
+ // TODO memoize these calculations so they don't take so long?
289
+ const oldPosition = (0, _calculateUtils.calcGridItemPosition)(this.getPositionParams(this.props), this.props.x, this.props.y, this.props.w, this.props.h, this.state);
290
+ const newPosition = (0, _calculateUtils.calcGridItemPosition)(this.getPositionParams(nextProps), nextProps.x, nextProps.y, nextProps.w, nextProps.h, nextState);
291
+ return !(0, _utils.fastPositionEqual)(oldPosition, newPosition) || this.props.useCSSTransforms !== nextProps.useCSSTransforms;
292
+ }
293
+ componentDidMount() {
294
+ this.moveDroppingItem({});
295
+ }
296
+ componentDidUpdate(prevProps /*: Props*/) {
297
+ this.moveDroppingItem(prevProps);
298
+ }
360
299
 
361
- }, {
362
- key: "mixinDraggable",
363
- value: function mixinDraggable(child
364
- /*: ReactElement<any>*/
365
- , isDraggable
366
- /*: boolean*/
367
- )
368
- /*: ReactElement<any>*/
369
- {
370
- return /*#__PURE__*/_react.default.createElement(_reactDraggable.DraggableCore, {
371
- disabled: !isDraggable,
372
- onStart: this.onDragStart,
373
- onDrag: this.onDrag,
374
- onStop: this.onDragStop,
375
- handle: this.props.handle,
376
- cancel: ".react-resizable-handle" + (this.props.cancel ? "," + this.props.cancel : ""),
377
- scale: this.props.transformScale,
378
- nodeRef: this.elementRef
379
- }, child);
300
+ // When a droppingPosition is present, this means we should fire a move event, as if we had moved
301
+ // this element by `x, y` pixels.
302
+ moveDroppingItem(prevProps /*: Props*/) {
303
+ const {
304
+ droppingPosition
305
+ } = this.props;
306
+ if (!droppingPosition) return;
307
+ const node = this.elementRef.current;
308
+ // Can't find DOM node (are we unmounted?)
309
+ if (!node) return;
310
+ const prevDroppingPosition = prevProps.droppingPosition || {
311
+ left: 0,
312
+ top: 0
313
+ };
314
+ const {
315
+ dragging
316
+ } = this.state;
317
+ const shouldDrag = dragging && droppingPosition.left !== prevDroppingPosition.left || droppingPosition.top !== prevDroppingPosition.top;
318
+ if (!dragging) {
319
+ this.onDragStart(droppingPosition.e, {
320
+ node,
321
+ deltaX: droppingPosition.left,
322
+ deltaY: droppingPosition.top
323
+ });
324
+ } else if (shouldDrag) {
325
+ const deltaX = droppingPosition.left - dragging.left;
326
+ const deltaY = droppingPosition.top - dragging.top;
327
+ this.onDrag(droppingPosition.e, {
328
+ node,
329
+ deltaX,
330
+ deltaY
331
+ });
380
332
  }
381
- /**
382
- * Mix a Resizable instance into a child.
383
- * @param {Element} child Child element.
384
- * @param {Object} position Position object (pixel values)
385
- * @return {Element} Child wrapped in Resizable.
386
- */
387
-
388
- }, {
389
- key: "mixinResizable",
390
- value: function mixinResizable(child
391
- /*: ReactElement<any>*/
392
- , position
393
- /*: Position*/
394
- , isResizable
395
- /*: boolean*/
396
- )
397
- /*: ReactElement<any>*/
398
- {
399
- var _this$props6 = this.props,
400
- cols = _this$props6.cols,
401
- x = _this$props6.x,
402
- minW = _this$props6.minW,
403
- minH = _this$props6.minH,
404
- maxW = _this$props6.maxW,
405
- maxH = _this$props6.maxH,
406
- transformScale = _this$props6.transformScale,
407
- resizeHandles = _this$props6.resizeHandles,
408
- resizeHandle = _this$props6.resizeHandle;
409
- var positionParams = this.getPositionParams(); // This is the max possible width - doesn't go to infinity because of the width of the window
410
-
411
- var maxWidth = (0, _calculateUtils.calcGridItemPosition)(positionParams, 0, 0, cols - x, 0).width; // Calculate min/max constraints using our min & maxes
333
+ }
334
+ getPositionParams() /*: PositionParams*/{
335
+ let props /*: Props*/ = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.props;
336
+ return {
337
+ cols: props.cols,
338
+ containerPadding: props.containerPadding,
339
+ containerWidth: props.containerWidth,
340
+ margin: props.margin,
341
+ maxRows: props.maxRows,
342
+ rowHeight: props.rowHeight
343
+ };
344
+ }
412
345
 
413
- var mins = (0, _calculateUtils.calcGridItemPosition)(positionParams, 0, 0, minW, minH);
414
- var maxes = (0, _calculateUtils.calcGridItemPosition)(positionParams, 0, 0, maxW, maxH);
415
- var minConstraints = [mins.width, mins.height];
416
- var maxConstraints = [Math.min(maxes.width, maxWidth), Math.min(maxes.height, Infinity)];
417
- return /*#__PURE__*/_react.default.createElement(_reactResizable.Resizable // These are opts for the resize handle itself
418
- , {
419
- draggableOpts: {
420
- disabled: !isResizable
421
- },
422
- className: isResizable ? undefined : "react-resizable-hide",
423
- width: position.width,
424
- height: position.height,
425
- minConstraints: minConstraints,
426
- maxConstraints: maxConstraints,
427
- onResizeStop: this.onResizeStop,
428
- onResizeStart: this.onResizeStart,
429
- onResize: this.onResize,
430
- transformScale: transformScale,
431
- resizeHandles: resizeHandles,
432
- handle: resizeHandle
433
- }, child);
346
+ /**
347
+ * This is where we set the grid item's absolute placement. It gets a little tricky because we want to do it
348
+ * well when server rendering, and the only way to do that properly is to use percentage width/left because
349
+ * we don't know exactly what the browser viewport is.
350
+ * Unfortunately, CSS Transforms, which are great for performance, break in this instance because a percentage
351
+ * left is relative to the item itself, not its container! So we cannot use them on the server rendering pass.
352
+ *
353
+ * @param {Object} pos Position object with width, height, left, top.
354
+ * @return {Object} Style object.
355
+ */
356
+ createStyle(pos /*: Position*/) /*: { [key: string]: ?string }*/{
357
+ const {
358
+ usePercentages,
359
+ containerWidth,
360
+ useCSSTransforms
361
+ } = this.props;
362
+ let style;
363
+ // CSS Transforms support (default)
364
+ if (useCSSTransforms) {
365
+ style = (0, _utils.setTransform)(pos);
366
+ } else {
367
+ // top,left (slow)
368
+ style = (0, _utils.setTopLeft)(pos);
369
+
370
+ // This is used for server rendering.
371
+ if (usePercentages) {
372
+ style.left = (0, _utils.perc)(pos.left / containerWidth);
373
+ style.width = (0, _utils.perc)(pos.width / containerWidth);
374
+ }
434
375
  }
435
- /**
436
- * onDragStart event handler
437
- * @param {Event} e event data
438
- * @param {Object} callbackData an object with node, delta and position information
439
- */
440
-
441
- }, {
442
- key: "onResizeHandler",
443
- value:
444
- /**
445
- * Wrapper around drag events to provide more useful data.
446
- * All drag events call the function with the given handler name,
447
- * with the signature (index, x, y).
448
- *
449
- * @param {String} handlerName Handler name to wrap.
450
- * @return {Function} Handler function.
451
- */
452
- function onResizeHandler(e
453
- /*: Event*/
454
- , _ref4, handlerName
455
- /*: string*/
456
- )
457
- /*: void*/
458
- {
459
- var node = _ref4.node,
460
- size = _ref4.size;
461
- var handler = this.props[handlerName];
462
- if (!handler) return;
463
- var _this$props7 = this.props,
464
- cols = _this$props7.cols,
465
- x = _this$props7.x,
466
- y = _this$props7.y,
467
- i = _this$props7.i,
468
- maxH = _this$props7.maxH,
469
- minH = _this$props7.minH;
470
- var _this$props8 = this.props,
471
- minW = _this$props8.minW,
472
- maxW = _this$props8.maxW; // Get new XY
473
-
474
- var _calcWH = (0, _calculateUtils.calcWH)(this.getPositionParams(), size.width, size.height, x, y),
475
- w = _calcWH.w,
476
- h = _calcWH.h; // minW should be at least 1 (TODO propTypes validation?)
477
-
376
+ return style;
377
+ }
478
378
 
479
- minW = Math.max(minW, 1); // maxW should be at most (cols - x)
379
+ /**
380
+ * Mix a Draggable instance into a child.
381
+ * @param {Element} child Child element.
382
+ * @return {Element} Child wrapped in Draggable.
383
+ */
384
+ mixinDraggable(child /*: ReactElement<any>*/, isDraggable /*: boolean*/) /*: ReactElement<any>*/{
385
+ return /*#__PURE__*/_react.default.createElement(_reactDraggable.DraggableCore, {
386
+ disabled: !isDraggable,
387
+ onStart: this.onDragStart,
388
+ onDrag: this.onDrag,
389
+ onStop: this.onDragStop,
390
+ handle: this.props.handle,
391
+ cancel: ".react-resizable-handle" + (this.props.cancel ? "," + this.props.cancel : ""),
392
+ scale: this.props.transformScale,
393
+ nodeRef: this.elementRef
394
+ }, child);
395
+ }
480
396
 
481
- maxW = Math.min(maxW, cols - x); // Min/max capping
397
+ /**
398
+ * Utility function to setup callback handler definitions for
399
+ * similarily structured resize events.
400
+ */
401
+ curryResizeHandler(position /*: Position*/, handler /*: Function*/) /*: Function*/{
402
+ return (e /*: Event*/, data /*: ResizeCallbackData*/) => /*: Function*/handler(e, data, position);
403
+ }
482
404
 
483
- w = (0, _calculateUtils.clamp)(w, minW, maxW);
484
- h = (0, _calculateUtils.clamp)(h, minH, maxH);
405
+ /**
406
+ * Mix a Resizable instance into a child.
407
+ * @param {Element} child Child element.
408
+ * @param {Object} position Position object (pixel values)
409
+ * @return {Element} Child wrapped in Resizable.
410
+ */
411
+ mixinResizable(child /*: ReactElement<any>*/, position /*: Position*/, isResizable /*: boolean*/) /*: ReactElement<any>*/{
412
+ const {
413
+ cols,
414
+ minW,
415
+ minH,
416
+ maxW,
417
+ maxH,
418
+ transformScale,
419
+ resizeHandles,
420
+ resizeHandle
421
+ } = this.props;
422
+ const positionParams = this.getPositionParams();
423
+
424
+ // This is the max possible width - doesn't go to infinity because of the width of the window
425
+ const maxWidth = (0, _calculateUtils.calcGridItemPosition)(positionParams, 0, 0, cols, 0).width;
426
+
427
+ // Calculate min/max constraints using our min & maxes
428
+ const mins = (0, _calculateUtils.calcGridItemPosition)(positionParams, 0, 0, minW, minH);
429
+ const maxes = (0, _calculateUtils.calcGridItemPosition)(positionParams, 0, 0, maxW, maxH);
430
+ const minConstraints = [mins.width, mins.height];
431
+ const maxConstraints = [Math.min(maxes.width, maxWidth), Math.min(maxes.height, Infinity)];
432
+ return /*#__PURE__*/_react.default.createElement(_reactResizable.Resizable
433
+ // These are opts for the resize handle itself
434
+ , {
435
+ draggableOpts: {
436
+ disabled: !isResizable
437
+ },
438
+ className: isResizable ? undefined : "react-resizable-hide",
439
+ width: position.width,
440
+ height: position.height,
441
+ minConstraints: minConstraints,
442
+ maxConstraints: maxConstraints,
443
+ onResizeStop: this.curryResizeHandler(position, this.onResizeStop),
444
+ onResizeStart: this.curryResizeHandler(position, this.onResizeStart),
445
+ onResize: this.curryResizeHandler(position, this.onResize),
446
+ transformScale: transformScale,
447
+ resizeHandles: resizeHandles,
448
+ handle: resizeHandle
449
+ }, child);
450
+ }
451
+ /**
452
+ * Wrapper around resize events to provide more useful data.
453
+ */
454
+ onResizeHandler(e /*: Event*/, _ref4 /*:: */,
455
+ // 'size' is updated position
456
+ position /*: Position*/,
457
+ // existing position
458
+ handlerName /*: string*/) /*: void*/{
459
+ let {
460
+ node,
461
+ size,
462
+ handle
463
+ } /*: ResizeCallbackData*/ = _ref4 /*: ResizeCallbackData*/;
464
+ const handler = this.props[handlerName];
465
+ if (!handler) return;
466
+ const {
467
+ x,
468
+ y,
469
+ i,
470
+ maxH,
471
+ minH,
472
+ containerWidth
473
+ } = this.props;
474
+ const {
475
+ minW,
476
+ maxW
477
+ } = this.props;
478
+
479
+ // Clamping of dimensions based on resize direction
480
+ let updatedSize = size;
481
+ if (node) {
482
+ updatedSize = (0, _utils.resizeItemInDirection)(handle, position, size, containerWidth);
485
483
  this.setState({
486
- resizing: handlerName === "onResizeStop" ? null : size
487
- });
488
- handler.call(this, i, w, h, {
489
- e: e,
490
- node: node,
491
- size: size
484
+ resizing: handlerName === "onResizeStop" ? null : updatedSize
492
485
  });
493
486
  }
494
- }, {
495
- key: "render",
496
- value: function render()
497
- /*: ReactNode*/
498
- {
499
- var _this$props9 = this.props,
500
- x = _this$props9.x,
501
- y = _this$props9.y,
502
- w = _this$props9.w,
503
- h = _this$props9.h,
504
- isDraggable = _this$props9.isDraggable,
505
- isResizable = _this$props9.isResizable,
506
- droppingPosition = _this$props9.droppingPosition,
507
- useCSSTransforms = _this$props9.useCSSTransforms;
508
- var pos = (0, _calculateUtils.calcGridItemPosition)(this.getPositionParams(), x, y, w, h, this.state);
509
-
510
- var child = _react.default.Children.only(this.props.children); // Create the child element. We clone the existing element but modify its className and style.
511
-
512
-
513
- var newChild = /*#__PURE__*/_react.default.cloneElement(child, {
514
- ref: this.elementRef,
515
- className: (0, _clsx.default)("react-grid-item", child.props.className, this.props.className, {
516
- static: this.props.static,
517
- resizing: Boolean(this.state.resizing),
518
- "react-draggable": isDraggable,
519
- "react-draggable-dragging": Boolean(this.state.dragging),
520
- dropping: Boolean(droppingPosition),
521
- cssTransforms: useCSSTransforms
522
- }),
523
- // We can set the width and height on the child, but unfortunately we can't set the position.
524
- style: _objectSpread(_objectSpread(_objectSpread({}, this.props.style), child.props.style), this.createStyle(pos))
525
- }); // Resizable support. This is usually on but the user can toggle it off.
526
-
527
-
528
- newChild = this.mixinResizable(newChild, pos, isResizable); // Draggable support. This is always on, except for with placeholders.
529
487
 
530
- newChild = this.mixinDraggable(newChild, isDraggable);
531
- return newChild;
532
- }
533
- }]);
488
+ // Get new XY based on pixel size
489
+ let {
490
+ w,
491
+ h
492
+ } = (0, _calculateUtils.calcWH)(this.getPositionParams(), updatedSize.width, updatedSize.height, x, y, handle);
493
+
494
+ // Min/max capping.
495
+ // minW should be at least 1 (TODO propTypes validation?)
496
+ w = (0, _calculateUtils.clamp)(w, Math.max(minW, 1), maxW);
497
+ h = (0, _calculateUtils.clamp)(h, minH, maxH);
498
+ handler.call(this, i, w, h, {
499
+ e,
500
+ node,
501
+ size: updatedSize,
502
+ handle
503
+ });
504
+ }
505
+ render() /*: ReactNode*/{
506
+ const {
507
+ x,
508
+ y,
509
+ w,
510
+ h,
511
+ isDraggable,
512
+ isResizable,
513
+ droppingPosition,
514
+ useCSSTransforms
515
+ } = this.props;
516
+ const pos = (0, _calculateUtils.calcGridItemPosition)(this.getPositionParams(), x, y, w, h, this.state);
517
+ const child = _react.default.Children.only(this.props.children);
518
+
519
+ // Create the child element. We clone the existing element but modify its className and style.
520
+ let newChild = /*#__PURE__*/_react.default.cloneElement(child, {
521
+ ref: this.elementRef,
522
+ className: (0, _clsx.default)("react-grid-item", child.props.className, this.props.className, {
523
+ static: this.props.static,
524
+ resizing: Boolean(this.state.resizing),
525
+ "react-draggable": isDraggable,
526
+ "react-draggable-dragging": Boolean(this.state.dragging),
527
+ dropping: Boolean(droppingPosition),
528
+ cssTransforms: useCSSTransforms
529
+ }),
530
+ // We can set the width and height on the child, but unfortunately we can't set the position.
531
+ style: {
532
+ ...this.props.style,
533
+ ...child.props.style,
534
+ ...this.createStyle(pos)
535
+ }
536
+ });
534
537
 
535
- return GridItem;
536
- }(_react.default.Component);
538
+ // Resizable support. This is usually on but the user can toggle it off.
539
+ newChild = this.mixinResizable(newChild, pos, isResizable);
537
540
 
541
+ // Draggable support. This is always on, except for with placeholders.
542
+ newChild = this.mixinDraggable(newChild, isDraggable);
543
+ return newChild;
544
+ }
545
+ }
538
546
  exports.default = GridItem;
539
-
540
547
  _defineProperty(GridItem, "propTypes", {
541
548
  // Children must be only a single element
542
549
  children: _propTypes.default.element,
@@ -553,39 +560,23 @@ _defineProperty(GridItem, "propTypes", {
553
560
  w: _propTypes.default.number.isRequired,
554
561
  h: _propTypes.default.number.isRequired,
555
562
  // All optional
556
- minW: function minW(props
557
- /*: Props*/
558
- , propName
559
- /*: string*/
560
- ) {
561
- var value = props[propName];
563
+ minW: function (props /*: Props*/, propName /*: string*/) {
564
+ const value = props[propName];
562
565
  if (typeof value !== "number") return new Error("minWidth not Number");
563
566
  if (value > props.w || value > props.maxW) return new Error("minWidth larger than item width/maxWidth");
564
567
  },
565
- maxW: function maxW(props
566
- /*: Props*/
567
- , propName
568
- /*: string*/
569
- ) {
570
- var value = props[propName];
568
+ maxW: function (props /*: Props*/, propName /*: string*/) {
569
+ const value = props[propName];
571
570
  if (typeof value !== "number") return new Error("maxWidth not Number");
572
571
  if (value < props.w || value < props.minW) return new Error("maxWidth smaller than item width/minWidth");
573
572
  },
574
- minH: function minH(props
575
- /*: Props*/
576
- , propName
577
- /*: string*/
578
- ) {
579
- var value = props[propName];
573
+ minH: function (props /*: Props*/, propName /*: string*/) {
574
+ const value = props[propName];
580
575
  if (typeof value !== "number") return new Error("minHeight not Number");
581
576
  if (value > props.h || value > props.maxH) return new Error("minHeight larger than item height/maxHeight");
582
577
  },
583
- maxH: function maxH(props
584
- /*: Props*/
585
- , propName
586
- /*: string*/
587
- ) {
588
- var value = props[propName];
578
+ maxH: function (props /*: Props*/, propName /*: string*/) {
579
+ const value = props[propName];
589
580
  if (typeof value !== "number") return new Error("maxHeight not Number");
590
581
  if (value < props.h || value < props.minH) return new Error("maxHeight smaller than item height/minHeight");
591
582
  },
@@ -622,7 +613,6 @@ _defineProperty(GridItem, "propTypes", {
622
613
  top: _propTypes.default.number.isRequired
623
614
  })
624
615
  });
625
-
626
616
  _defineProperty(GridItem, "defaultProps", {
627
617
  className: "",
628
618
  cancel: "",