@cosva-lab/form-builder
Version:
React form builder.
130 lines (128 loc) • 4.46 kB
JavaScript
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: 'Module' } });
const require_runtime = require('../../_virtual/_rolldown/runtime.cjs');
const require_enums = require('../../enums.cjs');
const require_decorate = require('../../_virtual/_@oxc-project_runtime@0.112.0/helpers/decorate.cjs');
let mobx = require("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 === require_enums.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 === require_enums.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 == require_enums.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 = require_enums.StatusField.VALID;
}
_setInitialStatus() {
this.status = require_enums.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 = require_enums.StatusField.VALID;
this.defaultInputValue = defaultInputValue;
this.label = label;
this.onChange = onChange;
this.onSetValue = onSetValue;
(0, mobx.makeObservable)(this);
}
};
require_decorate.__decorate([mobx.observable], BaseField.prototype, "type", void 0);
require_decorate.__decorate([mobx.observable], BaseField.prototype, "name", void 0);
require_decorate.__decorate([mobx.observable], BaseField.prototype, "value", void 0);
require_decorate.__decorate([mobx.observable], BaseField.prototype, "defaultInputValue", void 0);
require_decorate.__decorate([mobx.observable], BaseField.prototype, "label", void 0);
require_decorate.__decorate([mobx.observable], BaseField.prototype, "status", void 0);
require_decorate.__decorate([mobx.observable], BaseField.prototype, "disabled", void 0);
require_decorate.__decorate([mobx.observable], BaseField.prototype, "errors", void 0);
require_decorate.__decorate([mobx.observable], BaseField.prototype, "onChange", void 0);
require_decorate.__decorate([mobx.observable], BaseField.prototype, "onSetValue", void 0);
require_decorate.__decorate([mobx.action.bound], BaseField.prototype, "disable", null);
require_decorate.__decorate([mobx.action.bound], BaseField.prototype, "enable", null);
require_decorate.__decorate([mobx.action.bound], BaseField.prototype, "_setInitialStatus", null);
//#endregion
exports.BaseField = BaseField;
exports.default = BaseField;