UNPKG

wix-style-react

Version:
228 lines (187 loc) • 8.73 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = undefined; var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; var _createClass = 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _propTypes = require('prop-types'); var _propTypes2 = _interopRequireDefault(_propTypes); var _WixComponent2 = require('../BaseComponents/WixComponent'); var _WixComponent3 = _interopRequireDefault(_WixComponent2); var _Draggable = require('../DragAndDrop/Draggable'); var _Container = require('../DragAndDrop/Draggable/components/Container'); var _Container2 = _interopRequireDefault(_Container); var _DragDropContextProvider = require('../DragDropContextProvider'); var _DragDropContextProvider2 = _interopRequireDefault(_DragDropContextProvider); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } /** * Attaches Drag and Drop behavior to a list of items */ var SortableList = function (_WixComponent) { _inherits(SortableList, _WixComponent); function SortableList() { var _ref; var _temp, _this, _ret; _classCallCheck(this, SortableList); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = SortableList.__proto__ || Object.getPrototypeOf(SortableList)).call.apply(_ref, [this].concat(args))), _this), _this.state = { items: _this.props.items || [] }, _this.handleMoveOut = function (id) { _this.setState({ items: _this.state.items.filter(function (it) { return it.id !== id; }) }); }, _this.handleHover = function (removedIndex, addedIndex) { var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; _this.setState(function (prevState) { var nextItems = [].concat(_toConsumableArray(prevState.items)); if (!nextItems.find(function (it) { return it.id === options.id; })) { nextItems.splice(addedIndex, 0, options.item); } else { nextItems.splice.apply(nextItems, [addedIndex, 0].concat(_toConsumableArray(nextItems.splice(removedIndex, 1)))); } return { items: nextItems }; }); }, _this.handleDrop = function (_ref2) { var payload = _ref2.payload, addedIndex = _ref2.addedIndex, removedIndex = _ref2.removedIndex, addedToContainerId = _ref2.addedToContainerId, removedFromContainerId = _ref2.removedFromContainerId; _this.props.onDrop({ payload: payload, addedIndex: addedIndex, removedIndex: removedIndex, addedToContainerId: addedToContainerId, removedFromContainerId: removedFromContainerId }); }, _this.handleDragStart = function (data) { if (_this.props.onDragStart) { _this.props.onDragStart(data); } }, _this.handleDragEnd = function (data) { if (_this.props.onDragEnd) { _this.props.onDragEnd(data); } }, _temp), _possibleConstructorReturn(_this, _ret); } _createClass(SortableList, [{ key: 'componentWillReceiveProps', value: function componentWillReceiveProps(_ref3) { var items = _ref3.items; if (items) { this.setState({ items: items }); } } }, { key: 'renderPreview', value: function renderPreview() { var _this2 = this; var _props = this.props, className = _props.className, contentClassName = _props.contentClassName, renderItem = _props.renderItem; return _react2.default.createElement( 'div', { className: className }, _react2.default.createElement( 'div', { className: contentClassName }, this.state.items.map(function (item, index) { return _react2.default.createElement( 'div', { key: item.id + '-' + index + '-' + _this2.props.containerId }, renderItem({ item: item, id: item.id, isPlaceholder: false, isPreview: false }) ); }) ) ); } }, { key: 'render', value: function render() { var _this3 = this; var _props2 = this.props, className = _props2.className, contentClassName = _props2.contentClassName, groupName = _props2.groupName, dragPreview = _props2.dragPreview; var common = { groupName: groupName, containerId: this.props.containerId, onHover: this.handleHover, onMoveOut: this.handleMoveOut }; if (dragPreview) { return this.renderPreview(); } return _react2.default.createElement( _DragDropContextProvider2.default, null, _react2.default.createElement( _Container2.default, _extends({ className: className, total: this.state.items.length }, common), _react2.default.createElement( 'div', { className: contentClassName }, this.state.items.map(function (item, index) { return _react2.default.createElement(_Draggable.Draggable, _extends({}, common, { key: item.id + '-' + _this3.props.containerId, id: item.id, index: index, item: item, renderItem: _this3.props.renderItem, withHandle: _this3.props.withHandle, usePortal: _this3.props.usePortal, onDrop: _this3.handleDrop, onDragStart: _this3.handleDragStart, onDragEnd: _this3.handleDragEnd })); }) ) ) ); } }]); return SortableList; }(_WixComponent3.default); exports.default = SortableList; SortableList.displayName = 'SortableList'; SortableList.propTypes = _extends({}, _Draggable.Draggable.propTypes, { /** in case of wrong position of item during drag you can force SortableList to use portals */ usePortal: _propTypes2.default.bool, /** if you are having nested SortableLists, list that you are currently dragging need to be marked as dragPreview inside of renderItem callback */ dragPreview: _propTypes2.default.bool, /** list of items with {id: any} */ items: _propTypes2.default.array, /** callback for drag start */ onDragStart: _propTypes2.default.func, /** callback for drag end */ onDragEnd: _propTypes2.default.func, /** className of the root container */ className: _propTypes2.default.string, /** className of the first items parent container */ contentClassName: _propTypes2.default.string });