UNPKG

wix-style-react

Version:
124 lines (122 loc) 4.61 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); exports.__esModule = true; exports.default = void 0; var _react = _interopRequireDefault(require("react")); var _propTypes = _interopRequireDefault(require("prop-types")); var _reactDnd = require("react-dnd"); var _types = require("../types"); var _constants = require("../constants"); var _jsxFileName = "/home/builduser/work/a9c1ac8876d5057c/packages/wix-style-react/dist/cjs/DragAndDrop/Draggable/components/DraggableTarget.js"; /* eslint-disable new-cap */ var target = { drop(props, monitor) { /** if drop was already done(on child), we skip this drop call */ if (monitor.getDropResult()) { return; } /** after drop released we send containerId and index of dropped item to dropResult, so endDrag inside of drag source can use this data */ return { containerId: props.containerId, index: props.index, groupName: props.groupName }; }, canDrop(props) { return props.droppable; }, hover(props, monitor, component) { var monitorItem = monitor.getItem(); var dragIndex = monitorItem.index; // position of item that we drag var hoverIndex = props.index; // position of item that we hover(want to put draggable item on it) var isSameGroup = props.groupName && monitorItem.groupName && props.groupName === monitorItem.groupName; // check that items from same group var isSameContainer = props.containerId === monitorItem.realTime.containerId; // check that items from same container /** in case that item not in same group and not from same container - do nothing */ if (!isSameContainer && !isSameGroup) { return; } /** in case that we hover over itself - do nothing */ if (!props.droppable || !component || hoverIndex === dragIndex && isSameContainer) { return; } /** if item is from same group but different container, that's mean that we move item from one container to another, and we need to move out item from previous container */ if (isSameGroup && !isSameContainer) { monitorItem.realTime.onMoveOut(monitorItem.id); } /** as react-dnd store same snapshot in monitor(so containerId of item will be same, even if we moved it with hover to another container) after any hovers, we need to save also real position of monitor, with real links to current container */ monitorItem.realTime.onMoveOut = props.onMoveOut; monitorItem.realTime.onDrop = props.onDrop; monitorItem.realTime.containerId = props.containerId; /** call callback, to ask parent to do some action, for example swap items or add new one, we send original position of item and new one, also id of item and original item state( it required for case, when we moving item from 1 container to another ) */ props.onHover({ removedIndex: dragIndex, addedIndex: hoverIndex, id: monitorItem.id, item: monitorItem.originalItem }); /** set new index for item */ monitorItem.index = hoverIndex; } }; var collect = connect => ({ connectDropTarget: connect.dropTarget() }); class DraggableTarget extends _react.default.PureComponent { constructor() { super(...arguments); this.registerNode = node => { this.props.setWrapperNode(node, this.props.index, this.props.item); }; } render() { var { children, connectDropTarget } = this.props; if (!connectDropTarget) return null; return connectDropTarget(/*#__PURE__*/_react.default.createElement("div", { ref: this.registerNode, [_constants.dataAttributes.draggableTarget]: true, __self: this, __source: { fileName: _jsxFileName, lineNumber: 97, columnNumber: 7 } }, children)); } } DraggableTarget.defaultProps = { droppable: true }; DraggableTarget.propTypes = { dataHook: _propTypes.default.string, children: _propTypes.default.any, connectDropTarget: _propTypes.default.func, // from react-dnd containerId: _propTypes.default.string, droppable: _propTypes.default.bool, groupName: _propTypes.default.string, index: _propTypes.default.number, onMoveOut: _propTypes.default.func, onDrop: _propTypes.default.func, onHover: _propTypes.default.func, setWrapperNode: _propTypes.default.func, item: _propTypes.default.object }; var _default = exports.default = (0, _reactDnd.DropTarget)(_types.ItemTypes.DRAGGABLE, target, collect)(DraggableTarget); //# sourceMappingURL=DraggableTarget.js.map