UNPKG

@cosva-lab/form-builder

Version:
127 lines (125 loc) 3.9 kB
import { StatusField } from "../../enums.mjs"; import { __decorate } from "../../_virtual/_@oxc-project_runtime@0.112.0/helpers/decorate.mjs"; import { action, makeObservable, observable } from "mobx"; //#region src/utils/builders/BaseField.ts var BaseField = class { /** * 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 dirty() { return !this.pristine; } /** * A control is `valid` when its `status` is `VALID`. * * @see {@link StatusField} * * @returns True if the control has passed all of its validation tests, * false otherwise. */ get valid() { return this.status === StatusField.VALID; } /** * A control is `invalid` when its `status` is `INVALID`. * * * @returns True if this control has failed one or more of its validation checks, * false otherwise. */ get invalid() { return this.status === StatusField.INVALID; } /** * A control is `pending` when its `status` is `PENDING`. * * * @returns True if this control is in the process of conducting a validation check, * false otherwise. */ get pending() { return this.status == StatusField.PENDING; } /** * 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'. * * */ get enabled() { return !this.disabled; } /** * 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. * */ disable() { this.disabled = true; this.errors = void 0; } /** * 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. * */ enable() { this.disabled = false; this.status = StatusField.VALID; } _setInitialStatus() { this.status = StatusField.VALID; } constructor({ type, name, value, disabled, defaultInputValue, label, onChange, onSetValue }) { this.fieldsBuilder = void 0; this.type = void 0; this.defaultInputValue = void 0; this.disabled = false; this.errors = void 0; this.onChange = void 0; this.onSetValue = void 0; this.pristine = true; this.markAsDirty = () => { this.pristine = false; }; this.markAsPristine = () => { this.pristine = true; }; this.type = type; this.name = name; this.value = value; if (disabled) this.disable(); else this.status = StatusField.VALID; this.defaultInputValue = defaultInputValue; this.label = label; this.onChange = onChange; this.onSetValue = onSetValue; makeObservable(this); } }; __decorate([observable], BaseField.prototype, "type", void 0); __decorate([observable], BaseField.prototype, "name", void 0); __decorate([observable], BaseField.prototype, "value", void 0); __decorate([observable], BaseField.prototype, "defaultInputValue", void 0); __decorate([observable], BaseField.prototype, "label", void 0); __decorate([observable], BaseField.prototype, "status", void 0); __decorate([observable], BaseField.prototype, "disabled", void 0); __decorate([observable], BaseField.prototype, "errors", void 0); __decorate([observable], BaseField.prototype, "onChange", void 0); __decorate([observable], BaseField.prototype, "onSetValue", void 0); __decorate([action.bound], BaseField.prototype, "disable", null); __decorate([action.bound], BaseField.prototype, "enable", null); __decorate([action.bound], BaseField.prototype, "_setInitialStatus", null); //#endregion export { BaseField, BaseField as default };