@cosva-lab/form-builder
Version:
React form builder.
92 lines (90 loc) • 3.25 kB
JavaScript
import { __decorate } from "../../_virtual/_@oxc-project_runtime@0.112.0/helpers/decorate.mjs";
import { InputValidator } from "../validate/InputValidator.mjs";
import { action, makeObservable, observable, runInAction, toJS } from "mobx";
//#region src/utils/builders/FieldBuilder.ts
var FieldBuilder = class extends InputValidator {
get ns() {
return typeof this._ns === "undefined" ? this.fieldsBuilder && this.fieldsBuilder.ns : this._ns;
}
set ns(ns) {
this._ns = ns;
}
constructor(props) {
super(props);
this._ns = void 0;
this.render = void 0;
this.fullWidth = void 0;
this.autoComplete = void 0;
this.InputProps = void 0;
this.textFieldProps = void 0;
this.component = void 0;
this.getErrors = () => {
return this.getErrorsBase();
};
this.hasErrors = async () => {
const errors = await this.getErrorsBase({ sequential: true });
return !!(errors && errors.length);
};
this.hasValid = async () => {
return !await this.hasErrors();
};
makeObservable(this);
const { ns, render, fullWidth = true, autoComplete, InputProps, textFieldProps, component } = props;
this.validate = InputValidator.getValidation(this);
this.ns = ns;
this.render = render;
this.fullWidth = fullWidth;
this.autoComplete = autoComplete;
this.InputProps = InputProps;
this.textFieldProps = textFieldProps;
this.component = component;
}
async getErrorsBase(props) {
const { sequential = false } = { ...props };
const { validations, value } = this;
if (typeof this.validate !== "function") this._validate = true;
const validate = this.validate;
let errors = void 0;
if (!validate && !this.dirty && !this.enabled) return errors;
if (Array.isArray(validations) && validate) for (const validation of validations) {
let error;
if (typeof validation === "object") error = validation;
else if (typeof validation === "function") error = await validation({
field: this,
validate,
value
});
if (error) {
errors = [...errors || [], error];
if (sequential) break;
}
}
return (errors === null || errors === void 0 ? void 0 : errors.length) ? errors : void 0;
}
async setValue(value) {
var _this$onSetValue;
runInAction(() => {
this.value = value;
});
this.markAsDirty();
this.markAsTouched();
if (typeof this.validate !== "undefined" ? this.validate : this.dirty) runInAction(() => {
this.updateValueAndValidity();
});
(_this$onSetValue = this.onSetValue) === null || _this$onSetValue === void 0 || _this$onSetValue.call(this, {
lastValue: toJS(this.value),
newValue: value,
field: this
});
}
};
__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, "autoComplete", void 0);
__decorate([observable], FieldBuilder.prototype, "InputProps", void 0);
__decorate([observable], FieldBuilder.prototype, "textFieldProps", void 0);
__decorate([observable], FieldBuilder.prototype, "component", void 0);
__decorate([action.bound], FieldBuilder.prototype, "setValue", null);
//#endregion
export { FieldBuilder, FieldBuilder as default };