@bigfishtv/cockpit
Version:
122 lines (104 loc) • 4.51 kB
JavaScript
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 _class, _class2, _temp2;
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; }
import PropTypes from 'prop-types';
import React, { Component } from 'react';
import { withFormValue } from '@bigfishtv/react-forms';
import newId from '../../utils/newId';
var RepeatableFieldset = withFormValue(_class = (_temp2 = _class2 = function (_Component) {
_inherits(RepeatableFieldset, _Component);
function RepeatableFieldset() {
var _temp, _this, _ret;
_classCallCheck(this, RepeatableFieldset);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this = _possibleConstructorReturn(this, _Component.call.apply(_Component, [this].concat(args))), _this), _this.handleMove = function (id, afterId) {
var Cells = _this.props.formValue.value.slice();
var cell = Cells.filter(function (c) {
return c.id === id;
})[0];
var afterCell = Cells.filter(function (c) {
return c.id === afterId;
})[0];
var cellIndex = Cells.indexOf(cell);
var afterIndex = Cells.indexOf(afterCell);
Cells.splice(cellIndex, 1);
Cells.splice(afterIndex, 0, cell);
_this.props.formValue.update(Cells);
}, _temp), _possibleConstructorReturn(_this, _ret);
}
RepeatableFieldset.prototype.handleRemove = function handleRemove(index) {
var value = this.props.formValue.value.filter(function (val, i) {
return i !== index;
});
this.props.formValue.update(value);
};
RepeatableFieldset.prototype.handleDuplicate = function handleDuplicate(index) {
var formValue = this.props.formValue.value || [];
var items = formValue.filter(function (val, i) {
return i === index;
});
if (!items.length) {
console.warn('No item to duplicate???');
return;
}
var item = _extends({}, items[0]);
item.id = newId();
if (typeof item.title == 'string') item.title += ' copy';
formValue.push(item);
this.props.formValue.update(formValue);
};
RepeatableFieldset.prototype.render = function render() {
var _this2 = this;
var _props = this.props,
Item = _props.Item,
removable = _props.removable,
reorderable = _props.reorderable,
duplicable = _props.duplicable,
className = _props.className,
itemProps = _props.itemProps,
itemKey = _props.itemKey;
return React.createElement(
'div',
{ className: 'cells ' + className },
(this.props.formValue.value || []).map(function (item, i) {
return React.createElement(Item, _extends({
key: itemKey(item, i),
id: item.id,
select: [i],
removable: removable,
reorderable: reorderable,
duplicable: duplicable,
onRemove: function onRemove() {
return _this2.handleRemove(i);
},
onMove: _this2.handleMove,
onDuplicate: function onDuplicate() {
return _this2.handleDuplicate(i);
}
}, itemProps));
})
);
};
return RepeatableFieldset;
}(Component), _class2.propTypes = {
Item: PropTypes.func.isRequired,
formValue: PropTypes.object.isRequired,
removable: PropTypes.bool,
reorderable: PropTypes.bool,
duplicable: PropTypes.bool,
itemKey: PropTypes.func
}, _class2.defaultProps = {
removable: true,
reorderable: true,
duplicable: false,
className: '',
itemProps: {},
itemKey: function itemKey(item, i) {
return item.id || 'cell' + i;
}
}, _temp2)) || _class;
export { RepeatableFieldset as default };