@a-ludi/rjsf-formulas 0.6.0 → 0.6.2

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.
package/dist/index.mjs CHANGED
@@ -1,18 +1,18 @@
1
1
  import { findSchemaDefinition as ut } from "@rjsf/utils";
2
2
  import { jsx as at } from "react/jsx-runtime";
3
- import { useState as lt, useRef as m, useEffect as g, useCallback as Z, forwardRef as pt, useMemo as K } from "react";
4
- import yt from "@rjsf/core";
5
- const G = Symbol("arrayIndex");
6
- function dt(t, r) {
3
+ import { useState as lt, useRef as g, useEffect as m, useCallback as K, forwardRef as pt, useMemo as Q } from "react";
4
+ import dt from "@rjsf/core";
5
+ const U = Symbol("arrayIndex");
6
+ function yt(t, r) {
7
7
  const e = (r == null ? void 0 : r.formulaKey) ?? "x-formula", s = (r == null ? void 0 : r.formulaContextKey) ?? "x-formula-context", n = (r == null ? void 0 : r.formulaConflictBehavior) ?? "warn", o = [];
8
- return x(t, [], o, e, s, n, !0, t), o;
8
+ return O(t, [], o, e, s, n, !0, t), o;
9
9
  }
10
- function Q(t, r) {
10
+ function W(t, r) {
11
11
  if (t === !0) return r;
12
12
  const e = t;
13
13
  return Array.isArray(e.allOf) ? { allOf: [...e.allOf, r] } : { allOf: [t, r] };
14
14
  }
15
- function mt(t, r) {
15
+ function gt(t, r) {
16
16
  return t === void 0 || t === "siblings" ? "siblings" : t === "extended" ? "extended" : (console.warn(
17
17
  `[rjsf-formulas] Unknown x-formula-context value "${t}" at path [${H(r)}], treating as "siblings"`
18
18
  ), "siblings");
@@ -23,7 +23,7 @@ function H(t) {
23
23
  function L(t) {
24
24
  return t.map((r) => typeof r == "symbol" ? "[*]" : String(r)).join(".");
25
25
  }
26
- function gt(t, r, e) {
26
+ function mt(t, r, e) {
27
27
  const s = L(t.path);
28
28
  if (e === "error")
29
29
  throw new TypeError(
@@ -36,14 +36,14 @@ function gt(t, r, e) {
36
36
  function ht(t, r, e) {
37
37
  for (const s of r) {
38
38
  const n = L(s.path), o = t.findIndex((i) => L(i.path) === n);
39
- o !== -1 ? (gt(t[o], s, e), t[o] = s) : t.push(s);
39
+ o !== -1 ? (mt(t[o], s, e), t[o] = s) : t.push(s);
40
40
  }
41
41
  }
42
- function x(t, r, e, s, n, o, i, u) {
42
+ function O(t, r, e, s, n, o, i, u) {
43
43
  const f = t;
44
44
  if (typeof f.$ref == "string") {
45
45
  const c = ut(f.$ref, u);
46
- x(c, r, e, s, n, o, i, u);
46
+ O(c, r, e, s, n, o, i, u);
47
47
  return;
48
48
  }
49
49
  if (s in t) {
@@ -57,7 +57,7 @@ function x(t, r, e, s, n, o, i, u) {
57
57
  e.push({
58
58
  path: r,
59
59
  formula: c,
60
- contextMode: mt(f[n], r),
60
+ contextMode: gt(f[n], r),
61
61
  condition: i
62
62
  });
63
63
  return;
@@ -65,7 +65,7 @@ function x(t, r, e, s, n, o, i, u) {
65
65
  const l = t.type === "array" || t.type == null && (Array.isArray(f.prefixItems) || t.items != null);
66
66
  if (t.properties && !l)
67
67
  for (const [c, a] of Object.entries(t.properties))
68
- x(
68
+ O(
69
69
  a,
70
70
  [...r, c],
71
71
  e,
@@ -75,13 +75,13 @@ function x(t, r, e, s, n, o, i, u) {
75
75
  i,
76
76
  u
77
77
  );
78
- if (l && (Array.isArray(f.prefixItems) && f.prefixItems.forEach((a, d) => {
79
- x(a, [...r, d], e, s, n, o, i, u);
80
- }), Array.isArray(t.items) && t.items.forEach((c, a) => {
81
- x(c, [...r, a], e, s, n, o, i, u);
82
- }), t.items && !Array.isArray(t.items) && x(
78
+ if (l && (Array.isArray(f.prefixItems) && f.prefixItems.forEach((a, y) => {
79
+ O(a, [...r, y], e, s, n, o, i, u);
80
+ }), Array.isArray(t.items) && (t.items.forEach((c, a) => {
81
+ O(c, [...r, a], e, s, n, o, i, u);
82
+ }), t.additionalItems && typeof t.additionalItems == "object" && O(t.additionalItems, [...r, U], e, s, n, o, i, u)), t.items && !Array.isArray(t.items) && O(
83
83
  t.items,
84
- [...r, G],
84
+ [...r, U],
85
85
  e,
86
86
  s,
87
87
  n,
@@ -91,38 +91,38 @@ function x(t, r, e, s, n, o, i, u) {
91
91
  )), Array.isArray(f.allOf)) {
92
92
  const c = f.allOf;
93
93
  for (const a of c) {
94
- const d = [];
95
- x(a, r, d, s, n, o, i, u), ht(e, d, o);
94
+ const y = [];
95
+ O(a, r, y, s, n, o, i, u), ht(e, y, o);
96
96
  }
97
97
  }
98
98
  for (const c of ["oneOf", "anyOf"])
99
99
  if (Array.isArray(f[c])) {
100
100
  const a = f[c];
101
- for (const d of a) {
102
- const j = Q(i, d);
103
- x(d, r, e, s, n, o, j, u);
101
+ for (const y of a) {
102
+ const h = W(i, y);
103
+ O(y, r, e, s, n, o, h, u);
104
104
  }
105
105
  }
106
106
  if ("if" in f) {
107
- const c = f.if, a = f.then, d = f.else;
107
+ const c = f.if, a = f.then, y = f.else;
108
108
  if (s in c && console.warn(
109
109
  `[rjsf-formulas] Formula key "${s}" found inside an "if" schema at path [${H(r)}]. The "if" schema is a validation predicate and is never rendered — the formula will be ignored.`
110
110
  ), a !== void 0) {
111
- const j = Q(i, c);
112
- x(a, r, e, s, n, o, j, u);
111
+ const h = W(i, c);
112
+ O(a, r, e, s, n, o, h, u);
113
113
  }
114
- if (d !== void 0) {
115
- const j = Q(i, { not: c });
116
- x(d, r, e, s, n, o, j, u);
114
+ if (y !== void 0) {
115
+ const h = W(i, { not: c });
116
+ O(y, r, e, s, n, o, h, u);
117
117
  }
118
118
  }
119
119
  }
120
- function wt(t) {
120
+ function jt(t) {
121
121
  return t && t.__esModule && Object.prototype.hasOwnProperty.call(t, "default") ? t.default : t;
122
122
  }
123
- var W, et;
124
- function jt() {
125
- return et || (et = 1, W = function t(r, e) {
123
+ var Z, et;
124
+ function wt() {
125
+ return et || (et = 1, Z = function t(r, e) {
126
126
  if (r === e) return !0;
127
127
  if (r && e && typeof r == "object" && typeof e == "object") {
128
128
  if (r.constructor !== e.constructor) return !1;
@@ -146,10 +146,10 @@ function jt() {
146
146
  return !0;
147
147
  }
148
148
  return r !== r && e !== e;
149
- }), W;
149
+ }), Z;
150
150
  }
151
- var At = jt();
152
- const Ot = /* @__PURE__ */ wt(At);
151
+ var At = wt();
152
+ const Ot = /* @__PURE__ */ jt(At);
153
153
  function xt(t, r, e, s) {
154
154
  const o = r.slice(0, -1).reduce((u, f) => {
155
155
  if (!(u == null || typeof u != "object"))
@@ -166,7 +166,7 @@ function xt(t, r, e, s) {
166
166
  function nt(t) {
167
167
  return t.map((r) => typeof r == "symbol" ? "[*]" : String(r)).join(", ");
168
168
  }
169
- function M(t, r) {
169
+ function E(t, r) {
170
170
  return r.reduce((e, s) => {
171
171
  if (!(e == null || typeof e != "object"))
172
172
  return e[s];
@@ -174,21 +174,21 @@ function M(t, r) {
174
174
  }
175
175
  function B(t, r, e) {
176
176
  if (r.length === 0) return;
177
- const s = M(t, r.slice(0, -1));
177
+ const s = E(t, r.slice(0, -1));
178
178
  s != null && typeof s == "object" && (s[r[r.length - 1]] = e);
179
179
  }
180
- function E(t, r, e = []) {
180
+ function q(t, r, e = []) {
181
181
  if (t.length === 0) return [e];
182
182
  const [s, ...n] = t;
183
- if (s === G) {
184
- const o = M(r, e);
183
+ if (s === U) {
184
+ const o = E(r, e);
185
185
  if (!Array.isArray(o)) return [];
186
186
  const i = [];
187
187
  for (let u = 0; u < o.length; u++)
188
- i.push(...E(n, r, [...e, u]));
188
+ i.push(...q(n, r, [...e, u]));
189
189
  return i;
190
190
  }
191
- return E(n, r, [...e, s]);
191
+ return q(n, r, [...e, s]);
192
192
  }
193
193
  function tt(t, r) {
194
194
  return Ot(t, r);
@@ -196,7 +196,7 @@ function tt(t, r) {
196
196
  async function st(t, r, e, s) {
197
197
  const n = structuredClone(t), o = [];
198
198
  for (const f of r)
199
- for (const l of E(f.path, n)) {
199
+ for (const l of q(f.path, n)) {
200
200
  const c = xt(f, l, n, s);
201
201
  o.push({ field: f, concretePath: l, context: c });
202
202
  }
@@ -216,10 +216,10 @@ async function st(t, r, e, s) {
216
216
  }
217
217
  function Rt(t, r, e, s) {
218
218
  for (const n of e)
219
- for (const o of E(n.path, r))
219
+ for (const o of q(n.path, r))
220
220
  if (!tt(
221
- s(M(t, o), o),
222
- s(M(r, o), o)
221
+ s(E(t, o), o),
222
+ s(E(r, o), o)
223
223
  ))
224
224
  return !1;
225
225
  return !0;
@@ -229,10 +229,10 @@ async function bt(t, r, e, s, n, o, i, u, f, l = (c) => c) {
229
229
  (p) => p.condition === !0 || u(p.condition, t)
230
230
  );
231
231
  if (c.length === 0) return t;
232
- const a = /* @__PURE__ */ new Map(), d = [];
232
+ const a = /* @__PURE__ */ new Map(), y = [];
233
233
  for (const p of c) {
234
- const y = JSON.stringify(p.path);
235
- if (a.has(y)) {
234
+ const d = JSON.stringify(p.path);
235
+ if (a.has(d)) {
236
236
  if (f === "error")
237
237
  throw new TypeError(
238
238
  `[rjsf-formulas] Formula conflict: two active fields share path [${nt(p.path)}]`
@@ -240,136 +240,136 @@ async function bt(t, r, e, s, n, o, i, u, f, l = (c) => c) {
240
240
  f === "warn" && console.warn(
241
241
  `[rjsf-formulas] Formula conflict: two active fields share path [${nt(p.path)}]; taking last`
242
242
  );
243
- const h = d.indexOf(a.get(y));
244
- d[h] = p, a.set(y, p);
243
+ const j = y.indexOf(a.get(d));
244
+ y[j] = p, a.set(d, p);
245
245
  } else
246
- a.set(y, p), d.push(p);
246
+ a.set(d, p), y.push(p);
247
247
  }
248
- const j = { formulaDataKey: o, formulaPathKey: i };
249
- let O = t;
248
+ const h = { formulaDataKey: o, formulaPathKey: i };
249
+ let x = t;
250
250
  for (let p = 0; p < s; p++) {
251
- const { data: y, errors: h } = await st(O, d, e, j);
252
- if (Rt(O, y, d, l)) {
253
- for (const { path: A, error: b } of h)
251
+ const { data: d, errors: j } = await st(x, y, e, h);
252
+ if (Rt(x, d, y, l)) {
253
+ for (const { path: A, error: b } of j)
254
254
  n == null || n(A, b);
255
- return y;
255
+ return d;
256
256
  }
257
- O = y;
257
+ x = d;
258
258
  }
259
- const { data: R } = await st(O, d, e, j), k = structuredClone(R);
260
- for (const p of d)
261
- for (const y of E(p.path, R))
259
+ const { data: R } = await st(x, y, e, h), _ = structuredClone(R);
260
+ for (const p of y)
261
+ for (const d of q(p.path, R))
262
262
  tt(
263
- l(M(O, y), y),
264
- l(M(R, y), y)
263
+ l(E(x, d), d),
264
+ l(E(R, d), d)
265
265
  ) || (n == null || n(
266
- y,
266
+ d,
267
267
  new Error(
268
- `[rjsf-formulas] Formula at [${y.join(", ")}] did not converge after ${s} passes`
268
+ `[rjsf-formulas] Formula at [${d.join(", ")}] did not converge after ${s} passes`
269
269
  )
270
- ), B(k, y, void 0));
271
- return k;
270
+ ), B(_, d, void 0));
271
+ return _;
272
272
  }
273
- function $t(t) {
273
+ function It(t) {
274
274
  return JSON.stringify(t);
275
275
  }
276
- function It(t, r, e, s, n, o, i, u, f, l, c, a) {
277
- const [d, j] = lt(t), O = m(t), R = m(t), k = m(/* @__PURE__ */ new Map()), p = m(null), y = m(!1), h = m(o), A = m(i), b = m(e), U = m(r), J = m(n), q = m(u), v = m(f), _ = m(l), D = m(a);
278
- g(() => {
279
- h.current = o;
280
- }, [o]), g(() => {
276
+ function $t(t, r, e, s, n, o, i, u, f, l, c, a) {
277
+ const [y, h] = lt(t), x = g(t), R = g(t), _ = g(/* @__PURE__ */ new Map()), p = g(null), d = g(!1), j = g(o), A = g(i), b = g(e), J = g(r), z = g(n), P = g(u), v = g(f), D = g(l), F = g(a);
278
+ m(() => {
279
+ j.current = o;
280
+ }, [o]), m(() => {
281
281
  A.current = i;
282
- }, [i]), g(() => {
282
+ }, [i]), m(() => {
283
283
  b.current = e;
284
- }, [e]), g(() => {
285
- U.current = r;
286
- }, [r]), g(() => {
287
- J.current = n;
288
- }, [n]), g(() => {
289
- q.current = u;
290
- }, [u]), g(() => {
284
+ }, [e]), m(() => {
285
+ J.current = r;
286
+ }, [r]), m(() => {
287
+ z.current = n;
288
+ }, [n]), m(() => {
289
+ P.current = u;
290
+ }, [u]), m(() => {
291
291
  v.current = f;
292
- }, [f]), g(() => {
293
- _.current = l;
294
- }, [l]), g(() => {
295
- D.current = a;
292
+ }, [f]), m(() => {
293
+ D.current = l;
294
+ }, [l]), m(() => {
295
+ F.current = a;
296
296
  }, [a]);
297
- const z = Z(() => {
298
- if (y.current) return;
297
+ const X = K(() => {
298
+ if (d.current) return;
299
299
  (async () => {
300
- var P, Y;
301
- y.current = !0;
300
+ var I, V;
301
+ d.current = !0;
302
302
  try {
303
303
  for (; ; ) {
304
- const T = O.current, rt = U.current, ot = rt.filter(
305
- (I) => I.condition === !0 || v.current(I.condition, T)
304
+ const T = x.current, rt = J.current, ot = rt.filter(
305
+ (k) => k.condition === !0 || v.current(k.condition, T)
306
306
  ), N = [];
307
- for (const I of ot)
308
- for (const it of E(I.path, T))
307
+ for (const k of ot)
308
+ for (const it of q(k.path, T))
309
309
  N.push(it);
310
- const V = k.current;
311
- V.clear();
312
- for (const I of N) V.set($t(I), "running");
313
- N.length > 0 && ((P = A.current) == null || P.call(A, N));
310
+ const G = _.current;
311
+ G.clear();
312
+ for (const k of N) G.set(It(k), "running");
313
+ N.length > 0 && ((I = A.current) == null || I.call(A, N));
314
314
  const ft = await bt(
315
315
  T,
316
316
  rt,
317
317
  b.current,
318
- J.current,
319
- h.current,
320
- q.current.formulaDataKey,
321
- q.current.formulaPathKey,
318
+ z.current,
319
+ j.current,
320
+ P.current.formulaDataKey,
321
+ P.current.formulaPathKey,
322
322
  v.current,
323
- _.current,
324
- D.current
323
+ D.current,
324
+ F.current
325
325
  );
326
- if (w.current) break;
327
- const ct = [...V.values()].some((I) => I === "dirty");
328
- if (V.clear(), !ct) {
329
- j(ft), N.length > 0 && ((Y = A.current) == null || Y.call(A, []));
326
+ if (Y.current) break;
327
+ const ct = [...G.values()].some((k) => k === "dirty");
328
+ if (G.clear(), !ct) {
329
+ h(ft), N.length > 0 && ((V = A.current) == null || V.call(A, []));
330
330
  break;
331
331
  }
332
332
  }
333
333
  } finally {
334
- y.current = !1;
334
+ d.current = !1;
335
335
  }
336
336
  })();
337
- }, []), $ = Z(
338
- (F) => {
339
- O.current = F;
340
- const P = k.current;
341
- for (const [Y, T] of P)
342
- T === "running" && P.set(Y, "dirty");
343
- y.current || (p.current !== null && clearTimeout(p.current), p.current = setTimeout(() => {
344
- p.current = null, z();
337
+ }, []), $ = K(
338
+ (w) => {
339
+ x.current = w, h(w);
340
+ const I = _.current;
341
+ for (const [V, T] of I)
342
+ T === "running" && I.set(V, "dirty");
343
+ d.current || (p.current !== null && clearTimeout(p.current), p.current = setTimeout(() => {
344
+ p.current = null, X();
345
345
  }, s));
346
346
  },
347
- [s, z]
348
- ), X = m(!1), w = m(!1);
349
- return g(() => {
350
- X.current ? tt(t, R.current) || (R.current = t, $(t)) : (X.current = !0, R.current = t, c !== "skip" && $(t));
351
- }), g(() => (w.current = !1, () => {
352
- w.current = !0, X.current = !1, p.current !== null && clearTimeout(p.current);
353
- }), []), { enrichedFormData: d, handleInput: $ };
347
+ [s, X]
348
+ ), M = g(!1), Y = g(!1);
349
+ return m(() => {
350
+ M.current ? tt(t, R.current) || (R.current = t, $(t)) : (M.current = !0, R.current = t, c !== "skip" && $(t));
351
+ }), m(() => (Y.current = !1, () => {
352
+ Y.current = !0, M.current = !1, p.current !== null && clearTimeout(p.current);
353
+ }), []), { enrichedFormData: y, handleInput: $ };
354
354
  }
355
355
  function kt(t, r) {
356
- const e = structuredClone(t ?? {}), s = vt(r);
356
+ const e = structuredClone(t ?? {}), s = _t(r);
357
357
  for (const n of r)
358
358
  S(e, n.path, s);
359
359
  return e;
360
360
  }
361
- function vt(t) {
361
+ function _t(t) {
362
362
  const r = /* @__PURE__ */ new Set();
363
363
  for (let e = 0; e < t.length; e++) {
364
364
  const s = t[e].path;
365
365
  for (let n = 0; n < s.length; n++) {
366
366
  const o = s[n];
367
- if (typeof o != "number" && o !== G) continue;
367
+ if (typeof o != "number" && o !== U) continue;
368
368
  const i = C(s.slice(0, n)), u = typeof o == "number";
369
369
  t.some((l, c) => {
370
370
  if (c === e || l.path.length <= n || C(l.path.slice(0, n)) !== i) return !1;
371
371
  const a = l.path[n];
372
- return u ? a === G : typeof a == "number";
372
+ return u ? a === U : typeof a == "number";
373
373
  }) && r.add(i);
374
374
  }
375
375
  }
@@ -409,86 +409,92 @@ function S(t, r, e, s = []) {
409
409
  (!t.items || typeof t.items != "object") && (t.items = {}), S(t.items, o, e, [...s, n]);
410
410
  }
411
411
  }
412
- function _t(t, r) {
412
+ function vt(t, r) {
413
413
  const {
414
414
  schema: e,
415
415
  formData: s,
416
416
  validator: n,
417
417
  uiSchema: o,
418
418
  evaluator: i,
419
- Form: u = yt,
419
+ Form: u = dt,
420
420
  formulaKey: f = "x-formula",
421
421
  formulaContextKey: l = "x-formula-context",
422
422
  formulaDataKey: c = "__formData__",
423
423
  formulaPathKey: a = "__path__",
424
- maxConvergencePasses: d = 10,
425
- debounceMs: j = 300,
426
- onFormulaError: O,
424
+ maxConvergencePasses: y = 10,
425
+ debounceMs: h = 300,
426
+ onFormulaError: x,
427
427
  onLoadingChange: R,
428
- formulaConflictBehavior: k = "warn",
428
+ formulaConflictBehavior: _ = "warn",
429
429
  initialComputationMode: p = "always",
430
- convergenceKey: y = (w) => w,
431
- onChange: h,
430
+ convergenceKey: d = (w) => w,
431
+ onChange: j,
432
432
  ...A
433
- } = t, b = K(
434
- () => dt(e, { formulaKey: f, formulaContextKey: l }),
433
+ } = t, b = Q(
434
+ () => yt(e, { formulaKey: f, formulaContextKey: l }),
435
435
  [e, f, l]
436
- ), U = K(
436
+ ), J = Q(
437
437
  () => ({ formulaDataKey: c, formulaPathKey: a }),
438
438
  [c, a]
439
- ), J = K(
439
+ ), z = Q(
440
440
  () => kt(o, b),
441
441
  [o, b]
442
- ), q = Z(
443
- (w, F) => n.isValid(w, F, e),
442
+ ), P = K(
443
+ (w, I) => n.isValid(w, I, e),
444
444
  [n, e]
445
- ), v = p === "always" ? 0 : p === "skip" ? 1 : 2, _ = m(v);
446
- g(() => (_.current = v, () => {
447
- _.current = v;
445
+ ), v = p === "always" ? 0 : p === "skip" ? 1 : 2, D = g(v);
446
+ m(() => (D.current = v, () => {
447
+ D.current = v;
448
448
  }), []);
449
- const { enrichedFormData: D, handleInput: z } = It(
449
+ const { enrichedFormData: F, handleInput: X } = $t(
450
450
  s,
451
451
  b,
452
452
  i,
453
- j,
454
- d,
455
- O,
453
+ h,
454
+ y,
455
+ x,
456
456
  R,
457
- U,
458
- q,
459
- k,
457
+ J,
458
+ P,
459
+ _,
460
460
  p,
461
- y
462
- ), $ = m(h);
463
- return g(() => {
464
- $.current = h;
465
- }, [h]), g(() => {
461
+ d
462
+ ), $ = g(j);
463
+ m(() => {
464
+ $.current = j;
465
+ }, [j]);
466
+ const M = g(!1);
467
+ return m(() => {
466
468
  var w;
467
469
  if (b.length !== 0) {
468
- if (_.current > 0) {
469
- _.current--;
470
+ if (M.current) {
471
+ M.current = !1;
470
472
  return;
471
473
  }
472
- (w = $.current) == null || w.call($, { formData: D }, void 0);
474
+ if (D.current > 0) {
475
+ D.current--;
476
+ return;
477
+ }
478
+ (w = $.current) == null || w.call($, { formData: F }, void 0);
473
479
  }
474
- }, [D]), /* @__PURE__ */ at(
480
+ }, [F]), /* @__PURE__ */ at(
475
481
  u,
476
482
  {
477
483
  ref: r,
478
484
  ...A,
479
485
  schema: e,
480
486
  validator: n,
481
- formData: D,
482
- uiSchema: J,
483
- onChange: (w, F) => {
484
- z(w.formData), h == null || h(w, F);
487
+ formData: F,
488
+ uiSchema: z,
489
+ onChange: (w, I) => {
490
+ M.current = !0, X(w.formData), j == null || j(w, I);
485
491
  }
486
492
  }
487
493
  );
488
494
  }
489
- const qt = pt(_t);
495
+ const qt = pt(vt);
490
496
  export {
491
497
  qt as FormulaForm,
492
- dt as analyzeSchema
498
+ yt as analyzeSchema
493
499
  };
494
500
  //# sourceMappingURL=index.mjs.map