@bigfishtv/cockpit
Version:
83 lines (63 loc) • 4.75 kB
JavaScript
;
exports.__esModule = 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; };
exports.default = function (WrappedComponent) {
return function (_Component) {
_inherits(FormContextDecorator, _Component);
function FormContextDecorator() {
var _temp, _this, _ret;
_classCallCheck(this, FormContextDecorator);
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.onChange = function (nextFormValue) {
_this.setState({ formValue: nextFormValue });
}, _this.handleSave = function () {
_this.props.onSave(_this.state.formValue, _this.props.isNew);
_this.props.onClose(_this.state.formValue, true, _this.props.isNew);
_this.props.closeModal(true);
}, _this.handleClose = function () {
_this.props.onClose(_this.state.formValue, false, _this.props.isNew);
_this.props.closeModal();
}, _temp), _possibleConstructorReturn(_this, _ret);
}
FormContextDecorator.prototype.componentWillMount = function componentWillMount() {
var _props = this.props,
defaultValue = _props.defaultValue,
formValue = _props.formValue;
this.setState({
formValue: (0, _reactForms.createValue)({
schema: formValue && formValue.schema ? formValue.schema : undefined,
value: formValue && formValue.value ? formValue.value : defaultValue,
onChange: this.onChange,
params: formValue ? formValue.params : undefined,
errorList: formValue ? (0, _reactFormsUtils.extractErrorList)(formValue.completeErrorList, formValue.keyPath) : []
})
});
};
FormContextDecorator.prototype.render = function render() {
var props = _extends({}, this.props, {
formValue: this.state.formValue,
onClose: this.handleClose,
onSave: this.handleSave
});
return _react2.default.createElement(WrappedComponent, props);
};
return FormContextDecorator;
}(_react.Component);
};
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _reactForms = require('@bigfishtv/react-forms');
var _reactFormsUtils = require('../utils/reactFormsUtils');
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; } /**
* @module Decorators/modalFormValueContext
*/
/**
* Decorator to wrap a modal component with a contained form state and prop hooks for things like onSave
* @param {Component} WrappedComponent - Modal react component to wrap
* @return {Component} returns wrapped component
*/