UNPKG

@ea-lab/reactive-json

Version:

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

40 lines (39 loc) 1.08 kB
import { useContext as f, useEffect as V } from "react"; import { GlobalDataContext as d } from "../../engine/GlobalDataContext.js"; import { TemplateContext as x } from "../../engine/TemplateContext.js"; import { evaluateTemplateValue as b } from "../../engine/TemplateSystem.js"; const T = (a) => { const l = f(d), c = f(x), { attributesHolderRef: t } = a, { name: e, mode: s = "append", value: n, preventDuplicateValues: i = !0, separator: o = " " } = a.actionProps; return V(() => { if (!(t != null && t.current) || !e || n === void 0) return; const r = String( b({ valueToEvaluate: n, globalDataContext: l, templateContext: c }) ), u = t.current; if (s === "replace") u.setAttribute(e, r); else { const m = u.getAttribute(e) || "", p = m ? m.split(o) : []; if (!i || !p.includes(r)) { const v = [...p, r]; u.setAttribute(e, v.join(o)); } } }, [ e, s, n, i, o, l.data, c, t ]), a.children; }; export { T as SetAttributeValue };