UNPKG

@darwino/darwino-react

Version:

A set of Javascript classes and utilities

317 lines (246 loc) 8.51 kB
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } /* * (c) Copyright Darwino Inc. 2014-2017. */ import React, { Component } from 'react'; import { get as _get, set as _set, isEqual as _isEqual } from 'lodash'; import PropTypes from 'prop-types'; import { connect } from 'react-redux'; import { Field } from 'redux-form'; import BaseMainPage from "./BaseMainPage"; import BaseMessages from "./BaseMessages"; /* */ class BaseReduxForm extends BaseMainPage { // Read only means that the value is not displayed as a control but rather as piece of HTML // Read only means that the value is not displayed as a control but rather as piece of HTML // The value is displayed as a read only control // The value is displayed as an editable control // Form registered by name // Static member for now, until we find a better solution with the redux form team // https://github.com/erikras/redux-form/issues/3340 // Form validation // Calculate computed fields on document load _onLoad(values) { if (!values) return; this.fieldValues = values; this.computedValues = {}; this.calculateOnLoad(this.computedValues); this._getRegisteredComponents().forEach(f => { if (f.calculateOnLoad) { f.calculateOnLoad(this.computedValues); } }); } // Calculate computed fields on document change _onChange(values) { if (!values) return; this.fieldValues = values; if (this.context.dialog) { this.context.dialog.setState({ value: values }); } var oldValues = _objectSpread({}, this.computedValues); var computed = this.computedValues; if (this.calculateOnChange) this.calculateOnChange(computed); this._getRegisteredComponents().forEach(f => { if (f.calculateOnChange) { f.calculateOnChange(computed); } }); if (!_isEqual(oldValues, computed)) { this.forceUpdate(); } } // Dispatch onSubmit - Use for remote submit // https://redux-form.com/6.5.0/examples/remotesubmit/ constructor(props, context) { super(props, context); _defineProperty(this, "_onValidate", function (values) { var err = {}; Object.assign(err, this.validate(values)); this._getRegisteredComponents().forEach(f => { if (f.validate) { Object.assign(err, f.validate(values)); } }); return err; }); this.submit = this.submit.bind(this); this.cancel = this.cancel.bind(this); this.delete = this.delete.bind(this); this.handleUpdateDocument = this.handleUpdateDocument.bind(this); // Methods bellow to be removed! this.handleDeleteDocument = this.delete; this.handleCancel = this.cancel; // We store a copy of the fields and the computed fields in the component itself this.fieldValues = {}; this.computedValues = {}; // The state carries information on the current document assigned to the form this.state = _objectSpread({}, this.state, { forceMode: BaseReduxForm.DEFAULT_MODE, mode: BaseReduxForm.DISABLED, docInitialized: false }); } componentDidMount() { // Add the current component to the validation list BaseReduxForm.__forms[this.props.form] = this; } componentWillUnmount() { // Remove the validation function for that form if (BaseReduxForm.__forms[this.props.form] == this) { delete BaseReduxForm.__forms[this.props.form]; } } shouldComponentUpdate(nextProps, nextState) { // The doc has been tagged as initialized but the computed values are not yet available // We defer the redering to later if (nextState.docInitializing) { return false; } return true; } _registerComponent(subform) { super._registerComponent(subform); if (this.state.docInitialized) { if (subform.defaultValues && this.state.newDoc) { // Is there a better way to do this by updating the whole set of values in the redux store? //subform.defaultValues(this.fieldValues) //this.props.initialize(this.fieldValues); var json = {}; subform.defaultValues(json); for (var k in json) { this.props.change(k, json[k]); } } if (subform.calculateOnLoad) subform.calculateOnLoad(this.computedValues); } } getForm() { return this; } getMode() { return this.state.mode; } setMode(mode) { this.setState({ forceMode: mode, mode: mode || BaseReduxForm.EDITABLE }); } isReadOnly() { return this.state.mode == BaseReduxForm.READONLY; } isDisabled() { return this.state.mode == BaseReduxForm.DISABLED; } isEditable() { return this.state.mode == BaseReduxForm.EDITABLE; } isInitialized() { return this.state.docInitialized; } isInitializing() { return this.state.docInitializing; } isNewDoc() { return true; } getFieldValue(field, def) { var v = _get(this.fieldValues, field, undefined); if (v === undefined) { v = _get(this.computedValues, field, def); } return v; } setFieldValue(field, value) { this.props.change(field, value); _set(this.fieldValues, field, value); } getFieldValues() { return this.fieldValues; } getComputedValue(field, def) { return this.computedValues[field]; } getComputedValues() { return this.computedValues; } getAllValues() { return _objectSpread({}, this.fieldValues, {}, this.computedValues); } gotoNextPage(cancel) { if (this.props.onGotoNextPage) { this.props.onGotoNextPage(cancel); } } handleUpdateDocument(state, dispatch) {} validate(values) { if (this.props.onValidate) { return this.props.onValidate(values); } } calculateOnLoad(values) { if (this.props.onCalculateOnLoad) { return this.props.onCalculateOnLoad(values); } } querySave() { if (this.props.onQuerySave) { return this.props.onQuerySave(); } return true; } postSave(success, error) { if (this.props.onPostSave) { return this.props.onPostSave(); } if (success) { this.gotoNextPage(false); } else { var msg = this.refs.messages; if (msg) { msg.add({ key: 999, title: 'Error while saving document', message: error, type: BaseMessages.ERROR }); } } } postDelete() { if (this.props.onPostDelete) { return this.props.onPostDelete(); } this.gotoNextPage(false); } submit() { // https://github.com/erikras/redux-form/issues/1304 var submitter = this.props.handleSubmit(this.handleUpdateDocument); var r = submitter(); return r; } cancel() { this.gotoNextPage(true); } delete() {} } _defineProperty(BaseReduxForm, "DEFAULT_MODE", 0); _defineProperty(BaseReduxForm, "READONLY", 1); _defineProperty(BaseReduxForm, "DISABLED", 2); _defineProperty(BaseReduxForm, "EDITABLE", 3); _defineProperty(BaseReduxForm, "__forms", {}); _defineProperty(BaseReduxForm, "validateForm", function (values, props) { var c = BaseReduxForm.__forms[props.form]; return c ? c._onValidate(values) : {}; }); _defineProperty(BaseReduxForm, "onChange", function (values, dispatch, props) { var c = BaseReduxForm.__forms[props.form]; if (c) c._onChange(values); }); _defineProperty(BaseReduxForm, "onSubmit", function (state, dispatch, props) { var c = BaseReduxForm.__forms[props.form]; if (c) c.handleUpdateDocument(values, dispatch); }); export default BaseReduxForm; //# sourceMappingURL=BaseReduxForm.js.map