react-grid-layout 1.3.4 → 1.4.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.
@@ -1,253 +1,253 @@
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 = _interopRequireWildcard(require("react"));
11
-
12
- var _lodash = _interopRequireDefault(require("lodash.isequal"));
13
-
8
+ var _fastEquals = require("fast-equals");
14
9
  var _clsx = _interopRequireDefault(require("clsx"));
15
-
16
10
  var _utils = require("./utils");
17
-
18
11
  var _calculateUtils = require("./calculateUtils");
19
-
20
12
  var _GridItem = _interopRequireDefault(require("./GridItem"));
21
-
22
13
  var _ReactGridLayoutPropTypes = _interopRequireDefault(require("./ReactGridLayoutPropTypes"));
23
-
24
14
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
25
-
26
- function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
27
-
28
- function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && 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; }
29
-
30
- function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
31
-
32
- function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
33
-
34
- function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
35
-
36
- function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
37
-
38
- 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; }
39
-
40
- 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; }
41
-
42
- function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
43
-
44
- function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
45
-
46
- function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
47
-
48
- function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
49
-
50
- function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
51
-
52
- function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
53
-
54
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
55
-
56
- 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); } }
57
-
58
- function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
59
-
60
- 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); }
61
-
62
- function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
63
-
64
- 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); }; }
65
-
66
- 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); }
67
-
68
- function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
69
-
70
- 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; } }
71
-
72
- function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
73
-
74
- 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; }
75
-
15
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
16
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && 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; }
17
+ 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; }
18
+ function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
19
+ 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); }
20
+ /*:: import type {
21
+ ChildrenArray as ReactChildrenArray,
22
+ Element as ReactElement
23
+ } from "react";*/
24
+ /*:: import type {
25
+ CompactType,
26
+ GridResizeEvent,
27
+ GridDragEvent,
28
+ DragOverEvent,
29
+ Layout,
30
+ DroppingPosition,
31
+ LayoutItem
32
+ } from "./utils";*/
33
+ // Types
34
+ /*:: import type { PositionParams } from "./calculateUtils";*/
35
+ /*:: type State = {
36
+ activeDrag: ?LayoutItem,
37
+ layout: Layout,
38
+ mounted: boolean,
39
+ oldDragItem: ?LayoutItem,
40
+ oldLayout: ?Layout,
41
+ oldResizeItem: ?LayoutItem,
42
+ resizing: boolean,
43
+ droppingDOMNode: ?ReactElement<any>,
44
+ droppingPosition?: DroppingPosition,
45
+ // Mirrored props
46
+ children: ReactChildrenArray<ReactElement<any>>,
47
+ compactType?: CompactType,
48
+ propsLayout?: Layout
49
+ };*/
50
+ /*:: import type { Props, DefaultProps } from "./ReactGridLayoutPropTypes";*/
76
51
  // End Types
77
- var layoutClassName = "react-grid-layout";
78
- var isFirefox = false; // Try...catch will protect from navigator not existing (e.g. node) or a bad implementation of navigator
79
-
52
+ const layoutClassName = "react-grid-layout";
53
+ let isFirefox = false;
54
+ // Try...catch will protect from navigator not existing (e.g. node) or a bad implementation of navigator
80
55
  try {
81
56
  isFirefox = /firefox/i.test(navigator.userAgent);
82
57
  } catch (e) {
83
58
  /* Ignore */
84
59
  }
60
+
85
61
  /**
86
62
  * A reactive, fluid grid layout with draggable, resizable components.
87
63
  */
88
64
 
89
-
90
- var ReactGridLayout = /*#__PURE__*/function (_React$Component) {
91
- _inherits(ReactGridLayout, _React$Component);
92
-
93
- var _super = _createSuper(ReactGridLayout);
94
-
95
- function ReactGridLayout() {
96
- var _this;
97
-
98
- _classCallCheck(this, ReactGridLayout);
99
-
100
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
101
- args[_key] = arguments[_key];
102
- }
103
-
104
- _this = _super.call.apply(_super, [this].concat(args));
105
-
106
- _defineProperty(_assertThisInitialized(_this), "state", {
65
+ class ReactGridLayout extends React.Component /*:: <Props, State>*/{
66
+ constructor() {
67
+ super(...arguments);
68
+ _defineProperty(this, "state", {
107
69
  activeDrag: null,
108
- layout: (0, _utils.synchronizeLayoutWithChildren)(_this.props.layout, _this.props.children, _this.props.cols, // Legacy support for verticalCompact: false
109
- (0, _utils.compactType)(_this.props), _this.props.allowOverlap),
70
+ layout: (0, _utils.synchronizeLayoutWithChildren)(this.props.layout, this.props.children, this.props.cols,
71
+ // Legacy support for verticalCompact: false
72
+ (0, _utils.compactType)(this.props), this.props.allowOverlap),
110
73
  mounted: false,
111
74
  oldDragItem: null,
112
75
  oldLayout: null,
113
76
  oldResizeItem: null,
77
+ resizing: false,
114
78
  droppingDOMNode: null,
115
79
  children: []
116
80
  });
117
-
118
- _defineProperty(_assertThisInitialized(_this), "dragEnterCounter", 0);
119
-
120
- _defineProperty(_assertThisInitialized(_this), "onDragStart", function (i
121
- /*: string*/
122
- , x
123
- /*: number*/
124
- , y
125
- /*: number*/
126
- , _ref) {
127
- var e = _ref.e,
128
- node = _ref.node;
129
- var layout = _this.state.layout;
130
- var l = (0, _utils.getLayoutItem)(layout, i);
81
+ _defineProperty(this, "dragEnterCounter", 0);
82
+ /**
83
+ * When dragging starts
84
+ * @param {String} i Id of the child
85
+ * @param {Number} x X position of the move
86
+ * @param {Number} y Y position of the move
87
+ * @param {Event} e The mousedown event
88
+ * @param {Element} node The current dragging DOM element
89
+ */
90
+ _defineProperty(this, "onDragStart", (i /*: string*/, x /*: number*/, y /*: number*/, _ref /*:: */) => {
91
+ let {
92
+ e,
93
+ node
94
+ } /*: GridDragEvent*/ = _ref /*: GridDragEvent*/;
95
+ const {
96
+ layout
97
+ } = this.state;
98
+ const l = (0, _utils.getLayoutItem)(layout, i);
131
99
  if (!l) return;
132
-
133
- _this.setState({
100
+ this.setState({
134
101
  oldDragItem: (0, _utils.cloneLayoutItem)(l),
135
102
  oldLayout: layout
136
103
  });
137
-
138
- return _this.props.onDragStart(layout, l, l, null, e, node);
104
+ return this.props.onDragStart(layout, l, l, null, e, node);
139
105
  });
106
+ /**
107
+ * Each drag movement create a new dragelement and move the element to the dragged location
108
+ * @param {String} i Id of the child
109
+ * @param {Number} x X position of the move
110
+ * @param {Number} y Y position of the move
111
+ * @param {Event} e The mousedown event
112
+ * @param {Element} node The current dragging DOM element
113
+ */
114
+ _defineProperty(this, "onDrag", (i, x, y, _ref2) => {
115
+ let {
116
+ e,
117
+ node
118
+ } = _ref2;
119
+ const {
120
+ oldDragItem
121
+ } = this.state;
122
+ let {
123
+ layout
124
+ } = this.state;
125
+ const {
126
+ cols,
127
+ allowOverlap,
128
+ preventCollision
129
+ } = this.props;
130
+ const l = (0, _utils.getLayoutItem)(layout, i);
131
+ if (!l) return;
140
132
 
141
- _defineProperty(_assertThisInitialized(_this), "onDrag", function (i, x, y, _ref2) {
142
- var e = _ref2.e,
143
- node = _ref2.node;
144
- var oldDragItem = _this.state.oldDragItem;
145
- var layout = _this.state.layout;
146
- var _this$props = _this.props,
147
- cols = _this$props.cols,
148
- allowOverlap = _this$props.allowOverlap,
149
- preventCollision = _this$props.preventCollision;
150
- var l = (0, _utils.getLayoutItem)(layout, i);
151
- if (!l) return; // Create placeholder (display only)
152
-
153
- var placeholder = {
133
+ // Create placeholder (display only)
134
+ const placeholder = {
154
135
  w: l.w,
155
136
  h: l.h,
156
137
  x: l.x,
157
138
  y: l.y,
158
139
  placeholder: true,
159
140
  i: i
160
- }; // Move the element to the dragged location.
161
-
162
- var isUserAction = true;
163
- layout = (0, _utils.moveElement)(layout, l, x, y, isUserAction, preventCollision, (0, _utils.compactType)(_this.props), cols, allowOverlap);
164
-
165
- _this.props.onDrag(layout, oldDragItem, l, placeholder, e, node);
141
+ };
166
142
 
167
- _this.setState({
168
- layout: allowOverlap ? layout : (0, _utils.compact)(layout, (0, _utils.compactType)(_this.props), cols),
143
+ // Move the element to the dragged location.
144
+ const isUserAction = true;
145
+ layout = (0, _utils.moveElement)(layout, l, x, y, isUserAction, preventCollision, (0, _utils.compactType)(this.props), cols, allowOverlap);
146
+ this.props.onDrag(layout, oldDragItem, l, placeholder, e, node);
147
+ this.setState({
148
+ layout: allowOverlap ? layout : (0, _utils.compact)(layout, (0, _utils.compactType)(this.props), cols),
169
149
  activeDrag: placeholder
170
150
  });
171
151
  });
152
+ /**
153
+ * When dragging stops, figure out which position the element is closest to and update its x and y.
154
+ * @param {String} i Index of the child.
155
+ * @param {Number} x X position of the move
156
+ * @param {Number} y Y position of the move
157
+ * @param {Event} e The mousedown event
158
+ * @param {Element} node The current dragging DOM element
159
+ */
160
+ _defineProperty(this, "onDragStop", (i, x, y, _ref3) => {
161
+ let {
162
+ e,
163
+ node
164
+ } = _ref3;
165
+ if (!this.state.activeDrag) return;
166
+ const {
167
+ oldDragItem
168
+ } = this.state;
169
+ let {
170
+ layout
171
+ } = this.state;
172
+ const {
173
+ cols,
174
+ preventCollision,
175
+ allowOverlap
176
+ } = this.props;
177
+ const l = (0, _utils.getLayoutItem)(layout, i);
178
+ if (!l) return;
172
179
 
173
- _defineProperty(_assertThisInitialized(_this), "onDragStop", function (i, x, y, _ref3) {
174
- var e = _ref3.e,
175
- node = _ref3.node;
176
- if (!_this.state.activeDrag) return;
177
- var oldDragItem = _this.state.oldDragItem;
178
- var layout = _this.state.layout;
179
- var _this$props2 = _this.props,
180
- cols = _this$props2.cols,
181
- preventCollision = _this$props2.preventCollision,
182
- allowOverlap = _this$props2.allowOverlap;
183
- var l = (0, _utils.getLayoutItem)(layout, i);
184
- if (!l) return; // Move the element here
185
-
186
- var isUserAction = true;
187
- layout = (0, _utils.moveElement)(layout, l, x, y, isUserAction, preventCollision, (0, _utils.compactType)(_this.props), cols, allowOverlap);
188
-
189
- _this.props.onDragStop(layout, oldDragItem, l, null, e, node); // Set state
190
-
191
-
192
- var newLayout = allowOverlap ? layout : (0, _utils.compact)(layout, (0, _utils.compactType)(_this.props), cols);
193
- var oldLayout = _this.state.oldLayout;
180
+ // Move the element here
181
+ const isUserAction = true;
182
+ layout = (0, _utils.moveElement)(layout, l, x, y, isUserAction, preventCollision, (0, _utils.compactType)(this.props), cols, allowOverlap);
194
183
 
195
- _this.setState({
184
+ // Set state
185
+ const newLayout = allowOverlap ? layout : (0, _utils.compact)(layout, (0, _utils.compactType)(this.props), cols);
186
+ this.props.onDragStop(newLayout, oldDragItem, l, null, e, node);
187
+ const {
188
+ oldLayout
189
+ } = this.state;
190
+ this.setState({
196
191
  activeDrag: null,
197
192
  layout: newLayout,
198
193
  oldDragItem: null,
199
194
  oldLayout: null
200
195
  });
201
-
202
- _this.onLayoutMaybeChanged(newLayout, oldLayout);
196
+ this.onLayoutMaybeChanged(newLayout, oldLayout);
203
197
  });
204
-
205
- _defineProperty(_assertThisInitialized(_this), "onResizeStart", function (i, w, h, _ref4) {
206
- var e = _ref4.e,
207
- node = _ref4.node;
208
- var layout = _this.state.layout;
209
- var l = (0, _utils.getLayoutItem)(layout, i);
198
+ _defineProperty(this, "onResizeStart", (i, w, h, _ref4) => {
199
+ let {
200
+ e,
201
+ node
202
+ } = _ref4;
203
+ const {
204
+ layout
205
+ } = this.state;
206
+ const l = (0, _utils.getLayoutItem)(layout, i);
210
207
  if (!l) return;
211
-
212
- _this.setState({
208
+ this.setState({
213
209
  oldResizeItem: (0, _utils.cloneLayoutItem)(l),
214
- oldLayout: _this.state.layout
210
+ oldLayout: this.state.layout,
211
+ resizing: true
215
212
  });
216
-
217
- _this.props.onResizeStart(layout, l, l, null, e, node);
213
+ this.props.onResizeStart(layout, l, l, null, e, node);
218
214
  });
219
-
220
- _defineProperty(_assertThisInitialized(_this), "onResize", function (i, w, h, _ref5) {
221
- var e = _ref5.e,
222
- node = _ref5.node;
223
- var _this$state = _this.state,
224
- layout = _this$state.layout,
225
- oldResizeItem = _this$state.oldResizeItem;
226
- var _this$props3 = _this.props,
227
- cols = _this$props3.cols,
228
- preventCollision = _this$props3.preventCollision,
229
- allowOverlap = _this$props3.allowOverlap;
230
-
231
- var _withLayoutItem = (0, _utils.withLayoutItem)(layout, i, function (l) {
215
+ _defineProperty(this, "onResize", (i, w, h, _ref5) => {
216
+ let {
217
+ e,
218
+ node,
219
+ size,
220
+ handle
221
+ } = _ref5;
222
+ const {
223
+ oldResizeItem
224
+ } = this.state;
225
+ let {
226
+ layout
227
+ } = this.state;
228
+ const {
229
+ cols,
230
+ preventCollision,
231
+ allowOverlap
232
+ } = this.props;
233
+ const [newLayout, l] = (0, _utils.withLayoutItem)(layout, i, l => {
232
234
  // Something like quad tree should be used
233
235
  // to find collisions faster
234
- var hasCollisions;
235
-
236
+ let hasCollisions;
236
237
  if (preventCollision && !allowOverlap) {
237
- var collisions = (0, _utils.getAllCollisions)(layout, _objectSpread(_objectSpread({}, l), {}, {
238
- w: w,
239
- h: h
240
- })).filter(function (layoutItem) {
241
- return layoutItem.i !== l.i;
242
- });
243
- hasCollisions = collisions.length > 0; // If we're colliding, we need adjust the placeholder.
238
+ const collisions = (0, _utils.getAllCollisions)(layout, {
239
+ ...l,
240
+ w,
241
+ h
242
+ }).filter(layoutItem => layoutItem.i !== l.i);
243
+ hasCollisions = collisions.length > 0;
244
244
 
245
245
  // If we're colliding, we need adjust the placeholder.
246
246
  if (hasCollisions) {
247
247
  // adjust w && h to maximum allowed space
248
- var leastX = Infinity,
249
- leastY = Infinity;
250
- collisions.forEach(function (layoutItem) {
248
+ let leastX = Infinity,
249
+ leastY = Infinity;
250
+ collisions.forEach(layoutItem => {
251
251
  if (layoutItem.x > l.x) leastX = Math.min(leastX, layoutItem.x);
252
252
  if (layoutItem.y > l.y) leastY = Math.min(leastY, layoutItem.y);
253
253
  });
@@ -255,23 +255,38 @@ var ReactGridLayout = /*#__PURE__*/function (_React$Component) {
255
255
  if (Number.isFinite(leastY)) l.h = leastY - l.y;
256
256
  }
257
257
  }
258
-
259
- if (!hasCollisions) {
260
- // Set new width and height.
261
- l.w = w;
262
- l.h = h;
263
- }
264
-
265
258
  return l;
266
- }),
267
- _withLayoutItem2 = _slicedToArray(_withLayoutItem, 2),
268
- newLayout = _withLayoutItem2[0],
269
- l = _withLayoutItem2[1]; // Shouldn't ever happen, but typechecking makes it necessary
270
-
259
+ });
271
260
 
272
- if (!l) return; // Create placeholder element (display only)
261
+ // Shouldn't ever happen, but typechecking makes it necessary
262
+ if (!l) return;
263
+ let finalLayout;
264
+ if (["sw", "w", "nw", "n", "ne"].indexOf(handle) !== -1) {
265
+ let x = l.x;
266
+ let y = l.y;
267
+ if (["sw", "nw", "w"].indexOf(handle) !== -1) {
268
+ x = l.x + (l.w - w);
269
+ w = l.x !== x && x < 0 ? l.w : w;
270
+ x = x < 0 ? 0 : x;
271
+ }
272
+ if (["ne", "n", "nw"].indexOf(handle) !== -1) {
273
+ y = l.y + (l.h - h);
274
+ h = l.y !== y && y < 0 ? l.h : h;
275
+ y = y < 0 ? 0 : y;
276
+ }
277
+ l.w = w;
278
+ l.h = h;
279
+ // Move the element to the new position.
280
+ const isUserAction = true;
281
+ finalLayout = (0, _utils.moveElement)(newLayout, l, x, y, isUserAction, this.props.preventCollision, (0, _utils.compactType)(this.props), cols, allowOverlap);
282
+ } else {
283
+ l.w = w;
284
+ l.h = h;
285
+ finalLayout = newLayout;
286
+ }
273
287
 
274
- var placeholder = {
288
+ // Create placeholder element (display only)
289
+ const placeholder = {
275
290
  w: l.w,
276
291
  h: l.h,
277
292
  x: l.x,
@@ -279,474 +294,401 @@ var ReactGridLayout = /*#__PURE__*/function (_React$Component) {
279
294
  static: true,
280
295
  i: i
281
296
  };
297
+ this.props.onResize(finalLayout, oldResizeItem, l, placeholder, e, node);
282
298
 
283
- _this.props.onResize(newLayout, oldResizeItem, l, placeholder, e, node); // Re-compact the newLayout and set the drag placeholder.
284
-
285
-
286
- _this.setState({
287
- layout: allowOverlap ? newLayout : (0, _utils.compact)(newLayout, (0, _utils.compactType)(_this.props), cols),
299
+ // Re-compact the newLayout and set the drag placeholder.
300
+ this.setState({
301
+ layout: allowOverlap ? finalLayout : (0, _utils.compact)(finalLayout, (0, _utils.compactType)(this.props), cols),
288
302
  activeDrag: placeholder
289
303
  });
290
304
  });
291
-
292
- _defineProperty(_assertThisInitialized(_this), "onResizeStop", function (i, w, h, _ref6) {
293
- var e = _ref6.e,
294
- node = _ref6.node;
295
- var _this$state2 = _this.state,
296
- layout = _this$state2.layout,
297
- oldResizeItem = _this$state2.oldResizeItem;
298
- var _this$props4 = _this.props,
299
- cols = _this$props4.cols,
300
- allowOverlap = _this$props4.allowOverlap;
301
- var l = (0, _utils.getLayoutItem)(layout, i);
302
-
303
- _this.props.onResizeStop(layout, oldResizeItem, l, null, e, node); // Set state
304
-
305
-
306
- var newLayout = allowOverlap ? layout : (0, _utils.compact)(layout, (0, _utils.compactType)(_this.props), cols);
307
- var oldLayout = _this.state.oldLayout;
308
-
309
- _this.setState({
305
+ _defineProperty(this, "onResizeStop", (i, w, h, _ref6) => {
306
+ let {
307
+ e,
308
+ node
309
+ } = _ref6;
310
+ const {
311
+ layout,
312
+ oldResizeItem
313
+ } = this.state;
314
+ const {
315
+ cols,
316
+ allowOverlap
317
+ } = this.props;
318
+ const l = (0, _utils.getLayoutItem)(layout, i);
319
+
320
+ // Set state
321
+ const newLayout = allowOverlap ? layout : (0, _utils.compact)(layout, (0, _utils.compactType)(this.props), cols);
322
+ this.props.onResizeStop(newLayout, oldResizeItem, l, null, e, node);
323
+ const {
324
+ oldLayout
325
+ } = this.state;
326
+ this.setState({
310
327
  activeDrag: null,
311
328
  layout: newLayout,
312
329
  oldResizeItem: null,
313
- oldLayout: null
330
+ oldLayout: null,
331
+ resizing: false
314
332
  });
315
-
316
- _this.onLayoutMaybeChanged(newLayout, oldLayout);
333
+ this.onLayoutMaybeChanged(newLayout, oldLayout);
317
334
  });
318
-
319
- _defineProperty(_assertThisInitialized(_this), "onDragOver", function (e) {
335
+ // Called while dragging an element. Part of browser native drag/drop API.
336
+ // Native event target might be the layout itself, or an element within the layout.
337
+ _defineProperty(this, "onDragOver", e => {
320
338
  var _e$nativeEvent$target;
321
-
322
339
  e.preventDefault(); // Prevent any browser native action
340
+ e.stopPropagation();
323
341
 
324
- e.stopPropagation(); // we should ignore events from layout's children in Firefox
342
+ // we should ignore events from layout's children in Firefox
325
343
  // to avoid unpredictable jumping of a dropping placeholder
326
344
  // FIXME remove this hack
327
-
328
- if (isFirefox && // $FlowIgnore can't figure this out
345
+ if (isFirefox &&
346
+ // $FlowIgnore can't figure this out
329
347
  !((_e$nativeEvent$target = e.nativeEvent.target) !== null && _e$nativeEvent$target !== void 0 && _e$nativeEvent$target.classList.contains(layoutClassName))) {
330
348
  return false;
331
349
  }
332
-
333
- var _this$props5 = _this.props,
334
- droppingItem = _this$props5.droppingItem,
335
- onDropDragOver = _this$props5.onDropDragOver,
336
- margin = _this$props5.margin,
337
- cols = _this$props5.cols,
338
- rowHeight = _this$props5.rowHeight,
339
- maxRows = _this$props5.maxRows,
340
- width = _this$props5.width,
341
- containerPadding = _this$props5.containerPadding,
342
- transformScale = _this$props5.transformScale; // Allow user to customize the dropping item or short-circuit the drop based on the results
350
+ const {
351
+ droppingItem,
352
+ onDropDragOver,
353
+ margin,
354
+ cols,
355
+ rowHeight,
356
+ maxRows,
357
+ width,
358
+ containerPadding,
359
+ transformScale
360
+ } = this.props;
361
+ // Allow user to customize the dropping item or short-circuit the drop based on the results
343
362
  // of the `onDragOver(e: Event)` callback.
344
-
345
- var onDragOverResult = onDropDragOver === null || onDropDragOver === void 0 ? void 0 : onDropDragOver(e);
346
-
363
+ const onDragOverResult = onDropDragOver === null || onDropDragOver === void 0 ? void 0 : onDropDragOver(e);
347
364
  if (onDragOverResult === false) {
348
- if (_this.state.droppingDOMNode) {
349
- _this.removeDroppingPlaceholder();
365
+ if (this.state.droppingDOMNode) {
366
+ this.removeDroppingPlaceholder();
350
367
  }
351
-
352
368
  return false;
353
369
  }
354
-
355
- var finalDroppingItem = _objectSpread(_objectSpread({}, droppingItem), onDragOverResult);
356
-
357
- var layout = _this.state.layout; // This is relative to the DOM element that this event fired for.
358
-
359
- var _e$nativeEvent = e.nativeEvent,
360
- layerX = _e$nativeEvent.layerX,
361
- layerY = _e$nativeEvent.layerY;
362
- var droppingPosition = {
370
+ const finalDroppingItem = {
371
+ ...droppingItem,
372
+ ...onDragOverResult
373
+ };
374
+ const {
375
+ layout
376
+ } = this.state;
377
+ // This is relative to the DOM element that this event fired for.
378
+ const {
379
+ layerX,
380
+ layerY
381
+ } = e.nativeEvent;
382
+ const droppingPosition = {
363
383
  left: layerX / transformScale,
364
384
  top: layerY / transformScale,
365
- e: e
385
+ e
366
386
  };
367
-
368
- if (!_this.state.droppingDOMNode) {
369
- var positionParams
370
- /*: PositionParams*/
371
- = {
372
- cols: cols,
373
- margin: margin,
374
- maxRows: maxRows,
375
- rowHeight: rowHeight,
387
+ if (!this.state.droppingDOMNode) {
388
+ const positionParams /*: PositionParams*/ = {
389
+ cols,
390
+ margin,
391
+ maxRows,
392
+ rowHeight,
376
393
  containerWidth: width,
377
394
  containerPadding: containerPadding || margin
378
395
  };
379
- var calculatedPosition = (0, _calculateUtils.calcXY)(positionParams, layerY, layerX, finalDroppingItem.w, finalDroppingItem.h);
380
-
381
- _this.setState({
396
+ const calculatedPosition = (0, _calculateUtils.calcXY)(positionParams, layerY, layerX, finalDroppingItem.w, finalDroppingItem.h);
397
+ this.setState({
382
398
  droppingDOMNode: /*#__PURE__*/React.createElement("div", {
383
399
  key: finalDroppingItem.i
384
400
  }),
385
- droppingPosition: droppingPosition,
386
- layout: [].concat(_toConsumableArray(layout), [_objectSpread(_objectSpread({}, finalDroppingItem), {}, {
401
+ droppingPosition,
402
+ layout: [...layout, {
403
+ ...finalDroppingItem,
387
404
  x: calculatedPosition.x,
388
405
  y: calculatedPosition.y,
389
406
  static: false,
390
407
  isDraggable: true
391
- })])
408
+ }]
392
409
  });
393
- } else if (_this.state.droppingPosition) {
394
- var _this$state$droppingP = _this.state.droppingPosition,
395
- left = _this$state$droppingP.left,
396
- top = _this$state$droppingP.top;
397
- var shouldUpdatePosition = left != layerX || top != layerY;
398
-
410
+ } else if (this.state.droppingPosition) {
411
+ const {
412
+ left,
413
+ top
414
+ } = this.state.droppingPosition;
415
+ const shouldUpdatePosition = left != layerX || top != layerY;
399
416
  if (shouldUpdatePosition) {
400
- _this.setState({
401
- droppingPosition: droppingPosition
417
+ this.setState({
418
+ droppingPosition
402
419
  });
403
420
  }
404
421
  }
405
422
  });
406
-
407
- _defineProperty(_assertThisInitialized(_this), "removeDroppingPlaceholder", function () {
408
- var _this$props6 = _this.props,
409
- droppingItem = _this$props6.droppingItem,
410
- cols = _this$props6.cols;
411
- var layout = _this.state.layout;
412
- var newLayout = (0, _utils.compact)(layout.filter(function (l) {
413
- return l.i !== droppingItem.i;
414
- }), (0, _utils.compactType)(_this.props), cols);
415
-
416
- _this.setState({
423
+ _defineProperty(this, "removeDroppingPlaceholder", () => {
424
+ const {
425
+ droppingItem,
426
+ cols
427
+ } = this.props;
428
+ const {
429
+ layout
430
+ } = this.state;
431
+ const newLayout = (0, _utils.compact)(layout.filter(l => l.i !== droppingItem.i), (0, _utils.compactType)(this.props), cols, this.props.allowOverlap);
432
+ this.setState({
417
433
  layout: newLayout,
418
434
  droppingDOMNode: null,
419
435
  activeDrag: null,
420
436
  droppingPosition: undefined
421
437
  });
422
438
  });
423
-
424
- _defineProperty(_assertThisInitialized(_this), "onDragLeave", function (e) {
439
+ _defineProperty(this, "onDragLeave", e => {
425
440
  e.preventDefault(); // Prevent any browser native action
426
-
427
441
  e.stopPropagation();
428
- _this.dragEnterCounter--; // onDragLeave can be triggered on each layout's child.
442
+ this.dragEnterCounter--;
443
+
444
+ // onDragLeave can be triggered on each layout's child.
429
445
  // But we know that count of dragEnter and dragLeave events
430
446
  // will be balanced after leaving the layout's container
431
447
  // so we can increase and decrease count of dragEnter and
432
448
  // when it'll be equal to 0 we'll remove the placeholder
433
-
434
- if (_this.dragEnterCounter === 0) {
435
- _this.removeDroppingPlaceholder();
449
+ if (this.dragEnterCounter === 0) {
450
+ this.removeDroppingPlaceholder();
436
451
  }
437
452
  });
438
-
439
- _defineProperty(_assertThisInitialized(_this), "onDragEnter", function (e) {
453
+ _defineProperty(this, "onDragEnter", e => {
440
454
  e.preventDefault(); // Prevent any browser native action
441
-
442
455
  e.stopPropagation();
443
- _this.dragEnterCounter++;
456
+ this.dragEnterCounter++;
444
457
  });
445
-
446
- _defineProperty(_assertThisInitialized(_this), "onDrop", function (e
447
- /*: Event*/
448
- ) {
458
+ _defineProperty(this, "onDrop", (e /*: Event*/) => {
449
459
  e.preventDefault(); // Prevent any browser native action
450
-
451
460
  e.stopPropagation();
452
- var droppingItem = _this.props.droppingItem;
453
- var layout = _this.state.layout;
454
- var item = layout.find(function (l) {
455
- return l.i === droppingItem.i;
456
- }); // reset dragEnter counter on drop
457
-
458
- _this.dragEnterCounter = 0;
459
-
460
- _this.removeDroppingPlaceholder();
461
-
462
- _this.props.onDrop(layout, item, e);
461
+ const {
462
+ droppingItem
463
+ } = this.props;
464
+ const {
465
+ layout
466
+ } = this.state;
467
+ const item = layout.find(l => l.i === droppingItem.i);
468
+
469
+ // reset dragEnter counter on drop
470
+ this.dragEnterCounter = 0;
471
+ this.removeDroppingPlaceholder();
472
+ this.props.onDrop(layout, item, e);
463
473
  });
464
-
465
- return _this;
466
474
  }
467
-
468
- _createClass(ReactGridLayout, [{
469
- key: "componentDidMount",
470
- value: function componentDidMount() {
471
- this.setState({
472
- mounted: true
473
- }); // Possibly call back with layout on mount. This should be done after correcting the layout width
474
- // to ensure we don't rerender with the wrong width.
475
-
476
- this.onLayoutMaybeChanged(this.state.layout, this.props.layout);
477
- }
478
- }, {
479
- key: "shouldComponentUpdate",
480
- value: function shouldComponentUpdate(nextProps
481
- /*: Props*/
482
- , nextState
483
- /*: State*/
484
- )
485
- /*: boolean*/
486
- {
487
- return (// NOTE: this is almost always unequal. Therefore the only way to get better performance
488
- // from SCU is if the user intentionally memoizes children. If they do, and they can
489
- // handle changes properly, performance will increase.
490
- this.props.children !== nextProps.children || !(0, _utils.fastRGLPropsEqual)(this.props, nextProps, _lodash.default) || this.state.activeDrag !== nextState.activeDrag || this.state.mounted !== nextState.mounted || this.state.droppingPosition !== nextState.droppingPosition
491
- );
492
- }
493
- }, {
494
- key: "componentDidUpdate",
495
- value: function componentDidUpdate(prevProps
496
- /*: Props*/
497
- , prevState
498
- /*: State*/
499
- ) {
500
- if (!this.state.activeDrag) {
501
- var newLayout = this.state.layout;
502
- var oldLayout = prevState.layout;
503
- this.onLayoutMaybeChanged(newLayout, oldLayout);
504
- }
505
- }
506
- /**
507
- * Calculates a pixel value for the container.
508
- * @return {String} Container height in pixels.
509
- */
510
-
511
- }, {
512
- key: "containerHeight",
513
- value: function containerHeight()
514
- /*: ?string*/
515
- {
516
- if (!this.props.autoSize) return;
517
- var nbRow = (0, _utils.bottom)(this.state.layout);
518
- var containerPaddingY = this.props.containerPadding ? this.props.containerPadding[1] : this.props.margin[1];
519
- return nbRow * this.props.rowHeight + (nbRow - 1) * this.props.margin[1] + containerPaddingY * 2 + "px";
475
+ componentDidMount() {
476
+ this.setState({
477
+ mounted: true
478
+ });
479
+ // Possibly call back with layout on mount. This should be done after correcting the layout width
480
+ // to ensure we don't rerender with the wrong width.
481
+ this.onLayoutMaybeChanged(this.state.layout, this.props.layout);
482
+ }
483
+ static getDerivedStateFromProps(nextProps /*: Props*/, prevState /*: State*/) /*: $Shape<State> | null*/{
484
+ let newLayoutBase;
485
+ if (prevState.activeDrag) {
486
+ return null;
520
487
  }
521
- /**
522
- * When dragging starts
523
- * @param {String} i Id of the child
524
- * @param {Number} x X position of the move
525
- * @param {Number} y Y position of the move
526
- * @param {Event} e The mousedown event
527
- * @param {Element} node The current dragging DOM element
528
- */
529
488
 
530
- }, {
531
- key: "onLayoutMaybeChanged",
532
- value: function onLayoutMaybeChanged(newLayout
533
- /*: Layout*/
534
- , oldLayout
535
- /*: ?Layout*/
536
- ) {
537
- if (!oldLayout) oldLayout = this.state.layout;
538
-
539
- if (!(0, _lodash.default)(oldLayout, newLayout)) {
540
- this.props.onLayoutChange(newLayout);
541
- }
489
+ // Legacy support for compactType
490
+ // Allow parent to set layout directly.
491
+ if (!(0, _fastEquals.deepEqual)(nextProps.layout, prevState.propsLayout) || nextProps.compactType !== prevState.compactType) {
492
+ newLayoutBase = nextProps.layout;
493
+ } else if (!(0, _utils.childrenEqual)(nextProps.children, prevState.children)) {
494
+ // If children change, also regenerate the layout. Use our state
495
+ // as the base in case because it may be more up to date than
496
+ // what is in props.
497
+ newLayoutBase = prevState.layout;
542
498
  }
543
- }, {
544
- key: "placeholder",
545
- value:
546
- /**
547
- * Create a placeholder object.
548
- * @return {Element} Placeholder div.
549
- */
550
- function placeholder()
551
- /*: ?ReactElement<any>*/
552
- {
553
- var activeDrag = this.state.activeDrag;
554
- if (!activeDrag) return null;
555
- var _this$props7 = this.props,
556
- width = _this$props7.width,
557
- cols = _this$props7.cols,
558
- margin = _this$props7.margin,
559
- containerPadding = _this$props7.containerPadding,
560
- rowHeight = _this$props7.rowHeight,
561
- maxRows = _this$props7.maxRows,
562
- useCSSTransforms = _this$props7.useCSSTransforms,
563
- transformScale = _this$props7.transformScale; // {...this.state.activeDrag} is pretty slow, actually
564
499
 
565
- return /*#__PURE__*/React.createElement(_GridItem.default, {
566
- w: activeDrag.w,
567
- h: activeDrag.h,
568
- x: activeDrag.x,
569
- y: activeDrag.y,
570
- i: activeDrag.i,
571
- className: "react-grid-placeholder",
572
- containerWidth: width,
573
- cols: cols,
574
- margin: margin,
575
- containerPadding: containerPadding || margin,
576
- maxRows: maxRows,
577
- rowHeight: rowHeight,
578
- isDraggable: false,
579
- isResizable: false,
580
- isBounded: false,
581
- useCSSTransforms: useCSSTransforms,
582
- transformScale: transformScale
583
- }, /*#__PURE__*/React.createElement("div", null));
500
+ // We need to regenerate the layout.
501
+ if (newLayoutBase) {
502
+ const newLayout = (0, _utils.synchronizeLayoutWithChildren)(newLayoutBase, nextProps.children, nextProps.cols, (0, _utils.compactType)(nextProps), nextProps.allowOverlap);
503
+ return {
504
+ layout: newLayout,
505
+ // We need to save these props to state for using
506
+ // getDerivedStateFromProps instead of componentDidMount (in which we would get extra rerender)
507
+ compactType: nextProps.compactType,
508
+ children: nextProps.children,
509
+ propsLayout: nextProps.layout
510
+ };
584
511
  }
585
- /**
586
- * Given a grid item, set its style attributes & surround in a <Draggable>.
587
- * @param {Element} child React element.
588
- * @return {Element} Element wrapped in draggable and properly placed.
589
- */
590
-
591
- }, {
592
- key: "processGridItem",
593
- value: function processGridItem(child
594
- /*: ReactElement<any>*/
595
- , isDroppingItem
596
- /*: boolean*/
597
- )
598
- /*: ?ReactElement<any>*/
599
- {
600
- if (!child || !child.key) return;
601
- var l = (0, _utils.getLayoutItem)(this.state.layout, String(child.key));
602
- if (!l) return null;
603
- var _this$props8 = this.props,
604
- width = _this$props8.width,
605
- cols = _this$props8.cols,
606
- margin = _this$props8.margin,
607
- containerPadding = _this$props8.containerPadding,
608
- rowHeight = _this$props8.rowHeight,
609
- maxRows = _this$props8.maxRows,
610
- isDraggable = _this$props8.isDraggable,
611
- isResizable = _this$props8.isResizable,
612
- isBounded = _this$props8.isBounded,
613
- useCSSTransforms = _this$props8.useCSSTransforms,
614
- transformScale = _this$props8.transformScale,
615
- draggableCancel = _this$props8.draggableCancel,
616
- draggableHandle = _this$props8.draggableHandle,
617
- resizeHandles = _this$props8.resizeHandles,
618
- resizeHandle = _this$props8.resizeHandle;
619
- var _this$state3 = this.state,
620
- mounted = _this$state3.mounted,
621
- droppingPosition = _this$state3.droppingPosition; // Determine user manipulations possible.
622
- // If an item is static, it can't be manipulated by default.
623
- // Any properties defined directly on the grid item will take precedence.
624
-
625
- var draggable = typeof l.isDraggable === "boolean" ? l.isDraggable : !l.static && isDraggable;
626
- var resizable = typeof l.isResizable === "boolean" ? l.isResizable : !l.static && isResizable;
627
- var resizeHandlesOptions = l.resizeHandles || resizeHandles; // isBounded set on child if set on parent, and child is not explicitly false
628
-
629
- var bounded = draggable && isBounded && l.isBounded !== false;
630
- return /*#__PURE__*/React.createElement(_GridItem.default, {
631
- containerWidth: width,
632
- cols: cols,
633
- margin: margin,
634
- containerPadding: containerPadding || margin,
635
- maxRows: maxRows,
636
- rowHeight: rowHeight,
637
- cancel: draggableCancel,
638
- handle: draggableHandle,
639
- onDragStop: this.onDragStop,
640
- onDragStart: this.onDragStart,
641
- onDrag: this.onDrag,
642
- onResizeStart: this.onResizeStart,
643
- onResize: this.onResize,
644
- onResizeStop: this.onResizeStop,
645
- isDraggable: draggable,
646
- isResizable: resizable,
647
- isBounded: bounded,
648
- useCSSTransforms: useCSSTransforms && mounted,
649
- usePercentages: !mounted,
650
- transformScale: transformScale,
651
- w: l.w,
652
- h: l.h,
653
- x: l.x,
654
- y: l.y,
655
- i: l.i,
656
- minH: l.minH,
657
- minW: l.minW,
658
- maxH: l.maxH,
659
- maxW: l.maxW,
660
- static: l.static,
661
- droppingPosition: isDroppingItem ? droppingPosition : undefined,
662
- resizeHandles: resizeHandlesOptions,
663
- resizeHandle: resizeHandle
664
- }, child);
665
- } // Called while dragging an element. Part of browser native drag/drop API.
666
- // Native event target might be the layout itself, or an element within the layout.
667
-
668
- }, {
669
- key: "render",
670
- value: function render()
671
- /*: React.Element<"div">*/
672
- {
673
- var _this2 = this;
674
-
675
- var _this$props9 = this.props,
676
- className = _this$props9.className,
677
- style = _this$props9.style,
678
- isDroppable = _this$props9.isDroppable,
679
- innerRef = _this$props9.innerRef;
680
- var mergedClassName = (0, _clsx.default)(layoutClassName, className);
681
-
682
- var mergedStyle = _objectSpread({
683
- height: this.containerHeight()
684
- }, style);
685
-
686
- return /*#__PURE__*/React.createElement("div", {
687
- ref: innerRef,
688
- className: mergedClassName,
689
- style: mergedStyle,
690
- onDrop: isDroppable ? this.onDrop : _utils.noop,
691
- onDragLeave: isDroppable ? this.onDragLeave : _utils.noop,
692
- onDragEnter: isDroppable ? this.onDragEnter : _utils.noop,
693
- onDragOver: isDroppable ? this.onDragOver : _utils.noop
694
- }, React.Children.map(this.props.children, function (child) {
695
- return _this2.processGridItem(child);
696
- }), isDroppable && this.state.droppingDOMNode && this.processGridItem(this.state.droppingDOMNode, true), this.placeholder());
512
+ return null;
513
+ }
514
+ shouldComponentUpdate(nextProps /*: Props*/, nextState /*: State*/) /*: boolean*/{
515
+ return (
516
+ // NOTE: this is almost always unequal. Therefore the only way to get better performance
517
+ // from SCU is if the user intentionally memoizes children. If they do, and they can
518
+ // handle changes properly, performance will increase.
519
+ this.props.children !== nextProps.children || !(0, _utils.fastRGLPropsEqual)(this.props, nextProps, _fastEquals.deepEqual) || this.state.activeDrag !== nextState.activeDrag || this.state.mounted !== nextState.mounted || this.state.droppingPosition !== nextState.droppingPosition
520
+ );
521
+ }
522
+ componentDidUpdate(prevProps /*: Props*/, prevState /*: State*/) {
523
+ if (!this.state.activeDrag) {
524
+ const newLayout = this.state.layout;
525
+ const oldLayout = prevState.layout;
526
+ this.onLayoutMaybeChanged(newLayout, oldLayout);
697
527
  }
698
- }], [{
699
- key: "getDerivedStateFromProps",
700
- value: function getDerivedStateFromProps(nextProps
701
- /*: Props*/
702
- , prevState
703
- /*: State*/
704
- )
705
- /*: $Shape<State> | null*/
706
- {
707
- var newLayoutBase;
708
-
709
- if (prevState.activeDrag) {
710
- return null;
711
- } // Legacy support for compactType
712
- // Allow parent to set layout directly.
713
-
714
-
715
- if (!(0, _lodash.default)(nextProps.layout, prevState.propsLayout) || nextProps.compactType !== prevState.compactType) {
716
- newLayoutBase = nextProps.layout;
717
- } else if (!(0, _utils.childrenEqual)(nextProps.children, prevState.children)) {
718
- // If children change, also regenerate the layout. Use our state
719
- // as the base in case because it may be more up to date than
720
- // what is in props.
721
- newLayoutBase = prevState.layout;
722
- } // We need to regenerate the layout.
723
-
724
-
725
- if (newLayoutBase) {
726
- var newLayout = (0, _utils.synchronizeLayoutWithChildren)(newLayoutBase, nextProps.children, nextProps.cols, (0, _utils.compactType)(nextProps), nextProps.allowOverlap);
727
- return {
728
- layout: newLayout,
729
- // We need to save these props to state for using
730
- // getDerivedStateFromProps instead of componentDidMount (in which we would get extra rerender)
731
- compactType: nextProps.compactType,
732
- children: nextProps.children,
733
- propsLayout: nextProps.layout
734
- };
735
- }
528
+ }
736
529
 
737
- return null;
530
+ /**
531
+ * Calculates a pixel value for the container.
532
+ * @return {String} Container height in pixels.
533
+ */
534
+ containerHeight() /*: ?string*/{
535
+ if (!this.props.autoSize) return;
536
+ const nbRow = (0, _utils.bottom)(this.state.layout);
537
+ const containerPaddingY = this.props.containerPadding ? this.props.containerPadding[1] : this.props.margin[1];
538
+ return nbRow * this.props.rowHeight + (nbRow - 1) * this.props.margin[1] + containerPaddingY * 2 + "px";
539
+ }
540
+ onLayoutMaybeChanged(newLayout /*: Layout*/, oldLayout /*: ?Layout*/) {
541
+ if (!oldLayout) oldLayout = this.state.layout;
542
+ if (!(0, _fastEquals.deepEqual)(oldLayout, newLayout)) {
543
+ this.props.onLayoutChange(newLayout);
738
544
  }
739
- }]);
740
-
741
- return ReactGridLayout;
742
- }(React.Component);
545
+ }
546
+ /**
547
+ * Create a placeholder object.
548
+ * @return {Element} Placeholder div.
549
+ */
550
+ placeholder() /*: ?ReactElement<any>*/{
551
+ const {
552
+ activeDrag
553
+ } = this.state;
554
+ if (!activeDrag) return null;
555
+ const {
556
+ width,
557
+ cols,
558
+ margin,
559
+ containerPadding,
560
+ rowHeight,
561
+ maxRows,
562
+ useCSSTransforms,
563
+ transformScale
564
+ } = this.props;
565
+
566
+ // {...this.state.activeDrag} is pretty slow, actually
567
+ return /*#__PURE__*/React.createElement(_GridItem.default, {
568
+ w: activeDrag.w,
569
+ h: activeDrag.h,
570
+ x: activeDrag.x,
571
+ y: activeDrag.y,
572
+ i: activeDrag.i,
573
+ className: "react-grid-placeholder ".concat(this.state.resizing ? "placeholder-resizing" : ""),
574
+ containerWidth: width,
575
+ cols: cols,
576
+ margin: margin,
577
+ containerPadding: containerPadding || margin,
578
+ maxRows: maxRows,
579
+ rowHeight: rowHeight,
580
+ isDraggable: false,
581
+ isResizable: false,
582
+ isBounded: false,
583
+ useCSSTransforms: useCSSTransforms,
584
+ transformScale: transformScale
585
+ }, /*#__PURE__*/React.createElement("div", null));
586
+ }
743
587
 
588
+ /**
589
+ * Given a grid item, set its style attributes & surround in a <Draggable>.
590
+ * @param {Element} child React element.
591
+ * @return {Element} Element wrapped in draggable and properly placed.
592
+ */
593
+ processGridItem(child /*: ReactElement<any>*/, isDroppingItem /*: boolean*/) /*: ?ReactElement<any>*/{
594
+ if (!child || !child.key) return;
595
+ const l = (0, _utils.getLayoutItem)(this.state.layout, String(child.key));
596
+ if (!l) return null;
597
+ const {
598
+ width,
599
+ cols,
600
+ margin,
601
+ containerPadding,
602
+ rowHeight,
603
+ maxRows,
604
+ isDraggable,
605
+ isResizable,
606
+ isBounded,
607
+ useCSSTransforms,
608
+ transformScale,
609
+ draggableCancel,
610
+ draggableHandle,
611
+ resizeHandles,
612
+ resizeHandle
613
+ } = this.props;
614
+ const {
615
+ mounted,
616
+ droppingPosition
617
+ } = this.state;
618
+
619
+ // Determine user manipulations possible.
620
+ // If an item is static, it can't be manipulated by default.
621
+ // Any properties defined directly on the grid item will take precedence.
622
+ const draggable = typeof l.isDraggable === "boolean" ? l.isDraggable : !l.static && isDraggable;
623
+ const resizable = typeof l.isResizable === "boolean" ? l.isResizable : !l.static && isResizable;
624
+ const resizeHandlesOptions = l.resizeHandles || resizeHandles;
625
+
626
+ // isBounded set on child if set on parent, and child is not explicitly false
627
+ const bounded = draggable && isBounded && l.isBounded !== false;
628
+ return /*#__PURE__*/React.createElement(_GridItem.default, {
629
+ containerWidth: width,
630
+ cols: cols,
631
+ margin: margin,
632
+ containerPadding: containerPadding || margin,
633
+ maxRows: maxRows,
634
+ rowHeight: rowHeight,
635
+ cancel: draggableCancel,
636
+ handle: draggableHandle,
637
+ onDragStop: this.onDragStop,
638
+ onDragStart: this.onDragStart,
639
+ onDrag: this.onDrag,
640
+ onResizeStart: this.onResizeStart,
641
+ onResize: this.onResize,
642
+ onResizeStop: this.onResizeStop,
643
+ isDraggable: draggable,
644
+ isResizable: resizable,
645
+ isBounded: bounded,
646
+ useCSSTransforms: useCSSTransforms && mounted,
647
+ usePercentages: !mounted,
648
+ transformScale: transformScale,
649
+ w: l.w,
650
+ h: l.h,
651
+ x: l.x,
652
+ y: l.y,
653
+ i: l.i,
654
+ minH: l.minH,
655
+ minW: l.minW,
656
+ maxH: l.maxH,
657
+ maxW: l.maxW,
658
+ static: l.static,
659
+ droppingPosition: isDroppingItem ? droppingPosition : undefined,
660
+ resizeHandles: resizeHandlesOptions,
661
+ resizeHandle: resizeHandle
662
+ }, child);
663
+ }
664
+ render() /*: React.Element<"div">*/{
665
+ const {
666
+ className,
667
+ style,
668
+ isDroppable,
669
+ innerRef
670
+ } = this.props;
671
+ const mergedClassName = (0, _clsx.default)(layoutClassName, className);
672
+ const mergedStyle = {
673
+ height: this.containerHeight(),
674
+ ...style
675
+ };
676
+ return /*#__PURE__*/React.createElement("div", {
677
+ ref: innerRef,
678
+ className: mergedClassName,
679
+ style: mergedStyle,
680
+ onDrop: isDroppable ? this.onDrop : _utils.noop,
681
+ onDragLeave: isDroppable ? this.onDragLeave : _utils.noop,
682
+ onDragEnter: isDroppable ? this.onDragEnter : _utils.noop,
683
+ onDragOver: isDroppable ? this.onDragOver : _utils.noop
684
+ }, React.Children.map(this.props.children, child => this.processGridItem(child)), isDroppable && this.state.droppingDOMNode && this.processGridItem(this.state.droppingDOMNode, true), this.placeholder());
685
+ }
686
+ }
744
687
  exports.default = ReactGridLayout;
745
-
688
+ // TODO publish internal ReactClass displayName transform
746
689
  _defineProperty(ReactGridLayout, "displayName", "ReactGridLayout");
747
-
690
+ // Refactored to another module to make way for preval
748
691
  _defineProperty(ReactGridLayout, "propTypes", _ReactGridLayoutPropTypes.default);
749
-
750
692
  _defineProperty(ReactGridLayout, "defaultProps", {
751
693
  autoSize: true,
752
694
  cols: 12,