mobx-react-form
Version:
Reactive MobX Form State Management
191 lines (182 loc) • 6.22 kB
JavaScript
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory(require("_"));
else if(typeof define === 'function' && define.amd)
define(["_"], factory);
else if(typeof exports === 'object')
exports["MobxReactFormValidatorVJF"] = factory(require("_"));
else
root["MobxReactFormValidatorVJF"] = factory(root["_"]);
})(self, (__WEBPACK_EXTERNAL_MODULE_lodash__) => {
return /******/ (() => { // webpackBootstrap
/******/ "use strict";
/******/ var __webpack_modules__ = ({
/***/ "./src/validators/VJF.ts":
/*!*******************************!*\
!*** ./src/validators/VJF.ts ***!
\*******************************/
/***/ (function(__unused_webpack_module, exports, __webpack_require__) {
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.VJF = void 0;
const lodash_1 = __importDefault(__webpack_require__(/*! lodash */ "lodash"));
function isPromise(obj) {
return (!!obj &&
typeof obj.then === "function" &&
(typeof obj === "object" || typeof obj === "function"));
}
class VJF {
constructor({ config, state = null, promises = [], }) {
Object.defineProperty(this, "promises", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "config", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "state", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "extend", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "validator", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
this.state = state;
this.promises = promises;
this.config = config;
this.extend = config === null || config === void 0 ? void 0 : config.extend;
this.validator = config === null || config === void 0 ? void 0 : config.package;
this.extendValidator();
}
extendValidator() {
if (typeof this.extend === "function") {
this.extend({
validator: this.validator,
form: this.state.form,
});
}
}
validate(field) {
if (!field.validators)
return;
const validators = Array.isArray(field.validators) ? field.validators : [field.validators];
validators.forEach((fn) => this.collectData(fn, field));
this.executeValidation(field);
}
collectData(fn, field) {
const result = this.handleFunctionResult(fn, field);
if (isPromise(result)) {
const $p = result
.then(([valid, message]) => field.setValidationAsyncData(valid, message))
.then(() => this.executeAsyncValidation(field));
this.promises.push($p);
return;
}
field.validationFunctionsData.unshift({
valid: result[0],
message: result[1],
});
}
executeValidation(field) {
field.validationFunctionsData.forEach(({ valid, message }) => {
if (valid === false) {
field.invalidate(message, false);
}
});
}
executeAsyncValidation(field) {
const data = field.validationAsyncData;
if (data.valid === false) {
field.invalidate(data.message, false, true);
}
}
handleFunctionResult(fn, field) {
const result = fn({
validator: this.validator,
form: this.state.form,
field,
});
if (Array.isArray(result)) {
return [result[0] || false, result[1] || "Error"];
}
if (lodash_1.default.isBoolean(result)) {
return [result, "Error"];
}
if (lodash_1.default.isString(result)) {
return [false, result];
}
if (isPromise(result)) {
return result;
}
return [false, "Error"];
}
}
exports.VJF = VJF;
exports["default"] = (config) => ({
class: (VJF),
config,
});
/***/ }),
/***/ "lodash":
/*!********************!*\
!*** external "_" ***!
\********************/
/***/ ((module) => {
module.exports = __WEBPACK_EXTERNAL_MODULE_lodash__;
/***/ })
/******/ });
/************************************************************************/
/******/ // The module cache
/******/ var __webpack_module_cache__ = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ var cachedModule = __webpack_module_cache__[moduleId];
/******/ if (cachedModule !== undefined) {
/******/ return cachedModule.exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = __webpack_module_cache__[moduleId] = {
/******/ // no module.id needed
/******/ // no module.loaded needed
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ __webpack_modules__[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/************************************************************************/
/******/
/******/ // startup
/******/ // Load entry module and return exports
/******/ // This entry module is referenced by other modules so it can't be inlined
/******/ var __webpack_exports__ = __webpack_require__("./src/validators/VJF.ts");
/******/
/******/ return __webpack_exports__;
/******/ })()
;
});
//# sourceMappingURL=MobxReactFormValidatorVJF.umd.js.map