UNPKG

react-sortablejs-list

Version:
112 lines (82 loc) 4.34 kB
'use strict'; 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; }; }(); var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _sortablejs = require('sortablejs'); var _sortablejs2 = _interopRequireDefault(_sortablejs); 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; } var SortableList = function (_Component) { _inherits(SortableList, _Component); function SortableList(props) { _classCallCheck(this, SortableList); var _this = _possibleConstructorReturn(this, (SortableList.__proto__ || Object.getPrototypeOf(SortableList)).call(this, props)); _this.componentWillReceiveProps = function (nextProps) { // console.log(nextProps.options); }; _this.changerSort = function (_ref) { var oldIndex = _ref.oldIndex, newIndex = _ref.newIndex; var options = []; if (_this.props.input.value.length > 0) { options = [].concat(_toConsumableArray(_this.props.input.value)); } else { options = [].concat(_toConsumableArray(_this.props.options)); } var item = options[oldIndex]; options.splice(oldIndex, 1); options.splice(newIndex, 0, item); _this.props.onChange(options); _this.props.input.onChange(options); }; _this.el = null; return _this; } _createClass(SortableList, [{ key: 'componentDidMount', value: function componentDidMount() { var changeSort = this.changerSort; var sortable = new _sortablejs2.default(this.el, { onSort: function onSort(_ref2) { var oldIndex = _ref2.oldIndex, newIndex = _ref2.newIndex; changeSort({ oldIndex: oldIndex, newIndex: newIndex }); } }); } }, { key: 'render', value: function render() { var _this2 = this; var meta = this.props.meta; return _react2.default.createElement( 'div', { ref: function ref(el) { _this2.el = el; }, style: _extends({ width: '100%', height: '100%' }, this.props.style) }, this.props.children ); } }]); return SortableList; }(_react.Component); SortableList.defaultProps = { style: {}, input: { onChange: function onChange() {}, value: [] }, onChange: function onChange() {}, options: [] }; exports.default = SortableList;