{"dependencies":[{"name":"react","loc":{"line":30,"column":20}},{"name":"./inflateTree","loc":{"line":31,"column":28}},{"name":"./clone","loc":{"line":32,"column":22}},{"name":"./validation","loc":{"line":33,"column":27}},{"name":"./debounce","loc":{"line":34,"column":25}}],"generated":{"js":"\"use strict\";\nvar __extends = (this && this.__extends) || (function () {\n    var extendStatics = Object.setPrototypeOf ||\n        ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||\n        function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };\n    return function (d, b) {\n        extendStatics(d, b);\n        function __() { this.constructor = d; }\n        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());\n    };\n})();\nvar __assign = (this && this.__assign) || Object.assign || function(t) {\n    for (var s, i = 1, n = arguments.length; i < n; i++) {\n        s = arguments[i];\n        for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))\n            t[p] = s[p];\n    }\n    return t;\n};\nvar __rest = (this && this.__rest) || function (s, e) {\n    var t = {};\n    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)\n        t[p] = s[p];\n    if (s != null && typeof Object.getOwnPropertySymbols === \"function\")\n        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0)\n            t[p[i]] = s[p[i]];\n    return t;\n};\nObject.defineProperty(exports, \"__esModule\", { value: true });\nvar React = require(\"react\");\nvar inflateTree_1 = require(\"./inflateTree\");\nvar clone_1 = require(\"./clone\");\nvar validation_1 = require(\"./validation\");\nvar debounce_1 = require(\"./debounce\");\nexports.defaultFieldErrorsToProps = function (fieldErrors, props) { return ({\n    className: (fieldErrors.length ? 'error' : '') + \" \" + props.className,\n}); };\nexports.defaultConfigureInput = {\n    eventName: 'onChange',\n    getValueFromEvent: function (e) { return e.target.value; },\n    defaultProp: 'defaultValue',\n    valueProp: 'value',\n};\nvar defaultConfigureCheckbox = {\n    eventName: 'onChange',\n    getValueFromEvent: function (e) { return e.target.checked; },\n    defaultProp: 'defaultChecked',\n    valueProp: 'checked',\n};\nvar defaultConfigureUpload = {\n    eventName: 'onChange',\n    getValueFromEvent: function (e) { return e.target.files; },\n    defaultProp: 'defaultValue',\n    valueProp: 'value',\n};\nvar _Form = /** @class */ (function (_super) {\n    __extends(_Form, _super);\n    function _Form() {\n        var _this = _super !== null && _super.apply(this, arguments) || this;\n        _this.state = { errors: [] };\n        _this.dirtyNodes = [];\n        _this.tree = [];\n        _this.clear = function () {\n            _this.tree = [];\n            _this.dirtyNodes = [];\n            _this.setState({ errors: [] });\n            if (_this.props.onChange)\n                _this.props.onChange({});\n        };\n        _this.clearFieldErrors = function () {\n            _this.dirtyNodes = [];\n            _this.tree = _this.tree.map(function (node) { return (__assign({}, node, { fieldErrors: [] })); });\n            _this.setState({ errors: [] });\n        };\n        _this.showFieldErrors = function () {\n            _this.serialize().validation.then(function (_a) {\n                var validatedTree = _a.validatedTree, errors = _a.errors;\n                _this.tree = validatedTree;\n                _this.setState({ errors: errors });\n            });\n        };\n        // Only really ment for the outside world if they are doing some complex form stuff\n        _this.serialize = function () {\n            var paths = _this.tree.map(function (node) { return node.path.join('.'); });\n            var fieldValues = inflateTree_1.default('value', _this.tree);\n            var validation = validation_1.validate(_this.tree, fieldValues, 'serialize', paths);\n            return { fieldValues: fieldValues, validation: validation };\n        };\n        _this.validate = debounce_1.default(function (fieldValues, eventType, cb) {\n            var paths = eventType === 'onChange' && _this.props.showErrorsOnChange === 'field'\n                ? _this.dirtyNodes\n                : _this.tree.map(function (node) { return node.path.join('.'); });\n            _this.dirtyNodes = [];\n            return validation_1.validate(_this.tree, fieldValues, eventType, paths).then(function (_a) {\n                var validatedTree = _a.validatedTree, errors = _a.errors, fieldErrors = _a.fieldErrors, valid = _a.valid;\n                _this.tree = validatedTree;\n                cb({ errors: errors, fieldErrors: fieldErrors, valid: valid });\n            });\n        }, _this.props.debounceValidation);\n        _this.onChange = function (path, value) {\n            _this.dirtyNodes.push(path.join('.'));\n            _this.tree = _this.tree.map(function (node) {\n                return node.path === path ? __assign({}, node, { value: value }) : node;\n            });\n            // No callback and we are not showing errors, no need to do any work\n            if (!_this.props.onChange && !_this.props.showErrorsOnChange)\n                return;\n            // In both cases, we need to inflate the current form object\n            var fieldValues = inflateTree_1.default('value', _this.tree);\n            // Trigger on change immediately as to not block any potential dependencies on this data\n            if (_this.props.onChange)\n                _this.props.onChange(fieldValues);\n            _this.validate(fieldValues, 'onChange', function (validation) {\n                if (_this.props.showErrorsOnChange)\n                    _this.setState({ errors: validation.errors });\n                // After we are done validating, if the onChange asked for validation, give it to them\n                // We have to call onChange twice since debounceing means we won't always have validation\n                if (_this.props.onChange && _this.props.onChange.length !== 1) {\n                    _this.props.onChange(inflateTree_1.default('value', _this.tree), validation);\n                }\n            });\n        };\n        _this.onSubmit = function (e) {\n            e.preventDefault();\n            // No one is expecting work, no need to validate\n            if (!_this.props.onSubmit && !_this.props.showErrorsOnSubmit)\n                return;\n            var fieldValues = inflateTree_1.default('value', _this.tree);\n            // Clear old validations\n            _this.validate.cancel();\n            _this.validate(fieldValues, 'onSubmit', function (validation) {\n                if (_this.props.showErrorsOnSubmit)\n                    _this.setState({ errors: validation.errors });\n                if (_this.props.onSubmit && (validation.valid || _this.props.noValidate))\n                    _this.props.onSubmit(fieldValues, validation);\n            });\n            // Immediately execute submit validation\n            _this.validate.flush();\n        };\n        return _this;\n    }\n    _Form.prototype.render = function () {\n        var _a = this.props, removePropName = _a.removePropName, removeValidators = _a.removeValidators, onChange = _a.onChange, onSubmit = _a.onSubmit, showErrorsOnChange = _a.showErrorsOnChange, fieldErrorsToProps = _a.fieldErrorsToProps, showErrorsOnSubmit = _a.showErrorsOnSubmit, debounceValidation = _a.debounceValidation, propName = _a.propName, configureForm = _a.configureForm, props = __rest(_a, [\"removePropName\", \"removeValidators\", \"onChange\", \"onSubmit\", \"showErrorsOnChange\", \"fieldErrorsToProps\", \"showErrorsOnSubmit\", \"debounceValidation\", \"propName\", \"configureForm\"]);\n        var _b = clone_1.default({\n            children: this.props.children,\n            path: [],\n            tree: [],\n            nodeIndexCount: {},\n            removeValidators: removeValidators,\n            removePropName: removePropName,\n            propName: propName,\n            fieldErrorsToProps: fieldErrorsToProps,\n            configureForm: configureForm,\n            onChange: this.onChange,\n            previousRenderTree: this.tree || [],\n            errors: this.state.errors,\n        }), children = _b.children, tree = _b.tree;\n        this.tree = tree;\n        return (React.createElement(\"form\", __assign({}, props, { onSubmit: this.onSubmit, onChange: function () { }, onReset: this.clear, noValidate: true }), children));\n    };\n    _Form.defaultProps = {\n        propName: 'name',\n        showErrorsOnSubmit: true,\n        debounceValidation: 0,\n        removePropName: false,\n        fieldErrorsToProps: exports.defaultFieldErrorsToProps,\n        removeValidators: true,\n        noValidate: false,\n        configureForm: function (type, props) {\n            return type === 'input' && (props.type === 'radio' || props.type === 'checkbox')\n                ? defaultConfigureCheckbox\n                : type === 'input' && props.type === 'file'\n                    ? defaultConfigureUpload\n                    : exports.defaultConfigureInput;\n        },\n    };\n    return _Form;\n}(React.Component));\nexports.Form = _Form;\nexports.default = _Form;\n//# sourceMappingURL=Form.js.map"},"hash":"0329dfed6b0b332571b55488b57a19bf"}