snaphy-react-sortable-dnd
Version:
React Sortable HOC build from React HOC
223 lines (180 loc) • 8.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
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; }; }();
exports.default = sortableElement;
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _propTypes = require('prop-types');
var _propTypes2 = _interopRequireDefault(_propTypes);
var _reactDom = require('react-dom');
var _invariant = require('invariant');
var _invariant2 = _interopRequireDefault(_invariant);
var _utils = require('../utils');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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; }
var unselect = [];
// Export Higher Order Sortable Element Component
function sortableElement(WrappedComponent) {
var _class, _temp2;
var config = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : { withRef: false };
return _temp2 = _class = function (_Component) {
_inherits(_class, _Component);
function _class() {
var _ref;
var _temp, _this, _ret;
_classCallCheck(this, _class);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = _class.__proto__ || Object.getPrototypeOf(_class)).call.apply(_ref, [this].concat(args))), _this), _this.state = {
selected: false
}, _this.onSelect = function (e) {
if (!(e.metaKey || e.ctrlKey)) {
unselect.forEach(function (func) {
return func();
});
unselect = [];
}
_this.setState({
selected: !_this.state.selected
});
if (!_this.state.selected) {
_this.context.manager.selected.push(_this.node.sortableInfo.index);
unselect.push(_this.unselect);
} else {
var selected = _this.context.manager.selected;
var index = selected.indexOf(_this.node.sortableInfo);
selected.splice(index, 1);
unselect.splice(unselect.indexOf(_this.unselect), 1);
}
}, _this.unselect = function () {
_this.setState({
selected: false
});
var selected = _this.context.manager.selected;
var index = selected.indexOf(_this.node.sortableInfo);
selected.splice(index, 1);
}, _temp), _possibleConstructorReturn(_this, _ret);
}
_createClass(_class, [{
key: 'componentDidMount',
value: function componentDidMount() {
this.helperClass = this.context.manager.helperClass;
var _props = this.props,
collection = _props.collection,
disabled = _props.disabled,
index = _props.index;
if (!disabled) {
this.setDraggable(collection, index);
}
}
}, {
key: 'componentWillReceiveProps',
value: function componentWillReceiveProps(nextProps) {
if (this.props.index !== nextProps.index && this.node) {
this.node.sortableInfo.index = nextProps.index;
}
if (this.props.disabled !== nextProps.disabled) {
var collection = nextProps.collection,
disabled = nextProps.disabled,
index = nextProps.index;
if (disabled) {
this.removeDraggable(collection);
} else {
this.setDraggable(collection, index);
}
} else if (this.props.collection !== nextProps.collection || nextProps.item && nextProps.item.selectedItems) {
this.removeDraggable(this.props.collection);
this.setDraggable(nextProps.collection, nextProps.index);
}
if (!nextProps.selected) {
this.unselect();
}
}
}, {
key: 'componentWillUnmount',
value: function componentWillUnmount() {
var _props2 = this.props,
collection = _props2.collection,
disabled = _props2.disabled;
unselect.splice(unselect.indexOf(this.unselect), 1);
if (!disabled) {
this.removeDraggable(collection);
}
}
}, {
key: 'setDraggable',
value: function setDraggable(collection, index) {
var node = this.node = (0, _reactDom.findDOMNode)(this);
node.sortableInfo = {
index: index,
collection: collection,
manager: this.context.manager
};
this.ref = { node: node };
this.context.manager.add(collection, this.ref);
}
}, {
key: 'removeDraggable',
value: function removeDraggable(collection) {
this.context.manager.remove(collection, this.ref);
}
}, {
key: 'getWrappedInstance',
value: function getWrappedInstance() {
(0, _invariant2.default)(config.withRef, 'To access the wrapped instance, you need to pass in {withRef: true} as the second argument of the SortableElement() call');
return this.refs.wrappedInstance;
}
}, {
key: 'render',
value: function render() {
var _this2 = this;
var ref = config.withRef ? 'wrappedInstance' : null;
var props = {};
var item = this.props.item;
if (item && item.selectedItems) {
var items = item.selectedItems.map(function (value, index) {
return _react2.default.createElement(WrappedComponent, {
key: index,
item: value,
className: _this2.helperClass
});
});
return _react2.default.createElement(
'div',
null,
items
);
}
var component = _react2.default.createElement(WrappedComponent, _extends({
ref: ref,
className: this.state.selected ? this.helperClass : ''
}, (0, _utils.omit)(this.props, 'collection', 'disabled', 'index'), {
onSelect: this.onSelect
}, props));
if (this.context.manager.isMultiple) {
return _react2.default.createElement(
'div',
null,
component
);
}
return component;
}
}]);
return _class;
}(_react.Component), _class.displayName = (0, _utils.provideDisplayName)('sortableElement', WrappedComponent), _class.contextTypes = {
manager: _propTypes2.default.object.isRequired
}, _class.propTypes = {
index: _propTypes2.default.number.isRequired,
collection: _propTypes2.default.oneOfType([_propTypes2.default.number, _propTypes2.default.string]),
disabled: _propTypes2.default.bool
}, _class.defaultProps = {
collection: 0
}, _temp2;
}