UNPKG

scrivito-pisasales-questionnaire-builder

Version:
231 lines (230 loc) 6.58 kB
import { jsx as m, jsxs as y } from "react/jsx-runtime"; import { useState as b, useEffect as h } from "react"; import { isInPlaceEditingActive as P, ContentTag as H } from "scrivito"; import { u as G, a as U, b as B, M as Q, H as R } from "./useValidationField-BrRv6UCn.js"; import { u as z, Q as W } from "./FormContext-CyyAThXO.js"; import { y as j, z as D, E as q, M as X, B as Y, T as I, H as k, D as J, a as K, G as Z, Q as w, A as tt, V as et, j as C } from "./scrivitoConfig-WST4rsmc.js"; import { i as v } from "./isPisaDate-k2iL6dCC.js"; const A = (t, e) => { if (!t) return ""; const n = t.substring(0, 4), s = t.substring(4, 6), r = t.substring(6, 8); if (e === "date") return `${n}-${s}-${r}`; const c = t.substring(8, 10) || "12", o = t.substring(10, 12) || "00"; return `${n}-${s}-${r}T${c}:${o}`; }, T = (t, e) => { if (!t) return ""; const n = new Date(t); if (isNaN(n.getTime())) return ""; if (e === "date") { const f = new Date(n.getTime()); return f.setUTCHours(12, 0, 0, 0), f.toISOString().split("T")[0]; } const s = (f) => String(f).padStart(2, "0"), r = n.getFullYear(), c = s(n.getMonth() + 1), o = s(n.getDate()), i = s(n.getHours()), l = s(n.getMinutes()); return `${r}-${c}-${o}T${i}:${l}`; }, M = (t) => { const e = t.startsWith("-"), n = t.replace(/-/g, ""); return e ? "-" + n : n; }, L = (t) => { let e = t.replace(/[^\d-]/g, ""); return e = M(e), e; }, S = (t) => { let e = t.replace(/[^0-9,.\-]/g, ""); e = M(e); const n = e.search(/[.,]/); if (n !== -1) { const s = e.slice(0, n + 1), r = e.slice(n + 1).replace(/[.,]/g, ""); e = s + r; } return /^-[.,]$/.test(e) && (e = "-"), e; }, nt = (t, e) => { if (!t) return ""; let n = t; return e === j && (n = n.replace(",", ".")), n.endsWith(".") && (n = n.slice(0, -1)), n; }, st = ({ id: t, externalId: e, placeholder: n, value: s, defaultValue: r, isInvalid: c, onInputChange: o }) => { const [i, l] = b(""); return h(() => { if (s == r) { v(s) ? l(A(s, "date")) : l(T(s, "date")); return; } const g = s ? s.slice(0, 10) : ""; l(g); }, [s]), /* @__PURE__ */ m( "input", { className: `form-control ${c ? "is-invalid" : ""}`, id: t, name: e, placeholder: n, value: i, onChange: (g) => { const d = g.target.value; if (l(d), !d) { o([]); return; } const u = new Date(d); u.setUTCHours(12, 0, 0, 0); const a = u.toISOString(); o([a]); }, type: "date" } ); }, at = ({ id: t, externalId: e, placeholder: n, value: s, defaultValue: r, isInvalid: c, onInputChange: o }) => { const [i, l] = b(""); return h(() => { if (s == r) { l(v(s) ? A(s, "datetime") : T(s, "datetime")); return; } l(T(s, "datetime")); }, [s]), /* @__PURE__ */ m( "input", { className: `form-control ${c ? "is-invalid" : ""}`, id: t, name: e, placeholder: n, value: i, onChange: (g) => { const d = g.target.value; if (l(d), !d) { o([]); return; } const u = new Date(d); if (isNaN(u.getTime())) { o([]); return; } o([u.toISOString()], [e]); }, type: "datetime-local" } ); }, _ = ({ id: t, externalId: e, placeholder: n, value: s, isInvalid: r, type: c, onInputChange: o }) => { const i = c === D; return /* @__PURE__ */ m( "input", { className: `form-control ${r ? "is-invalid" : ""}`, id: t, name: e, placeholder: n, value: s, onChange: (u) => { const a = u.target.value, p = i ? L(a) : S(a), N = nt(p, c); o([N], [e]); }, onBlur: (u) => { let a = u.target.value; if (!a) { o([""], [e]); return; } if (c === D) { if (a = L(a), a === "-") { o([""], [e]); return; } o([a], [e]); } else { if (a = S(a).replace(",", "."), (a === "-" || a.endsWith(".")) && (a = a.replace(/\.$/, "")), !a || a === "-") { o([""], [e]); return; } o([a], [e]); } }, type: "number", inputMode: i ? "numeric" : "decimal", pattern: i ? "-?[0-9]*" : "-?[0-9]*[.,]?[0-9]*" } ); }, ot = ({ id: t, externalId: e, placeholder: n, value: s, isInvalid: r, onInputChange: c }) => /* @__PURE__ */ m( "textarea", { className: `form-control ${r ? "is-invalid" : ""}`, id: t, name: e, placeholder: n, value: s, onChange: (i) => { c([i.target.value]); }, rows: 3 } ), V = ({ id: t, externalId: e, placeholder: n, value: s, isInvalid: r, onInputChange: c }) => /* @__PURE__ */ m( "input", { className: `form-control ${r ? "is-invalid" : ""}`, id: t, name: e, maxLength: 2e3, placeholder: n, value: s, onChange: (i) => { c([i.target.value]); }, type: "text" } ), gt = ({ widget: t }) => { const e = `questionnaire_input_widget_${t.id()}`, n = t.get(q), s = t.get(X), r = t.get(Y), c = t.get(I), o = t.get(k), i = t.get(J), l = t.get(K) || Z, f = t.get(w), g = t.get(tt) || "left", d = t.get(et) || "Please fill out this field", { values: u, handleChange: a } = G(f, [i]), p = U(n, s); if (B(t), h(() => { P() && a([i]); }, [i]), !z() || !p) return /* @__PURE__ */ m(W, { status: "noFormContext" }); const $ = !p.isLocallyValid, x = (E, O) => { s && p.setIsLocallyValid(!C(E[0])), a(E, O); }, F = { string_single_line: V, string_multi_line: ot, integer: _, floating_point: _, date: st, date_time: at }[l] || V; return /* @__PURE__ */ y("div", { ref: p.ref, className: `mb-3 form-input-container ${l} ${g}`, children: [ !C(c) && /* @__PURE__ */ y("label", { htmlFor: e, className: "input-label", children: [ /* @__PURE__ */ m(H, { attribute: I, content: t, tag: "span" }), s && /* @__PURE__ */ m(Q, {}), o && /* @__PURE__ */ m(R, { widget: t }) ] }), /* @__PURE__ */ m( F, { id: e, externalId: n, placeholder: r, value: u[0], type: l, defaultValue: i, isInvalid: $, onInputChange: x } ), $ && /* @__PURE__ */ m("div", { className: `invalid-feedback ${g}`, children: d }) ] }); }; export { gt as default }; //# sourceMappingURL=InputQuestionWidgetContent-BdqbWD6n.js.map