UNPKG

@react-form-builder/core

Version:

React JSON Schema Form Builder to create complex, validated, reusable forms with no deep React knowledge required

132 lines (131 loc) 2.59 kB
import { makeAutoObservable as i } from "mobx"; import { nameObservable as a } from "../../../utils/observableNaming.js"; import { isUndefined as n } from "../../../utils/tools.js"; import { isStoreDataInParentForm as m } from "./util.js"; class u { /** * Creates the nested form field with form data for the component. * @param componentStore the component settings. * @param viewerStore the form viewer settings. */ constructor(e, t) { this.componentStore = e, this.viewerStore = t, i( this, void 0, { name: a("FormField", { key: e.key }), autoBind: !0 } ); } componentStore; viewerStore; /** * @inheritDoc */ valued = ""; /** * @inheritDoc */ touched = !1; /** * @inheritDoc */ get fieldType() { return "simple"; } /** * @inheritDoc */ get storeDataInParentForm() { return m(this.componentStore); } /** * @inheritDoc */ get value() { return this.form.generatedData(); } /** * @inheritDoc */ dispose() { this.viewerStore.dispose(); } /** * @inheritDoc */ clear() { this.form.clear(), this.touched = !1; } /** * @inheritDoc */ reset() { this.form.reset(); } /** * @inheritDoc */ setTouched() { this.touched = !0; } /** * @inheritDoc */ setValue(e) { if (!e) { this.form.reset(); return; } if (typeof e != "object") throw new Error(`Expected 'object' type, got '${typeof e}'`); const t = e; this.form.allComponentFields.forEach(({ field: r, dataKey: o }) => { const s = t[o]; n(s) ? r.reset() : r.setValue(s); }); } /** * @inheritDoc */ async validate() { await this.form.validate(); } /** * @inheritDoc */ async getValidationResult() { return await this.form.getValidationResult(); } /** * @inheritDoc */ init() { this.form.allFields.forEach((e) => e.init()); } /** * @returns the form for the field. */ get form() { return this.viewerStore.form.componentTree; } /** * @inheritDoc */ get errors() { return this.form.errors; } /** * @inheritDoc */ setError = (e) => { if (!e) return; if (typeof e != "object") throw new Error(`Expected 'object' type, got '${typeof e}'`); const t = e; this.form.allComponentFields.forEach(({ field: r, dataKey: o }) => { if (t[o]) return r.setError(t[o]); if (r.storeDataInParentForm) return r.setError(t); }); }; } export { u as TemplateField }; //# sourceMappingURL=TemplateField.js.map