UNPKG

@ea-lab/reactive-json

Version:

A REACT-based lib that transforms JSON (or YAML) into interactive HTML markup.

75 lines (74 loc) 2.66 kB
import { jsx as n, jsxs as h, Fragment as x } from "react/jsx-runtime"; import { useContext as T, useRef as y } from "react"; import { ActionDependant as C } from "../../../engine/Actions.js"; import { GlobalDataContext as j } from "../../../engine/GlobalDataContext.js"; import { normalizeAttributesForReactJsx as k } from "../../../engine/utility/reactJsxHelpers.js"; import { TemplateContext as O } from "../../../engine/TemplateContext.js"; import { evaluateAttributes as V, evaluateTemplateValue as D } from "../../../engine/TemplateSystem.js"; import { View as u } from "../../../engine/View.js"; const J = ({ props: t, currentData: f, datafield: R, path: A }) => { var v; const c = T(j), m = T(O), l = y(null), d = `${t.tag}`, o = t.extra ?? {}, r = k(t.attributes), i = k(f.attributes); for (const e of Object.keys(i)) { if (e.charAt(0) === "+") { const s = e.substring(1); r[s] = (r[s] ?? "").length > 0 ? ( // Append using a space. " " + i[e] ) : ( // Set directly. i[e] ); continue; } r[e] = i[e]; } const a = V({ attrs: r, globalDataContext: c, templateContext: m }); if (t.tag === "option" && t.content && a.value === void 0) { if (typeof t.content != "string") return null; const e = D({ valueToEvaluate: t.content, globalDataContext: c, templateContext: m }); if (typeof e != "string") return null; a.value = e; } (t.tag === "input" || t.tag === "textarea" || t.tag === "select") && (a.value === void 0 && (a.value = ""), t.tag === "input" && (a.type === "checkbox" || a.type === "radio") && a.checked === void 0 && (a.checked = !1)); const { useTransformedAttributes: g } = ((v = c.plugins) == null ? void 0 : v.hook) ?? {}, b = g ? g(a, t.attributeTransforms ?? []) : a; return /* @__PURE__ */ n(C, { ...t, attributesHolderRef: l, children: ((e) => e && [ "area", "base", "br", "col", "embed", "hr", "img", "input", "link", "meta", "param", "source", "track", "wbr" ].indexOf(e) !== -1)(t.tag) ? /* @__PURE__ */ h(x, { children: [ /* @__PURE__ */ n(d, { ref: l, ...b }), Object.keys(o).length ? /* @__PURE__ */ n(u, { props: o }) : "" ] }) : /* @__PURE__ */ h(x, { children: [ /* @__PURE__ */ n(d, { ref: l, ...b, children: t.content && /* @__PURE__ */ n( u, { currentData: f.content ?? void 0, datafield: "content", path: A + ".content", props: t.content } ) }), Object.keys(o).length ? /* @__PURE__ */ n(u, { props: o }) : "" ] }) }); }; export { J as Html };