react-grid-layout
Version:
A draggable and resizable grid layout with responsive breakpoints, for React.
804 lines (700 loc) • 31.9 kB
JavaScript
"use strict";
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); }
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var React = _interopRequireWildcard(require("react"));
var _lodash = _interopRequireDefault(require("lodash.isequal"));
var _classnames = _interopRequireDefault(require("classnames"));
var _utils = require("./utils");
var _calculateUtils = require("./calculateUtils");
var _GridItem = _interopRequireDefault(require("./GridItem"));
var _ReactGridLayoutPropTypes = _interopRequireDefault(require("./ReactGridLayoutPropTypes"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
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."); }
function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_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; }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
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; }
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; }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
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."); }
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); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
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; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
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); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
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 } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
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); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
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; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
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; }
// End Types
var layoutClassName = "react-grid-layout";
var isFirefox = false; // Try...catch will protect from navigator not existing (e.g. node) or a bad implementation of navigator
try {
isFirefox = /firefox/i.test(navigator.userAgent);
} catch (e) {
/* Ignore */
}
/**
* A reactive, fluid grid layout with draggable, resizable components.
*/
var ReactGridLayout = /*#__PURE__*/function (_React$Component) {
_inherits(ReactGridLayout, _React$Component);
var _super = _createSuper(ReactGridLayout);
// TODO publish internal ReactClass displayName transform
// Refactored to another module to make way for preval
function ReactGridLayout(props
/*: Props*/
, context
/*: any*/
)
/*: void*/
{
var _this;
_classCallCheck(this, ReactGridLayout);
_this = _super.call(this, props, context);
_defineProperty(_assertThisInitialized(_this), "state", {
activeDrag: null,
layout: (0, _utils.synchronizeLayoutWithChildren)(_this.props.layout, _this.props.children, _this.props.cols, // Legacy support for verticalCompact: false
(0, _utils.compactType)(_this.props)),
mounted: false,
oldDragItem: null,
oldLayout: null,
oldResizeItem: null,
droppingDOMNode: null,
children: []
});
_defineProperty(_assertThisInitialized(_this), "dragEnterCounter", 0);
_defineProperty(_assertThisInitialized(_this), "onDragOver", function (e) {
var _e$nativeEvent$target;
// we should ignore events from layout's children in Firefox
// to avoid unpredictable jumping of a dropping placeholder
// FIXME remove this hack
if (isFirefox && // $FlowIgnore can't figure this out
!((_e$nativeEvent$target = e.nativeEvent.target) !== null && _e$nativeEvent$target !== void 0 && _e$nativeEvent$target.classList.contains(layoutClassName))) {
// without this Firefox will not allow drop if currently over droppingItem
e.preventDefault();
return false;
}
var _this$props = _this.props,
droppingItem = _this$props.droppingItem,
margin = _this$props.margin,
cols = _this$props.cols,
rowHeight = _this$props.rowHeight,
maxRows = _this$props.maxRows,
width = _this$props.width,
containerPadding = _this$props.containerPadding;
var layout = _this.state.layout; // This is relative to the DOM element that this event fired for.
var _e$nativeEvent = e.nativeEvent,
layerX = _e$nativeEvent.layerX,
layerY = _e$nativeEvent.layerY;
var droppingPosition = {
left: layerX,
top: layerY,
e: e
};
if (!_this.state.droppingDOMNode) {
var positionParams
/*: PositionParams*/
= {
cols: cols,
margin: margin,
maxRows: maxRows,
rowHeight: rowHeight,
containerWidth: width,
containerPadding: containerPadding || margin
};
var calculatedPosition = (0, _calculateUtils.calcXY)(positionParams, layerY, layerX, droppingItem.w, droppingItem.h);
_this.setState({
droppingDOMNode: /*#__PURE__*/React.createElement("div", {
key: droppingItem.i
}),
droppingPosition: droppingPosition,
layout: [].concat(_toConsumableArray(layout), [_objectSpread(_objectSpread({}, droppingItem), {}, {
x: calculatedPosition.x,
y: calculatedPosition.y,
static: false,
isDraggable: true
})])
});
} else if (_this.state.droppingPosition) {
var _this$state$droppingP = _this.state.droppingPosition,
left = _this$state$droppingP.left,
top = _this$state$droppingP.top;
var shouldUpdatePosition = left != layerX || top != layerY;
if (shouldUpdatePosition) {
_this.setState({
droppingPosition: droppingPosition
});
}
}
e.stopPropagation();
e.preventDefault();
});
_defineProperty(_assertThisInitialized(_this), "removeDroppingPlaceholder", function () {
var _this$props2 = _this.props,
droppingItem = _this$props2.droppingItem,
cols = _this$props2.cols;
var layout = _this.state.layout;
var newLayout = (0, _utils.compact)(layout.filter(function (l) {
return l.i !== droppingItem.i;
}), (0, _utils.compactType)(_this.props), cols);
_this.setState({
layout: newLayout,
droppingDOMNode: null,
activeDrag: null,
droppingPosition: undefined
});
});
_defineProperty(_assertThisInitialized(_this), "onDragLeave", function () {
_this.dragEnterCounter--; // onDragLeave can be triggered on each layout's child.
// But we know that count of dragEnter and dragLeave events
// will be balanced after leaving the layout's container
// so we can increase and decrease count of dragEnter and
// when it'll be equal to 0 we'll remove the placeholder
if (_this.dragEnterCounter === 0) {
_this.removeDroppingPlaceholder();
}
});
_defineProperty(_assertThisInitialized(_this), "onDragEnter", function () {
_this.dragEnterCounter++;
});
_defineProperty(_assertThisInitialized(_this), "onDrop", function (e
/*: Event*/
) {
var droppingItem = _this.props.droppingItem;
var layout = _this.state.layout;
var item = layout.find(function (l) {
return l.i === droppingItem.i;
}); // reset dragEnter counter on drop
_this.dragEnterCounter = 0;
_this.removeDroppingPlaceholder();
_this.props.onDrop(layout, item, e);
});
(0, _utils.autoBindHandlers)(_assertThisInitialized(_this), ["onDragStart", "onDrag", "onDragStop", "onResizeStart", "onResize", "onResizeStop"]);
return _this;
}
_createClass(ReactGridLayout, [{
key: "componentDidMount",
value: function componentDidMount() {
this.setState({
mounted: true
}); // Possibly call back with layout on mount. This should be done after correcting the layout width
// to ensure we don't rerender with the wrong width.
this.onLayoutMaybeChanged(this.state.layout, this.props.layout);
}
}, {
key: "shouldComponentUpdate",
value: function shouldComponentUpdate(nextProps
/*: Props*/
, nextState
/*: State*/
)
/*: boolean*/
{
return (// NOTE: this is almost always unequal. Therefore the only way to get better performance
// from SCU is if the user intentionally memoizes children. If they do, and they can
// handle changes properly, performance will increase.
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
);
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps
/*: Props*/
, prevState
/*: State*/
) {
if (!this.state.activeDrag) {
var newLayout = this.state.layout;
var oldLayout = prevState.layout;
this.onLayoutMaybeChanged(newLayout, oldLayout);
}
}
/**
* Calculates a pixel value for the container.
* @return {String} Container height in pixels.
*/
}, {
key: "containerHeight",
value: function containerHeight()
/*: ?string*/
{
if (!this.props.autoSize) return;
var nbRow = (0, _utils.bottom)(this.state.layout);
var containerPaddingY = this.props.containerPadding ? this.props.containerPadding[1] : this.props.margin[1];
return nbRow * this.props.rowHeight + (nbRow - 1) * this.props.margin[1] + containerPaddingY * 2 + "px";
}
/**
* When dragging starts
* @param {String} i Id of the child
* @param {Number} x X position of the move
* @param {Number} y Y position of the move
* @param {Event} e The mousedown event
* @param {Element} node The current dragging DOM element
*/
}, {
key: "onDragStart",
value: function onDragStart(i
/*: string*/
, x
/*: number*/
, y
/*: number*/
, _ref)
/*: void*/
{
var e = _ref.e,
node = _ref.node;
var layout = this.state.layout;
var l = (0, _utils.getLayoutItem)(layout, i);
if (!l) return;
this.setState({
oldDragItem: (0, _utils.cloneLayoutItem)(l),
oldLayout: this.state.layout
});
return this.props.onDragStart(layout, l, l, null, e, node);
}
/**
* Each drag movement create a new dragelement and move the element to the dragged location
* @param {String} i Id of the child
* @param {Number} x X position of the move
* @param {Number} y Y position of the move
* @param {Event} e The mousedown event
* @param {Element} node The current dragging DOM element
*/
}, {
key: "onDrag",
value: function onDrag(i
/*: string*/
, x
/*: number*/
, y
/*: number*/
, _ref2)
/*: void*/
{
var e = _ref2.e,
node = _ref2.node;
var oldDragItem = this.state.oldDragItem;
var layout = this.state.layout;
var cols = this.props.cols;
var l = (0, _utils.getLayoutItem)(layout, i);
if (!l) return; // Create placeholder (display only)
var placeholder = {
w: l.w,
h: l.h,
x: l.x,
y: l.y,
placeholder: true,
i: i
}; // Move the element to the dragged location.
var isUserAction = true;
layout = (0, _utils.moveElement)(layout, l, x, y, isUserAction, this.props.preventCollision, (0, _utils.compactType)(this.props), cols);
this.props.onDrag(layout, oldDragItem, l, placeholder, e, node);
this.setState({
layout: (0, _utils.compact)(layout, (0, _utils.compactType)(this.props), cols),
activeDrag: placeholder
});
}
/**
* When dragging stops, figure out which position the element is closest to and update its x and y.
* @param {String} i Index of the child.
* @param {Number} x X position of the move
* @param {Number} y Y position of the move
* @param {Event} e The mousedown event
* @param {Element} node The current dragging DOM element
*/
}, {
key: "onDragStop",
value: function onDragStop(i
/*: string*/
, x
/*: number*/
, y
/*: number*/
, _ref3)
/*: void*/
{
var e = _ref3.e,
node = _ref3.node;
if (!this.state.activeDrag) return;
var oldDragItem = this.state.oldDragItem;
var layout = this.state.layout;
var _this$props3 = this.props,
cols = _this$props3.cols,
preventCollision = _this$props3.preventCollision;
var l = (0, _utils.getLayoutItem)(layout, i);
if (!l) return; // Move the element here
var isUserAction = true;
layout = (0, _utils.moveElement)(layout, l, x, y, isUserAction, preventCollision, (0, _utils.compactType)(this.props), cols);
this.props.onDragStop(layout, oldDragItem, l, null, e, node); // Set state
var newLayout = (0, _utils.compact)(layout, (0, _utils.compactType)(this.props), cols);
var oldLayout = this.state.oldLayout;
this.setState({
activeDrag: null,
layout: newLayout,
oldDragItem: null,
oldLayout: null
});
this.onLayoutMaybeChanged(newLayout, oldLayout);
}
}, {
key: "onLayoutMaybeChanged",
value: function onLayoutMaybeChanged(newLayout
/*: Layout*/
, oldLayout
/*: ?Layout*/
) {
if (!oldLayout) oldLayout = this.state.layout;
if (!(0, _lodash.default)(oldLayout, newLayout)) {
this.props.onLayoutChange(newLayout);
}
}
}, {
key: "onResizeStart",
value: function onResizeStart(i
/*: string*/
, w
/*: number*/
, h
/*: number*/
, _ref4) {
var e = _ref4.e,
node = _ref4.node;
var layout = this.state.layout;
var l = (0, _utils.getLayoutItem)(layout, i);
if (!l) return;
this.setState({
oldResizeItem: (0, _utils.cloneLayoutItem)(l),
oldLayout: this.state.layout
});
this.props.onResizeStart(layout, l, l, null, e, node);
}
}, {
key: "onResize",
value: function onResize(i
/*: string*/
, w
/*: number*/
, h
/*: number*/
, _ref5) {
var e = _ref5.e,
node = _ref5.node;
var _this$state = this.state,
layout = _this$state.layout,
oldResizeItem = _this$state.oldResizeItem;
var _this$props4 = this.props,
cols = _this$props4.cols,
preventCollision = _this$props4.preventCollision;
var _withLayoutItem = (0, _utils.withLayoutItem)(layout, i, function (l) {
// Something like quad tree should be used
// to find collisions faster
var hasCollisions;
if (preventCollision) {
var collisions = (0, _utils.getAllCollisions)(layout, _objectSpread(_objectSpread({}, l), {}, {
w: w,
h: h
})).filter(function (layoutItem) {
return layoutItem.i !== l.i;
});
hasCollisions = collisions.length > 0; // If we're colliding, we need adjust the placeholder.
if (hasCollisions) {
// adjust w && h to maximum allowed space
var leastX = Infinity,
leastY = Infinity;
collisions.forEach(function (layoutItem) {
if (layoutItem.x > l.x) leastX = Math.min(leastX, layoutItem.x);
if (layoutItem.y > l.y) leastY = Math.min(leastY, layoutItem.y);
});
if (Number.isFinite(leastX)) l.w = leastX - l.x;
if (Number.isFinite(leastY)) l.h = leastY - l.y;
}
}
if (!hasCollisions) {
// Set new width and height.
l.w = w;
l.h = h;
}
return l;
}),
_withLayoutItem2 = _slicedToArray(_withLayoutItem, 2),
newLayout = _withLayoutItem2[0],
l = _withLayoutItem2[1]; // Shouldn't ever happen, but typechecking makes it necessary
if (!l) return; // Create placeholder element (display only)
var placeholder = {
w: l.w,
h: l.h,
x: l.x,
y: l.y,
static: true,
i: i
};
this.props.onResize(newLayout, oldResizeItem, l, placeholder, e, node); // Re-compact the newLayout and set the drag placeholder.
this.setState({
layout: (0, _utils.compact)(newLayout, (0, _utils.compactType)(this.props), cols),
activeDrag: placeholder
});
}
}, {
key: "onResizeStop",
value: function onResizeStop(i
/*: string*/
, w
/*: number*/
, h
/*: number*/
, _ref6) {
var e = _ref6.e,
node = _ref6.node;
var _this$state2 = this.state,
layout = _this$state2.layout,
oldResizeItem = _this$state2.oldResizeItem;
var cols = this.props.cols;
var l = (0, _utils.getLayoutItem)(layout, i);
this.props.onResizeStop(layout, oldResizeItem, l, null, e, node); // Set state
var newLayout = (0, _utils.compact)(layout, (0, _utils.compactType)(this.props), cols);
var oldLayout = this.state.oldLayout;
this.setState({
activeDrag: null,
layout: newLayout,
oldResizeItem: null,
oldLayout: null
});
this.onLayoutMaybeChanged(newLayout, oldLayout);
}
/**
* Create a placeholder object.
* @return {Element} Placeholder div.
*/
}, {
key: "placeholder",
value: function placeholder()
/*: ?ReactElement<any>*/
{
var activeDrag = this.state.activeDrag;
if (!activeDrag) return null;
var _this$props5 = this.props,
width = _this$props5.width,
cols = _this$props5.cols,
margin = _this$props5.margin,
containerPadding = _this$props5.containerPadding,
rowHeight = _this$props5.rowHeight,
maxRows = _this$props5.maxRows,
useCSSTransforms = _this$props5.useCSSTransforms,
transformScale = _this$props5.transformScale; // {...this.state.activeDrag} is pretty slow, actually
return /*#__PURE__*/React.createElement(_GridItem.default, {
w: activeDrag.w,
h: activeDrag.h,
x: activeDrag.x,
y: activeDrag.y,
i: activeDrag.i,
className: "react-grid-placeholder",
containerWidth: width,
cols: cols,
margin: margin,
containerPadding: containerPadding || margin,
maxRows: maxRows,
rowHeight: rowHeight,
isDraggable: false,
isResizable: false,
isBounded: false,
useCSSTransforms: useCSSTransforms,
transformScale: transformScale
}, /*#__PURE__*/React.createElement("div", null));
}
/**
* Given a grid item, set its style attributes & surround in a <Draggable>.
* @param {Element} child React element.
* @return {Element} Element wrapped in draggable and properly placed.
*/
}, {
key: "processGridItem",
value: function processGridItem(child
/*: ReactElement<any>*/
, isDroppingItem
/*: boolean*/
)
/*: ?ReactElement<any>*/
{
if (!child || !child.key) return;
var l = (0, _utils.getLayoutItem)(this.state.layout, String(child.key));
if (!l) return null;
var _this$props6 = this.props,
width = _this$props6.width,
cols = _this$props6.cols,
margin = _this$props6.margin,
containerPadding = _this$props6.containerPadding,
rowHeight = _this$props6.rowHeight,
maxRows = _this$props6.maxRows,
isDraggable = _this$props6.isDraggable,
isResizable = _this$props6.isResizable,
isBounded = _this$props6.isBounded,
useCSSTransforms = _this$props6.useCSSTransforms,
transformScale = _this$props6.transformScale,
draggableCancel = _this$props6.draggableCancel,
draggableHandle = _this$props6.draggableHandle,
resizeHandles = _this$props6.resizeHandles,
resizeHandle = _this$props6.resizeHandle;
var _this$state3 = this.state,
mounted = _this$state3.mounted,
droppingPosition = _this$state3.droppingPosition; // Determine user manipulations possible.
// If an item is static, it can't be manipulated by default.
// Any properties defined directly on the grid item will take precedence.
var draggable = typeof l.isDraggable === "boolean" ? l.isDraggable : !l.static && isDraggable;
var resizable = typeof l.isResizable === "boolean" ? l.isResizable : !l.static && isResizable;
var resizeHandlesOptions = l.resizeHandles || resizeHandles; // isBounded set on child if set on parent, and child is not explicitly false
var bounded = draggable && isBounded && l.isBounded !== false;
return /*#__PURE__*/React.createElement(_GridItem.default, {
containerWidth: width,
cols: cols,
margin: margin,
containerPadding: containerPadding || margin,
maxRows: maxRows,
rowHeight: rowHeight,
cancel: draggableCancel,
handle: draggableHandle,
onDragStop: this.onDragStop,
onDragStart: this.onDragStart,
onDrag: this.onDrag,
onResizeStart: this.onResizeStart,
onResize: this.onResize,
onResizeStop: this.onResizeStop,
isDraggable: draggable,
isResizable: resizable,
isBounded: bounded,
useCSSTransforms: useCSSTransforms && mounted,
usePercentages: !mounted,
transformScale: transformScale,
w: l.w,
h: l.h,
x: l.x,
y: l.y,
i: l.i,
minH: l.minH,
minW: l.minW,
maxH: l.maxH,
maxW: l.maxW,
static: l.static,
droppingPosition: isDroppingItem ? droppingPosition : undefined,
resizeHandles: resizeHandlesOptions,
resizeHandle: resizeHandle
}, child);
} // Called while dragging an element. Part of browser native drag/drop API.
// Native event target might be the layout itself, or an element within the layout.
}, {
key: "render",
value: function render()
/*: React.Element<"div">*/
{
var _this2 = this;
var _this$props7 = this.props,
className = _this$props7.className,
style = _this$props7.style,
isDroppable = _this$props7.isDroppable,
innerRef = _this$props7.innerRef;
var mergedClassName = (0, _classnames.default)(layoutClassName, className);
var mergedStyle = _objectSpread({
height: this.containerHeight()
}, style);
return /*#__PURE__*/React.createElement("div", {
ref: innerRef,
className: mergedClassName,
style: mergedStyle,
onDrop: isDroppable ? this.onDrop : _utils.noop,
onDragLeave: isDroppable ? this.onDragLeave : _utils.noop,
onDragEnter: isDroppable ? this.onDragEnter : _utils.noop,
onDragOver: isDroppable ? this.onDragOver : _utils.noop
}, React.Children.map(this.props.children, function (child) {
return _this2.processGridItem(child);
}), isDroppable && this.state.droppingDOMNode && this.processGridItem(this.state.droppingDOMNode, true), this.placeholder());
}
}], [{
key: "getDerivedStateFromProps",
value: function getDerivedStateFromProps(nextProps
/*: Props*/
, prevState
/*: State*/
)
/*: $Shape<State> | null*/
{
var newLayoutBase;
if (prevState.activeDrag) {
return null;
} // Legacy support for compactType
// Allow parent to set layout directly.
if (!(0, _lodash.default)(nextProps.layout, prevState.propsLayout) || nextProps.compactType !== prevState.compactType) {
newLayoutBase = nextProps.layout;
} else if (!(0, _utils.childrenEqual)(nextProps.children, prevState.children)) {
// If children change, also regenerate the layout. Use our state
// as the base in case because it may be more up to date than
// what is in props.
newLayoutBase = prevState.layout;
} // We need to regenerate the layout.
if (newLayoutBase) {
var newLayout = (0, _utils.synchronizeLayoutWithChildren)(newLayoutBase, nextProps.children, nextProps.cols, (0, _utils.compactType)(nextProps));
return {
layout: newLayout,
// We need to save these props to state for using
// getDerivedStateFromProps instead of componentDidMount (in which we would get extra rerender)
compactType: nextProps.compactType,
children: nextProps.children,
propsLayout: nextProps.layout
};
}
return null;
}
}]);
return ReactGridLayout;
}(React.Component);
exports.default = ReactGridLayout;
_defineProperty(ReactGridLayout, "displayName", "ReactGridLayout");
_defineProperty(ReactGridLayout, "propTypes", _ReactGridLayoutPropTypes.default);
_defineProperty(ReactGridLayout, "defaultProps", {
autoSize: true,
cols: 12,
className: "",
style: {},
draggableHandle: "",
draggableCancel: "",
containerPadding: null,
rowHeight: 150,
maxRows: Infinity,
// infinite vertical growth
layout: [],
margin: [10, 10],
isBounded: false,
isDraggable: true,
isResizable: true,
isDroppable: false,
useCSSTransforms: true,
transformScale: 1,
verticalCompact: true,
compactType: "vertical",
preventCollision: false,
droppingItem: {
i: "__dropping-elem__",
h: 1,
w: 1
},
resizeHandles: ["se"],
onLayoutChange: _utils.noop,
onDragStart: _utils.noop,
onDrag: _utils.noop,
onDragStop: _utils.noop,
onResizeStart: _utils.noop,
onResize: _utils.noop,
onResizeStop: _utils.noop,
onDrop: _utils.noop
});