@jfdevelops/react-multi-step-form 1.0.0-alpha.15 → 1.0.0-alpha.16

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.
Files changed (51) hide show
  1. package/dist/create-context.cjs +92 -0
  2. package/dist/create-context.cjs.map +1 -0
  3. package/dist/create-context.d.cts +154 -0
  4. package/dist/create-context.d.mts +154 -0
  5. package/dist/create-context.mjs +92 -0
  6. package/dist/create-context.mjs.map +1 -0
  7. package/dist/field.cjs +22 -0
  8. package/dist/field.cjs.map +1 -0
  9. package/dist/field.d.cts +38 -0
  10. package/dist/field.d.mts +38 -0
  11. package/dist/field.mjs +16 -0
  12. package/dist/field.mjs.map +1 -0
  13. package/dist/form-config.cjs +32 -0
  14. package/dist/form-config.cjs.map +1 -0
  15. package/dist/form-config.d.cts +160 -0
  16. package/dist/form-config.d.mts +160 -0
  17. package/dist/form-config.mjs +27 -0
  18. package/dist/form-config.mjs.map +1 -0
  19. package/dist/hooks/use-multi-step-form-data.cjs +41 -0
  20. package/dist/hooks/use-multi-step-form-data.cjs.map +1 -0
  21. package/dist/hooks/use-multi-step-form-data.d.cts +28 -0
  22. package/dist/hooks/use-multi-step-form-data.d.mts +28 -0
  23. package/dist/hooks/use-multi-step-form-data.mjs +39 -0
  24. package/dist/hooks/use-multi-step-form-data.mjs.map +1 -0
  25. package/dist/index.cjs +16 -47
  26. package/dist/index.d.cts +5 -0
  27. package/dist/index.d.mts +5 -0
  28. package/dist/index.mjs +5 -1911
  29. package/dist/schema.cjs +44 -0
  30. package/dist/schema.cjs.map +1 -0
  31. package/dist/schema.d.cts +34 -0
  32. package/dist/schema.d.mts +34 -0
  33. package/dist/schema.mjs +43 -0
  34. package/dist/schema.mjs.map +1 -0
  35. package/dist/step-schema.cjs +222 -0
  36. package/dist/step-schema.cjs.map +1 -0
  37. package/dist/step-schema.d.cts +132 -0
  38. package/dist/step-schema.d.mts +132 -0
  39. package/dist/step-schema.mjs +215 -0
  40. package/dist/step-schema.mjs.map +1 -0
  41. package/package.json +9 -9
  42. package/dist/create-context.d.ts +0 -156
  43. package/dist/field.d.ts +0 -23
  44. package/dist/form-config.d.ts +0 -161
  45. package/dist/hooks/use-multi-step-form-data.d.ts +0 -26
  46. package/dist/index.cjs.map +0 -1
  47. package/dist/index.d.ts +0 -4
  48. package/dist/index.mjs.map +0 -1
  49. package/dist/schema.d.ts +0 -30
  50. package/dist/step-schema.d.ts +0 -131
  51. package/dist/utils.d.ts +0 -0
package/dist/index.mjs CHANGED
@@ -1,1912 +1,6 @@
1
- import { jsx as Te, jsxs as ie } from "react/jsx-runtime";
2
- import R from "react";
3
- function M(o, e, t) {
4
- const r = [], s = new Set(o), n = t ? t.format(o.map((c) => `${c}`)) : `[${o.join(", ")}]`;
5
- for (let c = 0; c < e.length; c++) {
6
- const S = e[c];
7
- s.has(S) || r.push(
8
- `Extra element at index ${c}: "${S}" is not in ${n}`
9
- );
10
- }
11
- return e.some((c) => s.has(c)) || r.push(
12
- `Array must contain at least one valid element from ${n}`
13
- ), r.length > 0 ? { status: "error", errors: r } : { status: "success" };
14
- }
15
- function U(o, e) {
16
- const t = (r, s) => `❌ ${s + 1}. ${r}`;
17
- return o.map((r, s) => t(r, s)).join(`
18
- `);
19
- }
20
- function ae(o) {
21
- return Object.keys(o);
22
- }
23
- function Ae(o, ...e) {
24
- return () => String.raw({ raw: o }, ...e);
25
- }
26
- function _(o, e, t = Error) {
27
- if (!o) {
28
- const r = new Intl.ListFormat("en", {
29
- style: "long",
30
- type: "conjunction"
31
- }), s = typeof e == "function" ? e(r) : Ae`${e}`();
32
- throw new t(s);
33
- }
34
- }
35
- function q(o) {
36
- _(o.includes("step"), "Can't extract a valid step number since");
37
- const e = o.replace("step", "");
38
- return _(/^\d+$/.test(e), `Invalid step format: "${o}"`), Number.parseInt(e, 10);
39
- }
40
- function ee(o) {
41
- return function(e) {
42
- const { step: t } = e, r = `step${t}`, s = o[r];
43
- return { step: t, data: s };
44
- };
45
- }
46
- function K(o, e) {
47
- return e.reduce((t, r) => {
48
- const s = q(r), { data: n } = ee(o)({
49
- step: s
50
- });
51
- for (const [a, c] of Object.entries(n))
52
- typeof c == "function" && a !== "update" || (n[a] = c);
53
- return t[r] = n, t;
54
- }, {});
55
- }
56
- function k(o, e) {
57
- const t = new Intl.ListFormat("en", {
58
- style: "long",
59
- type: "disjunction"
60
- }), r = Object.keys(o), s = () => `"stepData" must be set to an array of available steps (${t.format(
61
- r
62
- )})`;
63
- if (e === "all") {
64
- let n = {};
65
- for (const a of r)
66
- n = {
67
- ...n,
68
- [a]: ee(o)({
69
- step: q(a)
70
- })
71
- };
72
- return K(
73
- o,
74
- r
75
- );
76
- }
77
- if (Array.isArray(e))
78
- return _(
79
- e.every((n) => r.includes(n)),
80
- () => {
81
- const n = M(
82
- e,
83
- r.map((a) => q(a)),
84
- t
85
- );
86
- return n.status === "error" ? `${s()}. See errors:
87
- ${U(
88
- n.errors
89
- )}` : s();
90
- }
91
- ), K(
92
- o,
93
- e
94
- );
95
- if (typeof e == "object") {
96
- const n = Object.keys(e);
97
- return _(
98
- n.every((a) => r.includes(a)),
99
- () => {
100
- const a = M(
101
- n,
102
- r,
103
- t
104
- );
105
- return a.status === "error" ? `${s()}. See errors:
106
- ${U(
107
- a.errors
108
- )}` : s();
109
- }
110
- ), K(
111
- o,
112
- n
113
- );
114
- }
115
- throw new Error(`${s()} OR to "all"`);
116
- }
117
- function Ve(o, e) {
118
- return typeof o == "function" ? o(e) : o;
119
- }
120
- const G = "MultiStepFormSchema";
121
- class te {
122
- debug;
123
- prefix = G;
124
- includeTimestamp = !1;
125
- throwOnError = !1;
126
- constructor(e) {
127
- if (this.debug = e?.debug ?? !1, this.debug && e?.debug) {
128
- const {
129
- includeTimestamp: t = !1,
130
- prefix: r = G,
131
- throwOnError: s = !1
132
- } = e;
133
- this.includeTimestamp = t, this.prefix = typeof r == "string" ? r : r(G), this.throwOnError = s;
134
- }
135
- }
136
- wrapValue(e, t) {
137
- if (e === "[]")
138
- return `[${t}]`;
139
- if (e === "{}")
140
- return `{${t}}`;
141
- if (e === "none")
142
- return t;
143
- throw new Error(
144
- `The first argument ${e} is not valid. Valid options include: "[]", "{}", and "none"`
145
- );
146
- }
147
- formatPrefix(e) {
148
- if (!e)
149
- return this.prefix;
150
- const { action: t, value: r, wrapWith: s } = e;
151
- if (t === "replace")
152
- return this.wrapValue(s ?? "[]", r);
153
- const { delimiter: n } = e;
154
- return t === "append" ? `${this.wrapValue(
155
- s?.originalPrefix ?? "[]",
156
- this.prefix
157
- )}${n}${this.wrapValue(
158
- s?.addedPrefix ?? "[]",
159
- r
160
- )}`.trim() : t === "prepend" ? `${this.wrapValue(
161
- s?.addedPrefix ?? "[]",
162
- r
163
- )}${n}${this.wrapValue(
164
- s?.originalPrefix ?? "[]",
165
- this.prefix
166
- )}`.trim() : this.prefix;
167
- }
168
- formatMessage(e, t, r) {
169
- const {
170
- includeTimestamp: s = this.includeTimestamp,
171
- prefix: n,
172
- logLevelOptions: a
173
- } = r ?? {}, c = this.formatPrefix(n), S = this.wrapValue(
174
- a?.wrapWith ?? "[]",
175
- (a?.level ?? e).toUpperCase()
176
- ), g = `${c} ${S} ${t}`;
177
- return s ? `[${(/* @__PURE__ */ new Date()).toISOString()}] ${g}` : g;
178
- }
179
- info(e, t) {
180
- this.debug && console.info(this.formatMessage("info", e, t));
181
- }
182
- warn(e, t) {
183
- this.debug && console.warn(this.formatMessage("warn", e, t));
184
- }
185
- error(e, t) {
186
- if (!this.debug)
187
- return;
188
- const r = this.formatMessage("error", e, t);
189
- if (console.error(r), t?.throw ?? this.throwOnError)
190
- throw new Error(r);
191
- }
192
- }
193
- var V;
194
- ((o) => {
195
- function e(i, l) {
196
- return l.split(".").reduce((h, u) => h?.[u], i);
197
- }
198
- function t(i, l, h) {
199
- const u = l.split(".");
200
- let d = i;
201
- for (let E = 0; E < u.length; E++) {
202
- const $ = u[E];
203
- E === u.length - 1 ? d[$] !== void 0 && typeof d[$] == "object" && typeof h == "object" && h !== null ? Object.assign(d[$], h) : d[$] = h : ((typeof d[$] != "object" || d[$] === null) && (d[$] = {}), d = d[$]);
204
- }
205
- return d;
206
- }
207
- o.setBy = t;
208
- function r(...i) {
209
- const l = [...i].sort(
210
- (u, d) => d.split(".").length - u.split(".").length
211
- ), h = [];
212
- for (const u of l)
213
- h.some((E) => E.startsWith(u + ".")) || h.push(u);
214
- return h.reverse();
215
- }
216
- o.normalizePaths = r;
217
- function s(i, ...l) {
218
- const h = r(...l);
219
- if (h.length === 1)
220
- return e(i, h[0]);
221
- const u = {};
222
- for (const d of h) {
223
- const E = e(i, d);
224
- t(u, d, E);
225
- }
226
- return u;
227
- }
228
- o.pickBy = s;
229
- function n(i) {
230
- const l = [];
231
- function h(u, d = "") {
232
- if (u == null || typeof u != "object" || Array.isArray(u))
233
- return;
234
- const E = Object.keys(u);
235
- for (const $ of E) {
236
- const A = d ? `${d}.${$}` : $;
237
- l.push(A);
238
- const T = u[$];
239
- T != null && typeof T == "object" && !Array.isArray(T) && h(T, A);
240
- }
241
- }
242
- return h(i), l;
243
- }
244
- o.createDeep = n;
245
- function a(i) {
246
- if (i === null) return "null";
247
- if (Array.isArray(i)) return "Array";
248
- const l = typeof i;
249
- if (l !== "object") return l;
250
- const h = i?.constructor?.name;
251
- return h && h !== "Object" ? h : "object";
252
- }
253
- function c(i) {
254
- if (!i) return [];
255
- const l = [], h = /[^.[\]]+|\[(\d+)\]/g;
256
- let u;
257
- for (; u = h.exec(i); )
258
- l.push(u[1] ?? u[0]);
259
- return l;
260
- }
261
- function S(i, l) {
262
- if (!i) return;
263
- if (typeof i == "function")
264
- return i;
265
- const h = c(l);
266
- let u = i, d;
267
- for (const E of h) {
268
- if (!u) break;
269
- if (typeof u == "function") {
270
- d = u;
271
- break;
272
- }
273
- if (u = u[E], typeof u == "function") {
274
- d = u;
275
- break;
276
- }
277
- }
278
- return d;
279
- }
280
- function g(i, l, h) {
281
- const u = S(h, l);
282
- return u ? u(i, l) : a(i);
283
- }
284
- function p(i) {
285
- return typeof i == "object" && i !== null;
286
- }
287
- function f(i, l, h, u, d) {
288
- if (i === l)
289
- return u;
290
- if (i === null || l === null || typeof i != "object" || typeof l != "object") {
291
- const $ = h || "(root)";
292
- return typeof i != typeof l ? (u.push({
293
- path: $,
294
- expected: g(i, $, d.transformExpected),
295
- actual: l,
296
- reason: "type-mismatch"
297
- }), u) : (d.includeValueMismatch && u.push({
298
- path: $,
299
- expected: g(i, $, d.transformExpected),
300
- actual: l,
301
- reason: "value-mismatch"
302
- }), u);
303
- }
304
- if (Array.isArray(i) || Array.isArray(l)) {
305
- if (!Array.isArray(i) || !Array.isArray(l)) {
306
- const A = h || "(root)";
307
- return u.push({
308
- path: A,
309
- expected: g(i, A, d.transformExpected),
310
- actual: l,
311
- reason: "type-mismatch"
312
- }), u;
313
- }
314
- const $ = i.length;
315
- for (let A = 0; A < $; A++) {
316
- const T = i[A], C = l[A], N = h === "" ? `[${A}]` : `${h}[${A}]`;
317
- f(T, C, N, u, d);
318
- }
319
- return u;
320
- }
321
- if (p(i) && p(l)) {
322
- const $ = Object.keys(i), A = Object.keys(l);
323
- for (const T of $) {
324
- const C = i[T], N = l[T], F = h ? `${h}.${T}` : T;
325
- if (!(T in l)) {
326
- u.push({
327
- path: F,
328
- expected: g(C, F, d.transformExpected),
329
- actual: void 0,
330
- reason: "missing-key"
331
- });
332
- continue;
333
- }
334
- f(C, N, F, u, d);
335
- }
336
- for (const T of A)
337
- if (!(T in i)) {
338
- const C = h ? `${h}.${T}` : T;
339
- u.push({
340
- path: C,
341
- expected: void 0,
342
- actual: l[T],
343
- reason: "extra-key"
344
- });
345
- }
346
- return u;
347
- }
348
- const E = h || "(root)";
349
- return typeof i != typeof l ? u.push({
350
- path: E,
351
- expected: g(i, E, d.transformExpected),
352
- actual: l,
353
- reason: "type-mismatch"
354
- }) : d.includeValueMismatch && u.push({
355
- path: E,
356
- expected: g(i, E, d.transformExpected),
357
- actual: l,
358
- reason: "value-mismatch"
359
- }), u;
360
- }
361
- function v(i, l, h, u) {
362
- const d = s(i, ...l), E = [], $ = l.length === 1 ? l[0] : "";
363
- return f(d, h, $, E, {
364
- transformExpected: u?.transformExpected
365
- }), {
366
- ok: E.length === 0,
367
- mismatches: E
368
- };
369
- }
370
- o.equalsAtPaths = v;
371
- function y(i) {
372
- if (i === void 0) return "undefined";
373
- if (typeof i == "string") return JSON.stringify(i);
374
- try {
375
- return JSON.stringify(i, null, 2);
376
- } catch {
377
- return String(i);
378
- }
379
- }
380
- function m(i) {
381
- switch (i.reason) {
382
- case "missing-key":
383
- return "Missing key";
384
- case "extra-key":
385
- return "Extra key";
386
- case "type-mismatch":
387
- return "Type mismatch";
388
- case "value-mismatch":
389
- return "Value mismatch";
390
- default:
391
- return i.reason;
392
- }
393
- }
394
- function b(i) {
395
- if (i.ok || i.mismatches.length === 0)
396
- return "No mismatches.";
397
- const l = [];
398
- for (const h of i.mismatches)
399
- l.push(
400
- `
401
- ● ${m(h)} at "${h.path}":`,
402
- ` expected: ${y(h.expected)}`,
403
- ` actual: ${y(h.actual)}`,
404
- ""
405
- // blank line between entries
406
- );
407
- return l.at(-1) === "" && l.pop(), l.join(`
408
- `);
409
- }
410
- o.formatMismatches = b;
411
- function O(i) {
412
- const l = b(i);
413
- l !== "No mismatches." && console.error(l);
414
- }
415
- o.printMismatches = O;
416
- function w(i, l, h) {
417
- const u = l.split(".");
418
- function d(E, $) {
419
- const A = u[$], T = Array.isArray(E) ? [...E] : { ...E };
420
- if ($ === u.length - 1) {
421
- let P = E && typeof E == "object" ? T : {};
422
- return P = {
423
- ...P,
424
- [A]: h
425
- }, P;
426
- }
427
- const C = E && typeof E == "object" ? E[A] : void 0, N = d(C ?? {}, $ + 1);
428
- let F = E && typeof E == "object" ? T : {};
429
- return F = {
430
- ...F,
431
- [A]: N
432
- }, F;
433
- }
434
- return d(i, 0);
435
- }
436
- function j(i, l, h) {
437
- const u = r(...l);
438
- if (u.length === 0) return i;
439
- let d = i;
440
- if (u.length === 1) {
441
- const E = u[0];
442
- return d = w(d, E, h), d;
443
- }
444
- for (const E of u) {
445
- const $ = e(h, E);
446
- d = w(d, E, $);
447
- }
448
- return d;
449
- }
450
- o.updateAt = j;
451
- })(V || (V = {}));
452
- function Se(o, e) {
453
- const t = o["~standard"].validate(e);
454
- if (t instanceof Promise)
455
- throw new TypeError("Schema validation must be synchronous", {
456
- cause: o
457
- });
458
- if (t.issues)
459
- throw new Error(JSON.stringify(t.issues, null, 2));
460
- return t.value;
461
- }
462
- function be(o, e) {
463
- return Object.keys(o).includes(e);
464
- }
465
- class re {
466
- #e;
467
- #t;
468
- get value() {
469
- return this.#e();
470
- }
471
- constructor(e) {
472
- const { getValue: t, setValue: r } = e;
473
- this.#e = t, this.#t = r;
474
- }
475
- handlePostUpdate(e) {
476
- this.#t(this.enrichValues(e));
477
- }
478
- createStepUpdaterFnImpl(e) {
479
- const { targetStep: t, updater: r, ctxData: s, fields: n = "all", debug: a } = e, c = new te({
480
- debug: a,
481
- prefix: (m) => `${m}:update${t}`
482
- });
483
- c.info(`${t} will be updated`), _(
484
- be(this.value, t),
485
- `[update]: The target step ${t} isn't a valid step. Please select a valid step`
486
- );
487
- const { [t]: S, ...g } = this.value;
488
- let p = { ...this.value }, f = k(p, [t]);
489
- if (s) {
490
- _(
491
- typeof s == "function",
492
- '[update]: "ctxData" must be a function'
493
- ), c.info('Custom "ctx" will be used');
494
- const m = s({ ctx: g });
495
- _(
496
- typeof m == "object" && Object.keys(m).length > 0,
497
- '[update]: "ctxData" must return an object with keys'
498
- ), c.info(
499
- `Custom "ctx" consists of the following keys: ${new Intl.ListFormat(
500
- "en",
501
- {
502
- style: "long",
503
- type: "conjunction"
504
- }
505
- ).format(Object.keys(m))}`
506
- ), f = {
507
- ...f,
508
- ...m
509
- };
510
- }
511
- const v = Ve(r, {
512
- ctx: f,
513
- update: this.createHelperFnInputUpdate([t])
514
- });
515
- if (c.info(`The updated data is ${JSON.stringify(v, null, 2)}`), !n) {
516
- _(
517
- typeof v == "object",
518
- '[update]: "updater" must be an object or a function that returns an object'
519
- );
520
- const m = Object.keys(this.value), b = Object.keys(v);
521
- _(b.length === m.length, () => {
522
- const i = m.filter(
523
- (h) => !b.includes(h)
524
- );
525
- return `[update]: "updater" is missing keys ${new Intl.ListFormat("en", {
526
- style: "long",
527
- type: "conjunction"
528
- }).format(
529
- i
530
- )}`;
531
- });
532
- const O = V.createDeep(this.value), { mismatches: w, ok: j } = V.equalsAtPaths(
533
- this.value,
534
- O,
535
- v
536
- );
537
- _(
538
- j && w.length === 0,
539
- `[update]: found value mismatches in ${V.printMismatches({
540
- mismatches: w,
541
- ok: j
542
- })}`
543
- ), c.info("The entire step will be updated"), p = {
544
- ...p,
545
- [t]: V.updateAt(p, O, v)
546
- }, this.handlePostUpdate(p), c.info(`The new value is: ${JSON.stringify(p, null, 2)}`);
547
- return;
548
- }
549
- const y = V.createDeep(S);
550
- if (Array.isArray(n)) {
551
- const m = M(y, n);
552
- _(
553
- m.status === "success",
554
- `[update]: Found errors with the provided fields
555
- ${m.status === "error" ? U(m.errors) : ""}`
556
- );
557
- const { mismatches: b, ok: O } = V.equalsAtPaths(
558
- S,
559
- n,
560
- v
561
- );
562
- _(
563
- O && b.length === 0,
564
- `[update]: found value mismatches in ${V.printMismatches({
565
- ok: O,
566
- mismatches: b
567
- })}`
568
- ), c.info(
569
- `The following fields will be updated: ${new Intl.ListFormat("en", {
570
- type: "conjunction",
571
- style: "long"
572
- }).format(n)}`
573
- ), p = {
574
- ...p,
575
- [t]: V.updateAt(S, n, v)
576
- }, this.handlePostUpdate(p), c.info(`The new value is: ${JSON.stringify(p, null, 2)}`);
577
- return;
578
- }
579
- if (typeof n == "object" && Object.keys(n).length > 0) {
580
- const m = V.createDeep(n), b = M(
581
- y,
582
- m
583
- );
584
- _(
585
- b.status === "success",
586
- `[update]: Found errors with the provided fields
587
- ${b.status === "error" ? U(b.errors) : ""}`
588
- );
589
- const { mismatches: O, ok: w } = V.equalsAtPaths(
590
- S,
591
- m,
592
- v
593
- );
594
- _(
595
- w && O.length === 0,
596
- `[update]: found value mismatches in ${V.printMismatches({
597
- ok: w,
598
- mismatches: O
599
- })}`
600
- ), p = {
601
- ...p,
602
- [t]: V.updateAt(
603
- S,
604
- m,
605
- v
606
- )
607
- }, c.info(
608
- `The following fields will be updated: ${new Intl.ListFormat("en", {
609
- type: "conjunction",
610
- style: "long"
611
- }).format(Object.keys(n))}`
612
- ), this.handlePostUpdate(p), c.info(`The new value is: ${JSON.stringify(p, null, 2)}`);
613
- return;
614
- }
615
- throw c.error('Unsupported value for the "fields" option'), new TypeError(
616
- `[update]: property "fields" must be set to one of the following: "all", an array of deep paths to update, or an object of paths. Was ${typeof r}`,
617
- { cause: r }
618
- );
619
- }
620
- createStepUpdaterFn(e) {
621
- return (t) => {
622
- this.createStepUpdaterFnImpl({ targetStep: e, ...t });
623
- };
624
- }
625
- update(e) {
626
- return this.createStepUpdaterFnImpl(e);
627
- }
628
- createHelperFnInputUpdate(e) {
629
- if (x.isAll(e)) {
630
- const r = ae(this.value).reduce((n, a) => (n[a] = this.createStepUpdaterFn(a), n), {});
631
- return Object.assign(
632
- this.update,
633
- r
634
- );
635
- }
636
- const t = Object.keys(this.value);
637
- if (x.isTuple(e, t)) {
638
- const r = e.reduce((n, a) => (n[a] = this.createStepUpdaterFn(a), n), {});
639
- return Object.assign(
640
- this.update,
641
- r
642
- );
643
- }
644
- if (x.isObject(e, t)) {
645
- const r = ae(e).reduce((n, a) => (n[a] = this.createStepUpdaterFn(a), n), {});
646
- return Object.assign(
647
- this.update,
648
- r
649
- );
650
- }
651
- throw new TypeError(`[update]: ${x.CATCH_ALL_MESSAGE}`);
652
- }
653
- createStepHelperFn(e) {
654
- return (t, r) => {
655
- const s = this.createHelperFnInputUpdate(
656
- e
657
- );
658
- if (typeof t == "function")
659
- return () => {
660
- const n = k(
661
- this.value,
662
- e
663
- );
664
- return t({
665
- ctx: n,
666
- update: s
667
- });
668
- };
669
- if (typeof t == "object")
670
- return (n) => {
671
- const a = k(
672
- this.value,
673
- e
674
- );
675
- if ("validator" in t) {
676
- _(
677
- typeof n == "object",
678
- "An input is expected since you provided a validator"
679
- ), Se(
680
- t.validator,
681
- n.data
682
- );
683
- let c = a;
684
- if (t.ctxData) {
685
- const S = e[0], { [S]: g, ...p } = this.value;
686
- c = {
687
- ...c,
688
- ...t.ctxData({ ctx: p })
689
- };
690
- }
691
- return r({
692
- ctx: c,
693
- update: s,
694
- ...n
695
- });
696
- }
697
- return r({
698
- ctx: a,
699
- update: s
700
- });
701
- };
702
- throw new Error(
703
- `The first argument must be a function or an object, (was ${typeof t})`
704
- );
705
- };
706
- }
707
- enrichValues(e, t) {
708
- let r = { ...e };
709
- for (const [s, n] of Object.entries(r)) {
710
- const [a] = [s], c = Number.parseInt(
711
- a.replace("step", "")
712
- );
713
- r[a] = {
714
- ...n,
715
- update: this.createStepUpdaterFn(a),
716
- createHelperFn: this.createStepHelperFn([a]),
717
- ...t?.(c)
718
- };
719
- }
720
- return r;
721
- }
722
- }
723
- var Y;
724
- ((o) => {
725
- function e(t, r, s = "defaultValue") {
726
- const [n, ...a] = t.split("."), c = `${n}.${s}`, S = a.length === 0 ? c : `${c}.${a.join(".")}`;
727
- return V.pickBy(r, S);
728
- }
729
- o.resolvedDeepPath = e;
730
- })(Y || (Y = {}));
731
- var x;
732
- ((o) => {
733
- o.CATCH_ALL_MESSAGE = 'The chosen steps must either be set to on of the following: "all", an array of steps (["step1", "step2", ...]), or an object containing the steps to chose ({ step1: true, step2: true, ...})';
734
- function e(s) {
735
- return !!(s && typeof s == "string" && s === "all");
736
- }
737
- o.isAll = e;
738
- function t(s, n) {
739
- return Array.isArray(s) ? n ? s.every((a) => n.includes(a)) : !0 : !1;
740
- }
741
- o.isTuple = t;
742
- function r(s, n) {
743
- if (!s)
744
- return !1;
745
- const a = Object.keys(s);
746
- return a.length === 0 || n && !a.every((c) => n.includes(c)) ? !1 : Object.entries(s).every(([c, S]) => S === !0);
747
- }
748
- o.isObject = r;
749
- })(x || (x = {}));
750
- const Ce = [
751
- "sentence",
752
- "title",
753
- "camel",
754
- "lower",
755
- "upper",
756
- "pascal",
757
- "snake",
758
- "screaming-snake",
759
- "flat",
760
- "kebab"
761
- ], ne = "title";
762
- function Fe(o, e) {
763
- const t = o.replace(/([a-z])([A-Z])/g, "$1 $2").replace(/[-_]+/g, " ").trim().split(/\s+/).map((r) => r.toLowerCase());
764
- switch (e) {
765
- case "sentence":
766
- return t[0].charAt(0).toUpperCase() + t[0].slice(1) + (t.length > 1 ? " " + t.slice(1).join(" ") : "");
767
- case "title":
768
- return t.map((r) => r.charAt(0).toUpperCase() + r.slice(1)).join(" ");
769
- case "camel":
770
- return t[0] + t.slice(1).map((r) => r.charAt(0).toUpperCase() + r.slice(1)).join("");
771
- case "pascal":
772
- return t.map((r) => r.charAt(0).toUpperCase() + r.slice(1)).join("");
773
- case "lower":
774
- return t.join(" ");
775
- case "upper":
776
- return t.join(" ").toUpperCase();
777
- case "snake":
778
- return t.join("_");
779
- case "screaming-snake":
780
- return t.join("_").toUpperCase();
781
- case "kebab":
782
- return t.join("-");
783
- case "flat":
784
- return t.join("");
785
- default:
786
- return o;
787
- }
788
- }
789
- function X(o) {
790
- return typeof o != "string" ? !1 : Ce.includes(o);
791
- }
792
- function ve(o, e = ne) {
793
- return X(o) ? o : e;
794
- }
795
- const ke = [
796
- "string",
797
- "string.phone",
798
- "string.email",
799
- "string.time",
800
- "number",
801
- "number.counter",
802
- "date",
803
- "dateTime",
804
- "boolean.switch"
805
- ], ue = "string";
806
- function xe(o) {
807
- return typeof o == "string" && ke.includes(o);
808
- }
809
- const W = '"window" in undefined. No storage is available', se = "MultiStepForm";
810
- class Ee {
811
- key;
812
- store;
813
- data;
814
- log;
815
- shouldRunActions;
816
- throwWhenUndefined;
817
- constructor(e) {
818
- const { key: t, data: r, store: s, throwWhenUndefined: n = !1 } = e;
819
- this.log = new te({
820
- prefix: se
821
- }), this.key = t, this.data = r, this.throwWhenUndefined = n, s ? (this.store = s, this.shouldRunActions = !0, typeof window > "u" && (this.shouldRunActions = !1, this.log.warn(W))) : typeof window < "u" ? (this.store = window.localStorage, this.shouldRunActions = !0) : (this.shouldRunActions = !1, this.log.warn(W));
822
- }
823
- throwOnEmptyStore() {
824
- this.throwWhenUndefined && _(this.store, () => this.shouldRunActions ? W : "No storage available");
825
- }
826
- resolveValue(e) {
827
- if (typeof e == "object")
828
- return e;
829
- if (typeof e == "function")
830
- return e(this.data);
831
- this.log.error(
832
- `The updater value must be a function or object. Was a ${typeof e}`,
833
- { throw: !0 }
834
- );
835
- }
836
- hasKey() {
837
- return this.store.getItem(this.key) !== null;
838
- }
839
- get() {
840
- if (this.throwOnEmptyStore(), !this.shouldRunActions)
841
- return;
842
- const e = this.store.getItem(this.key);
843
- if (e)
844
- return JSON.parse(e);
845
- }
846
- remove() {
847
- this.throwOnEmptyStore(), this.shouldRunActions && this.store.removeItem(this.key);
848
- }
849
- add(e) {
850
- if (this.throwOnEmptyStore(), !this.shouldRunActions)
851
- return;
852
- const t = JSON.stringify(this.resolveValue(e));
853
- this.store.setItem(this.key, t);
854
- }
855
- }
856
- class _e {
857
- listeners = /* @__PURE__ */ new Set();
858
- constructor() {
859
- this.subscribe = this.subscribe.bind(this);
860
- }
861
- subscribe(e) {
862
- return this.listeners.add(e), this.onSubscribe(), () => {
863
- this.listeners.delete(e), this.onUnsubscribe();
864
- };
865
- }
866
- hasListeners() {
867
- return this.listeners.size > 0;
868
- }
869
- onSubscribe() {
870
- }
871
- onUnsubscribe() {
872
- }
873
- }
874
- const Le = [
875
- "string",
876
- "number",
877
- "array.string",
878
- "array.string.untyped"
879
- ], we = /^step\d+$/i;
880
- function ce(o, e) {
881
- if (typeof o != "object" || o === null) return !1;
882
- for (const t of Object.keys(e)) {
883
- if (!(t in o)) return !1;
884
- const r = e[t], s = o[t];
885
- if (!r(s)) return !1;
886
- }
887
- return !0;
888
- }
889
- function le(o, e, t) {
890
- return o ?? Fe(e, t);
891
- }
892
- function Ne(o) {
893
- const e = {}, { fields: t, defaultCasing: r, validateFields: s } = o;
894
- for (const [n, a] of Object.entries(t)) {
895
- _(
896
- typeof n == "string",
897
- `Each key for the "fields" option must be a string. Key ${n} was a ${typeof n}`
898
- ), _(
899
- typeof a == "object",
900
- `The value for key ${n} must be an object. Was ${typeof a}`
901
- );
902
- const {
903
- defaultValue: c,
904
- label: S,
905
- nameTransformCasing: g,
906
- type: p = ue
907
- } = a;
908
- if (s)
909
- e[n] = c;
910
- else {
911
- const f = g ?? r;
912
- e[n] = {
913
- ...e[n],
914
- nameTransformCasing: f,
915
- type: p,
916
- defaultValue: c,
917
- label: le(S, n, f)
918
- // TODO add more fields here
919
- };
920
- }
921
- }
922
- if (s) {
923
- const n = Se(
924
- s,
925
- e
926
- );
927
- _(
928
- typeof n == "object",
929
- `The result of the validated fields must be an object, was (${typeof n}). This is probably an internal error, so open up an issue about it`
930
- ), _(
931
- !!n,
932
- "The result of the validated fields must be defined. This is probably an internal error, so open up an issue about it"
933
- );
934
- for (const [a, c] of Object.entries(n)) {
935
- const S = t[a];
936
- _(
937
- S,
938
- `No field found in the fields config for "${a}"`
939
- );
940
- const {
941
- label: g,
942
- type: p = ue,
943
- nameTransformCasing: f
944
- } = S, v = f ?? r;
945
- e[a] = {
946
- ...e[a],
947
- nameTransformCasing: v,
948
- type: p,
949
- defaultValue: c,
950
- label: le(g, a, v)
951
- };
952
- }
953
- }
954
- return e;
955
- }
956
- function Oe(o) {
957
- const e = {};
958
- _(!!o, "The steps config must be defined", TypeError), _(
959
- typeof o == "object",
960
- `The steps config must be an object, was (${typeof o})`,
961
- TypeError
962
- );
963
- for (const [t, r] of Object.entries(o)) {
964
- _(
965
- typeof t == "string",
966
- `Each key for the step config must be a string. Key "${t}" was ${typeof t} `,
967
- TypeError
968
- ), _(
969
- we.test(t),
970
- `The key "${t}" isn't formatted properly. Each key in the step config must be the following format: "step{number}"`
971
- );
972
- const s = t, {
973
- fields: n,
974
- title: a,
975
- nameTransformCasing: c = ne,
976
- description: S,
977
- validateFields: g
978
- } = r, p = s.toString().replace("step", "");
979
- _(
980
- n,
981
- `Missing fields for step ${p} (${String(s)})`,
982
- TypeError
983
- ), _(
984
- typeof n == "object",
985
- "Fields must be an object",
986
- TypeError
987
- ), _(
988
- Object.keys(n).length > 0,
989
- `The fields config for step ${p} (${String(
990
- s
991
- )}) is empty. Please add a field`
992
- ), _(
993
- typeof n == "object",
994
- `The "fields" property must be an object. Was ${typeof n}`
995
- );
996
- const f = Ne({
997
- defaultCasing: c,
998
- fields: n,
999
- validateFields: g
1000
- });
1001
- e[s] = {
1002
- ...e[s],
1003
- title: a,
1004
- nameTransformCasing: c,
1005
- // Only add the description if it's defined
1006
- ...typeof S == "string" ? { description: S } : {},
1007
- fields: f
1008
- };
1009
- }
1010
- return e;
1011
- }
1012
- let oe = class extends _e {
1013
- /**
1014
- * The original config before any validation or transformations have been applied.
1015
- */
1016
- original;
1017
- /**
1018
- * The resolved step values.
1019
- */
1020
- value;
1021
- steps;
1022
- defaultNameTransformationCasing;
1023
- //@ts-ignore
1024
- firstStep;
1025
- lastStep;
1026
- stepNumbers;
1027
- storage;
1028
- #e;
1029
- constructor(e) {
1030
- super();
1031
- const { steps: t, nameTransformCasing: r, storage: s } = e;
1032
- this.defaultNameTransformationCasing = ve(
1033
- r
1034
- ), this.original = t, this.value = Oe(this.original), this.storage = new Ee({
1035
- data: this.value,
1036
- key: s?.key ?? se,
1037
- store: s?.store,
1038
- throwWhenUndefined: s?.throwWhenUndefined ?? !1
1039
- }), this.#e = new re({
1040
- getValue: () => this.value,
1041
- setValue: (n) => this.handlePostUpdate(n)
1042
- }), this.value = this.#e.enrichValues(this.value), this.stepNumbers = Object.keys(this.value).map(
1043
- (n) => Number.parseInt(n.replace("step", ""))
1044
- ), this.firstStep = this.first(), this.lastStep = this.last(), this.steps = {
1045
- first: this.firstStep.step,
1046
- last: this.lastStep.step,
1047
- value: this.stepNumbers,
1048
- as: (n) => {
1049
- if (_(
1050
- typeof n == "string",
1051
- `The type of the target transformation type must be a string, was ${typeof n}`
1052
- ), n === "string")
1053
- return this.stepNumbers.map((a) => `'${a}'`).join(" | ");
1054
- if (n === "number")
1055
- return this.stepNumbers.join(" | ");
1056
- if (n.includes("array.string"))
1057
- return this.stepNumbers.map((a) => `${a}`);
1058
- throw new Error(
1059
- `Transformation type "${n}" is not supported. Available transformations include: ${Le.map(
1060
- (a) => `"${a}"`
1061
- ).join(", ")}`
1062
- );
1063
- },
1064
- isValidStepNumber: (n) => this.stepNumbers.includes(n),
1065
- isValidStepKey: (n) => be(this.value, n)
1066
- }, this.sync();
1067
- }
1068
- /**
1069
- * @internal
1070
- */
1071
- __getStorage() {
1072
- return this.storage;
1073
- }
1074
- getSnapshot() {
1075
- return this;
1076
- }
1077
- /**
1078
- * Syncs the values from storage to {@linkcode value}.
1079
- */
1080
- sync() {
1081
- const e = this.__getStorage().get();
1082
- if (e) {
1083
- const t = this.#e.enrichValues(e);
1084
- this.value = { ...t };
1085
- }
1086
- }
1087
- notify() {
1088
- for (const e of this.listeners)
1089
- e({
1090
- defaultNameTransformationCasing: this.defaultNameTransformationCasing,
1091
- original: this.original,
1092
- steps: this.steps,
1093
- value: this.value
1094
- });
1095
- }
1096
- /**
1097
- * Gets the data for a specific step.
1098
- * @param options The options for getting the step data.
1099
- * @returns The step data for the target step.
1100
- */
1101
- get(e) {
1102
- return ee(this.value)(e);
1103
- }
1104
- /**
1105
- * Gets the data for the first step.
1106
- * @returns The data for the first step.
1107
- */
1108
- first() {
1109
- const e = Math.min(...this.stepNumbers);
1110
- return this.get({ step: e });
1111
- }
1112
- /**
1113
- * Gets the data for the last step.
1114
- * @returns The data for the last step.
1115
- */
1116
- last() {
1117
- const e = Math.max(...this.stepNumbers);
1118
- return this.get({ step: e });
1119
- }
1120
- handlePostUpdate(e) {
1121
- this.value = { ...e }, this.__getStorage().add(this.value), this.sync(), this.notify();
1122
- }
1123
- update(e) {
1124
- this.#e.update(e);
1125
- }
1126
- // Implementation
1127
- createHelperFn(e, t) {
1128
- const { stepData: r, ...s } = e;
1129
- return this.#e.createStepHelperFn(r)(s, t);
1130
- }
1131
- /**
1132
- * Validates that a given object is the proper shape for step data.
1133
- * @param value
1134
- */
1135
- static hasData(e, t) {
1136
- return e === null || typeof e != "object" ? !1 : ce(e, {
1137
- title: (r) => typeof r == "string",
1138
- fields: (r) => {
1139
- if (r === null || typeof r != "object")
1140
- return !1;
1141
- for (const s of Object.keys(r)) {
1142
- if (typeof s != "string" || !(s in r))
1143
- return !1;
1144
- const n = r[s];
1145
- if (n === null || typeof n != "object" || !ce(n, {
1146
- defaultValue: (c) => c !== "undefined" && c !== null,
1147
- label: (c) => typeof c == "string" || typeof c == "boolean" && !c,
1148
- nameTransformCasing: X,
1149
- type: xe
1150
- }))
1151
- return !1;
1152
- }
1153
- return !0;
1154
- },
1155
- createHelperFn: (r) => typeof r == "function",
1156
- // update: (v): v is GetCurrentStep<resolvedStep, stepNumbers>['update'] =>
1157
- // typeof v === 'function',
1158
- nameTransformCasing: X,
1159
- ...t?.optionalKeysToCheck
1160
- });
1161
- }
1162
- /**
1163
- * Gets the value of a given field for a given step.
1164
- * @param step The step to get the value from.
1165
- * @param field The field to get the value from.
1166
- * @returns The value of the {@linkcode field}.
1167
- */
1168
- getValue(e, t) {
1169
- const r = this.value[e], s = `Unable to get the value for "${String(
1170
- e
1171
- )}.fields.${String(t)}"`;
1172
- _("fields" in r, s), _(
1173
- typeof r.fields == "object",
1174
- `${s} because "fields" is not an object. This shouldn't be the case, so please open an issue`
1175
- );
1176
- const n = r.fields;
1177
- return Y.resolvedDeepPath(t, n);
1178
- }
1179
- }, De = class extends _e {
1180
- defaultNameTransformationCasing;
1181
- stepSchema;
1182
- storage;
1183
- mountCount = 0;
1184
- constructor(e) {
1185
- super();
1186
- const { steps: t, nameTransformCasing: r, storage: s } = e;
1187
- this.defaultNameTransformationCasing = ve(
1188
- r
1189
- ), this.stepSchema = new oe({
1190
- steps: t,
1191
- nameTransformCasing: this.defaultNameTransformationCasing
1192
- }), this.storage = new Ee({
1193
- key: s?.key ?? se,
1194
- data: this.stepSchema.value,
1195
- store: s?.store,
1196
- throwWhenUndefined: s?.throwWhenUndefined ?? !1
1197
- }), this.stepSchema.subscribe(() => {
1198
- this.notify();
1199
- });
1200
- }
1201
- getSnapshot() {
1202
- return this;
1203
- }
1204
- mount() {
1205
- return this.mountCount++, this.mountCount === 1 && this.onMount(), () => {
1206
- this.unmount();
1207
- };
1208
- }
1209
- unmount() {
1210
- this.mountCount = Math.max(0, this.mountCount - 1), this.mountCount === 0 && this.onUnmount();
1211
- }
1212
- isMounted() {
1213
- return this.mountCount > 0;
1214
- }
1215
- onMount() {
1216
- }
1217
- onUnmount() {
1218
- }
1219
- notify() {
1220
- for (const e of this.listeners)
1221
- e({
1222
- nameTransformCasing: this.defaultNameTransformationCasing,
1223
- storage: {
1224
- key: this.storage.key,
1225
- store: this.storage.store
1226
- },
1227
- steps: this.stepSchema.original
1228
- });
1229
- }
1230
- };
1231
- function Ie(o) {
1232
- return (t) => {
1233
- const { children: r, name: s } = t, n = o(s);
1234
- return r(n);
1235
- };
1236
- }
1237
- var L;
1238
- ((o) => {
1239
- o.DEFAULT_FORM_ALIAS = "Form";
1240
- function e(r, s) {
1241
- if (Array.isArray(r)) {
1242
- if (s === "all")
1243
- return !0;
1244
- if (typeof s == "object" && !Array.isArray(s))
1245
- return Object.keys(s).some(
1246
- (n) => r.includes(n)
1247
- );
1248
- if (Array.isArray(s))
1249
- return s.some((n) => r.includes(n));
1250
- }
1251
- return !1;
1252
- }
1253
- o.isFormAvailable = e;
1254
- function t(r) {
1255
- return (s) => /* @__PURE__ */ Te("form", { id: r, ...s });
1256
- }
1257
- o.createDefaultForm = t;
1258
- })(L || (L = {}));
1259
- var Z;
1260
- ((o) => {
1261
- o.DEFAULT_FORM_INSTANCE_ALIAS = "form";
1262
- })(Z || (Z = {}));
1263
- function Me(o, e) {
1264
- _(
1265
- e in o,
1266
- `The target step ${e} is not a valid step key`
1267
- );
1268
- const t = o[e];
1269
- _("fields" in t, `No "fields" were found for ${e}`);
1270
- let r = {};
1271
- for (const [s, n] of Object.entries(
1272
- t.fields
1273
- ))
1274
- r = {
1275
- ...r,
1276
- [s]: n.defaultValue
1277
- };
1278
- return r;
1279
- }
1280
- class Ue extends oe {
1281
- // @ts-ignore type doesn't match `MultiStepFormSchemaBase.value`
1282
- value;
1283
- #e;
1284
- constructor(e) {
1285
- const { form: t, ...r } = e;
1286
- super(r), this.value = Oe(this.original), this.#e = new re({
1287
- getValue: () => this.value,
1288
- setValue: (s) => this.handlePostUpdate(s)
1289
- }), this.sync(), this.value = this.#e.enrichValues(this.value, (s) => {
1290
- const n = `step${s}`, a = [n], c = t?.id ?? n, S = k(this.value, a);
1291
- return {
1292
- createComponent: this.createStepSpecificComponentFactory(
1293
- a,
1294
- {
1295
- isStepSpecific: !0,
1296
- defaultId: c,
1297
- form: t
1298
- },
1299
- S
1300
- )
1301
- };
1302
- });
1303
- }
1304
- createFormComponent(e, t) {
1305
- const { render: r, enabledForSteps: s = "all", id: n = t } = e, a = {
1306
- id: n,
1307
- steps: k(this.value, s)
1308
- };
1309
- return (c) => r(a, c);
1310
- }
1311
- createStepSpecificComponentImpl(e, t, r, s = {}) {
1312
- return (n) => ((a) => {
1313
- const c = {};
1314
- for (const [w, j] of Object.entries(s))
1315
- if (typeof j == "function")
1316
- try {
1317
- const i = j();
1318
- c[w] = i;
1319
- } catch (i) {
1320
- const l = i instanceof Error ? i.message : String(i);
1321
- throw new Error(
1322
- `[multi-step-form] Error calling hook "${w}" in useFormInstance.render: ${l}
1
+ import { MultiStepFormStepSchema, StepSpecificComponent, createDefaultValues } from "./step-schema.mjs";
2
+ import { MultiStepFormSchema, createMultiStepFormSchema } from "./schema.mjs";
3
+ import { useMultiStepFormData } from "./hooks/use-multi-step-form-data.mjs";
4
+ import { createMultiStepFormContext } from "./create-context.mjs";
1323
5
 
1324
- This usually means:
1325
- 1. The hook is being called outside of a React component
1326
- 2. The hook has invalid dependencies or configuration
1327
- 3. There's an error in your hook implementation
1328
-
1329
- Original error: ${l}`,
1330
- { cause: i }
1331
- );
1332
- }
1333
- else
1334
- c[w] = j;
1335
- const { defaultId: S, form: g } = t, { ctx: p } = r, [f] = e;
1336
- _(
1337
- this.steps.isValidStepKey(f),
1338
- `[createComponent]: the target step ${f} is invalid. Note, this error shouldn't appear as the target step should always be valid. If you see this error, please open an issue.`
1339
- );
1340
- const v = Number.parseInt(f.replace("step", ""));
1341
- _(
1342
- !Number.isNaN(v),
1343
- `[${f}:"createComponent"]: an error occurred while extracting the number`
1344
- );
1345
- const y = this.value[f];
1346
- _(
1347
- "fields" in y,
1348
- `[${f}:createComponent]: unable to find the "fields" for the current step`
1349
- ), _(
1350
- typeof y.fields == "object",
1351
- `[${f}:createComponent]: the "fields" property must be an object, was ${typeof y.fields}`
1352
- );
1353
- const m = this.#e.createStepUpdaterFn(f), b = Ie((w) => {
1354
- _(typeof w == "string", () => {
1355
- const u = new Intl.ListFormat("en", {
1356
- style: "long",
1357
- type: "disjunction"
1358
- });
1359
- return `[${f}:Field]: the "name" prop must be a string and a valid field for ${f}. Available fields include ${u.format(
1360
- Object.keys(y.fields)
1361
- )}`;
1362
- }), _(
1363
- w in y.fields,
1364
- `[${f}:Field]: the field "${w}" doesn't exist for the current step`
1365
- ), _(
1366
- "update" in y,
1367
- `[${f}:Field]: No "update" function was found`
1368
- );
1369
- const j = this.getValue(f, w), { label: i, nameTransformCasing: l, type: h } = y.fields[w];
1370
- return {
1371
- defaultValue: j,
1372
- label: i,
1373
- nameTransformCasing: l,
1374
- type: h,
1375
- name: w,
1376
- onInputChange: (u) => {
1377
- let d;
1378
- if (typeof u == "function") {
1379
- const E = this.getValue(
1380
- f,
1381
- w
1382
- );
1383
- d = u(E);
1384
- } else
1385
- d = u;
1386
- this.update({
1387
- targetStep: f,
1388
- updater: d,
1389
- fields: [`fields.${w}.defaultValue`]
1390
- });
1391
- }
1392
- };
1393
- });
1394
- let O = {
1395
- ctx: p,
1396
- onInputChange: m,
1397
- Field: b,
1398
- ...c
1399
- };
1400
- if (g) {
1401
- const {
1402
- alias: w = L.DEFAULT_FORM_ALIAS,
1403
- ...j
1404
- } = g, i = j.enabledForSteps ?? "all";
1405
- return L.isFormAvailable(
1406
- e,
1407
- i
1408
- ) && (O = {
1409
- ...O,
1410
- [w]: this.createFormComponent(j, S)
1411
- }), n(O, a);
1412
- }
1413
- return n(
1414
- {
1415
- ...O,
1416
- [L.DEFAULT_FORM_ALIAS]: L.createDefaultForm(S)
1417
- },
1418
- a
1419
- );
1420
- });
1421
- }
1422
- createStepSpecificComponentFactory(e, t, r) {
1423
- const s = this.createStepSpecificComponentImpl.bind(this), n = this.createDefaultValues.bind(this);
1424
- debugger;
1425
- const a = this.value, c = e[0], S = this.#e.createStepUpdaterFn(c).bind(this.#e);
1426
- function g(p, f) {
1427
- function v(m) {
1428
- return m ? { ctx: { ...r, ...m } } : { ctx: r };
1429
- }
1430
- function y() {
1431
- return _(
1432
- typeof p == "function",
1433
- "The first argument must be a function"
1434
- ), s(
1435
- e,
1436
- t,
1437
- v(void 0)
1438
- )(p);
1439
- }
1440
- if (typeof p == "object") {
1441
- const { useFormInstance: m, ctxData: b, debug: O } = p, w = new te({
1442
- debug: O,
1443
- prefix(l) {
1444
- return `${l}-${c}-createComponent`;
1445
- }
1446
- });
1447
- w.info("First argument is an object");
1448
- const { [c]: j, ...i } = a;
1449
- if (_(
1450
- typeof f == "function",
1451
- "The second argument must be a function"
1452
- ), m) {
1453
- const {
1454
- render: l,
1455
- alias: h = Z.DEFAULT_FORM_INSTANCE_ALIAS
1456
- } = m, [u] = e, d = n(u), E = b ? {
1457
- ...r,
1458
- ...b({ ctx: i })
1459
- } : r, $ = { ctx: E, defaultValues: d };
1460
- return s(
1461
- e,
1462
- t,
1463
- E,
1464
- {
1465
- [h]: () => l($)
1466
- }
1467
- )(f);
1468
- }
1469
- if (b) {
1470
- w.info('Option "ctxData" is defined'), _(
1471
- typeof b == "function",
1472
- 'Option "ctxData" must be a function'
1473
- );
1474
- const l = b({ ctx: i });
1475
- w.info(
1476
- `Addition context is: ${JSON.stringify(l, null, 2)}`
1477
- );
1478
- const h = {
1479
- ...r,
1480
- ...l
1481
- };
1482
- return w.info(
1483
- `Resolved context is: ${JSON.stringify(h, null, 2)}`
1484
- ), s(e, t, {
1485
- ctx: h,
1486
- update: S
1487
- })(f);
1488
- }
1489
- return y();
1490
- }
1491
- return y();
1492
- }
1493
- return g;
1494
- }
1495
- /**
1496
- * A helper function to create a component for a specific step.
1497
- * @param options The options for creating the step specific component.
1498
- * @param fn A callback that is used for accessing the target step's data and defining
1499
- * any props that the component should have. This function must return a valid `JSX` element.
1500
- * @returns The created component for the step.
1501
- */
1502
- createComponent(e, t) {
1503
- const { stepData: r } = e, s = k(
1504
- this.value,
1505
- r
1506
- );
1507
- return ((n) => t(s, n));
1508
- }
1509
- createDefaultValues(e) {
1510
- return Me(this.value, e);
1511
- }
1512
- }
1513
- class Re extends De {
1514
- // @ts-ignore
1515
- stepSchema;
1516
- #e;
1517
- constructor(e) {
1518
- const { nameTransformCasing: t = ne, storage: r, ...s } = e, n = { nameTransformCasing: t, storage: r, ...s };
1519
- super(n), this.stepSchema = new Ue(n), this.#e = new re({
1520
- getValue: () => this.stepSchema.value,
1521
- setValue: (a) => {
1522
- this.stepSchema.value = { ...a }, this.storage.add(a), this.notify();
1523
- }
1524
- });
1525
- }
1526
- createComponent(e, t) {
1527
- const { stepData: r } = e, s = k(
1528
- this.stepSchema.value,
1529
- r
1530
- );
1531
- return ((n) => t(
1532
- {
1533
- ctx: s,
1534
- update: this.#e.createHelperFnInputUpdate(r)
1535
- },
1536
- n
1537
- ));
1538
- }
1539
- }
1540
- function Xe(o) {
1541
- return new Re(o);
1542
- }
1543
- var D = { exports: {} }, H = {}, I = { exports: {} }, B = {};
1544
- /**
1545
- * @license React
1546
- * use-sync-external-store-shim.production.js
1547
- *
1548
- * Copyright (c) Meta Platforms, Inc. and affiliates.
1549
- *
1550
- * This source code is licensed under the MIT license found in the
1551
- * LICENSE file in the root directory of this source tree.
1552
- */
1553
- var fe;
1554
- function Pe() {
1555
- if (fe) return B;
1556
- fe = 1;
1557
- var o = R;
1558
- function e(f, v) {
1559
- return f === v && (f !== 0 || 1 / f === 1 / v) || f !== f && v !== v;
1560
- }
1561
- var t = typeof Object.is == "function" ? Object.is : e, r = o.useState, s = o.useEffect, n = o.useLayoutEffect, a = o.useDebugValue;
1562
- function c(f, v) {
1563
- var y = v(), m = r({ inst: { value: y, getSnapshot: v } }), b = m[0].inst, O = m[1];
1564
- return n(
1565
- function() {
1566
- b.value = y, b.getSnapshot = v, S(b) && O({ inst: b });
1567
- },
1568
- [f, y, v]
1569
- ), s(
1570
- function() {
1571
- return S(b) && O({ inst: b }), f(function() {
1572
- S(b) && O({ inst: b });
1573
- });
1574
- },
1575
- [f]
1576
- ), a(y), y;
1577
- }
1578
- function S(f) {
1579
- var v = f.getSnapshot;
1580
- f = f.value;
1581
- try {
1582
- var y = v();
1583
- return !t(f, y);
1584
- } catch {
1585
- return !0;
1586
- }
1587
- }
1588
- function g(f, v) {
1589
- return v();
1590
- }
1591
- var p = typeof window > "u" || typeof window.document > "u" || typeof window.document.createElement > "u" ? g : c;
1592
- return B.useSyncExternalStore = o.useSyncExternalStore !== void 0 ? o.useSyncExternalStore : p, B;
1593
- }
1594
- var z = {};
1595
- /**
1596
- * @license React
1597
- * use-sync-external-store-shim.development.js
1598
- *
1599
- * Copyright (c) Meta Platforms, Inc. and affiliates.
1600
- *
1601
- * This source code is licensed under the MIT license found in the
1602
- * LICENSE file in the root directory of this source tree.
1603
- */
1604
- var pe;
1605
- function Ke() {
1606
- return pe || (pe = 1, process.env.NODE_ENV !== "production" && (function() {
1607
- function o(y, m) {
1608
- return y === m && (y !== 0 || 1 / y === 1 / m) || y !== y && m !== m;
1609
- }
1610
- function e(y, m) {
1611
- p || s.startTransition === void 0 || (p = !0, console.error(
1612
- "You are using an outdated, pre-release alpha of React 18 that does not support useSyncExternalStore. The use-sync-external-store shim will not work correctly. Upgrade to a newer pre-release."
1613
- ));
1614
- var b = m();
1615
- if (!f) {
1616
- var O = m();
1617
- n(b, O) || (console.error(
1618
- "The result of getSnapshot should be cached to avoid an infinite loop"
1619
- ), f = !0);
1620
- }
1621
- O = a({
1622
- inst: { value: b, getSnapshot: m }
1623
- });
1624
- var w = O[0].inst, j = O[1];
1625
- return S(
1626
- function() {
1627
- w.value = b, w.getSnapshot = m, t(w) && j({ inst: w });
1628
- },
1629
- [y, b, m]
1630
- ), c(
1631
- function() {
1632
- return t(w) && j({ inst: w }), y(function() {
1633
- t(w) && j({ inst: w });
1634
- });
1635
- },
1636
- [y]
1637
- ), g(b), b;
1638
- }
1639
- function t(y) {
1640
- var m = y.getSnapshot;
1641
- y = y.value;
1642
- try {
1643
- var b = m();
1644
- return !n(y, b);
1645
- } catch {
1646
- return !0;
1647
- }
1648
- }
1649
- function r(y, m) {
1650
- return m();
1651
- }
1652
- typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ < "u" && typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart == "function" && __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart(Error());
1653
- var s = R, n = typeof Object.is == "function" ? Object.is : o, a = s.useState, c = s.useEffect, S = s.useLayoutEffect, g = s.useDebugValue, p = !1, f = !1, v = typeof window > "u" || typeof window.document > "u" || typeof window.document.createElement > "u" ? r : e;
1654
- z.useSyncExternalStore = s.useSyncExternalStore !== void 0 ? s.useSyncExternalStore : v, typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ < "u" && typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop == "function" && __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop(Error());
1655
- })()), z;
1656
- }
1657
- var de;
1658
- function $e() {
1659
- return de || (de = 1, process.env.NODE_ENV === "production" ? I.exports = Pe() : I.exports = Ke()), I.exports;
1660
- }
1661
- /**
1662
- * @license React
1663
- * use-sync-external-store-shim/with-selector.production.js
1664
- *
1665
- * Copyright (c) Meta Platforms, Inc. and affiliates.
1666
- *
1667
- * This source code is licensed under the MIT license found in the
1668
- * LICENSE file in the root directory of this source tree.
1669
- */
1670
- var he;
1671
- function Ge() {
1672
- if (he) return H;
1673
- he = 1;
1674
- var o = R, e = $e();
1675
- function t(g, p) {
1676
- return g === p && (g !== 0 || 1 / g === 1 / p) || g !== g && p !== p;
1677
- }
1678
- var r = typeof Object.is == "function" ? Object.is : t, s = e.useSyncExternalStore, n = o.useRef, a = o.useEffect, c = o.useMemo, S = o.useDebugValue;
1679
- return H.useSyncExternalStoreWithSelector = function(g, p, f, v, y) {
1680
- var m = n(null);
1681
- if (m.current === null) {
1682
- var b = { hasValue: !1, value: null };
1683
- m.current = b;
1684
- } else b = m.current;
1685
- m = c(
1686
- function() {
1687
- function w(u) {
1688
- if (!j) {
1689
- if (j = !0, i = u, u = v(u), y !== void 0 && b.hasValue) {
1690
- var d = b.value;
1691
- if (y(d, u))
1692
- return l = d;
1693
- }
1694
- return l = u;
1695
- }
1696
- if (d = l, r(i, u)) return d;
1697
- var E = v(u);
1698
- return y !== void 0 && y(d, E) ? (i = u, d) : (i = u, l = E);
1699
- }
1700
- var j = !1, i, l, h = f === void 0 ? null : f;
1701
- return [
1702
- function() {
1703
- return w(p());
1704
- },
1705
- h === null ? void 0 : function() {
1706
- return w(h());
1707
- }
1708
- ];
1709
- },
1710
- [p, f, v, y]
1711
- );
1712
- var O = s(g, m[0], m[1]);
1713
- return a(
1714
- function() {
1715
- b.hasValue = !0, b.value = O;
1716
- },
1717
- [O]
1718
- ), S(O), O;
1719
- }, H;
1720
- }
1721
- var J = {};
1722
- /**
1723
- * @license React
1724
- * use-sync-external-store-shim/with-selector.development.js
1725
- *
1726
- * Copyright (c) Meta Platforms, Inc. and affiliates.
1727
- *
1728
- * This source code is licensed under the MIT license found in the
1729
- * LICENSE file in the root directory of this source tree.
1730
- */
1731
- var me;
1732
- function We() {
1733
- return me || (me = 1, process.env.NODE_ENV !== "production" && (function() {
1734
- function o(g, p) {
1735
- return g === p && (g !== 0 || 1 / g === 1 / p) || g !== g && p !== p;
1736
- }
1737
- typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ < "u" && typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart == "function" && __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart(Error());
1738
- var e = R, t = $e(), r = typeof Object.is == "function" ? Object.is : o, s = t.useSyncExternalStore, n = e.useRef, a = e.useEffect, c = e.useMemo, S = e.useDebugValue;
1739
- J.useSyncExternalStoreWithSelector = function(g, p, f, v, y) {
1740
- var m = n(null);
1741
- if (m.current === null) {
1742
- var b = { hasValue: !1, value: null };
1743
- m.current = b;
1744
- } else b = m.current;
1745
- m = c(
1746
- function() {
1747
- function w(u) {
1748
- if (!j) {
1749
- if (j = !0, i = u, u = v(u), y !== void 0 && b.hasValue) {
1750
- var d = b.value;
1751
- if (y(d, u))
1752
- return l = d;
1753
- }
1754
- return l = u;
1755
- }
1756
- if (d = l, r(i, u))
1757
- return d;
1758
- var E = v(u);
1759
- return y !== void 0 && y(d, E) ? (i = u, d) : (i = u, l = E);
1760
- }
1761
- var j = !1, i, l, h = f === void 0 ? null : f;
1762
- return [
1763
- function() {
1764
- return w(p());
1765
- },
1766
- h === null ? void 0 : function() {
1767
- return w(h());
1768
- }
1769
- ];
1770
- },
1771
- [p, f, v, y]
1772
- );
1773
- var O = s(g, m[0], m[1]);
1774
- return a(
1775
- function() {
1776
- b.hasValue = !0, b.value = O;
1777
- },
1778
- [O]
1779
- ), S(O), O;
1780
- }, typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ < "u" && typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop == "function" && __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop(Error());
1781
- })()), J;
1782
- }
1783
- var ye;
1784
- function He() {
1785
- return ye || (ye = 1, process.env.NODE_ENV === "production" ? D.exports = Ge() : D.exports = We()), D.exports;
1786
- }
1787
- var Be = He();
1788
- function Q(o, e) {
1789
- _(
1790
- typeof e == "string",
1791
- `The target step must be a string, was ${typeof e}`
1792
- );
1793
- const { as: t, isValidStepNumber: r } = o.stepSchema.steps, n = new Intl.ListFormat("en", {
1794
- type: "disjunction",
1795
- style: "long"
1796
- }).format(t("array.string.untyped"));
1797
- _(
1798
- we.test(e),
1799
- `The target step must match the following format: "step{number}". Available steps are ${n}`
1800
- );
1801
- const a = Number.parseInt(e.replace("step", ""));
1802
- return _(
1803
- r(a),
1804
- `The step number "${a}" is not a valid step number. Valid step numbers include ${n}`,
1805
- TypeError
1806
- ), a;
1807
- }
1808
- function je(o) {
1809
- function e(t) {
1810
- return Be.useSyncExternalStoreWithSelector(
1811
- o.subscribe,
1812
- () => o.getSnapshot(),
1813
- () => o.getSnapshot(),
1814
- (r) => {
1815
- if (typeof t == "object") {
1816
- const s = Q(
1817
- r,
1818
- t.targetStep
1819
- );
1820
- return r.stepSchema.get({ step: s }).data;
1821
- }
1822
- return typeof t == "function" ? t(r) : r;
1823
- }
1824
- );
1825
- }
1826
- return e;
1827
- }
1828
- function Ze(o, e) {
1829
- const t = je(o);
1830
- return typeof e == "object" || typeof e == "function" ? t(e) : t();
1831
- }
1832
- function ge(o) {
1833
- return function(e) {
1834
- return ((t) => e(o, t));
1835
- };
1836
- }
1837
- function Qe(o) {
1838
- const e = je(o);
1839
- function t(c) {
1840
- const { targetStep: S, notFoundMessage: g, isDataGuaranteed: p } = c, f = e({
1841
- targetStep: S
1842
- }), v = g ? a({ targetStep: S }, g) : (y) => /* @__PURE__ */ ie("div", { ...y, children: [
1843
- "No data found for step ",
1844
- String(S)
1845
- ] });
1846
- return p ? {
1847
- data: f,
1848
- NoCurrentData: v
1849
- } : oe.hasData(f) ? {
1850
- data: f,
1851
- hasData: !0,
1852
- NoCurrentData: v
1853
- } : {
1854
- data: void 0,
1855
- hasData: !1,
1856
- NoCurrentData: v
1857
- };
1858
- }
1859
- function r(c) {
1860
- const S = e(
1861
- (O) => O.stepSchema.steps.value.length
1862
- ), {
1863
- targetStep: g,
1864
- maxProgressValue: p = 100,
1865
- totalSteps: f = S,
1866
- progressTextTransformer: v
1867
- } = c, y = Q(o, g), m = y / f * p, b = v ? n(
1868
- { targetStep: g, maxProgressValue: p, totalSteps: f },
1869
- v
1870
- ) : (O) => /* @__PURE__ */ ie("div", { ...O, children: [
1871
- "Step ",
1872
- y,
1873
- "/",
1874
- f
1875
- ] });
1876
- return {
1877
- value: m,
1878
- maxProgressValue: p,
1879
- ProgressText: b
1880
- };
1881
- }
1882
- function s(c) {
1883
- const g = e((f) => f.storage).get(), p = !!(g && typeof g == "object" && Object.keys(g).length > 0);
1884
- return c ? c(p) : p;
1885
- }
1886
- function n(c, S) {
1887
- const g = o.getSnapshot().stepSchema.steps.value.length, { targetStep: p, maxProgressValue: f = 100, totalSteps: v = g } = c;
1888
- return ge({ targetStep: p, maxProgressValue: f, totalSteps: v })(S);
1889
- }
1890
- function a(c, S) {
1891
- return Q(o, c.targetStep), ge(c)(S);
1892
- }
1893
- return {
1894
- // MultiStepFormContext: Context,
1895
- useMultiStepFormData: e,
1896
- useCurrentStepData: t,
1897
- useProgress: r,
1898
- useCanRestartForm: s,
1899
- withProgressText: n,
1900
- withNoStepDataFound: a
1901
- };
1902
- }
1903
- export {
1904
- Re as MultiStepFormSchema,
1905
- Ue as MultiStepFormStepSchema,
1906
- Z as StepSpecificComponent,
1907
- Me as createDefaultValues,
1908
- Qe as createMultiStepFormContext,
1909
- Xe as createMultiStepFormSchema,
1910
- Ze as useMultiStepFormData
1911
- };
1912
- //# sourceMappingURL=index.mjs.map
6
+ export { MultiStepFormSchema, MultiStepFormStepSchema, StepSpecificComponent, createDefaultValues, createMultiStepFormContext, createMultiStepFormSchema, useMultiStepFormData };