UNPKG

@react-form-builder/core

Version:

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

63 lines (62 loc) 2.14 kB
import { jsx as s } from "react/jsx-runtime"; import { useMemo as p, useRef as l, useLayoutEffect as u, useEffect as m } from "react"; import { useStore as a } from "../../../utils/contexts/StoreContext.js"; import { isPromise as d } from "../../../utils/isPromise.js"; import { namedObserver as g } from "../../../utils/namedObserver.js"; import { globalDefaultLanguage as w } from "../../localization/default.js"; import { ComponentTree as v } from "../../ui/ComponentTree.js"; import { buildInternalErrorStore as h } from "../../ui/internalErrorModel.js"; import { useViewerProps as c } from "./ViewerPropsContext.js"; const E = () => { const r = globalThis.innerWidth; return r <= 600 ? "mobile" : r <= 900 ? "tablet" : "desktop"; }; function b() { const r = a(), e = c(); m(() => { if (e.viewMode) { r.viewMode = e.viewMode; return; } const o = () => r.viewMode = E(); return globalThis.addEventListener("resize", o), () => globalThis.removeEventListener("resize", o); }, [e.viewMode, r]); } const i = (r, e) => { r.form.componentTree.errors = e ?? {}; }, y = (r, e) => { console.error(e), r.formLoadError = e?.message ?? e; const o = h(e); r.applyPersistedForm({ form: o, localization: {}, defaultLanguage: w.fullCode, languages: [] }); }, L = async (r, e, o, n) => { if (e) try { const t = e(o, n); if (d(t)) { const f = await t; r.applyStringForm(f); return; } r.applyStringForm(t); } catch (t) { y(r, t); } }, M = () => { const r = a(), { formLoadError: e } = r, o = c(), n = p(() => [r.form.componentTree], [r.form.componentTree]), t = l(o.errors); return u(() => { t.current = o.errors, i(r, o.errors); }, [o.errors, r]), b(), m(() => { L(r, o.getForm, o.formName, o.formOptions).then(() => { i(r, t.current); }).catch(console.error); }, [r, o.getForm, o.formName, o.formOptions]), e ? /* @__PURE__ */ s("div", { className: "form-error", children: e }) : /* @__PURE__ */ s(v, { data: n }); }, x = g("Viewer", M); export { x as Viewer }; //# sourceMappingURL=Viewer.js.map