UNPKG

@react-form-builder/core

Version:

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

170 lines (169 loc) 4.15 kB
import { makeAutoObservable as d } from "mobx"; import { KeySymbol as r } from "../consts.js"; import { nameObservable as u } from "../utils/observableNaming.js"; let o = 0; function c(a) { a[r] || (++o, a[r] = `actionData_${o}`); } function l(a) { const t = a.events; t && Object.values(t).forEach((i) => i.forEach(c)), a.children?.forEach(l); } function h(a) { return a?.computeType === "function"; } function m(a) { return a?.computeType === "localization"; } function v(a) { return h(a) || m(a); } function b(a) { return a.dataKey ?? a.key; } class n { /** * The React component key. */ key = ""; /** * The component data key. */ dataKey; /** * The component type of the form viewer. */ type = ""; /** * The component properties. */ props = {}; /** * The component CSS styles. */ css; /** * The component wrapper CSS styles. */ wrapperCss; /** * The component styles for the `style` attribute. */ style; /** * The component wrapper styles for the `style` attribute. */ wrapperStyle; /** * The set of event handlers. */ events; /** * The array of child components. */ children; /** * The component value validation settings. */ schema; /** * The set of arbitrary HTML attributes added to the component. */ htmlAttributes; /** * The tooltip settings. */ tooltipProps; /** * The modal settings. */ modal; /** * The name of the occupied component property in the parent component. */ slot; /** * The condition for binding a child element to a parent element. */ slotCondition; /** * The expression or function to conditionally render a component. */ renderWhen; /** * Disables data binding for the component. */ disableDataBinding; /** * Creates the component settings. * @param key the React component key. * @param type the component type of the form viewer. */ constructor(t, i) { this.key = t, this.type = i, d(this, void 0, { name: u("ComponentStore", { key: t }) }); } /** * Adds a validation rule to the component. * @param store the component settings. * @param key the validation key. * @param type the validation type. */ static addValidationRule(t, i, e = "internal") { if (n.hasValidationRule(t, i)) return; t.schema ??= { validations: [] }; const s = e === "internal" ? { key: i } : { key: i, type: e }; t.schema.validations?.push(s); } /** * Returns true if the component has a validation rule. * @param store the component settings. * @param key the validation key. * @returns true if the component has a validation rule, otherwise false. */ static hasValidationRule(t, i) { return t.schema?.validations?.some((s) => s.key === i); } /** * Removes a validation rule from the component. * @param store the component settings. * @param key the validation key. */ static removeValidationRule(t, i) { t.schema?.validations?.length && (t.schema.validations = t.schema.validations.filter((e) => e.key !== i)); } /** * Correctly creates the {@link ComponentStore} from deserialized data. * @param value the deserialized data. * @returns the component Store. */ static createFromObject(t) { const i = Object.assign(new n(t.key, t.type), t); return l(i), i; } /** * Adds the event handler for component. * @param store the target {@link ComponentStore}. * @param eventName the target event name. * @param data the {@link ActionData}. */ static addEventHandler(t, i, e) { c(e), t.events ??= {}, t.events[i] ??= [], t.events[i].push(e); } /** * Returns a clone of the specified component settings. * @param store the component settings. * @returns the clone of the specified component settings. */ static clone(t) { const i = JSON.parse(JSON.stringify(t)); return n.createFromObject(i); } } export { n as ComponentStore, b as dataKey, v as isComputedProperty, h as isFunctionalProperty, m as isLocalizedProperty }; //# sourceMappingURL=ComponentStore.js.map