@bigfishtv/cockpit
Version:
75 lines (60 loc) • 3.52 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; };
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
*/
import React, { Component } from 'react';
import { createValue } from '@bigfishtv/react-forms';
import { extractErrorList } from '../utils/reactFormsUtils';
/**
* 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
*/
export 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: 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 ? 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 React.createElement(WrappedComponent, props);
};
return FormContextDecorator;
}(Component);
}