UNPKG

@cosva-lab/form-builder

Version:
227 lines (222 loc) 7.66 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var _tslib = require('../../_virtual/_tslib.js'); var mobx = require('mobx'); var enums = require('../../enums.js'); var Field = /** @class */ (function () { function Field(_a) { var type = _a.type, name = _a.name, value = _a.value, disabled = _a.disabled, defaultInputValue = _a.defaultInputValue, label = _a.label, onChange = _a.onChange, onSetValue = _a.onSetValue; this.fieldsBuilder = undefined; this.type = undefined; this.defaultInputValue = undefined; this.errors = []; this.onChange = undefined; this.onSetValue = undefined; this.pristine = true; this.type = type; this.name = name; this.value = value; if (disabled) this.disable(); else this.status = enums.StatusField.VALID; this.defaultInputValue = defaultInputValue; this.label = label; this.onChange = onChange; this.onSetValue = onSetValue; mobx.makeObservable(this); } Object.defineProperty(Field.prototype, "dirty", { /** * A control is `dirty` if the user has changed the value * in the UI. * * @returns True if the user has changed the value of this control in the UI; compare `pristine`. * Programmatic changes to a control's value do not mark it dirty. */ get: function () { return !this.pristine; }, enumerable: false, configurable: true }); Object.defineProperty(Field.prototype, "valid", { /** * A control is `valid` when its `status` is `VALID`. * * @see {@link Field.status} * * @returns True if the control has passed all of its validation tests, * false otherwise. */ get: function () { return this.status === enums.StatusField.VALID; }, enumerable: false, configurable: true }); Object.defineProperty(Field.prototype, "invalid", { /** * A control is `invalid` when its `status` is `INVALID`. * * @see {@link Field.status} * * @returns True if this control has failed one or more of its validation checks, * false otherwise. */ get: function () { return this.status === enums.StatusField.INVALID; }, enumerable: false, configurable: true }); Object.defineProperty(Field.prototype, "pending", { /** * A control is `pending` when its `status` is `PENDING`. * * @see {@link Field.status} * * @returns True if this control is in the process of conducting a validation check, * false otherwise. */ get: function () { return this.status == enums.StatusField.PENDING; }, enumerable: false, configurable: true }); Object.defineProperty(Field.prototype, "disabled", { /** * A control is `disabled` when its `status` is `DISABLED`. * * Disabled controls are exempt from validation checks and * are not included in the aggregate value of their ancestor * controls. * * @see {@link Field.status} * * @returns True if the control is disabled, false otherwise. */ get: function () { return this.status === enums.StatusField.DISABLED; }, enumerable: false, configurable: true }); Object.defineProperty(Field.prototype, "enabled", { /** * A control is `enabled` as long as its `status` is not `DISABLED`. * * @returns True if the control has any status other than 'DISABLED', * false if the status is 'DISABLED'. * * @see {@link Field.status} * */ get: function () { return this.status !== enums.StatusField.DISABLED; }, enumerable: false, configurable: true }); /** * Disables the control. This means the control is exempt from validation checks and * excluded from the aggregate value of any parent. Its status is `DISABLED`. * * If the control has children, all children are also disabled. * * @see {@link Field.status} */ Field.prototype.disable = function () { // If parent has been marked artificially dirty we don't want to re-calculate the // parent's dirtiness based on the children. this.status = enums.StatusField.DISABLED; this.errors = this.errors = []; }; /** * Enables the control. This means the control is included in validation checks and * the aggregate value of its parent. Its status recalculates based on its value and * its validators. * * By default, if the control has children, all children are enabled. * * @see {@link Field.status} */ Field.prototype.enable = function () { // If parent has been marked artificially dirty we don't want to re-calculate the // parent's dirtiness based on the children. this.status = enums.StatusField.VALID; }; /** * Marks the control as `dirty`. A control becomes dirty when * the control's value is changed through the UI; compare `markAsTouched`. * * @see `markAsTouched()` * @see `markAsUntouched()` * @see `markAsPristine()` * */ Field.prototype.markAsDirty = function () { this.pristine = false; }; /** * Marks the control as `pristine`. * * If the control has any children, marks all children as `pristine`, * and recalculates the `pristine` status of all parent * controls. * * @see `markAsTouched()` * @see `markAsUntouched()` * @see `markAsDirty()` * */ Field.prototype.markAsPristine = function () { this.pristine = true; }; Field.prototype._setInitialStatus = function () { this.status = this.disabled ? enums.StatusField.DISABLED : enums.StatusField.VALID; }; _tslib.__decorate([ mobx.observable ], Field.prototype, "type", void 0); _tslib.__decorate([ mobx.observable ], Field.prototype, "name", void 0); _tslib.__decorate([ mobx.observable ], Field.prototype, "value", void 0); _tslib.__decorate([ mobx.observable ], Field.prototype, "defaultInputValue", void 0); _tslib.__decorate([ mobx.observable ], Field.prototype, "label", void 0); _tslib.__decorate([ mobx.observable ], Field.prototype, "status", void 0); _tslib.__decorate([ mobx.observable ], Field.prototype, "errors", void 0); _tslib.__decorate([ mobx.observable ], Field.prototype, "onChange", void 0); _tslib.__decorate([ mobx.observable ], Field.prototype, "onSetValue", void 0); _tslib.__decorate([ mobx.action ], Field.prototype, "disable", null); _tslib.__decorate([ mobx.action ], Field.prototype, "enable", null); _tslib.__decorate([ mobx.action ], Field.prototype, "_setInitialStatus", null); return Field; }()); exports.Field = Field; exports.default = Field; //# sourceMappingURL=Field.js.map