doiforms
Version:
A generalized tool for building formatted forms on multiple platforms
154 lines • 7.35 kB
JavaScript
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var React = require("react");
var FormReducer_1 = require("./components/FormReducer");
var ButtonFields_1 = require("./Utilities/ButtonFields");
var CompareFields_1 = require("./Utilities/CompareFields");
var FormSerialize_1 = require("./Utilities/FormSerialize");
var InitializeState_1 = require("./Utilities/InitializeState");
var JSONSerialize_1 = require("./Utilities/JSONSerialize");
// Enumerations
var EnumFieldStatus;
(function (EnumFieldStatus) {
EnumFieldStatus[EnumFieldStatus["Initial"] = 0] = "Initial";
EnumFieldStatus[EnumFieldStatus["Modified"] = 1] = "Modified";
EnumFieldStatus[EnumFieldStatus["Error"] = 2] = "Error";
EnumFieldStatus[EnumFieldStatus["Empty"] = 3] = "Empty";
})(EnumFieldStatus || (EnumFieldStatus = {}));
exports.EnumFieldStatus = EnumFieldStatus;
var EnumFieldType;
(function (EnumFieldType) {
EnumFieldType[EnumFieldType["color"] = 0] = "color";
EnumFieldType[EnumFieldType["checkbox"] = 1] = "checkbox";
EnumFieldType[EnumFieldType["custom"] = 2] = "custom";
EnumFieldType[EnumFieldType["date"] = 3] = "date";
EnumFieldType[EnumFieldType["datetime"] = 4] = "datetime";
EnumFieldType[EnumFieldType["datetime-local"] = 5] = "datetime-local";
EnumFieldType[EnumFieldType["email"] = 6] = "email";
EnumFieldType[EnumFieldType["file"] = 7] = "file";
EnumFieldType[EnumFieldType["month"] = 8] = "month";
EnumFieldType[EnumFieldType["number"] = 9] = "number";
EnumFieldType[EnumFieldType["password"] = 10] = "password";
EnumFieldType[EnumFieldType["radio"] = 11] = "radio";
EnumFieldType[EnumFieldType["range"] = 12] = "range";
EnumFieldType[EnumFieldType["reset"] = 13] = "reset";
EnumFieldType[EnumFieldType["search"] = 14] = "search";
EnumFieldType[EnumFieldType["select"] = 15] = "select";
EnumFieldType[EnumFieldType["submit"] = 16] = "submit";
EnumFieldType[EnumFieldType["tel"] = 17] = "tel";
EnumFieldType[EnumFieldType["text"] = 18] = "text";
EnumFieldType[EnumFieldType["textarea"] = 19] = "textarea";
EnumFieldType[EnumFieldType["time"] = 20] = "time";
EnumFieldType[EnumFieldType["url"] = 21] = "url";
EnumFieldType[EnumFieldType["week"] = 22] = "week";
})(EnumFieldType || (EnumFieldType = {}));
exports.EnumFieldType = EnumFieldType;
var EnumFormAction;
(function (EnumFormAction) {
EnumFormAction[EnumFormAction["NoOp"] = 0] = "NoOp";
EnumFormAction[EnumFormAction["Submit"] = 1] = "Submit";
EnumFormAction[EnumFormAction["Reset"] = 2] = "Reset";
EnumFormAction[EnumFormAction["Update"] = 3] = "Update";
EnumFormAction[EnumFormAction["HandleError"] = 4] = "HandleError";
})(EnumFormAction || (EnumFormAction = {}));
exports.EnumFormAction = EnumFormAction;
var DOIFormComponent = /** @class */ (function (_super) {
__extends(DOIFormComponent, _super);
function DOIFormComponent(props) {
var _this = _super.call(this, props) || this;
_this.state = InitializeState_1.InitializeState(props.Fields);
_this.handleResetFunction = _this.HandleReset.bind(_this);
_this.handleSubmitFunction = _this.HandleSubmit.bind(_this);
_this.sendMessageFunction = _this.SendMessage.bind(_this);
return _this;
}
DOIFormComponent.prototype.componentDidCatch = function (Error, ErrorInfo) {
this.FormLogic({
Action: EnumFormAction.HandleError,
Update: {
ErrorMessage: Error.message,
ErrorStack: ErrorInfo.componentStack,
HasError: true,
},
});
};
DOIFormComponent.prototype.render = function () {
var shouldRedirect = this.state.Submitted && !!this.props.RedirectURL;
if (shouldRedirect) {
window.location.assign(this.props.RedirectURL);
}
var shouldShowSubmitPage = this.state.Submitted && !!this.props.SubmitPage;
return !shouldShowSubmitPage ? (React.createElement("div", { className: "container-fluid" },
this.RenderForm(),
this.state.ErrorMessage ? this.RenderError() : null)) : (this.props.SubmitPage);
};
DOIFormComponent.prototype.HandleSubmit = function (Event) {
var _this = this;
var fieldStates = this.state.FieldStates || [];
this.props
.SubmitAction(fieldStates, function () { return FormSerialize_1.default(fieldStates); }, function () { return JSONSerialize_1.default(fieldStates); })
.then(function () { return _this.FormLogic({ Action: EnumFormAction.Submit }); })
.catch(function (reason) {
return _this.FormLogic({
Action: EnumFormAction.HandleError,
Update: {
ErrorMessage: reason.toString(),
HasError: true,
},
});
});
Event.preventDefault();
};
DOIFormComponent.prototype.HandleReset = function () {
if (this.props.ResetAction) {
this.props.ResetAction();
}
this.FormLogic({ Action: EnumFormAction.Reset });
};
DOIFormComponent.prototype.SendMessage = function (action) {
this.FormLogic(action);
};
DOIFormComponent.prototype.Styles = function () {
return this.props.CustomCSS ? this.props.CustomCSS : {};
};
DOIFormComponent.prototype.FormLogic = function (action) {
this.setState((this.props.CustomFormLogic || FormReducer_1.FormReducer)(this.state, action));
};
DOIFormComponent.prototype.RenderError = function () {
return this.props.ErrorHandler(this.state.ErrorMessage, this.state.ErrorStack);
};
DOIFormComponent.prototype.RenderForm = function () {
return this.props.FormTemplate({
Buttons: ButtonFields_1.ButtonFields({
CompareFields: CompareFields_1.CompareFieldsFunction,
Fields: this.props.Fields,
}),
ErrorHandler: this.props.ErrorHandler,
FieldStates: this.state.FieldStates || [],
Footer: this.props.Footer,
HandleReset: this.handleResetFunction,
HandleSubmit: this.handleSubmitFunction,
Header: this.props.Header,
IsError: this.state.HasError,
SendMessage: this.sendMessageFunction,
Styles: this.Styles(),
SystemErrorMessage: this.state.ErrorMessage,
});
};
return DOIFormComponent;
}(React.Component));
exports.DOIFormComponent = DOIFormComponent;
//# sourceMappingURL=index.js.map