@ea-lab/reactive-json
Version:
A REACT-based lib that transforms JSON (or YAML) into interactive HTML markup.
77 lines (76 loc) • 2.41 kB
JavaScript
import { jsx as l } from "react/jsx-runtime";
import { useContext as y } from "react";
import { GlobalDataContext as D } from "./GlobalDataContext.js";
import { TemplateContext as C } from "./TemplateContext.js";
import L, { evaluateTemplateValue as x, dataLocationToPath as P } from "./TemplateSystem.js";
function u({ props: t, currentData: o, datafield: c, path: n }) {
const i = y(D), v = y(C), f = i.plugins ?? {}, d = (f == null ? void 0 : f.element) ?? {};
if (!d)
return null;
const { element: T } = i;
if (o === void 0 && (o = ""), t != null && t.type) {
let e = d[t.type] ?? void 0;
const a = d.Html;
return e === void 0 && (e = a), e ? (a === e && (t.tag = t.tag ?? t.type), /* @__PURE__ */ l(e, { path: n, props: t, currentData: o, datafield: c })) : (console.warn(`No component found for type "${t.type}" and no Html fallback available.`), null);
}
if (t != null && t.load) {
let e;
const a = (t == null ? void 0 : t.customDataLocation) ?? void 0, m = a ? (
// The data is located somewhere in the current data.
x({
globalDataContext: i,
templateContext: v,
valueToEvaluate: a
})
) : (
// The data is the current data.
o
), r = a ? P({
dataLocation: a,
currentPath: n,
globalDataContext: i,
templateContext: v
}) : n;
typeof t.load == "function" ? e = t.load(m) : e = T[t.load];
const { load: h, customDataLocation: g, ...b } = t;
return e = { ...e, ...b }, t.keepTemplateContext ? /* @__PURE__ */ l(
u,
{
currentData: m,
datafield: c,
path: r,
props: e
}
) : /* @__PURE__ */ l(C.Provider, { value: { templateData: m, templatePath: r }, children: /* @__PURE__ */ l(
u,
{
currentData: m,
datafield: c,
path: r,
props: e
}
) });
}
return Array.isArray(t) ? t.map((e, a) => /* @__PURE__ */ l(
u,
{
currentData: o[a] ?? void 0,
datafield: a,
path: n + "." + a,
props: e ?? void 0
},
n + "." + a
)) : typeof t == "object" ? Object.entries(t).map(([e, a]) => /* @__PURE__ */ l(
u,
{
currentData: o[e] ?? void 0,
datafield: e ?? void 0,
path: n + "." + e,
props: a
},
n + "." + e
)) : /* @__PURE__ */ l(L, { valueToEvaluate: o || (t ?? null) });
}
export {
u as View
};