react-formawesome-core
Version:
React validation form core lib
955 lines (736 loc) • 38.6 kB
JavaScript
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
else if(typeof define === 'function' && define.amd)
define([], factory);
else if(typeof exports === 'object')
exports["react-formawesome-core"] = factory();
else
root["react-formawesome-core"] = factory();
})(global, function() {
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
/******/ }
/******/ };
/******/
/******/ // define __esModule on exports
/******/ __webpack_require__.r = function(exports) {
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ }
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/ };
/******/
/******/ // create a fake namespace object
/******/ // mode & 1: value is a module id, require it
/******/ // mode & 2: merge all properties of value into the ns
/******/ // mode & 4: return value when already ns object
/******/ // mode & 8|1: behave like require
/******/ __webpack_require__.t = function(value, mode) {
/******/ if(mode & 1) value = __webpack_require__(value);
/******/ if(mode & 8) return value;
/******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
/******/ var ns = Object.create(null);
/******/ __webpack_require__.r(ns);
/******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
/******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
/******/ return ns;
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "/";
/******/
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 0);
/******/ })
/************************************************************************/
/******/ ({
/***/ "./src/index.ts":
/***/ (function(module, __webpack_exports__, __webpack_require__) {
;
__webpack_require__.r(__webpack_exports__);
// EXTERNAL MODULE: external "class-validator"
var external_class_validator_ = __webpack_require__("class-validator");
// CONCATENATED MODULE: ./src/Validators/utils/checkers.ts
const invariant = __webpack_require__("invariant");
function checkForDefaults(defaults, attributes) {
invariant(typeof defaults === "object", "'defaults' argument should be an object");
invariant(Object.keys(defaults).length, "Defaults does not have any values." + "Remove 'defaults' argument instead passing empty object");
Object.keys(defaults).forEach(key => invariant(attributes.includes(key), `Attribute '${key}' passed with defaults does not exist in model/schema`));
}
function checkForModel(Model) {
invariant(typeof Model === "function", "You should pass valid model class");
}
function checkForSchema(schema) {
invariant(typeof schema === "object" && typeof schema.name === "string" && typeof schema.properties === "object", "You should pass valid schema object");
}
function checkForInstance(instance) {
invariant(Object.keys(instance).length, "Model/schema does not have any attributes. Did you forget set the values?");
}
function checkForAttribute(modelAttributes, attribute, modelName, config = {}) {
if (config.skipAttributeCheck) {
return modelAttributes.includes(attribute);
}
invariant(modelAttributes.includes(attribute), `Attribute '${attribute}' does not exist in model/schema '${modelName}'`);
return true;
}
function checkForConfig(config) {
invariant(typeof config === "object", "You should pass valid config object");
}
function checkForValue(attribute, value) {
invariant(typeof value !== "function" && typeof value !== "object", `Value '${value}' can't be setted to attribute ${attribute}`);
}
function checkForDetails(details) {
invariant(typeof details === "string", "'details' value should be a string");
}
// CONCATENATED MODULE: ./src/Validators/AbstractValidator.ts
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
;
let AbstractValidator_AbstractValidator =
/*#__PURE__*/
function () {
function AbstractValidator() {
_classCallCheck(this, AbstractValidator);
this.modelErrorsContainer = new Map();
this.modelContainer = {};
this.config = {};
this.setModelValue = (attribute, value) => {
checkForValue(attribute, value);
if (checkForAttribute(this.modelAttributes, attribute, this.modelName, this.config)) {
this.modelContainer.instance[attribute] = value;
}
};
this.setDefaults = defaults => {
checkForDefaults(defaults, this.modelAttributes);
Object.keys(defaults).forEach(key => this.modelContainer.defaults[key] = defaults[key]);
};
this.dropToDefaults = () => {
this.modelAttributes.forEach(attribute => {
if (this.modelContainer.defaults[attribute] !== undefined) {
this.modelContainer.instance[attribute] = this.modelContainer.defaults[attribute];
}
});
this.modelErrorsContainer.clear();
};
this.clear = () => {
this.modelAttributes.forEach(attribute => {
this.modelContainer.instance[attribute] = undefined;
});
this.modelErrorsContainer.clear();
};
this.addErrors = errors => {
errors.forEach(({
attribute,
details
}) => {
if (!this.config.skipAttributeCheck) {
checkForAttribute(this.modelAttributes, attribute, this.modelName);
}
checkForDetails(details);
this.modelErrorsContainer.set(attribute, [{
attribute,
details
}]);
});
};
this.handleErrors = (errors, groups) => {
if (!groups) {
this.modelErrorsContainer.clear();
return this.addErrors(this.convertVendorErrors(errors));
}
groups.forEach(group => this.modelErrorsContainer.set(group, this.convertVendorErrors(errors)));
};
this.protectContainer = () => {
Object.freeze(this.modelContainer);
Object.seal(this.modelContainer.instance);
};
this.convertVendorErrors = errors => errors.map(({
property,
constraints
}) => ({
attribute: property,
details: constraints[Object.keys(constraints)[0]]
}));
}
_createClass(AbstractValidator, [{
key: "modelAttributes",
get: function () {
return Object.keys(this.modelContainer.instance);
}
}, {
key: "modelValues",
get: function () {
const values = {};
this.modelAttributes.forEach(attribute => {
if (this.modelContainer.instance[attribute] !== undefined) {
values[attribute] = this.modelContainer.instance[attribute];
}
});
return values;
}
}, {
key: "modelErrors",
get: function () {
const errors = {};
Array.from(this.modelErrorsContainer.keys()).forEach(group => this.modelErrorsContainer.get(group).forEach(({
attribute,
details
}) => {
errors[attribute] = details;
}));
return errors;
}
}]);
return AbstractValidator;
}();
// CONCATENATED MODULE: ./src/Validators/ModelValidator/ModelValidator.ts
function ModelValidator_classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function ModelValidator_defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function ModelValidator_createClass(Constructor, protoProps, staticProps) { if (protoProps) ModelValidator_defineProperties(Constructor.prototype, protoProps); if (staticProps) ModelValidator_defineProperties(Constructor, staticProps); return Constructor; }
function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
let ModelValidator_ModelValidator =
/*#__PURE__*/
function (_AbstractValidator) {
_inherits(ModelValidator, _AbstractValidator);
function ModelValidator(Model, defaults, config) {
var _this;
ModelValidator_classCallCheck(this, ModelValidator);
_this = _possibleConstructorReturn(this, _getPrototypeOf(ModelValidator).call(this));
_this.validate = groups => {
return external_class_validator_["validate"](_this.modelContainer.instance, Object.assign({}, groups ? {
groups
} : {}, {
skipMissingProperties: true,
validationError: {
target: false,
value: false
}
})).then(errors => _this.handleErrors(errors, groups));
};
_this.instantiateModel = (Model, defaults) => {
checkForModel(Model);
const instance = new Model();
checkForInstance(instance);
defaults !== undefined && checkForDefaults(defaults, Object.keys(instance));
_this.modelContainer = {
instance,
defaults: defaults || {}
};
_this.protectContainer();
defaults && _this.dropToDefaults();
};
if (config) {
checkForConfig(config);
_this.config = config;
Object.seal(_this.config);
}
_this.instantiateModel(Model, defaults);
return _this;
}
ModelValidator_createClass(ModelValidator, [{
key: "modelName",
get: function () {
return this.modelContainer.instance.constructor.name;
}
}]);
return ModelValidator;
}(AbstractValidator_AbstractValidator);
// CONCATENATED MODULE: ./src/Validators/ModelValidator/index.ts
// CONCATENATED MODULE: ./src/Validators/SchemaValidator/SchemaValidator.ts
function SchemaValidator_classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function SchemaValidator_defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function SchemaValidator_createClass(Constructor, protoProps, staticProps) { if (protoProps) SchemaValidator_defineProperties(Constructor.prototype, protoProps); if (staticProps) SchemaValidator_defineProperties(Constructor, staticProps); return Constructor; }
function SchemaValidator_possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } return SchemaValidator_assertThisInitialized(self); }
function SchemaValidator_assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function SchemaValidator_getPrototypeOf(o) { SchemaValidator_getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return SchemaValidator_getPrototypeOf(o); }
function SchemaValidator_inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) SchemaValidator_setPrototypeOf(subClass, superClass); }
function SchemaValidator_setPrototypeOf(o, p) { SchemaValidator_setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return SchemaValidator_setPrototypeOf(o, p); }
let SchemaValidator_SchemaValidator =
/*#__PURE__*/
function (_AbstractValidator) {
SchemaValidator_inherits(SchemaValidator, _AbstractValidator);
function SchemaValidator(schema, defaults, config) {
var _this;
SchemaValidator_classCallCheck(this, SchemaValidator);
_this = SchemaValidator_possibleConstructorReturn(this, SchemaValidator_getPrototypeOf(SchemaValidator).call(this));
_this.validate = groups => {
return external_class_validator_["validate"](_this.modelName, _this.modelContainer.instance, Object.assign({}, groups ? {
groups
} : {}, {
skipMissingProperties: true,
validationError: {
target: false,
value: false
}
})).then(errors => _this.handleErrors(errors, groups));
};
_this.instantiateModel = (schema, defaults) => {
checkForSchema(schema);
defaults !== undefined && checkForDefaults(defaults, Object.keys(schema.properties));
external_class_validator_["registerSchema"](schema);
_this.schemaName = schema.name;
_this.modelContainer = {
instance: {},
defaults: defaults || {}
};
Object.keys(schema.properties).forEach(attribute => {
_this.modelContainer.instance[attribute] = undefined;
});
_this.protectContainer();
defaults && _this.dropToDefaults();
};
if (config) {
checkForConfig(config);
_this.config = config;
Object.seal(_this.config);
}
_this.instantiateModel(schema, defaults);
return _this;
}
SchemaValidator_createClass(SchemaValidator, [{
key: "modelName",
get: function () {
return this.schemaName;
}
}]);
return SchemaValidator;
}(AbstractValidator_AbstractValidator);
// CONCATENATED MODULE: ./src/Validators/SchemaValidator/index.ts
// CONCATENATED MODULE: ./src/Validators/index.ts
// EXTERNAL MODULE: external "prop-types"
var external_prop_types_ = __webpack_require__("prop-types");
// CONCATENATED MODULE: ./src/Components/FormProvider/FormProviderProps.ts
const ValidatorPropTypes = {
modelAttributes: external_prop_types_["arrayOf"](external_prop_types_["string"].isRequired).isRequired,
modelValues: external_prop_types_["object"].isRequired,
modelErrors: external_prop_types_["object"].isRequired,
modelName: external_prop_types_["string"].isRequired,
dropToDefaults: external_prop_types_["func"].isRequired,
setModelValue: external_prop_types_["func"].isRequired,
setDefaults: external_prop_types_["func"].isRequired,
addErrors: external_prop_types_["func"].isRequired,
validate: external_prop_types_["func"].isRequired
};
const FormProviderPropTypes = {
validator: external_prop_types_["shape"](ValidatorPropTypes).isRequired,
onSubmit: external_prop_types_["func"].isRequired,
errorParser: external_prop_types_["func"],
handleUnparsedErrors: external_prop_types_["bool"],
beforeSubmit: external_prop_types_["func"],
afterSubmit: external_prop_types_["func"]
};
// EXTERNAL MODULE: external "react"
var external_react_ = __webpack_require__("react");
// CONCATENATED MODULE: ./src/Components/FormProvider/FormContext.ts
const FormContext = external_react_["createContext"]({
modelErrors: {},
modelValues: {},
loading: false,
hasErrors: false,
registeredElements: {}
});
// CONCATENATED MODULE: ./src/Components/FormProvider/FormProvider.tsx
function FormProvider_classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function FormProvider_defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function FormProvider_createClass(Constructor, protoProps, staticProps) { if (protoProps) FormProvider_defineProperties(Constructor.prototype, protoProps); if (staticProps) FormProvider_defineProperties(Constructor, staticProps); return Constructor; }
function FormProvider_possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } return FormProvider_assertThisInitialized(self); }
function FormProvider_assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function FormProvider_getPrototypeOf(o) { FormProvider_getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return FormProvider_getPrototypeOf(o); }
function FormProvider_inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) FormProvider_setPrototypeOf(subClass, superClass); }
function FormProvider_setPrototypeOf(o, p) { FormProvider_setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return FormProvider_setPrototypeOf(o, p); }
let FormProvider_FormProvider =
/*#__PURE__*/
function (_React$Component) {
FormProvider_inherits(FormProvider, _React$Component);
function FormProvider() {
var _this;
FormProvider_classCallCheck(this, FormProvider);
_this = FormProvider_possibleConstructorReturn(this, FormProvider_getPrototypeOf(FormProvider).apply(this, arguments));
_this.state = {
loading: false
};
_this.registeredElements = {};
_this.registerElement = (attribute, element) => {
if (!element || typeof element.focus !== "function") {
return false;
}
if (!!_this.registeredElements[attribute]) {
return false;
}
checkForAttribute(_this.props.validator.modelAttributes, attribute, _this.props.validator.modelName);
_this.registeredElements[attribute] = element;
_this.forceUpdate();
return true;
};
_this.unregisterElement = attribute => {
delete _this.registeredElements[attribute];
_this.forceUpdate();
};
_this.handleSubmit = async () => {
_this.setState({
loading: true,
unparsedError: undefined
}, _this.getBeforeSubmitHandler());
await _this.handleValidate();
if (_this.hasErrors) {
_this.focusOnError();
return _this.setState({
loading: false
}, _this.getAfterSubmitHandler(true));
}
let response;
try {
response = await _this.props.onSubmit(_this.props.validator.modelValues);
} catch (error) {
_this.setState({
loading: false
}, _this.getAfterSubmitHandler(true));
if (_this.props.errorParser) {
_this.tryToParseError(error);
} else {
throw error;
}
return;
}
if (response && response.cancelUpdate) {
return;
}
_this.setState({
loading: false
}, _this.getAfterSubmitHandler(false));
};
_this.handleValidate = async groups => {
await _this.props.validator.validate(groups);
_this.forceUpdate();
};
_this.handleSetModelValue = (attribute, value) => {
_this.props.validator.setModelValue(attribute, value);
_this.forceUpdate();
};
_this.focusOnError = () => {
const element = _this.registeredElements[Object.keys(_this.props.validator.modelErrors)[0]];
element && element.focus();
};
_this.tryToParseError = error => {
const parsedErrors = _this.props.errorParser(error);
if (Array.isArray(parsedErrors)) {
_this.props.validator.addErrors(parsedErrors);
_this.hasErrors && _this.focusOnError();
_this.forceUpdate();
return;
}
;
if (_this.props.handleUnparsedErrors) {
return _this.setState({
unparsedError: parsedErrors
});
} else {
throw error;
}
};
_this.getBeforeSubmitHandler = () => {
return _this.props.beforeSubmit ? _this.props.beforeSubmit : undefined;
};
_this.getAfterSubmitHandler = hasError => {
return _this.props.afterSubmit ? () => _this.props.afterSubmit(hasError) : undefined;
};
return _this;
}
FormProvider_createClass(FormProvider, [{
key: "render",
value: function render() {
return external_react_["createElement"](FormContext.Provider, {
value: this.childContext
}, this.props.children);
}
}, {
key: "childContext",
get: function () {
return {
onSubmit: this.handleSubmit,
onValidate: this.handleValidate,
setModelValue: this.handleSetModelValue,
registerElement: this.registerElement,
unregisterElement: this.unregisterElement,
hasErrors: this.hasErrors,
loading: this.state.loading,
unparsedError: this.state.unparsedError,
registeredElements: this.registeredElements,
modelErrors: this.props.validator.modelErrors,
modelValues: this.props.validator.modelValues
};
}
}, {
key: "hasErrors",
get: function () {
return !!Object.keys(this.props.validator.modelErrors).length;
}
}]);
return FormProvider;
}(external_react_["Component"]);
FormProvider_FormProvider.propTypes = FormProviderPropTypes;
// CONCATENATED MODULE: ./src/Components/FormProvider/index.ts
// CONCATENATED MODULE: ./src/Components/FormGroupProvider/FormGroupProviderProps.ts
var Event;
(function (Event) {
Event["change"] = "change";
Event["focus"] = "focus";
Event["blur"] = "blur";
})(Event || (Event = {}));
const FormGroupProviderPropTypes = {
attribute: external_prop_types_["string"].isRequired,
validateOn: external_prop_types_["oneOfType"]([external_prop_types_["oneOf"](Object.keys(Event).map(key => Event[key])), external_prop_types_["func"], external_prop_types_["arrayOf"](external_prop_types_["oneOfType"]([external_prop_types_["oneOf"](Object.keys(Event).map(key => Event[key])), external_prop_types_["func"]]))])
};
// CONCATENATED MODULE: ./src/Components/FormGroupProvider/FormGroupContext.ts
;
const FormGroupContext = external_react_["createContext"]({
isFocused: false,
value: ""
});
// CONCATENATED MODULE: ./src/Components/FormGroupProvider/FormGroupProvider.tsx
function FormGroupProvider_classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function FormGroupProvider_defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function FormGroupProvider_createClass(Constructor, protoProps, staticProps) { if (protoProps) FormGroupProvider_defineProperties(Constructor.prototype, protoProps); if (staticProps) FormGroupProvider_defineProperties(Constructor, staticProps); return Constructor; }
function FormGroupProvider_possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } return FormGroupProvider_assertThisInitialized(self); }
function FormGroupProvider_assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function FormGroupProvider_getPrototypeOf(o) { FormGroupProvider_getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return FormGroupProvider_getPrototypeOf(o); }
function FormGroupProvider_inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) FormGroupProvider_setPrototypeOf(subClass, superClass); }
function FormGroupProvider_setPrototypeOf(o, p) { FormGroupProvider_setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return FormGroupProvider_setPrototypeOf(o, p); }
let FormGroupProvider_FormGroupProvider =
/*#__PURE__*/
function (_React$PureComponent) {
FormGroupProvider_inherits(FormGroupProvider, _React$PureComponent);
function FormGroupProvider() {
var _this;
FormGroupProvider_classCallCheck(this, FormGroupProvider);
_this = FormGroupProvider_possibleConstructorReturn(this, FormGroupProvider_getPrototypeOf(FormGroupProvider).apply(this, arguments));
_this.state = {
isFocused: false
};
_this.renderChildren = formContext => {
return external_react_["createElement"](FormGroupContext.Provider, {
value: _this.createChildContext(formContext)
}, _this.props.children);
};
_this.createChildContext = formContext => {
return {
unregisterElement: _this.handleUnergisterElement(formContext),
registerElement: _this.handleRegisterElement(formContext),
onChange: _this.handleChange(formContext),
onFocus: _this.handleFocus(formContext),
onBlur: _this.handleBlur(formContext),
isFocused: _this.state.isFocused,
error: formContext.modelErrors[_this.props.attribute],
value: formContext.modelValues[_this.props.attribute]
};
};
_this.handleRegisterElement = formContext => element => {
formContext.registerElement(_this.props.attribute, element);
};
_this.handleUnergisterElement = formContext => () => {
formContext.unregisterElement(_this.props.attribute);
};
_this.handleChange = formContext => value => {
formContext.setModelValue(_this.props.attribute, value);
const manualUpdatedValues = Object.assign({}, formContext.modelValues, {
[_this.props.attribute]: value
});
if (_this.shouldValidateOnChange) {
return formContext.onValidate([_this.props.attribute]);
}
if (_this.shouldValidateOnMethod(manualUpdatedValues, formContext.modelErrors)) {
return formContext.onValidate([_this.props.attribute]);
}
};
_this.handleBlur = formContext => () => {
_this.setState({
isFocused: false
});
if (_this.shouldValidateOnBlur) {
return formContext.onValidate([_this.props.attribute]);
}
};
_this.handleFocus = formContext => () => {
_this.setState({
isFocused: true
});
if (_this.shouldValidateOnFocus) {
return formContext.onValidate([_this.props.attribute]);
}
};
_this.shouldValidateOnMethod = (values, errors) => {
if (Array.isArray(_this.props.validateOn)) {
const conditionMethod = _this.props.validateOn.find(event => typeof event === "function");
return conditionMethod && conditionMethod(values, errors);
}
return typeof _this.props.validateOn === "function" && _this.props.validateOn(values, errors);
};
return _this;
}
FormGroupProvider_createClass(FormGroupProvider, [{
key: "render",
value: function render() {
return external_react_["createElement"](FormContext.Consumer, null, this.renderChildren);
}
}, {
key: "shouldValidateOnBlur",
get: function () {
if (Array.isArray(this.props.validateOn) && this.props.validateOn.includes(Event.blur)) {
return true;
}
return this.props.validateOn === Event.blur;
}
}, {
key: "shouldValidateOnFocus",
get: function () {
if (Array.isArray(this.props.validateOn) && this.props.validateOn.includes(Event.focus)) {
return true;
}
return this.props.validateOn === Event.focus;
}
}, {
key: "shouldValidateOnChange",
get: function () {
if (Array.isArray(this.props.validateOn) && this.props.validateOn.includes(Event.change)) {
return true;
}
return this.props.validateOn === Event.change;
}
}]);
return FormGroupProvider;
}(external_react_["PureComponent"]);
FormGroupProvider_FormGroupProvider.propTypes = FormGroupProviderPropTypes;
// CONCATENATED MODULE: ./src/Components/FormGroupProvider/index.ts
// CONCATENATED MODULE: ./src/Components/UnparsedErrorProvider/UnparsedErrorProviderProps.ts
const UnparsedErrorProviderPropTypes = {
children: external_prop_types_["func"].isRequired
};
// CONCATENATED MODULE: ./src/Components/UnparsedErrorProvider/UnparsedErrorProvider.tsx
function UnparsedErrorProvider_classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function UnparsedErrorProvider_defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function UnparsedErrorProvider_createClass(Constructor, protoProps, staticProps) { if (protoProps) UnparsedErrorProvider_defineProperties(Constructor.prototype, protoProps); if (staticProps) UnparsedErrorProvider_defineProperties(Constructor, staticProps); return Constructor; }
function UnparsedErrorProvider_possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } return UnparsedErrorProvider_assertThisInitialized(self); }
function UnparsedErrorProvider_assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function UnparsedErrorProvider_getPrototypeOf(o) { UnparsedErrorProvider_getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return UnparsedErrorProvider_getPrototypeOf(o); }
function UnparsedErrorProvider_inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) UnparsedErrorProvider_setPrototypeOf(subClass, superClass); }
function UnparsedErrorProvider_setPrototypeOf(o, p) { UnparsedErrorProvider_setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return UnparsedErrorProvider_setPrototypeOf(o, p); }
let UnparsedErrorProvider_UnparsedErrorProvider =
/*#__PURE__*/
function (_React$Component) {
UnparsedErrorProvider_inherits(UnparsedErrorProvider, _React$Component);
function UnparsedErrorProvider() {
var _this;
UnparsedErrorProvider_classCallCheck(this, UnparsedErrorProvider);
_this = UnparsedErrorProvider_possibleConstructorReturn(this, UnparsedErrorProvider_getPrototypeOf(UnparsedErrorProvider).apply(this, arguments));
_this.renderChildren = context => {
if (!context.unparsedError) {
return null;
}
return _this.props.children(context.unparsedError);
};
return _this;
}
UnparsedErrorProvider_createClass(UnparsedErrorProvider, [{
key: "render",
value: function render() {
return external_react_["createElement"](FormContext.Consumer, null, this.renderChildren);
}
}]);
return UnparsedErrorProvider;
}(external_react_["Component"]);
UnparsedErrorProvider_UnparsedErrorProvider.propTypes = UnparsedErrorProviderPropTypes;
// CONCATENATED MODULE: ./src/Components/UnparsedErrorProvider/index.ts
// CONCATENATED MODULE: ./src/Components/index.ts
// CONCATENATED MODULE: ./src/index.ts
/* concated harmony reexport AbstractValidator */__webpack_require__.d(__webpack_exports__, "AbstractValidator", function() { return AbstractValidator_AbstractValidator; });
/* concated harmony reexport ModelValidator */__webpack_require__.d(__webpack_exports__, "ModelValidator", function() { return ModelValidator_ModelValidator; });
/* concated harmony reexport SchemaValidator */__webpack_require__.d(__webpack_exports__, "SchemaValidator", function() { return SchemaValidator_SchemaValidator; });
/* concated harmony reexport ValidatorPropTypes */__webpack_require__.d(__webpack_exports__, "ValidatorPropTypes", function() { return ValidatorPropTypes; });
/* concated harmony reexport FormProviderPropTypes */__webpack_require__.d(__webpack_exports__, "FormProviderPropTypes", function() { return FormProviderPropTypes; });
/* concated harmony reexport FormProvider */__webpack_require__.d(__webpack_exports__, "FormProvider", function() { return FormProvider_FormProvider; });
/* concated harmony reexport FormContext */__webpack_require__.d(__webpack_exports__, "FormContext", function() { return FormContext; });
/* concated harmony reexport Event */__webpack_require__.d(__webpack_exports__, "Event", function() { return Event; });
/* concated harmony reexport FormGroupProviderPropTypes */__webpack_require__.d(__webpack_exports__, "FormGroupProviderPropTypes", function() { return FormGroupProviderPropTypes; });
/* concated harmony reexport FormGroupProvider */__webpack_require__.d(__webpack_exports__, "FormGroupProvider", function() { return FormGroupProvider_FormGroupProvider; });
/* concated harmony reexport FormGroupContext */__webpack_require__.d(__webpack_exports__, "FormGroupContext", function() { return FormGroupContext; });
/* concated harmony reexport UnparsedErrorProvider */__webpack_require__.d(__webpack_exports__, "UnparsedErrorProvider", function() { return UnparsedErrorProvider_UnparsedErrorProvider; });
/* concated harmony reexport UnparsedErrorProviderPropTypes */__webpack_require__.d(__webpack_exports__, "UnparsedErrorProviderPropTypes", function() { return UnparsedErrorProviderPropTypes; });
/***/ }),
/***/ 0:
/***/ (function(module, exports, __webpack_require__) {
module.exports = __webpack_require__("./src/index.ts");
/***/ }),
/***/ "class-validator":
/***/ (function(module, exports) {
module.exports = require("class-validator");
/***/ }),
/***/ "invariant":
/***/ (function(module, exports) {
module.exports = require("invariant");
/***/ }),
/***/ "prop-types":
/***/ (function(module, exports) {
module.exports = require("prop-types");
/***/ }),
/***/ "react":
/***/ (function(module, exports) {
module.exports = require("react");
/***/ })
/******/ });
});