@ea-lab/reactive-json
Version:
A REACT-based lib that transforms JSON (or YAML) into interactive HTML markup.
61 lines (60 loc) • 1.67 kB
JavaScript
import { useContext as p, useEffect as g } from "react";
import { GlobalDataContext as E } from "../../engine/GlobalDataContext.js";
import { TemplateContext as N } from "../../engine/TemplateContext.js";
import { evaluateTemplateValue as A } from "../../engine/TemplateSystem.js";
const I = (a) => {
const l = p(E), s = p(N), { attributesHolderRef: r } = a, { name: n, value: i, separator: c = " ", unsetAllOccurrences: C, unsetCount: f } = a.actionProps || {};
return g(() => {
if (!(r != null && r.current) || !n || i === void 0)
return;
const o = String(
A({
valueToEvaluate: i,
globalDataContext: l,
templateContext: s
})
), m = f !== void 0 ? A({
valueToEvaluate: f,
globalDataContext: l,
templateContext: s
}) : void 0, v = r.current, d = v.getAttribute(n) || "";
if (!d)
return;
let t = [...d.split(c)];
(() => {
if (C === !0) {
t = t.filter((e) => e !== o);
return;
}
const u = parseInt(m, 10);
if (!(!isNaN(u) && m !== void 0 && m !== null)) {
t = t.filter((e) => e !== o);
return;
}
if (u === 0)
return;
if (u > 0) {
let e = 0;
t = t.filter((T) => T === o && e < u ? (e++, !1) : !0);
return;
}
const x = Math.abs(u);
let b = 0;
t = t.reverse().filter((e) => e === o && b < x ? (b++, !1) : !0).reverse();
})();
const V = t.join(c);
V.trim() === "" ? v.removeAttribute(n) : v.setAttribute(n, V);
}, [
n,
i,
c,
C,
f,
l.data,
s,
r
]), a.children;
};
export {
I as UnsetAttributeValue
};