@connextar/house 0.3.0 → 0.5.0
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/README.md +242 -6
- package/dist/setup-guide/bits.d.ts +89 -0
- package/dist/setup-guide/bits.d.ts.map +1 -0
- package/dist/setup-guide/bits.js +134 -0
- package/dist/setup-guide/bits.js.map +1 -0
- package/dist/setup-guide/card.d.ts +33 -0
- package/dist/setup-guide/card.d.ts.map +1 -0
- package/dist/setup-guide/card.js +56 -0
- package/dist/setup-guide/card.js.map +1 -0
- package/dist/setup-guide/class-names.d.ts +68 -0
- package/dist/setup-guide/class-names.d.ts.map +1 -0
- package/dist/setup-guide/class-names.js +63 -0
- package/dist/setup-guide/class-names.js.map +1 -0
- package/dist/setup-guide/context.d.ts +89 -0
- package/dist/setup-guide/context.d.ts.map +1 -0
- package/dist/setup-guide/context.js +124 -0
- package/dist/setup-guide/context.js.map +1 -0
- package/dist/setup-guide/guide.d.ts +159 -0
- package/dist/setup-guide/guide.d.ts.map +1 -0
- package/dist/setup-guide/guide.js +128 -0
- package/dist/setup-guide/guide.js.map +1 -0
- package/dist/setup-guide/index.d.ts +27 -0
- package/dist/setup-guide/index.d.ts.map +1 -0
- package/dist/setup-guide/index.js +27 -0
- package/dist/setup-guide/index.js.map +1 -0
- package/dist/setup-guide/launcher.d.ts +38 -0
- package/dist/setup-guide/launcher.d.ts.map +1 -0
- package/dist/setup-guide/launcher.js +60 -0
- package/dist/setup-guide/launcher.js.map +1 -0
- package/dist/setup-guide/strip.d.ts +21 -0
- package/dist/setup-guide/strip.d.ts.map +1 -0
- package/dist/setup-guide/strip.js +42 -0
- package/dist/setup-guide/strip.js.map +1 -0
- package/dist/wizard/index.d.ts +26 -0
- package/dist/wizard/index.d.ts.map +1 -0
- package/dist/wizard/index.js +26 -0
- package/dist/wizard/index.js.map +1 -0
- package/dist/wizard/invalid.d.ts +91 -0
- package/dist/wizard/invalid.d.ts.map +1 -0
- package/dist/wizard/invalid.js +121 -0
- package/dist/wizard/invalid.js.map +1 -0
- package/dist/wizard/react-hook-form-index.d.ts +12 -0
- package/dist/wizard/react-hook-form-index.d.ts.map +1 -0
- package/dist/wizard/react-hook-form-index.js +12 -0
- package/dist/wizard/react-hook-form-index.js.map +1 -0
- package/dist/wizard/rhf-adapter.d.ts +32 -0
- package/dist/wizard/rhf-adapter.d.ts.map +1 -0
- package/dist/wizard/rhf-adapter.js +58 -0
- package/dist/wizard/rhf-adapter.js.map +1 -0
- package/dist/wizard/rules.d.ts +92 -0
- package/dist/wizard/rules.d.ts.map +1 -0
- package/dist/wizard/rules.js +105 -0
- package/dist/wizard/rules.js.map +1 -0
- package/dist/wizard/wizard.d.ts +127 -0
- package/dist/wizard/wizard.d.ts.map +1 -0
- package/dist/wizard/wizard.js +195 -0
- package/dist/wizard/wizard.js.map +1 -0
- package/package.json +19 -2
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import * as React from "react";
|
|
3
|
+
import { useActionReporter } from "../ux/feedback.js";
|
|
4
|
+
/**
|
|
5
|
+
* Saying why a form refused, when the reason is somewhere nobody can look.
|
|
6
|
+
*
|
|
7
|
+
* A wizard keeps fields on steps that are mounted but hidden, and a form library
|
|
8
|
+
* will not call a submit handler while any field is invalid. An error shown
|
|
9
|
+
* beside its field speaks for itself. One on a field nobody can see — another
|
|
10
|
+
* step, a field the form hides because it does not apply, a rule that belongs to
|
|
11
|
+
* no field at all — makes the button look broken, and that one has to be said
|
|
12
|
+
* out loud. This is the half of the wizard that has to read the DOM, so it is
|
|
13
|
+
* kept apart from the rules.
|
|
14
|
+
*
|
|
15
|
+
* Where it is said is the app's decision, not this package's: the message goes to
|
|
16
|
+
* the `ActionFeedbackProvider` that `@connextar/house/ux` already defines, so an
|
|
17
|
+
* app wires one reporter to whatever it uses to tell people things and this
|
|
18
|
+
* module imports no toast library.
|
|
19
|
+
*/
|
|
20
|
+
/**
|
|
21
|
+
* The attribute a field's error message carries, so a visible one can be told
|
|
22
|
+
* from a hidden one. `FieldError` sets it; an app's own message component sets
|
|
23
|
+
* it too (`<p data-error-for="title">`), which is the whole integration.
|
|
24
|
+
*/
|
|
25
|
+
export const ERROR_ATTRIBUTE = "data-error-for";
|
|
26
|
+
/** How long to wait before looking. */
|
|
27
|
+
export const REPORT_DELAY = 50;
|
|
28
|
+
export const INVALID_MESSAGES = {
|
|
29
|
+
title: "That can't be saved yet",
|
|
30
|
+
fallback: "Something on the form needs changing — check the highlighted fields.",
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* The problems in an error object, whatever shape the form library gave them.
|
|
34
|
+
* Only the entries that are objects count: a form library's error map holds one
|
|
35
|
+
* per field, and anything else in there is not a field error.
|
|
36
|
+
*/
|
|
37
|
+
export function fieldProblems(errors) {
|
|
38
|
+
return Object.entries(errors).flatMap(([name, value]) => {
|
|
39
|
+
if (typeof value !== "object" || value === null)
|
|
40
|
+
return [];
|
|
41
|
+
const message = value.message;
|
|
42
|
+
return [{ name, message: typeof message === "string" && message !== "" ? message : null }];
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Rendered, outside a hidden step, and — where the page has layout at all —
|
|
47
|
+
* taking up space.
|
|
48
|
+
*
|
|
49
|
+
* A DOM without layout, like a test's, has no boxes to measure, so `hidden`
|
|
50
|
+
* decides there on its own. Measuring alone was AltEd's version, and under jsdom
|
|
51
|
+
* it called every message invisible and said everything twice.
|
|
52
|
+
*/
|
|
53
|
+
export function isOnScreen(element) {
|
|
54
|
+
if (element.closest("[hidden]"))
|
|
55
|
+
return false;
|
|
56
|
+
const hasLayout = element.ownerDocument.documentElement.getClientRects().length > 0;
|
|
57
|
+
return !hasLayout || element.getClientRects().length > 0;
|
|
58
|
+
}
|
|
59
|
+
/** The field names whose message is on screen right now. */
|
|
60
|
+
export function shownErrorFields(root) {
|
|
61
|
+
return [...root.querySelectorAll(`[${ERROR_ATTRIBUTE}]`)]
|
|
62
|
+
.filter(isOnScreen)
|
|
63
|
+
.map((element) => element.getAttribute(ERROR_ATTRIBUTE) ?? "");
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* The problems with nothing on screen to show them.
|
|
67
|
+
*
|
|
68
|
+
* A name matches a shown message exactly, or as its prefix: an error on
|
|
69
|
+
* `times` is answered by a message on `times.0.start`, because that is where a
|
|
70
|
+
* form library puts the message for an array field's row.
|
|
71
|
+
*/
|
|
72
|
+
export function unseenProblems(problems, shown) {
|
|
73
|
+
return problems.filter(({ name }) => !shown.some((on) => on === name || on.startsWith(`${name}.`)));
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Report the errors an invalid form refused on, if any of them has nowhere to
|
|
77
|
+
* show itself.
|
|
78
|
+
*
|
|
79
|
+
* Deferred, because it has to run after the form has re-rendered with its errors
|
|
80
|
+
* and after a wizard has moved to the failing step — before that, every message
|
|
81
|
+
* is still hidden and it would say everything twice. Returns a way to cancel,
|
|
82
|
+
* for a component that unmounts in between.
|
|
83
|
+
*
|
|
84
|
+
* Two silences and one noise:
|
|
85
|
+
*
|
|
86
|
+
* - Problems, all of them showing → quiet. The fields say it better.
|
|
87
|
+
* - Problems, at least one unseen → the first unseen message, or the fallback.
|
|
88
|
+
* - **No problems at all → the fallback**, not quiet. A form that refuses with
|
|
89
|
+
* an empty error map is the worst of the cases, not the mildest: something
|
|
90
|
+
* said no and named nothing, and the person is left pressing a dead button.
|
|
91
|
+
*/
|
|
92
|
+
export function reportInvalid(errors, report, options = {}) {
|
|
93
|
+
const { delay = REPORT_DELAY, messages } = options;
|
|
94
|
+
const words = { ...INVALID_MESSAGES, ...messages };
|
|
95
|
+
const problems = fieldProblems(errors);
|
|
96
|
+
const timer = setTimeout(() => {
|
|
97
|
+
const root = options.root ?? globalThis.document;
|
|
98
|
+
const unseen = unseenProblems(problems, shownErrorFields(root));
|
|
99
|
+
if (problems.length > 0 && unseen.length === 0)
|
|
100
|
+
return;
|
|
101
|
+
const message = unseen.map((problem) => problem.message).find((text) => text !== null);
|
|
102
|
+
report({ tone: "error", title: words.title, description: message ?? words.fallback });
|
|
103
|
+
}, delay);
|
|
104
|
+
return () => clearTimeout(timer);
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* `reportInvalid` bound to the app's reporter — pass it straight to a form
|
|
108
|
+
* library's invalid handler (`handleSubmit(onSubmit, report)`), or let
|
|
109
|
+
* `stepValidator` from `@connextar/house/wizard/react-hook-form` call it.
|
|
110
|
+
*/
|
|
111
|
+
export function useInvalidReporter(messages) {
|
|
112
|
+
const report = useActionReporter();
|
|
113
|
+
const title = messages?.title;
|
|
114
|
+
const fallback = messages?.fallback;
|
|
115
|
+
return React.useCallback((errors) => {
|
|
116
|
+
reportInvalid(errors, report, {
|
|
117
|
+
messages: { ...(title === undefined ? {} : { title }), ...(fallback === undefined ? {} : { fallback }) },
|
|
118
|
+
});
|
|
119
|
+
}, [report, title, fallback]);
|
|
120
|
+
}
|
|
121
|
+
//# sourceMappingURL=invalid.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"invalid.js","sourceRoot":"","sources":["../../src/wizard/invalid.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAE/B,OAAO,EAAE,iBAAiB,EAAuB,MAAM,mBAAmB,CAAC;AAE3E;;;;;;;;;;;;;;;GAeG;AAEH;;;;GAIG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,gBAAgB,CAAC;AAEhD,uCAAuC;AACvC,MAAM,CAAC,MAAM,YAAY,GAAG,EAAE,CAAC;AAS/B,MAAM,CAAC,MAAM,gBAAgB,GAAoB;IAC/C,KAAK,EAAE,yBAAyB;IAChC,QAAQ,EAAE,sEAAsE;CACjF,CAAC;AAOF;;;;GAIG;AACH,MAAM,UAAU,aAAa,CAAC,MAAc;IAC1C,OAAO,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,O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AM,KAAK,GAAG,QAAQ,EAAE,KAAK,CAAC;IAC9B,MAAM,QAAQ,GAAG,QAAQ,EAAE,QAAQ,CAAC;IACpC,OAAO,KAAK,CAAC,WAAW,CACtB,CAAC,MAAc,EAAE,EAAE;QACjB,aAAa,CAAC,MAAM,EAAE,MAAM,EAAE;YAC5B,QAAQ,EAAE,EAAE,GAAG,CAAC,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,GAAG,CAAC,QAAQ,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,CAAC,EAAE;SACzG,CAAC,CAAC;IACL,CAAC,EACD,CAAC,MAAM,EAAE,KAAK,EAAE,QAAQ,CAAC,CAC1B,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@connextar/house/wizard/react-hook-form` — the step check for a
|
|
3
|
+
* react-hook-form wizard.
|
|
4
|
+
*
|
|
5
|
+
* Its own entry point so that `react-hook-form` stays an optional peer
|
|
6
|
+
* dependency: every import of it in here is `import type`, which erases, but a
|
|
7
|
+
* declaration file that names a package an app has not installed still fails
|
|
8
|
+
* that app's typecheck. An app on plain state imports `@connextar/house/wizard`
|
|
9
|
+
* and never comes near this.
|
|
10
|
+
*/
|
|
11
|
+
export { stepValidator, useStepValidator } from "./rhf-adapter.js";
|
|
12
|
+
//# sourceMappingURL=react-hook-form-index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"react-hook-form-index.d.ts","sourceRoot":"","sources":["../../src/wizard/react-hook-form-index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,OAAO,EAAE,aAAa,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@connextar/house/wizard/react-hook-form` — the step check for a
|
|
3
|
+
* react-hook-form wizard.
|
|
4
|
+
*
|
|
5
|
+
* Its own entry point so that `react-hook-form` stays an optional peer
|
|
6
|
+
* dependency: every import of it in here is `import type`, which erases, but a
|
|
7
|
+
* declaration file that names a package an app has not installed still fails
|
|
8
|
+
* that app's typecheck. An app on plain state imports `@connextar/house/wizard`
|
|
9
|
+
* and never comes near this.
|
|
10
|
+
*/
|
|
11
|
+
export { stepValidator, useStepValidator } from "./rhf-adapter.js";
|
|
12
|
+
//# sourceMappingURL=react-hook-form-index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"react-hook-form-index.js","sourceRoot":"","sources":["../../src/wizard/react-hook-form-index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,OAAO,EAAE,aAAa,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { FieldValues, UseFormReturn } from "react-hook-form";
|
|
2
|
+
import { type InvalidReporter } from "./invalid.js";
|
|
3
|
+
import type { StepCheck } from "./rules.js";
|
|
4
|
+
/**
|
|
5
|
+
* `@connextar/house/wizard/react-hook-form` — the step check most of the cohort
|
|
6
|
+
* wants, for the library most of the cohort uses.
|
|
7
|
+
*
|
|
8
|
+
* Its own entry point, and the reason is types rather than code: every import
|
|
9
|
+
* from `react-hook-form` here is `import type`, so the emitted JavaScript
|
|
10
|
+
* mentions it nowhere and an app without it can still run every part of
|
|
11
|
+
* `@connextar/house/wizard`. What an app without it *cannot* do is typecheck a
|
|
12
|
+
* declaration file that names the package — so the names live behind a subpath
|
|
13
|
+
* nobody has to reach for. MAS has no `react-hook-form` and never will;
|
|
14
|
+
* `entry-points.test.ts` keeps this split honest.
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* The usual step check for a react-hook-form wizard: validate just this step's
|
|
18
|
+
* fields, and when one fails, say why if its message is not on screen.
|
|
19
|
+
*
|
|
20
|
+
* Returns `false` rather than a sentence, because a form library puts its
|
|
21
|
+
* messages beside the fields they belong to and a second copy in a banner above
|
|
22
|
+
* them is noise. The exception is the message with nowhere to go, which is what
|
|
23
|
+
* the reporter is for.
|
|
24
|
+
*/
|
|
25
|
+
export declare function stepValidator<T extends FieldValues>(form: UseFormReturn<T>, report: InvalidReporter): StepCheck;
|
|
26
|
+
/**
|
|
27
|
+
* `stepValidator` wired to the app's `ActionFeedbackProvider`, with an identity
|
|
28
|
+
* that does not change between renders — a check that is a new function on every
|
|
29
|
+
* render makes the wizard re-render every step's content for nothing.
|
|
30
|
+
*/
|
|
31
|
+
export declare function useStepValidator<T extends FieldValues>(form: UseFormReturn<T>): StepCheck;
|
|
32
|
+
//# sourceMappingURL=rhf-adapter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rhf-adapter.d.ts","sourceRoot":"","sources":["../../src/wizard/rhf-adapter.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAa,WAAW,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAE7E,OAAO,EAAsB,KAAK,eAAe,EAAE,MAAM,cAAc,CAAC;AACxE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAE5C;;;;;;;;;;;GAWG;AAEH;;;;;;;;GAQG;AACH,wBAAgB,aAAa,CAAC,CAAC,SAAS,WAAW,EAAE,IAAI,EAAE,aAAa,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,eAAe,GAAG,SAAS,CAgB/G;AAED;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,SAAS,WAAW,EAAE,IAAI,EAAE,aAAa,CAAC,CAAC,CAAC,GAAG,SAAS,CAazF"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import * as React from "react";
|
|
3
|
+
import { useInvalidReporter } from "./invalid.js";
|
|
4
|
+
/**
|
|
5
|
+
* `@connextar/house/wizard/react-hook-form` — the step check most of the cohort
|
|
6
|
+
* wants, for the library most of the cohort uses.
|
|
7
|
+
*
|
|
8
|
+
* Its own entry point, and the reason is types rather than code: every import
|
|
9
|
+
* from `react-hook-form` here is `import type`, so the emitted JavaScript
|
|
10
|
+
* mentions it nowhere and an app without it can still run every part of
|
|
11
|
+
* `@connextar/house/wizard`. What an app without it *cannot* do is typecheck a
|
|
12
|
+
* declaration file that names the package — so the names live behind a subpath
|
|
13
|
+
* nobody has to reach for. MAS has no `react-hook-form` and never will;
|
|
14
|
+
* `entry-points.test.ts` keeps this split honest.
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* The usual step check for a react-hook-form wizard: validate just this step's
|
|
18
|
+
* fields, and when one fails, say why if its message is not on screen.
|
|
19
|
+
*
|
|
20
|
+
* Returns `false` rather than a sentence, because a form library puts its
|
|
21
|
+
* messages beside the fields they belong to and a second copy in a banner above
|
|
22
|
+
* them is noise. The exception is the message with nowhere to go, which is what
|
|
23
|
+
* the reporter is for.
|
|
24
|
+
*/
|
|
25
|
+
export function stepValidator(form, report) {
|
|
26
|
+
return async (fields) => {
|
|
27
|
+
if (fields.length === 0)
|
|
28
|
+
return true;
|
|
29
|
+
const names = fields;
|
|
30
|
+
const ok = await form.trigger(names);
|
|
31
|
+
if (ok)
|
|
32
|
+
return true;
|
|
33
|
+
report(Object.fromEntries(names.flatMap((name) => {
|
|
34
|
+
const error = form.getFieldState(name).error;
|
|
35
|
+
return error ? [[name, error]] : [];
|
|
36
|
+
})));
|
|
37
|
+
return false;
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* `stepValidator` wired to the app's `ActionFeedbackProvider`, with an identity
|
|
42
|
+
* that does not change between renders — a check that is a new function on every
|
|
43
|
+
* render makes the wizard re-render every step's content for nothing.
|
|
44
|
+
*/
|
|
45
|
+
export function useStepValidator(form) {
|
|
46
|
+
const report = useInvalidReporter();
|
|
47
|
+
// Written after the render, so the check always reads the current form and
|
|
48
|
+
// reporter without itself having to change.
|
|
49
|
+
const latest = React.useRef({ form, report });
|
|
50
|
+
React.useEffect(() => {
|
|
51
|
+
latest.current = { form, report };
|
|
52
|
+
});
|
|
53
|
+
return React.useCallback((fields, step) => {
|
|
54
|
+
const { form: current, report: tell } = latest.current;
|
|
55
|
+
return stepValidator(current, tell)(fields, step);
|
|
56
|
+
}, []);
|
|
57
|
+
}
|
|
58
|
+
//# sourceMappingURL=rhf-adapter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rhf-adapter.js","sourceRoot":"","sources":["../../src/wizard/rhf-adapter.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAG/B,OAAO,EAAE,kBAAkB,EAAwB,MAAM,cAAc,CAAC;AAGxE;;;;;;;;;;;GAWG;AAEH;;;;;;;;GAQG;AACH,MAAM,UAAU,aAAa,CAAwB,IAAsB,EAAE,MAAuB;IAClG,OAAO,KAAK,EAAE,MAAM,EAAE,EAAE;QACtB,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC;YAAE,OAAO,IAAI,CAAC;QACrC,MAAM,KAAK,GAAG,MAAwB,CAAC;QACvC,MAAM,EAAE,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;QACrC,IAAI,EAAE;YAAE,OAAO,IAAI,CAAC;QACpB,MAAM,CACJ,MAAM,CAAC,WAAW,CAChB,KAAK,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE;YACrB,MAAM,KAAK,GAAG,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC,KAAK,CAAC;YAC7C,OAAO,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,KAAK,CAAU,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;QAC/C,CAAC,CAAC,CACH,CACF,CAAC;QACF,OAAO,KAAK,CAAC;IACf,CAAC,CAAC;AACJ,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,gBAAgB,CAAwB,IAAsB;IAC5E,MAAM,MAAM,GAAG,kBAAkB,EAAE,CAAC;IACpC,2EAA2E;IAC3E,4CAA4C;IAC5C,MAAM,MAAM,GAAG,KAAK,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC,CAAC;IAC9C,KAAK,CAAC,SAAS,CAAC,GAAG,EAAE;QACnB,MAAM,CAAC,OAAO,GAAG,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC;IACpC,CAAC,CAAC,CAAC;IAEH,OAAO,KAAK,CAAC,WAAW,CAAY,CAAC,MAAM,EAAE,IAAI,EAAE,EAAE;QACnD,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,MAAM,CAAC,OAAO,CAAC;QACvD,OAAO,aAAa,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;IACpD,CAAC,EAAE,EAAE,CAAC,CAAC;AACT,CAAC"}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The rules a stepped form follows — where you are, where you may go, and what
|
|
3
|
+
* has to pass before you get there. No React and no DOM, so they are tested
|
|
4
|
+
* without a browser and hold whatever draws them.
|
|
5
|
+
*
|
|
6
|
+
* The one decision worth spelling out is what a *check* is. Five apps wrote a
|
|
7
|
+
* wizard and arrived at two different answers: a react-hook-form app asks the
|
|
8
|
+
* form to validate some field names and gets a boolean back, while an app
|
|
9
|
+
* holding its values in plain state knows the sentence it wants to show and
|
|
10
|
+
* returns that. Neither is wrong, and a shared wizard that picked one of them
|
|
11
|
+
* would shut the other out — so a check may answer either way:
|
|
12
|
+
*
|
|
13
|
+
* | Returned | Means |
|
|
14
|
+
* | ---------------------------- | ------------------------------------------------ |
|
|
15
|
+
* | `true`, `null`, `undefined` | The step is complete |
|
|
16
|
+
* | `false` | Not complete; the host is showing why, in place |
|
|
17
|
+
* | a sentence | Not complete, and this is what to say |
|
|
18
|
+
*
|
|
19
|
+
* An empty string counts as complete. It is not part of either contract, and a
|
|
20
|
+
* check that returns one by accident must not be able to wedge a form shut with
|
|
21
|
+
* nothing on screen — which is the whole failure this module exists to prevent.
|
|
22
|
+
*/
|
|
23
|
+
/** What a check answers. See the table above. */
|
|
24
|
+
export type StepOutcome = boolean | string | null | undefined | void;
|
|
25
|
+
/** The part of a step these rules need: what it owns, and its own check. */
|
|
26
|
+
export interface StepCheckable {
|
|
27
|
+
/**
|
|
28
|
+
* Field names this step owns. Passed to the form-level check, which is how a
|
|
29
|
+
* react-hook-form host validates one step's worth of fields and no more.
|
|
30
|
+
*/
|
|
31
|
+
fields?: readonly string[];
|
|
32
|
+
/**
|
|
33
|
+
* This step's own check, for a host that keeps validation beside the step
|
|
34
|
+
* rather than in one function. Asked before the form-level check.
|
|
35
|
+
*/
|
|
36
|
+
validate?: StepCheck;
|
|
37
|
+
}
|
|
38
|
+
/** Is this step complete? Asked before leaving it, and again for every step on submit. */
|
|
39
|
+
export type StepCheck = (fields: string[], step: StepCheckable) => StepOutcome | Promise<StepOutcome>;
|
|
40
|
+
export type StepResult = {
|
|
41
|
+
ok: true;
|
|
42
|
+
} | {
|
|
43
|
+
ok: false;
|
|
44
|
+
message: string | null;
|
|
45
|
+
};
|
|
46
|
+
/** Normalise whatever a check answered. */
|
|
47
|
+
export declare function stepResult(outcome: StepOutcome): StepResult;
|
|
48
|
+
/**
|
|
49
|
+
* Ask everything that has a say about one step. The step's own check first, then
|
|
50
|
+
* the form-level one; the first refusal is the answer, so its message is the one
|
|
51
|
+
* shown rather than the last one computed.
|
|
52
|
+
*/
|
|
53
|
+
export declare function checkStep(step: StepCheckable, onValidateStep?: StepCheck): Promise<StepResult>;
|
|
54
|
+
export interface StepFailure {
|
|
55
|
+
index: number;
|
|
56
|
+
message: string | null;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* The first step that will not pass, checked in order from the beginning.
|
|
60
|
+
*
|
|
61
|
+
* This is what a submit does, and checking only the last step is the defect
|
|
62
|
+
* every one of the five copies had at some point. Step back by a marker (which
|
|
63
|
+
* does not validate), clear a required field, jump to the end and press Save:
|
|
64
|
+
* the form refuses with the problem on a step nobody is looking at, and the
|
|
65
|
+
* button looks broken. In MAS it was worse — nothing was checked on submit at
|
|
66
|
+
* all, so a second press after a successful create sent a record whose first
|
|
67
|
+
* step had been cleared.
|
|
68
|
+
*
|
|
69
|
+
* In order matters as much as every: the person is sent to the earliest thing
|
|
70
|
+
* that needs fixing, which is the one they will understand.
|
|
71
|
+
*/
|
|
72
|
+
export declare function firstFailure<S extends StepCheckable>(steps: readonly S[], onValidateStep?: StepCheck): Promise<StepFailure | null>;
|
|
73
|
+
export type MarkerState = "done" | "current" | "upcoming";
|
|
74
|
+
/** How a marker reads: behind you, where you are, or ahead. */
|
|
75
|
+
export declare function markerState(index: number, current: number): MarkerState;
|
|
76
|
+
/**
|
|
77
|
+
* May this marker be clicked?
|
|
78
|
+
*
|
|
79
|
+
* Anything reached before now, including the step you are on — clicking that one
|
|
80
|
+
* does nothing, and disabling it takes the tracker's "you are here" out of the
|
|
81
|
+
* tab order for no gain. Steps never reached stay locked, because skipping
|
|
82
|
+
* forward past a step that has never been checked is how people arrive at a
|
|
83
|
+
* submit button that will not work with nothing saying why.
|
|
84
|
+
*/
|
|
85
|
+
export declare function isReachable(index: number, furthest: number): boolean;
|
|
86
|
+
/** The next index, not past the end. */
|
|
87
|
+
export declare function nextIndex(current: number, total: number): number;
|
|
88
|
+
/** The previous index, not past the beginning. */
|
|
89
|
+
export declare function previousIndex(current: number): number;
|
|
90
|
+
/** How far through, for the apps that show a bar rather than a count. */
|
|
91
|
+
export declare function percentComplete(current: number, total: number): number;
|
|
92
|
+
//# sourceMappingURL=rules.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rules.d.ts","sourceRoot":"","sources":["../../src/wizard/rules.ts"],"names":[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,EAAE,OAAO,EAAE,MAAM,GAAG,WAAW,CAGvE;AAED;;;;;;;;GAQG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAEpE;AAED,wCAAwC;AACxC,wBAAgB,SAAS,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAEhE;AAED,kDAAkD;AAClD,wBAAgB,aAAa,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAErD;AAED,yEAAyE;AACzE,wBAAgB,eAAe,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAGtE"}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The rules a stepped form follows — where you are, where you may go, and what
|
|
3
|
+
* has to pass before you get there. No React and no DOM, so they are tested
|
|
4
|
+
* without a browser and hold whatever draws them.
|
|
5
|
+
*
|
|
6
|
+
* The one decision worth spelling out is what a *check* is. Five apps wrote a
|
|
7
|
+
* wizard and arrived at two different answers: a react-hook-form app asks the
|
|
8
|
+
* form to validate some field names and gets a boolean back, while an app
|
|
9
|
+
* holding its values in plain state knows the sentence it wants to show and
|
|
10
|
+
* returns that. Neither is wrong, and a shared wizard that picked one of them
|
|
11
|
+
* would shut the other out — so a check may answer either way:
|
|
12
|
+
*
|
|
13
|
+
* | Returned | Means |
|
|
14
|
+
* | ---------------------------- | ------------------------------------------------ |
|
|
15
|
+
* | `true`, `null`, `undefined` | The step is complete |
|
|
16
|
+
* | `false` | Not complete; the host is showing why, in place |
|
|
17
|
+
* | a sentence | Not complete, and this is what to say |
|
|
18
|
+
*
|
|
19
|
+
* An empty string counts as complete. It is not part of either contract, and a
|
|
20
|
+
* check that returns one by accident must not be able to wedge a form shut with
|
|
21
|
+
* nothing on screen — which is the whole failure this module exists to prevent.
|
|
22
|
+
*/
|
|
23
|
+
const PASSED = { ok: true };
|
|
24
|
+
/** Normalise whatever a check answered. */
|
|
25
|
+
export function stepResult(outcome) {
|
|
26
|
+
if (outcome === false)
|
|
27
|
+
return { ok: false, message: null };
|
|
28
|
+
if (typeof outcome === "string" && outcome !== "")
|
|
29
|
+
return { ok: false, message: outcome };
|
|
30
|
+
return PASSED;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Ask everything that has a say about one step. The step's own check first, then
|
|
34
|
+
* the form-level one; the first refusal is the answer, so its message is the one
|
|
35
|
+
* shown rather than the last one computed.
|
|
36
|
+
*/
|
|
37
|
+
export async function checkStep(step, onValidateStep) {
|
|
38
|
+
const fields = [...(step.fields ?? [])];
|
|
39
|
+
for (const check of [step.validate, onValidateStep]) {
|
|
40
|
+
if (!check)
|
|
41
|
+
continue;
|
|
42
|
+
const result = stepResult(await check(fields, step));
|
|
43
|
+
if (!result.ok)
|
|
44
|
+
return result;
|
|
45
|
+
}
|
|
46
|
+
return PASSED;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* The first step that will not pass, checked in order from the beginning.
|
|
50
|
+
*
|
|
51
|
+
* This is what a submit does, and checking only the last step is the defect
|
|
52
|
+
* every one of the five copies had at some point. Step back by a marker (which
|
|
53
|
+
* does not validate), clear a required field, jump to the end and press Save:
|
|
54
|
+
* the form refuses with the problem on a step nobody is looking at, and the
|
|
55
|
+
* button looks broken. In MAS it was worse — nothing was checked on submit at
|
|
56
|
+
* all, so a second press after a successful create sent a record whose first
|
|
57
|
+
* step had been cleared.
|
|
58
|
+
*
|
|
59
|
+
* In order matters as much as every: the person is sent to the earliest thing
|
|
60
|
+
* that needs fixing, which is the one they will understand.
|
|
61
|
+
*/
|
|
62
|
+
export async function firstFailure(steps, onValidateStep) {
|
|
63
|
+
for (let index = 0; index < steps.length; index++) {
|
|
64
|
+
const step = steps[index];
|
|
65
|
+
if (!step)
|
|
66
|
+
continue;
|
|
67
|
+
const result = await checkStep(step, onValidateStep);
|
|
68
|
+
if (!result.ok)
|
|
69
|
+
return { index, message: result.message };
|
|
70
|
+
}
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
/** How a marker reads: behind you, where you are, or ahead. */
|
|
74
|
+
export function markerState(index, current) {
|
|
75
|
+
if (index < current)
|
|
76
|
+
return "done";
|
|
77
|
+
return index === current ? "current" : "upcoming";
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* May this marker be clicked?
|
|
81
|
+
*
|
|
82
|
+
* Anything reached before now, including the step you are on — clicking that one
|
|
83
|
+
* does nothing, and disabling it takes the tracker's "you are here" out of the
|
|
84
|
+
* tab order for no gain. Steps never reached stay locked, because skipping
|
|
85
|
+
* forward past a step that has never been checked is how people arrive at a
|
|
86
|
+
* submit button that will not work with nothing saying why.
|
|
87
|
+
*/
|
|
88
|
+
export function isReachable(index, furthest) {
|
|
89
|
+
return index <= furthest;
|
|
90
|
+
}
|
|
91
|
+
/** The next index, not past the end. */
|
|
92
|
+
export function nextIndex(current, total) {
|
|
93
|
+
return Math.min(current + 1, Math.max(total - 1, 0));
|
|
94
|
+
}
|
|
95
|
+
/** The previous index, not past the beginning. */
|
|
96
|
+
export function previousIndex(current) {
|
|
97
|
+
return Math.max(current - 1, 0);
|
|
98
|
+
}
|
|
99
|
+
/** How far through, for the apps that show a bar rather than a count. */
|
|
100
|
+
export function percentComplete(current, total) {
|
|
101
|
+
if (total <= 0)
|
|
102
|
+
return 0;
|
|
103
|
+
return Math.round((Math.min(current + 1, total) / total) * 100);
|
|
104
|
+
}
|
|
105
|
+
//# sourceMappingURL=rules.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rules.js","sourceRoot":"","sources":["../../src/wizard/rules.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAwBH,MAAM,MAAM,GAAe,EAAE,EAAE,EAAE,IAAI,EAAE,CAAC;AAExC,2CAA2C;AAC3C,MAAM,UAAU,UAAU,CAAC,OAAoB;IAC7C,IAAI,OAAO,K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I,CAAC;AACd,CAAC;AAID,+DAA+D;AAC/D,MAAM,UAAU,WAAW,CAAC,KAAa,EAAE,OAAe;IACxD,IAAI,KAAK,GAAG,OAAO;QAAE,OAAO,MAAM,CAAC;IACnC,OAAO,KAAK,KAAK,OAAO,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,UAAU,CAAC;AACpD,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,WAAW,CAAC,KAAa,EAAE,QAAgB;IACzD,OAAO,KAAK,IAAI,QAAQ,CAAC;AAC3B,CAAC;AAED,wCAAwC;AACxC,MAAM,UAAU,SAAS,CAAC,OAAe,EAAE,KAAa;IACtD,OAAO,IAAI,CAAC,GAAG,CAAC,OAAO,GAAG,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,KAAK,GAAG,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;AACvD,CAAC;AAED,kDAAkD;AAClD,MAAM,UAAU,aAAa,CAAC,OAAe;IAC3C,OAAO,IAAI,CAAC,GAAG,CAAC,OAAO,GAAG,CAAC,EAAE,CAAC,CAAC,CAAC;AAClC,CAAC;AAED,yEAAyE;AACzE,MAAM,UAAU,eAAe,CAAC,OAAe,EAAE,KAAa;IAC5D,IAAI,KAAK,IAAI,CAAC;QAAE,OAAO,CAAC,CAAC;IACzB,OAAO,IAAI,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,OAAO,GAAG,CAAC,EAAE,KAAK,CAAC,GAAG,KAAK,CAAC,GAAG,GAAG,CAAC,CAAC;AAClE,CAAC"}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { type StepCheck, type StepCheckable } from "./rules.js";
|
|
3
|
+
export interface WizardStep extends StepCheckable {
|
|
4
|
+
id: string;
|
|
5
|
+
title: string;
|
|
6
|
+
/** One line saying what this step is for. */
|
|
7
|
+
description?: string;
|
|
8
|
+
content: React.ReactNode;
|
|
9
|
+
}
|
|
10
|
+
/** Every element the wizard draws. Each value replaces the default for that part. */
|
|
11
|
+
export interface WizardClassNames {
|
|
12
|
+
form: string;
|
|
13
|
+
markers: string;
|
|
14
|
+
markerItem: string;
|
|
15
|
+
marker: string;
|
|
16
|
+
markerLocked: string;
|
|
17
|
+
circle: string;
|
|
18
|
+
circleDone: string;
|
|
19
|
+
circleCurrent: string;
|
|
20
|
+
circleUpcoming: string;
|
|
21
|
+
markerTitle: string;
|
|
22
|
+
markerTitleCurrent: string;
|
|
23
|
+
connector: string;
|
|
24
|
+
connectorDone: string;
|
|
25
|
+
bar: string;
|
|
26
|
+
barFill: string;
|
|
27
|
+
heading: string;
|
|
28
|
+
description: string;
|
|
29
|
+
alert: string;
|
|
30
|
+
panel: string;
|
|
31
|
+
footer: string;
|
|
32
|
+
counter: string;
|
|
33
|
+
button: string;
|
|
34
|
+
backButton: string;
|
|
35
|
+
forwardButton: string;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* The house look, on the shared theme tokens. An app without them — MAS keeps
|
|
39
|
+
* its palette in literal classes — replaces the parts it needs through
|
|
40
|
+
* `classNames`, exactly as the guide renderer allows.
|
|
41
|
+
*/
|
|
42
|
+
export declare const WIZARD_CLASS_NAMES: WizardClassNames;
|
|
43
|
+
export interface FormWizardProps {
|
|
44
|
+
steps: WizardStep[];
|
|
45
|
+
/**
|
|
46
|
+
* Is the step being left complete? Asked before each advance, and again for
|
|
47
|
+
* every step on submit. See `StepCheck` for the ways it may answer.
|
|
48
|
+
*/
|
|
49
|
+
onValidateStep?: StepCheck;
|
|
50
|
+
/** Called once every step has passed, and never before. */
|
|
51
|
+
onSubmit: () => void | Promise<void>;
|
|
52
|
+
/** Shown in place of Back on the first step; without it, Back is disabled there. */
|
|
53
|
+
onCancel?: () => void;
|
|
54
|
+
submitLabel?: React.ReactNode;
|
|
55
|
+
nextLabel?: React.ReactNode;
|
|
56
|
+
cancelLabel?: React.ReactNode;
|
|
57
|
+
backLabel?: React.ReactNode;
|
|
58
|
+
/** The submission is in flight: navigation locks and the button says so. */
|
|
59
|
+
pending?: boolean;
|
|
60
|
+
/**
|
|
61
|
+
* Every step open from the start — for editing, where the values are all there
|
|
62
|
+
* already and the change is usually to one field on a later step.
|
|
63
|
+
*/
|
|
64
|
+
allReachable?: boolean;
|
|
65
|
+
/** Show how far through as a bar as well as a count. */
|
|
66
|
+
showProgressBar?: boolean;
|
|
67
|
+
/** A heading or introduction above the tracker, inside the form. */
|
|
68
|
+
header?: React.ReactNode;
|
|
69
|
+
className?: string;
|
|
70
|
+
footerClassName?: string;
|
|
71
|
+
classNames?: Partial<WizardClassNames>;
|
|
72
|
+
"aria-label"?: string;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* A long form split into steps. The one stepper for every multi-step form in
|
|
76
|
+
* every app — the union of what five of them each learnt the hard way.
|
|
77
|
+
*
|
|
78
|
+
* - **It renders the `<form>` itself,** so nothing reaches `onSubmit`
|
|
79
|
+
* unchecked and so Enter has one place to land. A wizard sitting inside
|
|
80
|
+
* somebody else's form cannot promise either.
|
|
81
|
+
* - **Enter never skips ahead.** Pressing Enter in a one-line field submits the
|
|
82
|
+
* form the browser's way, and before the last step that submission means
|
|
83
|
+
* Continue: this step is checked and, if it passes, the next one opens. Only
|
|
84
|
+
* the last step's Enter submits.
|
|
85
|
+
* - **There is one submit button and its `type` never changes.** PharmaLine put
|
|
86
|
+
* a `type="button"` Continue and a `type="submit"` Create in the same slot;
|
|
87
|
+
* React reused the one DOM node and switched its type mid-click, and Chromium
|
|
88
|
+
* submitted — every booking made from the dashboard skipped Confirm and sent a
|
|
89
|
+
* confirmation nobody chose. jsdom's timing hid it, so the guard here is
|
|
90
|
+
* structural rather than behavioural: one button, always `type="submit"`.
|
|
91
|
+
* - **Steps are checked as you leave them.** `onValidateStep` (or the step's own
|
|
92
|
+
* `validate`) is asked before each advance, and the step stays put if it
|
|
93
|
+
* refuses, saying why when the check gave a message.
|
|
94
|
+
* - **Submitting checks every step, in order, and lands on the first that
|
|
95
|
+
* fails.** Checking only the last one, or none at all, is how a form refuses
|
|
96
|
+
* with the problem on a screen nobody is looking at.
|
|
97
|
+
* - **Continue is never disabled for being incomplete.** A greyed-out button
|
|
98
|
+
* that will not say what is missing is a refusal with no words; it is only
|
|
99
|
+
* disabled while something is actually in flight.
|
|
100
|
+
* - **Visited markers are clickable; steps ahead stay locked** (unless
|
|
101
|
+
* `allReachable`), so the tracker is navigation rather than a way past a step
|
|
102
|
+
* that has never been checked.
|
|
103
|
+
* - **All steps stay mounted,** hidden rather than unmounted: one register, and
|
|
104
|
+
* nothing typed is lost by stepping back to check something.
|
|
105
|
+
*
|
|
106
|
+
* What it deliberately does not do is validate anything itself, or know what a
|
|
107
|
+
* button looks like. The check is a function the host supplies, so
|
|
108
|
+
* react-hook-form, plain state and a form built out of `useState` can all host
|
|
109
|
+
* it; the look is theme-token classes an app replaces through `classNames`.
|
|
110
|
+
*/
|
|
111
|
+
export declare function FormWizard({ steps, onValidateStep, onSubmit, onCancel, submitLabel, nextLabel, cancelLabel, backLabel, pending, allReachable, showProgressBar, header, className, footerClassName, classNames, "aria-label": ariaLabel, }: FormWizardProps): React.JSX.Element | null;
|
|
112
|
+
/**
|
|
113
|
+
* A field's error message, marked so the reporter can tell a visible error from
|
|
114
|
+
* a hidden one. An app whose form library draws its own message adds
|
|
115
|
+
* `data-error-for` to that component instead of using this.
|
|
116
|
+
*/
|
|
117
|
+
export declare function FieldError({ name, message, className, }: {
|
|
118
|
+
name: string;
|
|
119
|
+
message?: string | null;
|
|
120
|
+
className?: string;
|
|
121
|
+
}): React.JSX.Element | null;
|
|
122
|
+
/** A read-only label/value row, for the review step at the end of a wizard. */
|
|
123
|
+
export declare function ReviewRow({ label, value }: {
|
|
124
|
+
label: React.ReactNode;
|
|
125
|
+
value: React.ReactNode;
|
|
126
|
+
}): React.JSX.Element;
|
|
127
|
+
//# sourceMappingURL=wizard.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"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