react-grid-layout 0.17.1 → 0.18.3

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
@@ -17,15 +17,17 @@ var _reactResizable = require("react-resizable");
17
17
 
18
18
  var _utils = require("./utils");
19
19
 
20
+ var _calculateUtils = require("./calculateUtils");
21
+
20
22
  var _classnames = _interopRequireDefault(require("classnames"));
21
23
 
22
24
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
23
25
 
24
- function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
26
+ function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
25
27
 
26
28
  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; }
27
29
 
28
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
30
+ 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; }
29
31
 
30
32
  function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
31
33
 
@@ -101,9 +103,9 @@ function (_React$Component) {
101
103
  dragging: newPosition
102
104
  });
103
105
 
104
- var _this$calcXY = _this.calcXY(newPosition.top, newPosition.left),
105
- x = _this$calcXY.x,
106
- y = _this$calcXY.y;
106
+ var _calcXY = (0, _calculateUtils.calcXY)(_this.getPositionParams(), newPosition.top, newPosition.left, _this.props.w, _this.props.h),
107
+ x = _calcXY.x,
108
+ y = _calcXY.y;
107
109
 
108
110
  return _this.props.onDragStart && _this.props.onDragStart.call(_assertThisInitialized(_this), _this.props.i, x, y, {
109
111
  e: e,
@@ -118,7 +120,12 @@ function (_React$Component) {
118
120
  var node = _ref2.node,
119
121
  deltaX = _ref2.deltaX,
120
122
  deltaY = _ref2.deltaY;
121
- if (!_this.props.onDrag) return;
123
+ var _this$props = _this.props,
124
+ onDrag = _this$props.onDrag,
125
+ transformScale = _this$props.transformScale;
126
+ if (!onDrag) return;
127
+ deltaX /= transformScale;
128
+ deltaY /= transformScale;
122
129
  var newPosition
123
130
  /*: PartialPosition*/
124
131
  = {
@@ -133,11 +140,11 @@ function (_React$Component) {
133
140
  dragging: newPosition
134
141
  });
135
142
 
136
- var _this$calcXY2 = _this.calcXY(newPosition.top, newPosition.left),
137
- x = _this$calcXY2.x,
138
- y = _this$calcXY2.y;
143
+ var _calcXY2 = (0, _calculateUtils.calcXY)(_this.getPositionParams(), newPosition.top, newPosition.left, _this.props.w, _this.props.h),
144
+ x = _calcXY2.x,
145
+ y = _calcXY2.y;
139
146
 
140
- return _this.props.onDrag && _this.props.onDrag.call(_assertThisInitialized(_this), _this.props.i, x, y, {
147
+ return onDrag && onDrag.call(_assertThisInitialized(_this), _this.props.i, x, y, {
141
148
  e: e,
142
149
  node: node,
143
150
  newPosition: newPosition
@@ -163,9 +170,9 @@ function (_React$Component) {
163
170
  dragging: null
164
171
  });
165
172
 
166
- var _this$calcXY3 = _this.calcXY(newPosition.top, newPosition.left),
167
- x = _this$calcXY3.x,
168
- y = _this$calcXY3.y;
173
+ var _calcXY3 = (0, _calculateUtils.calcXY)(_this.getPositionParams(), newPosition.top, newPosition.left, _this.props.w, _this.props.h),
174
+ x = _calcXY3.x,
175
+ y = _calcXY3.y;
169
176
 
170
177
  return _this.props.onDragStop && _this.props.onDragStop.call(_assertThisInitialized(_this), _this.props.i, x, y, {
171
178
  e: e,
@@ -202,26 +209,48 @@ function (_React$Component) {
202
209
  }
203
210
 
204
211
  _createClass(GridItem, [{
212
+ key: "shouldComponentUpdate",
213
+ value: function shouldComponentUpdate(nextProps
214
+ /*: Props*/
215
+ , nextState
216
+ /*: State*/
217
+ ) {
218
+ // We can't deeply compare children. If the developer memoizes them, we can
219
+ // use this optimization.
220
+ if (this.props.children !== nextProps.children) return true;
221
+ if (this.props.droppingPosition !== nextProps.droppingPosition) return true; // TODO memoize these calculations so they don't take so long?
222
+
223
+ var oldPosition = (0, _calculateUtils.calcGridItemPosition)(this.getPositionParams(this.props), this.props.x, this.props.y, this.props.w, this.props.h, this.state);
224
+ var newPosition = (0, _calculateUtils.calcGridItemPosition)(this.getPositionParams(nextProps), nextProps.x, nextProps.y, nextProps.w, nextProps.h, nextState);
225
+ return !(0, _utils.fastPositionEqual)(oldPosition, newPosition) || this.props.useCSSTransforms !== nextProps.useCSSTransforms;
226
+ }
227
+ }, {
228
+ key: "componentDidMount",
229
+ value: function componentDidMount() {
230
+ this.moveDroppingItem({});
231
+ }
232
+ }, {
205
233
  key: "componentDidUpdate",
206
234
  value: function componentDidUpdate(prevProps
207
235
  /*: Props*/
208
236
  ) {
209
- if (this.props.droppingPosition && prevProps.droppingPosition) {
210
- this.moveDroppingItem(prevProps);
211
- }
212
- }
237
+ this.moveDroppingItem(prevProps);
238
+ } // When a droppingPosition is present, this means we should fire a move event, as if we had moved
239
+ // this element by `x, y` pixels.
240
+
213
241
  }, {
214
242
  key: "moveDroppingItem",
215
243
  value: function moveDroppingItem(prevProps
216
244
  /*: Props*/
217
245
  ) {
218
246
  var droppingPosition = this.props.droppingPosition;
247
+ if (!droppingPosition) return;
248
+ var prevDroppingPosition = prevProps.droppingPosition || {
249
+ left: 0,
250
+ top: 0
251
+ };
219
252
  var dragging = this.state.dragging;
220
253
 
221
- if (!droppingPosition || !prevProps.droppingPosition) {
222
- return;
223
- }
224
-
225
254
  if (!this.currentNode) {
226
255
  // eslint-disable-next-line react/no-find-dom-node
227
256
  this.currentNode = ((_reactDom.default.findDOMNode(this)
@@ -231,167 +260,39 @@ function (_React$Component) {
231
260
  );
232
261
  }
233
262
 
234
- var shouldDrag = dragging && droppingPosition.x !== prevProps.droppingPosition.x || droppingPosition.y !== prevProps.droppingPosition.y;
263
+ var shouldDrag = dragging && droppingPosition.left !== prevDroppingPosition.left || droppingPosition.top !== prevDroppingPosition.top;
235
264
 
236
265
  if (!dragging) {
237
266
  this.onDragStart(droppingPosition.e, {
238
267
  node: this.currentNode,
239
- deltaX: droppingPosition.x,
240
- deltaY: droppingPosition.y
268
+ deltaX: droppingPosition.left,
269
+ deltaY: droppingPosition.top
241
270
  });
242
271
  } else if (shouldDrag) {
243
- var deltaX = droppingPosition.x - dragging.left;
244
- var deltaY = droppingPosition.y - dragging.top;
272
+ var deltaX = droppingPosition.left - dragging.left;
273
+ var deltaY = droppingPosition.top - dragging.top;
245
274
  this.onDrag(droppingPosition.e, {
246
275
  node: this.currentNode,
247
276
  deltaX: deltaX,
248
277
  deltaY: deltaY
249
278
  });
250
279
  }
251
- } // Helper for generating column width
252
-
253
- }, {
254
- key: "calcColWidth",
255
- value: function calcColWidth()
256
- /*: number*/
257
- {
258
- var _this$props = this.props,
259
- margin = _this$props.margin,
260
- containerPadding = _this$props.containerPadding,
261
- containerWidth = _this$props.containerWidth,
262
- cols = _this$props.cols;
263
- return (containerWidth - margin[0] * (cols - 1) - containerPadding[0] * 2) / cols;
264
- }
265
- /**
266
- * Return position on the page given an x, y, w, h.
267
- * left, top, width, height are all in pixels.
268
- * @param {Number} x X coordinate in grid units.
269
- * @param {Number} y Y coordinate in grid units.
270
- * @param {Number} w W coordinate in grid units.
271
- * @param {Number} h H coordinate in grid units.
272
- * @return {Object} Object containing coords.
273
- */
274
-
275
- }, {
276
- key: "calcPosition",
277
- value: function calcPosition(x
278
- /*: number*/
279
- , y
280
- /*: number*/
281
- , w
282
- /*: number*/
283
- , h
284
- /*: number*/
285
- , state
286
- /*: ?Object*/
287
- )
288
- /*: Position*/
289
- {
290
- var _this$props2 = this.props,
291
- margin = _this$props2.margin,
292
- containerPadding = _this$props2.containerPadding,
293
- rowHeight = _this$props2.rowHeight;
294
- var colWidth = this.calcColWidth();
295
- var out = {}; // If resizing, use the exact width and height as returned from resizing callbacks.
296
-
297
- if (state && state.resizing) {
298
- out.width = Math.round(state.resizing.width);
299
- out.height = Math.round(state.resizing.height);
300
- } // Otherwise, calculate from grid units.
301
- else {
302
- // 0 * Infinity === NaN, which causes problems with resize constraints;
303
- // Fix this if it occurs.
304
- // Note we do it here rather than later because Math.round(Infinity) causes deopt
305
- out.width = w === Infinity ? w : Math.round(colWidth * w + Math.max(0, w - 1) * margin[0]);
306
- out.height = h === Infinity ? h : Math.round(rowHeight * h + Math.max(0, h - 1) * margin[1]);
307
- } // If dragging, use the exact width and height as returned from dragging callbacks.
308
-
309
-
310
- if (state && state.dragging) {
311
- out.top = Math.round(state.dragging.top);
312
- out.left = Math.round(state.dragging.left);
313
- } // Otherwise, calculate from grid units.
314
- else {
315
- out.top = Math.round((rowHeight + margin[1]) * y + containerPadding[1]);
316
- out.left = Math.round((colWidth + margin[0]) * x + containerPadding[0]);
317
- }
318
-
319
- return out;
320
- }
321
- /**
322
- * Translate x and y coordinates from pixels to grid units.
323
- * @param {Number} top Top position (relative to parent) in pixels.
324
- * @param {Number} left Left position (relative to parent) in pixels.
325
- * @return {Object} x and y in grid units.
326
- */
327
-
328
- }, {
329
- key: "calcXY",
330
- value: function calcXY(top
331
- /*: number*/
332
- , left
333
- /*: number*/
334
- )
335
- /*: { x: number, y: number }*/
336
- {
337
- var _this$props3 = this.props,
338
- margin = _this$props3.margin,
339
- cols = _this$props3.cols,
340
- rowHeight = _this$props3.rowHeight,
341
- w = _this$props3.w,
342
- h = _this$props3.h,
343
- maxRows = _this$props3.maxRows;
344
- var colWidth = this.calcColWidth(); // left = colWidth * x + margin * (x + 1)
345
- // l = cx + m(x+1)
346
- // l = cx + mx + m
347
- // l - m = cx + mx
348
- // l - m = x(c + m)
349
- // (l - m) / (c + m) = x
350
- // x = (left - margin) / (coldWidth + margin)
351
-
352
- var x = Math.round((left - margin[0]) / (colWidth + margin[0]));
353
- var y = Math.round((top - margin[1]) / (rowHeight + margin[1])); // Capping
354
-
355
- x = Math.max(Math.min(x, cols - w), 0);
356
- y = Math.max(Math.min(y, maxRows - h), 0);
357
- return {
358
- x: x,
359
- y: y
360
- };
361
280
  }
362
- /**
363
- * Given a height and width in pixel values, calculate grid units.
364
- * @param {Number} height Height in pixels.
365
- * @param {Number} width Width in pixels.
366
- * @return {Object} w, h as grid units.
367
- */
368
-
369
281
  }, {
370
- key: "calcWH",
371
- value: function calcWH(_ref4)
372
- /*: { w: number, h: number }*/
282
+ key: "getPositionParams",
283
+ value: function getPositionParams()
284
+ /*: PositionParams*/
373
285
  {
374
- var height = _ref4.height,
375
- width = _ref4.width;
376
- var _this$props4 = this.props,
377
- margin = _this$props4.margin,
378
- maxRows = _this$props4.maxRows,
379
- cols = _this$props4.cols,
380
- rowHeight = _this$props4.rowHeight,
381
- x = _this$props4.x,
382
- y = _this$props4.y;
383
- var colWidth = this.calcColWidth(); // width = colWidth * w - (margin * (w - 1))
384
- // ...
385
- // w = (width + margin) / (colWidth + margin)
386
-
387
- var w = Math.round((width + margin[0]) / (colWidth + margin[0]));
388
- var h = Math.round((height + margin[1]) / (rowHeight + margin[1])); // Capping
389
-
390
- w = Math.max(Math.min(w, cols - x), 0);
391
- h = Math.max(Math.min(h, maxRows - y), 0);
286
+ var props
287
+ /*: Props*/
288
+ = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.props;
392
289
  return {
393
- w: w,
394
- h: h
290
+ cols: props.cols,
291
+ containerPadding: props.containerPadding,
292
+ containerWidth: props.containerWidth,
293
+ margin: props.margin,
294
+ maxRows: props.maxRows,
295
+ rowHeight: props.rowHeight
395
296
  };
396
297
  }
397
298
  /**
@@ -412,10 +313,10 @@ function (_React$Component) {
412
313
  )
413
314
  /*: { [key: string]: ?string }*/
414
315
  {
415
- var _this$props5 = this.props,
416
- usePercentages = _this$props5.usePercentages,
417
- containerWidth = _this$props5.containerWidth,
418
- useCSSTransforms = _this$props5.useCSSTransforms;
316
+ var _this$props2 = this.props,
317
+ usePercentages = _this$props2.usePercentages,
318
+ containerWidth = _this$props2.containerWidth,
319
+ useCSSTransforms = _this$props2.useCSSTransforms;
419
320
  var style; // CSS Transforms support (default)
420
321
 
421
322
  if (useCSSTransforms) {
@@ -442,10 +343,13 @@ function (_React$Component) {
442
343
  key: "mixinDraggable",
443
344
  value: function mixinDraggable(child
444
345
  /*: ReactElement<any>*/
346
+ , isDraggable
347
+ /*: boolean*/
445
348
  )
446
349
  /*: ReactElement<any>*/
447
350
  {
448
351
  return _react.default.createElement(_reactDraggable.DraggableCore, {
352
+ disabled: !isDraggable,
449
353
  onStart: this.onDragStart,
450
354
  onDrag: this.onDrag,
451
355
  onStop: this.onDragStop,
@@ -467,31 +371,40 @@ function (_React$Component) {
467
371
  /*: ReactElement<any>*/
468
372
  , position
469
373
  /*: Position*/
374
+ , isResizable
375
+ /*: boolean*/
470
376
  )
471
377
  /*: ReactElement<any>*/
472
378
  {
473
- var _this$props6 = this.props,
474
- cols = _this$props6.cols,
475
- x = _this$props6.x,
476
- minW = _this$props6.minW,
477
- minH = _this$props6.minH,
478
- maxW = _this$props6.maxW,
479
- maxH = _this$props6.maxH; // This is the max possible width - doesn't go to infinity because of the width of the window
480
-
481
- var maxWidth = this.calcPosition(0, 0, cols - x, 0).width; // Calculate min/max constraints using our min & maxes
482
-
483
- var mins = this.calcPosition(0, 0, minW, minH);
484
- var maxes = this.calcPosition(0, 0, maxW, maxH);
379
+ var _this$props3 = this.props,
380
+ cols = _this$props3.cols,
381
+ x = _this$props3.x,
382
+ minW = _this$props3.minW,
383
+ minH = _this$props3.minH,
384
+ maxW = _this$props3.maxW,
385
+ maxH = _this$props3.maxH,
386
+ transformScale = _this$props3.transformScale;
387
+ var positionParams = this.getPositionParams(); // This is the max possible width - doesn't go to infinity because of the width of the window
388
+
389
+ var maxWidth = (0, _calculateUtils.calcGridItemPosition)(positionParams, 0, 0, cols - x, 0).width; // Calculate min/max constraints using our min & maxes
390
+
391
+ var mins = (0, _calculateUtils.calcGridItemPosition)(positionParams, 0, 0, minW, minH);
392
+ var maxes = (0, _calculateUtils.calcGridItemPosition)(positionParams, 0, 0, maxW, maxH);
485
393
  var minConstraints = [mins.width, mins.height];
486
394
  var maxConstraints = [Math.min(maxes.width, maxWidth), Math.min(maxes.height, Infinity)];
487
395
  return _react.default.createElement(_reactResizable.Resizable, {
396
+ draggableOpts: {
397
+ disabled: !isResizable
398
+ },
399
+ className: isResizable ? undefined : "react-resizable-hide",
488
400
  width: position.width,
489
401
  height: position.height,
490
402
  minConstraints: minConstraints,
491
403
  maxConstraints: maxConstraints,
492
404
  onResizeStop: this.onResizeStop,
493
405
  onResizeStart: this.onResizeStart,
494
- onResize: this.onResize
406
+ onResize: this.onResize,
407
+ transformScale: transformScale
495
408
  }, child);
496
409
  }
497
410
  /**
@@ -513,25 +426,26 @@ function (_React$Component) {
513
426
  */
514
427
  value: function onResizeHandler(e
515
428
  /*: Event*/
516
- , _ref5, handlerName
429
+ , _ref4, handlerName
517
430
  /*: string*/
518
431
  ) {
519
- var node = _ref5.node,
520
- size = _ref5.size;
432
+ var node = _ref4.node,
433
+ size = _ref4.size;
521
434
  var handler = this.props[handlerName];
522
435
  if (!handler) return;
523
- var _this$props7 = this.props,
524
- cols = _this$props7.cols,
525
- x = _this$props7.x,
526
- i = _this$props7.i,
527
- maxW = _this$props7.maxW,
528
- minW = _this$props7.minW,
529
- maxH = _this$props7.maxH,
530
- minH = _this$props7.minH; // Get new XY
436
+ var _this$props4 = this.props,
437
+ cols = _this$props4.cols,
438
+ x = _this$props4.x,
439
+ y = _this$props4.y,
440
+ i = _this$props4.i,
441
+ maxW = _this$props4.maxW,
442
+ minW = _this$props4.minW,
443
+ maxH = _this$props4.maxH,
444
+ minH = _this$props4.minH; // Get new XY
531
445
 
532
- var _this$calcWH = this.calcWH(size),
533
- w = _this$calcWH.w,
534
- h = _this$calcWH.h; // Cap w at numCols
446
+ var _calcWH = (0, _calculateUtils.calcWH)(this.getPositionParams(), size.width, size.height, x, y),
447
+ w = _calcWH.w,
448
+ h = _calcWH.h; // Cap w at numCols
535
449
 
536
450
 
537
451
  w = Math.min(w, cols - x); // Ensure w is at least 1
@@ -554,16 +468,16 @@ function (_React$Component) {
554
468
  value: function render()
555
469
  /*: ReactNode*/
556
470
  {
557
- var _this$props8 = this.props,
558
- x = _this$props8.x,
559
- y = _this$props8.y,
560
- w = _this$props8.w,
561
- h = _this$props8.h,
562
- isDraggable = _this$props8.isDraggable,
563
- isResizable = _this$props8.isResizable,
564
- droppingPosition = _this$props8.droppingPosition,
565
- useCSSTransforms = _this$props8.useCSSTransforms;
566
- var pos = this.calcPosition(x, y, w, h, this.state);
471
+ var _this$props5 = this.props,
472
+ x = _this$props5.x,
473
+ y = _this$props5.y,
474
+ w = _this$props5.w,
475
+ h = _this$props5.h,
476
+ isDraggable = _this$props5.isDraggable,
477
+ isResizable = _this$props5.isResizable,
478
+ droppingPosition = _this$props5.droppingPosition,
479
+ useCSSTransforms = _this$props5.useCSSTransforms;
480
+ var pos = (0, _calculateUtils.calcGridItemPosition)(this.getPositionParams(), x, y, w, h, this.state);
567
481
 
568
482
  var child = _react.default.Children.only(this.props.children); // Create the child element. We clone the existing element but modify its className and style.
569
483
 
@@ -582,9 +496,9 @@ function (_React$Component) {
582
496
  }); // Resizable support. This is usually on but the user can toggle it off.
583
497
 
584
498
 
585
- if (isResizable) newChild = this.mixinResizable(newChild, pos); // Draggable support. This is always on, except for with placeholders.
499
+ newChild = this.mixinResizable(newChild, pos, isResizable); // Draggable support. This is always on, except for with placeholders.
586
500
 
587
- if (isDraggable) newChild = this.mixinDraggable(newChild);
501
+ newChild = this.mixinDraggable(newChild, isDraggable);
588
502
  return newChild;
589
503
  }
590
504
  }]);
@@ -671,8 +585,8 @@ _defineProperty(GridItem, "propTypes", {
671
585
  // Current position of a dropping element
672
586
  droppingPosition: _propTypes.default.shape({
673
587
  e: _propTypes.default.object.isRequired,
674
- x: _propTypes.default.number.isRequired,
675
- y: _propTypes.default.number.isRequired
588
+ left: _propTypes.default.number.isRequired,
589
+ top: _propTypes.default.number.isRequired
676
590
  })
677
591
  });
678
592