treege 3.0.0-beta.82 → 3.0.0-beta.83

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,527 +0,0 @@
1
- import { A as e, D as t, E as n, G as r, H as i, N as a, O as o, P as s, R as c, U as l, W as u, a as d, c as f, d as p, f as m, i as h, j as g, k as _, l as v, o as y, p as b, s as x, u as S, x as C, z as w } from "./ThemeContext-BgTIhoK6.js";
2
- import { Fragment as T, createContext as E, useCallback as D, useContext as O, useEffect as k, useMemo as A, useRef as j, useState as M } from "react";
3
- import { jsx as N } from "react/jsx-runtime";
4
- //#region src/renderer/context/TreegeRendererProvider.tsx
5
- var P = E(void 0), ee = () => O(P), F = ({ children: e, ...t }) => /* @__PURE__ */ N(P.Provider, {
6
- value: t,
7
- children: e
8
- }), I = async (r, i, a, s, c) => {
9
- if (!r.url || r.url.trim() === "") return {
10
- error: "No URL configured for submission",
11
- success: !1
12
- };
13
- let l = g(_(r.url, i, { encode: !0 }), c);
14
- if (!l || l.trim() === "") return {
15
- error: "Invalid URL after template replacement",
16
- success: !1
17
- };
18
- let u = r.payloadTemplate ? C(r.payloadTemplate, i, a) : void 0, d = await n({
19
- body: u === void 0 ? r.sendAllFormValues ? JSON.stringify(x(i, a)) : void 0 : JSON.stringify(u),
20
- headers: t(e(s, i), e(r.headers, i)),
21
- method: r.method || "POST",
22
- queryParams: e(r.queryParams, i),
23
- url: l
24
- });
25
- if (!d.success) return {
26
- data: d.data,
27
- error: d.error,
28
- success: !1
29
- };
30
- let f = r.redirectUrl ? (() => {
31
- let e = o(r.redirectUrl, d.data);
32
- return e && e.trim() !== "" ? e : void 0;
33
- })() : void 0;
34
- return {
35
- data: d.data,
36
- redirectUrl: f,
37
- success: !0
38
- };
39
- }, L = (e) => {
40
- e && (e.startsWith("http://") || e.startsWith("https://") ? window.location.href = e : window.history?.pushState ? (window.history.pushState({}, "", e), window.dispatchEvent(new PopStateEvent("popstate"))) : window.location.href = e);
41
- }, te = (e, t, n, r, a, o) => {
42
- let [s, c] = M(!1), [l, d] = M(null), f = A(() => {
43
- let t = e.find((e) => u(e) && e.data?.type === "submit");
44
- return t && u(t) ? t : null;
45
- }, [e]), p = D(async (e) => {
46
- if (!f?.data?.submitConfig) return null;
47
- let s = f.data.submitConfig;
48
- if (!s.url || s.url.trim() === "") return null;
49
- d(null), s.showLoading !== !1 && c(!0);
50
- try {
51
- let c = await I(s, t, r, a, o);
52
- if (c.success) {
53
- let t = i(s.successMessage, n);
54
- if (t && d({
55
- message: t,
56
- type: "success"
57
- }), e && e(c.data), c.redirectUrl) {
58
- let e = c.redirectUrl;
59
- setTimeout(() => L(e), 1e3);
60
- }
61
- } else d({
62
- message: i(s.errorMessage, n) || c.error || "An error occurred during submission",
63
- type: "error"
64
- });
65
- return c;
66
- } catch (e) {
67
- let t = i(s.errorMessage, n) || (e instanceof Error ? e.message : "An unexpected error occurred");
68
- return d({
69
- message: t,
70
- type: "error"
71
- }), {
72
- error: t,
73
- success: !1
74
- };
75
- } finally {
76
- c(!1);
77
- }
78
- }, [
79
- f,
80
- t,
81
- n,
82
- r,
83
- a,
84
- o
85
- ]);
86
- return {
87
- clearSubmitMessage: D(() => {
88
- d(null);
89
- }, []),
90
- handleSubmitWithConfig: p,
91
- hasSubmitConfig: !!f?.data?.submitConfig,
92
- isSubmitting: s,
93
- submitButtonNode: f,
94
- submitMessage: l
95
- };
96
- }, R = (e) => e == null ? null : typeof e == "boolean" || typeof e == "number" ? e : typeof e == "string" ? e === "" ? null : e : typeof e == "object" ? JSON.stringify(e) : String(e), z = (e, t, n) => {
97
- let r = R(e), i = R(t);
98
- if (r === null || i === null) switch (n) {
99
- case "===": return r === i;
100
- case "!==": return r !== i;
101
- default: return !1;
102
- }
103
- if ([
104
- ">",
105
- "<",
106
- ">=",
107
- "<="
108
- ].includes(n)) {
109
- let e = Number(r), t = Number(i);
110
- if (!(Number.isNaN(e) || Number.isNaN(t))) switch (n) {
111
- case ">": return e > t;
112
- case "<": return e < t;
113
- case ">=": return e >= t;
114
- case "<=": return e <= t;
115
- default: return !1;
116
- }
117
- return !1;
118
- }
119
- switch (n) {
120
- case "===": {
121
- let e = Number(r), t = Number(i);
122
- return Number.isNaN(e) || Number.isNaN(t) ? String(r) === String(i) : e === t;
123
- }
124
- case "!==": {
125
- let e = Number(r), t = Number(i);
126
- return Number.isNaN(e) || Number.isNaN(t) ? String(r) !== String(i) : e !== t;
127
- }
128
- default: return !1;
129
- }
130
- }, B = (e, t, n) => {
131
- let { field: r, operator: i, value: a } = e;
132
- if (!(r && i) || a === void 0) return !0;
133
- let o = t[r], s = o === void 0 && n ? n.get(r) : void 0, c = s && u(s) ? t[s.id] : o;
134
- return Array.isArray(c) ? i === "===" ? c.some((e) => z(e, a, "===")) || z(c, a, "===") : i === "!==" ? c.every((e) => z(e, a, "!==")) && z(c, a, "!==") : c.some((e) => z(e, a, i)) : z(c, a, i);
135
- }, V = (e, t, n) => {
136
- if (!e || e.length === 0) return !0;
137
- if (e.length === 1) return B(e[0], t, n);
138
- let r = B(e[0], t, n);
139
- for (let i = 1; i < e.length; i += 1) {
140
- let a = e[i], o = B(a, t, n), s = e[i - 1].logicalOperator || w.AND;
141
- if (s === w.AND) {
142
- if (r &&= o, !r) return !1;
143
- } else if (s === w.OR && (r ||= o, r)) return !0;
144
- }
145
- return r;
146
- }, ne = (e) => {
147
- let t = /* @__PURE__ */ new Map();
148
- return e.forEach((e) => {
149
- let n = t.get(e.source);
150
- n ? n.push(e) : t.set(e.source, [e]);
151
- }), t;
152
- }, re = (e, t, n) => {
153
- let r = [], i = [], a = [];
154
- e.forEach((e) => {
155
- let t = e.data?.isFallback, n = e.data?.conditions?.length;
156
- t ? a.push(e) : n ? i.push(e) : r.push(e);
157
- });
158
- let o = [...r];
159
- if (i.length === 0) return a.length > 0 && o.length === 0 && o.push(...a), {
160
- edgesToFollow: o,
161
- waitingForInput: !1
162
- };
163
- if (!i.every((e) => {
164
- let r = e.data?.conditions;
165
- return r ? r.every((e) => {
166
- if (!e.field) return !0;
167
- let r = n.get(e.field);
168
- return y(u(r) ? r.id : e.field, t);
169
- }) : !1;
170
- })) return a.length > 0 && o.push(...a), {
171
- edgesToFollow: o,
172
- waitingForInput: o.length === 0
173
- };
174
- let s = i.filter((e) => V(e.data?.conditions, t, n));
175
- return s.length > 0 ? (o.push(...s), {
176
- edgesToFollow: o,
177
- waitingForInput: !1
178
- }) : (o.push(...a), {
179
- edgesToFollow: o,
180
- waitingForInput: !1
181
- });
182
- }, H = (e, t) => !t.some((t) => t.target === e), U = (e, t) => {
183
- let n = e.filter((e) => H(e.id, t));
184
- return n.find(u) || n[0];
185
- }, W = (e, t, n = /* @__PURE__ */ new Set()) => !e || n.has(e) ? [] : (n.add(e), [e, ...W(t.get(e)?.parentId, t, n)]), G = (e, t, n, r) => {
186
- e.forEach((e) => {
187
- let i = n.get(e);
188
- W(r.get(e)?.parentId, r).forEach((e) => {
189
- t.add(e), !n.has(e) && i !== void 0 && n.set(e, i);
190
- });
191
- });
192
- }, ie = (e, t, n) => {
193
- let r = U(e, t);
194
- if (!r) return {
195
- endOfPathReached: !0,
196
- visibleNodeIds: /* @__PURE__ */ new Set(),
197
- visibleNodes: [],
198
- visibleRootNodes: []
199
- };
200
- let i = new Map(e.map((e) => [e.id, e])), a = ne(t), o = [], s = /* @__PURE__ */ new Set(), c = /* @__PURE__ */ new Set(), l = !1, u = (e) => {
201
- if (c.has(e)) return;
202
- c.add(e);
203
- let t = i.get(e);
204
- if (!t) return;
205
- s.add(e), o.push(t);
206
- let { edgesToFollow: r, waitingForInput: d } = re(a.get(e) || [], n, i);
207
- if (d) {
208
- l = !0;
209
- return;
210
- }
211
- r.forEach((e) => {
212
- u(e.target);
213
- });
214
- };
215
- u(r.id);
216
- let d = new Map(o.map((e, t) => [e.id, t])), f = new Set(s);
217
- G(s, f, d, i);
218
- let p = e.filter((e) => f.has(e.id)).sort((e, t) => (d.get(e.id) ?? 2 ** 53 - 1) - (d.get(t.id) ?? 2 ** 53 - 1)), m = p.filter((e) => !(e.parentId && f.has(e.parentId)));
219
- return {
220
- endOfPathReached: !l,
221
- visibleNodeIds: f,
222
- visibleNodes: p,
223
- visibleRootNodes: m
224
- };
225
- }, ae = (e) => e.filter((e) => !l(e)).reduce((e, t) => {
226
- let n = t.parentId ?? null, r = e.at(-1);
227
- return r && r.groupId === n ? [...e.slice(0, -1), {
228
- ...r,
229
- nodes: [...r.nodes, t]
230
- }] : [...e, {
231
- groupId: n,
232
- nodes: [t]
233
- }];
234
- }, []), K = (e) => {
235
- if (typeof e != "object" || !e) return JSON.stringify(e) ?? "null";
236
- if (Array.isArray(e)) return `[${e.map(K).join(",")}]`;
237
- let t = e;
238
- return `{${Object.keys(t).sort().map((e) => `${JSON.stringify(e)}:${K(t[e])}`).join(",")}}`;
239
- }, oe = ({ baseUrl: e, components: n, flow: r, googleApiKey: i, headers: o, language: s, onChange: c, onSubmit: p, showPoweredBy: m, theme: g, validate: _, validationMode: y, initialValues: b = {} }) => {
240
- let w = ee(), T = A(() => ({
241
- baseUrl: e ?? w?.baseUrl,
242
- components: {
243
- form: n?.form ?? w?.components?.form,
244
- inputs: {
245
- ...w?.components?.inputs,
246
- ...n?.inputs
247
- },
248
- loadingSkeleton: n?.loadingSkeleton ?? w?.components?.loadingSkeleton,
249
- step: n?.step ?? w?.components?.step,
250
- submitButton: n?.submitButton ?? w?.components?.submitButton,
251
- submitButtonWrapper: n?.submitButtonWrapper ?? w?.components?.submitButtonWrapper,
252
- ui: {
253
- ...w?.components?.ui,
254
- ...n?.ui
255
- }
256
- },
257
- googleApiKey: i ?? w?.googleApiKey,
258
- headers: t(w?.headers, o),
259
- language: s ?? w?.language ?? "en",
260
- showPoweredBy: m ?? w?.showPoweredBy ?? !0,
261
- theme: g ?? w?.theme ?? "dark",
262
- validationMode: y ?? w?.validationMode ?? "onSubmit"
263
- }), [
264
- e,
265
- n,
266
- w,
267
- i,
268
- o,
269
- s,
270
- m,
271
- g,
272
- y
273
- ]), E = A(() => r?.nodes ?? [], [r]), O = A(() => r?.edges ?? [], [r]), N = A(() => S(E), [E]), P = a(T.language), F = j({}), I = j(null), [L, R] = M({}), [z, B] = M(() => h(b, N)), V = A(() => K(b), [b]), ne = j(V), { endOfPathReached: re, visibleNodes: H, visibleRootNodes: U } = A(() => ie(E, O, z), [
274
- E,
275
- O,
276
- z
277
- ]), W = A(() => S(H), [H]), G = A(() => ae(H), [H]), oe = A(() => {
278
- let e = /* @__PURE__ */ new Map();
279
- return E.forEach((t) => {
280
- l(t) && e.set(t.id, t);
281
- }), e;
282
- }, [E]), [q, J] = M(0), Y = G.length === 0 ? 0 : Math.min(q, G.length - 1), X = G[Y], se = X?.groupId ? oe.get(X.groupId) : void 0, ce = Y === 0, le = G.length === 0 || Y >= G.length - 1, { clearSubmitMessage: ue, handleSubmitWithConfig: de, hasSubmitConfig: fe, isSubmitting: pe, submitButtonNode: me, submitMessage: he } = te(H, z, T.language, W, T.headers, T.baseUrl), ge = j(c), _e = j(_), Z = A(() => x(z, W), [z, W]), Q = A(() => {
283
- let e = me?.data?.submitConfig?.payloadTemplate;
284
- return e ? C(e, z, W) ?? Z : Z;
285
- }, [
286
- me,
287
- z,
288
- W,
289
- Z
290
- ]), ve = D((e, t) => {
291
- B((n) => ({
292
- ...n,
293
- [e]: t
294
- })), R((t) => {
295
- let n = { ...t };
296
- return delete n[e], n;
297
- });
298
- }, []), ye = D((e) => {
299
- Object.keys(e).length !== 0 && (B((t) => ({
300
- ...t,
301
- ...e
302
- })), R((t) => {
303
- let n = { ...t };
304
- return Object.keys(e).forEach((e) => {
305
- delete n[e];
306
- }), n;
307
- }));
308
- }, []), $ = D((e) => {
309
- let t = {};
310
- H.forEach((e) => {
311
- if (u(e)) {
312
- let n = e.id, r = z[n];
313
- if (e.data.required && f(r)) {
314
- t[n] = P(e.data.errorMessage) || P("validation.required");
315
- return;
316
- }
317
- if (!f(r) && e.data.pattern) try {
318
- new RegExp(e.data.pattern).test(String(r)) || (t[n] = P(e.data.errorMessage) || P("validation.invalidFormat"));
319
- } catch (e) {
320
- console.error(`Invalid pattern for field ${n}:`, e);
321
- }
322
- }
323
- });
324
- let n = e ? e(z, H) : {}, r = {
325
- ...t,
326
- ...n
327
- };
328
- return R(r), {
329
- errors: r,
330
- hasCustomErrors: Object.keys(n).length > 0,
331
- isValid: Object.keys(r).length === 0
332
- };
333
- }, [
334
- H,
335
- z,
336
- P
337
- ]), be = D(() => {
338
- J((e) => e >= G.length - 1 ? e : e + 1);
339
- }, [G.length]), xe = D(() => {
340
- J((e) => Math.max(0, e - 1));
341
- }, []), Se = D(async () => {
342
- let { isValid: e } = $(_e.current);
343
- if (!e) return !1;
344
- if (fe) {
345
- let e = await de((e) => {
346
- p && p(Q, { httpResponse: e });
347
- });
348
- if (e === null) return p?.(Q), !0;
349
- if (!e.success) return !0;
350
- } else p && p(Q);
351
- return !0;
352
- }, [
353
- $,
354
- fe,
355
- de,
356
- p,
357
- Q
358
- ]), Ce = A(() => {
359
- if (!X) return [];
360
- let e = [];
361
- return X.nodes.forEach((t) => {
362
- u(t) && t.data.type !== "submit" && t.data.required && f(z[t.id]) && e.push(P(t.data.label) || t.id);
363
- }), e;
364
- }, [
365
- X,
366
- z,
367
- P
368
- ]), we = A(() => H.some((e) => u(e) && e.data.type === "submit"), [H]), Te = A(() => X ? X.nodes.every((e) => !u(e) || !e.data.required || e.data.type === "submit" ? !0 : !f(z[e.id])) : !1, [X, z]), Ee = A(() => {
369
- let e = Object.keys(L);
370
- return e.length > 0 ? e[0] : void 0;
371
- }, [L]);
372
- return k(() => {
373
- if (G.length === 0) {
374
- q !== 0 && J(0);
375
- return;
376
- }
377
- q > G.length - 1 && J(G.length - 1);
378
- }, [G.length, q]), k(() => {
379
- ge.current = c;
380
- }, [c]), k(() => {
381
- _e.current = _;
382
- }, [_]), k(() => {
383
- ge.current?.(Z);
384
- }, [Z]), k(() => {
385
- T.validationMode === "onChange" && $(_e.current);
386
- }, [T.validationMode, $]), k(() => {
387
- let e = d(N, z, F.current);
388
- Object.keys(e).length > 0 && ye(e), F.current = z;
389
- }, [
390
- z,
391
- N,
392
- ye
393
- ]), k(() => {
394
- let e = I.current, t = new Set(W.map((e) => e.id));
395
- if (I.current = t, !e) return;
396
- let n = [...e].filter((e) => !t.has(e)), r = {};
397
- W.forEach((t) => {
398
- if (e.has(t.id) || z[t.id] !== void 0) return;
399
- let n = v(t, z);
400
- n !== void 0 && (r[t.id] = n);
401
- }), !(n.length === 0 && Object.keys(r).length === 0) && (B((e) => {
402
- let t = {
403
- ...e,
404
- ...r
405
- };
406
- return n.forEach((e) => {
407
- delete t[e];
408
- }), t;
409
- }), n.length > 0 && R((e) => {
410
- let t = !1, r = { ...e };
411
- return n.forEach((e) => {
412
- e in r && (delete r[e], t = !0);
413
- }), t ? r : e;
414
- }));
415
- }, [W, z]), k(() => {
416
- ne.current !== V && (ne.current = V, B(h(b, N)), R({}), I.current = null);
417
- }, [
418
- V,
419
- b,
420
- N
421
- ]), {
422
- canContinueStep: Te,
423
- canSubmit: (we || re) && E.length > 0,
424
- clearSubmitMessage: ue,
425
- config: T,
426
- currentStep: X,
427
- currentStepGroupNode: se,
428
- currentStepIndex: Y,
429
- firstErrorFieldId: Ee,
430
- formErrors: L,
431
- formValues: z,
432
- goToNextStep: be,
433
- goToPreviousStep: xe,
434
- handleSubmit: Se,
435
- hasSubmitInput: we,
436
- inputNodes: N,
437
- isFirstStep: ce,
438
- isLastStep: le,
439
- isSubmitting: pe,
440
- missingRequiredFields: Ce,
441
- prevFormValuesRef: F,
442
- setFieldErrors: R,
443
- setFieldValue: ve,
444
- setMultipleFieldValues: ye,
445
- steps: G,
446
- submitMessage: he,
447
- t: P,
448
- validateForm: $,
449
- visibleNodes: H,
450
- visibleRootNodes: U
451
- };
452
- }, q = /\{\{([\w-]+)}}/g, J = (e) => e == null || e === "", Y = (...e) => {
453
- let t = [];
454
- for (let n of e) if (n) for (let e of n.matchAll(q)) t.includes(e[1]) || t.push(e[1]);
455
- return t;
456
- }, X = (e) => {
457
- let t = e.data.httpConfig ?? e.data.optionsSource;
458
- if (!t) return [];
459
- let n = Object.values(t.queryParams ?? {});
460
- return Y(t.url, t.body, ...n);
461
- }, se = (e, t, n, r) => X(e).filter((e) => J(t[e])).map((e) => {
462
- let t = n.find((t) => t.id === e);
463
- return {
464
- id: e,
465
- label: t ? b(t, r) : e
466
- };
467
- }), ce = ({ DefaultFormWrapper: e, DefaultInputLabel: t, DefaultInputWrapper: n, DefaultSubmitButton: a, DefaultSubmitButtonWrapper: o, config: l, defaultInputRenderers: d, defaultUI: f, formErrors: h, formValues: g, inputNodes: _, isSubmitting: v, missingRequiredFields: y, setFieldValue: b }) => {
468
- let x = A(() => l.components.form || e, [l.components.form, e]), S = A(() => l.components.submitButton || a, [l.components.submitButton, a]), C = A(() => l.components.submitButtonWrapper || o || T, [l.components.submitButtonWrapper, o]), w = A(() => l.components.inputLabel || t, [l.components.inputLabel, t]);
469
- return {
470
- FormWrapper: x,
471
- renderNode: D(function(e) {
472
- let { type: t } = e;
473
- switch (t) {
474
- case c.input: {
475
- if (!u(e)) return null;
476
- let t = e.data, r = t.type || "text", a = l.components.inputs?.[r], o = d[r], c = a || o, f = e.id, x = (e) => b(f, e), S = g[f], C = h[f], T = i(t.label, l.language), E = p(t, l.language), D = i(t.helperText, l.language), O = m(e), k = s(T), A = s(E), j = s(D);
477
- return c ? /* @__PURE__ */ N(n, {
478
- node: e,
479
- children: /* @__PURE__ */ N(c, {
480
- field: {
481
- "aria-invalid": C ? !0 : void 0,
482
- id: e.id,
483
- name: O,
484
- placeholder: A,
485
- required: t.required,
486
- value: S
487
- },
488
- extra: {
489
- error: C,
490
- helperText: j,
491
- InputLabel: w,
492
- isSubmitting: v,
493
- label: k,
494
- missingDependencies: se(e, g, _, l.language),
495
- missingRequiredFields: y,
496
- node: e,
497
- setValue: x
498
- }
499
- }, r)
500
- }, e.id) : (console.warn("No renderer found for input type:", r), null);
501
- }
502
- case c.group: return null;
503
- case c.ui: {
504
- if (!r(e)) return null;
505
- let t = e.data.type || "title", n = l.components.ui?.[t], i = f[t], a = n || i;
506
- return a ? /* @__PURE__ */ N(a, { node: e }, e.id) : null;
507
- }
508
- default: return console.warn("Unknown node type:", t), null;
509
- }
510
- }, [
511
- l,
512
- g,
513
- h,
514
- b,
515
- v,
516
- y,
517
- _,
518
- d,
519
- f,
520
- n
521
- ]),
522
- SubmitButton: S,
523
- SubmitButtonWrapper: C
524
- };
525
- };
526
- //#endregion
527
- export { H as a, F as c, ie as i, ee as l, oe as n, B as o, U as r, V as s, ce as t };