UNPKG

@ea-lab/reactive-json

Version:

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

65 lines (64 loc) 2.6 kB
import { jsx as o, jsxs as g, Fragment as O } from "react/jsx-runtime"; import { useContext as x, useRef as P, useMemo as R } from "react"; import { ActionDependant as W } from "../../../engine/Actions.js"; import { useEvaluatedAttributes as u, evaluateTemplateValue as V } from "../../../engine/TemplateSystem.js"; import { GlobalDataContext as w } from "../../../engine/GlobalDataContext.js"; import { TemplateContext as H } from "../../../engine/TemplateContext.js"; import { propsDataLocationToPathAndValue as M } from "../../../engine/utility/formElementsCommon.js"; import { View as B } from "../../../engine/View.js"; const K = ({ props: t, datafield: C, path: c, currentData: r }) => { const i = x(w), s = x(H), f = P(null), d = u(t.attributes), E = u(t.inputAttributes ?? []), L = u(t.labelAttributes ?? []), { formData: T, formDataPath: y } = M({ currentPath: c, datafield: C, dataLocation: t.dataLocation, defaultValue: t.defaultFieldValue, globalDataContext: i, templateContext: s }), D = (n) => { let e = n.currentTarget.value; e === "" && !t.allowEmptyStringAsValue ? e = void 0 : e === "true" ? e = !0 : e === "false" ? e = !1 : e === "null" && (e = null), i.updateData(e, y); }; let l; t.dynamicOptions ? l = V({ valueToEvaluate: t.dynamicOptions, globalDataContext: i, templateContext: s }) : t.options ? l = t.options : l = [], Array.isArray(l) || (l = []); const m = !!t.label, b = t.forceWrapper, v = b === !0 || b !== !1 && m, a = { onChange: D, value: T ?? "", ...E }; v || Object.assign(a, d); const h = R(() => a.id ? a.id : `select-${Math.random().toString(36).substring(2, 9)}`, [a.id]); a.id = h; const F = { htmlFor: h, ...L }, A = /* @__PURE__ */ o("select", { ref: f, ...a, children: l.length > 0 ? l.map((n, e) => { const S = n.value ?? "", j = V({ valueToEvaluate: n.label, globalDataContext: i, templateContext: s }) ?? ""; return /* @__PURE__ */ o("option", { value: S, children: j }, e); }) : /* @__PURE__ */ o("option", { value: "", children: "No options available" }) }), p = m && /* @__PURE__ */ o("label", { ...F, children: /* @__PURE__ */ o( B, { currentData: (r == null ? void 0 : r.label) ?? void 0, datafield: "label", path: c + ".label", props: t.label } ) }); return /* @__PURE__ */ o(W, { ...t, attributesHolderRef: f, children: v ? /* @__PURE__ */ g("div", { ...d, children: [ p, A ] }) : /* @__PURE__ */ g(O, { children: [ p, A ] }) }); }; export { K as SelectField };