UNPKG

@react-form-builder/core

Version:

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

172 lines (171 loc) 4.3 kB
import { makeAutoObservable as d, observable as u, autorun as h } from "mobx"; import { isString as n } from "../../../utils/isString.js"; import { nameObservable as f, nameAutorun as V } from "../../../utils/observableNaming.js"; import { isUndefined as c, isNull as v } from "../../../utils/tools.js"; import { autoConvertField as y } from "./autoConvertField.js"; class T { /** * Creates the field with form data for the component. * @param getProxyValue the function for retrieving data. * @param setProxyValue the function for setting data. * @param createDataValidator the function to create a data validator. * @param componentStore the component settings. * @param model the component metadata for the form viewer. * @param deferFieldCalculation if true, then the calculated field must be explicitly initialized. */ constructor(e, a, l, i, t, s) { if (this.getProxyValue = e, this.setProxyValue = a, this.createDataValidator = l, this.componentStore = i, this.model = t, this.deferFieldCalculation = s, !t.valued) throw new Error("'model.valued' is falsy"); if (!t.valueType) throw new Error("'model.typeOfValue' is undefined"); this.valued = t.valued, this.valueType = t.valueType; const r = "ProxyField"; d(this, { model: !1, dataValidator: u.ref }, { name: f(r, { key: i.key }), autoBind: !0 }), this.#e = [ h( () => { this.dataValidator = this.createDataValidator( this.valueType, (o) => this.error = o ); }, { name: V(r, "setValidator", { key: i.key }) } ) ]; } getProxyValue; setProxyValue; createDataValidator; componentStore; model; deferFieldCalculation; /** * @inheritDoc */ error; /** * @inheritDoc */ touched = !1; /** * @inheritDoc */ valued; /** * The type of the field value. */ valueType; dataValidator; #e; /** * @inheritDoc */ get value() { return this.getProxyValue(); } /** * @inheritDoc */ get fieldType() { return "simple"; } /** * @inheritDoc */ init() { this.deferFieldCalculation = !1; } /** * @returns the initial value for the field. */ get initialValue() { return this.componentStore.props[this.valued]?.value ?? this.defaultValue; } /** * @returns the default value for the field. */ get defaultValue() { return this.model.defaultProps?.[this.valued]; } /** * @inheritDoc */ dispose() { this.#e.forEach((e) => e()); } /** * @inheritDoc */ setValue(e) { const a = y(e, this.valueType); if (this.setProxyValue(a), !this.needValidate) { this.clearError(); return; } this.componentStore.schema?.autoValidate !== !1 && this.dataValidator?.sendValidationEvent?.(this.value); } /** * @inheritDoc */ setTouched() { this.touched = !0; } /** * @inheritDoc */ async validate() { if (!this.needValidate) { this.clearError(); return; } await this.dataValidator?.validate?.(this.value); } /** * @inheritDoc */ async getValidationResult() { if (this.needValidate) return this.dataValidator?.getValidationResult?.(this.value); } /** * @inheritDoc */ reset() { this.setProxyValue(this.initialValue ?? this.defaultValue), this.clearError(); } /** * @inheritDoc */ clear() { this.setProxyValue(this.defaultValue), this.touched = !1, this.clearError(); } /** * @returns true if the field should be validated, false otherwise. */ get needValidate() { return !1; } /** * Clears the error message for this field. */ clearError() { this.error = void 0; } /** * Sets the error value. * @param error the error value to be set. If the error is `undefined` or `null`, the error value will be reset to `undefined`. * @throws {Error} throws an error if the provided value is not a string, undefined, or null. */ setError = (e) => { if (n(e)) this.error = e; else if (c(e) || v(e)) this.error = void 0; else throw new Error(`Expected 'string | undefined | null' type, got '${typeof e}'`); }; } export { T as ProxyField }; //# sourceMappingURL=ProxyField.js.map