UNPKG

@cosva-lab/form-builder

Version:
187 lines (184 loc) 7.92 kB
import { __extends, __awaiter, __decorate, __generator, __assign, __spreadArray } from '../../_virtual/_tslib.js'; import { observable, action, makeObservable, runInAction, toJS } from 'mobx'; import { InputValidator } from '../validate/InputValidator.js'; var FieldBuilder = /** @class */ (function (_super) { __extends(FieldBuilder, _super); function FieldBuilder(props) { var _this = _super.call(this, props) || this; _this._ns = undefined; _this.render = undefined; _this.fullWidth = undefined; _this.grid = undefined; _this.autoComplete = undefined; _this.InputProps = undefined; _this.textFieldProps = undefined; _this.breakpoints = undefined; _this.component = undefined; makeObservable(_this); var ns = props.ns, render = props.render, _a = props.fullWidth, fullWidth = _a === void 0 ? true : _a, _b = props.grid, grid = _b === void 0 ? true : _b, autoComplete = props.autoComplete, InputProps = props.InputProps, textFieldProps = props.textFieldProps, breakpoints = props.breakpoints, component = props.component, renderErrors = props.renderErrors; _this.validate = InputValidator.getValidation(_this); _this.ns = ns; _this.render = render; _this.fullWidth = fullWidth; _this.grid = grid; _this.autoComplete = autoComplete; _this.InputProps = InputProps; _this.textFieldProps = textFieldProps; _this.breakpoints = breakpoints; _this.component = component; _this.renderErrors = renderErrors; _this.getErrors = _this.getErrors.bind(_this); return _this; } Object.defineProperty(FieldBuilder.prototype, "ns", { get: function () { return typeof this._ns === 'undefined' ? this.fieldsBuilder && this.fieldsBuilder.ns : this._ns; }, set: function (ns) { this._ns = ns; }, enumerable: false, configurable: true }); FieldBuilder.prototype.getErrorsBase = function (props) { return __awaiter(this, void 0, void 0, function () { var _a, sequential, _b, validations, value, validate, errors, _i, validations_1, validation, error; return __generator(this, function (_c) { switch (_c.label) { case 0: _a = __assign({}, props).sequential, sequential = _a === void 0 ? false : _a; _b = this, validations = _b.validations, value = _b.value; if (typeof this.validate !== 'function') this._validate = true; validate = this.validate; errors = []; if (!validate && !this.dirty && !this.enabled) return [2 /*return*/, errors]; if (!(Array.isArray(validations) && validate)) return [3 /*break*/, 6]; _i = 0, validations_1 = validations; _c.label = 1; case 1: if (!(_i < validations_1.length)) return [3 /*break*/, 6]; validation = validations_1[_i]; error = void 0; if (!(typeof validation === 'object')) return [3 /*break*/, 2]; if (this.hasValidationError(validation)) error = validation; return [3 /*break*/, 4]; case 2: if (!(typeof validation === 'function')) return [3 /*break*/, 4]; return [4 /*yield*/, validation({ field: this, fieldsBuilder: this.fieldsBuilder, validate: validate, value: value, })]; case 3: error = _c.sent(); _c.label = 4; case 4: if (error) { errors = __spreadArray(__spreadArray([], errors, true), [error], false); if (sequential) return [3 /*break*/, 6]; } _c.label = 5; case 5: _i++; return [3 /*break*/, 1]; case 6: return [2 /*return*/, errors.length ? errors : undefined]; } }); }); }; FieldBuilder.prototype.getErrors = function () { return this.getErrorsBase(); }; FieldBuilder.prototype.hasErrors = function () { return __awaiter(this, void 0, void 0, function () { var errors; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.getErrorsBase({ sequential: true })]; case 1: errors = _a.sent(); return [2 /*return*/, !!(errors && errors.length)]; } }); }); }; FieldBuilder.prototype.hasValid = function () { return __awaiter(this, void 0, void 0, function () { var hasErrors; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.hasErrors()]; case 1: hasErrors = _a.sent(); return [2 /*return*/, !hasErrors]; } }); }); }; FieldBuilder.prototype.setValue = function (value) { return __awaiter(this, void 0, void 0, function () { var _this = this; return __generator(this, function (_a) { runInAction(function () { _this.value = value; }); this.markAsDirty(); this.markAsTouched(); if (typeof this.validate !== 'undefined' ? this.validate : this.dirty) { runInAction(function () { _this.updateValueAndValidity(); }); } this.onSetValue && this.onSetValue({ lastValue: toJS(this.value), newValue: value, field: this, }); return [2 /*return*/]; }); }); }; __decorate([ observable ], FieldBuilder.prototype, "_ns", void 0); __decorate([ observable ], FieldBuilder.prototype, "render", void 0); __decorate([ observable ], FieldBuilder.prototype, "fullWidth", void 0); __decorate([ observable ], FieldBuilder.prototype, "grid", void 0); __decorate([ observable ], FieldBuilder.prototype, "autoComplete", void 0); __decorate([ observable ], FieldBuilder.prototype, "InputProps", void 0); __decorate([ observable ], FieldBuilder.prototype, "textFieldProps", void 0); __decorate([ observable ], FieldBuilder.prototype, "breakpoints", void 0); __decorate([ observable ], FieldBuilder.prototype, "component", void 0); __decorate([ action ], FieldBuilder.prototype, "setValue", null); return FieldBuilder; }(InputValidator)); export { FieldBuilder, FieldBuilder as default }; //# sourceMappingURL=FieldBuilder.js.map