@wizardkit/core 0.0.0-stage → 1.13.1
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/LICENSE +21 -0
- package/README.md +480 -2
- package/dist/builders/create-step.d.ts +96 -0
- package/dist/builders/create-wizard.d.ts +64 -0
- package/dist/errors.d.ts +58 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +1318 -0
- package/dist/machine/lazy-steps.d.ts +42 -0
- package/dist/machine/step-resolver.d.ts +25 -0
- package/dist/machine/transitions.d.ts +22 -0
- package/dist/machine/validators.d.ts +39 -0
- package/dist/machine/wizard-machine.d.ts +731 -0
- package/dist/plugins/analytics.d.ts +58 -0
- package/dist/plugins/index.d.ts +8 -0
- package/dist/plugins/logging.d.ts +12 -0
- package/dist/plugins/persistence.d.ts +132 -0
- package/dist/plugins/plugin-host.d.ts +71 -0
- package/dist/plugins/storage-adapters.d.ts +20 -0
- package/dist/plugins/types.d.ts +67 -0
- package/dist/plugins.js +2 -0
- package/dist/storage-adapters-BE71wCcH.js +431 -0
- package/dist/types/base.d.ts +53 -0
- package/dist/types/context.d.ts +30 -0
- package/dist/types/definition.d.ts +16 -0
- package/dist/types/step.d.ts +97 -0
- package/dist/types/transitions.d.ts +32 -0
- package/package.json +67 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Georgiy Tsamaladze
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,481 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @wizardkit/core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A framework-agnostic, type-safe state machine for building multi-step wizards in TypeScript.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Framework Agnostic** - Use with React, Vue, Svelte, Solid, vanilla JS, or any TypeScript project
|
|
8
|
+
- **Type Safe** - Full TypeScript generics support for your data types
|
|
9
|
+
- **Flexible Navigation** - Static, conditional, or dynamic step transitions
|
|
10
|
+
- **Navigation History** - Built-in history stack for reliable back navigation through conditional flows
|
|
11
|
+
- **Validation** - Built-in validators, schema support, and custom predicates
|
|
12
|
+
- **Extensible** - Context, guards, lifecycle hooks, and side effects
|
|
13
|
+
- **Lazy Steps** - Load a step's `validate` / lifecycle hooks on first use (`load` / `.lazy()`), with `preloadStep` and `isLoadingStep`
|
|
14
|
+
- **State Machine** - Predictable behavior based on finite state machine principles
|
|
15
|
+
- **Zero Dependencies** - Core library has no external dependencies (only for Standard Schema validation)
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install @wizardkit/core
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Quick Start
|
|
24
|
+
|
|
25
|
+
```typescript
|
|
26
|
+
import { WizardMachine, createLinearWizard } from "@wizardkit/core";
|
|
27
|
+
|
|
28
|
+
// 1. Define your data type
|
|
29
|
+
type SignupData = {
|
|
30
|
+
name: string;
|
|
31
|
+
email: string;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
// 2. Create a wizard
|
|
35
|
+
const wizard = createLinearWizard<SignupData>({
|
|
36
|
+
id: "signup",
|
|
37
|
+
steps: [
|
|
38
|
+
{
|
|
39
|
+
id: "name",
|
|
40
|
+
title: "What's your name?",
|
|
41
|
+
validate: (data) => ({
|
|
42
|
+
valid: Boolean(data.name),
|
|
43
|
+
errors: data.name ? undefined : { name: "Required" },
|
|
44
|
+
}),
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
id: "email",
|
|
48
|
+
title: "What's your email?",
|
|
49
|
+
validate: (data) => ({
|
|
50
|
+
valid: data.email?.includes("@") ?? false,
|
|
51
|
+
errors: data.email?.includes("@")
|
|
52
|
+
? undefined
|
|
53
|
+
: { email: "Invalid email" },
|
|
54
|
+
}),
|
|
55
|
+
onSubmit: async (data) => {
|
|
56
|
+
console.log("Form submitted:", data);
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
],
|
|
60
|
+
onComplete: (data) => {
|
|
61
|
+
console.log("Wizard complete:", data);
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
// 3. Create a state machine
|
|
66
|
+
const machine = new WizardMachine(wizard, {}, { name: "", email: "" });
|
|
67
|
+
|
|
68
|
+
// 4. Navigate
|
|
69
|
+
console.log(machine.snapshot.currentStepId); // "name"
|
|
70
|
+
await machine.goNext();
|
|
71
|
+
console.log(machine.snapshot.currentStepId); // "email"
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## Core Concepts
|
|
75
|
+
|
|
76
|
+
### Declarative Definitions
|
|
77
|
+
|
|
78
|
+
Define wizards as data, not imperative code:
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
import type { WizardDefinition } from "@wizardkit/core";
|
|
82
|
+
|
|
83
|
+
const wizard: WizardDefinition<MyData> = {
|
|
84
|
+
id: "my-wizard",
|
|
85
|
+
initialStepId: "step1",
|
|
86
|
+
steps: {
|
|
87
|
+
step1: {
|
|
88
|
+
/* ... */
|
|
89
|
+
},
|
|
90
|
+
step2: {
|
|
91
|
+
/* ... */
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### State Machine
|
|
98
|
+
|
|
99
|
+
All navigation and validation goes through `WizardMachine`:
|
|
100
|
+
|
|
101
|
+
```typescript
|
|
102
|
+
const machine = new WizardMachine(definition, context, initialData, {
|
|
103
|
+
onStateChange: (state) => {
|
|
104
|
+
// React to state changes
|
|
105
|
+
},
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
await machine.goNext();
|
|
109
|
+
const current = await machine.validate(); // ValidationResult for the current step
|
|
110
|
+
const all = await machine.validateAll(); // ValidationSummary for every enabled step
|
|
111
|
+
await machine.cancel(); // onCancel handlers, then reset
|
|
112
|
+
const snapshot = machine.serialize(); // JSON-safe runtime state
|
|
113
|
+
machine.restore(snapshot);
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
For automatic persistence (restore on init + debounced auto-save) use
|
|
117
|
+
`createPersistencePlugin` instead of wiring `serialize` / `restore` by hand — see Plugins
|
|
118
|
+
below.
|
|
119
|
+
|
|
120
|
+
### Plugins
|
|
121
|
+
|
|
122
|
+
```typescript
|
|
123
|
+
import { createLoggingPlugin, type WizardPlugin } from "@wizardkit/core";
|
|
124
|
+
// or: import { createLoggingPlugin } from "@wizardkit/core/plugins";
|
|
125
|
+
|
|
126
|
+
const plugins: WizardPlugin<MyData>[] = [
|
|
127
|
+
createLoggingPlugin({ level: "debug" }),
|
|
128
|
+
];
|
|
129
|
+
|
|
130
|
+
const machine = new WizardMachine(definition, {}, initialData, events, plugins);
|
|
131
|
+
// or: machine.use(createLoggingPlugin());
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
Built-in persistence (restore on init, debounced auto-save, clear on complete/reset):
|
|
135
|
+
|
|
136
|
+
```typescript
|
|
137
|
+
import {
|
|
138
|
+
createPersistencePlugin,
|
|
139
|
+
localStorageAdapter,
|
|
140
|
+
} from "@wizardkit/core";
|
|
141
|
+
|
|
142
|
+
machine.use(
|
|
143
|
+
createPersistencePlugin<MyData>({
|
|
144
|
+
adapter: localStorageAdapter(`wizard:${definition.id}`),
|
|
145
|
+
debounceMs: 300, // trailing-edge debounce for data changes
|
|
146
|
+
}),
|
|
147
|
+
);
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
See the [Plugins guide](https://wizardkit.dev/guide/plugins) for veto semantics and lifecycle hooks.
|
|
151
|
+
|
|
152
|
+
### Lazy Steps
|
|
153
|
+
|
|
154
|
+
Defer a step's heavy implementation (`validate`, `onEnter`, `onLeave`, `onSubmit`) to a dynamic `import()`. The step skeleton (`id`, `next`, `previous`, `enabled`, `meta`) stays eager, so progress, `isLastStep` and disabled-step skipping never wait for a download.
|
|
155
|
+
|
|
156
|
+
```typescript
|
|
157
|
+
createWizard<Application>("loan")
|
|
158
|
+
.step("documents", (s) =>
|
|
159
|
+
s
|
|
160
|
+
.title("Documents")
|
|
161
|
+
.previous("personal")
|
|
162
|
+
.next("summary")
|
|
163
|
+
.lazy(() => import("./steps/documents")), // default export: { validate, onEnter, ... }
|
|
164
|
+
)
|
|
165
|
+
.build();
|
|
166
|
+
|
|
167
|
+
machine.snapshot.isLoadingStep; // true while the current/target step loads
|
|
168
|
+
machine.preloadStep("documents").catch(() => {}); // prefetch, e.g. on hover of "Next"
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
A hook defined on both the skeleton and the loaded implementation is composed, not replaced (`validate` via `combineValidators`, both must pass; `onEnter` / `onLeave` / `onSubmit` run the skeleton's first), so `.required("passport").lazy(...)` keeps its required check.
|
|
172
|
+
|
|
173
|
+
A failed load rejects the operation with `WizardStepLoadError` (message `Failed to load step "<id>": <cause message>`, native `cause`), is reported with `ErrorContext.phase === "load"`, leaves the wizard on its current step and is retried by the next attempt. Leaving a step whose own chunk failed is not blocked (its `onLeave` falls back to the skeleton's); only a failing target load blocks. `machine.preloadStep()` rejects, bindings' `actions.preloadStep()` never does. See the [Lazy Steps guide](https://wizardkit.dev/guide/defining-wizards#lazy-steps).
|
|
174
|
+
|
|
175
|
+
### Three Transition Types
|
|
176
|
+
|
|
177
|
+
Navigate between steps in different ways:
|
|
178
|
+
|
|
179
|
+
**Static** - Always go to the same step:
|
|
180
|
+
|
|
181
|
+
```typescript
|
|
182
|
+
next: { type: "static", to: "next-step" }
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
**Conditional** - Branch based on data:
|
|
186
|
+
|
|
187
|
+
```typescript
|
|
188
|
+
next: {
|
|
189
|
+
type: "conditional",
|
|
190
|
+
branches: [
|
|
191
|
+
{ when: (d) => d.isPremium, to: "premium-path" },
|
|
192
|
+
{ when: () => true, to: "standard-path" },
|
|
193
|
+
],
|
|
194
|
+
}
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
**Resolver** - Dynamic resolution with async logic:
|
|
198
|
+
|
|
199
|
+
```typescript
|
|
200
|
+
next: {
|
|
201
|
+
type: "resolver",
|
|
202
|
+
resolve: async (data, ctx) => {
|
|
203
|
+
const plan = await ctx.api.getPlan(data.userId);
|
|
204
|
+
return plan.recommended;
|
|
205
|
+
},
|
|
206
|
+
}
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
### Type Safety
|
|
210
|
+
|
|
211
|
+
Your entire wizard is typed with your data:
|
|
212
|
+
|
|
213
|
+
```typescript
|
|
214
|
+
type CheckoutData = {
|
|
215
|
+
email: string;
|
|
216
|
+
cardNumber: string;
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
const wizard: WizardDefinition<CheckoutData> = {
|
|
220
|
+
// TypeScript ensures all steps match CheckoutData
|
|
221
|
+
steps: {
|
|
222
|
+
payment: {
|
|
223
|
+
validate: (data) => {
|
|
224
|
+
// data is CheckoutData
|
|
225
|
+
data.cardNumber; // ✅ TypeScript knows this exists
|
|
226
|
+
data.unknownField; // ❌ TypeScript error
|
|
227
|
+
},
|
|
228
|
+
},
|
|
229
|
+
},
|
|
230
|
+
};
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
## Building Wizards
|
|
234
|
+
|
|
235
|
+
### With Builders (Recommended)
|
|
236
|
+
|
|
237
|
+
Use fluent API for readability:
|
|
238
|
+
|
|
239
|
+
```typescript
|
|
240
|
+
import { createWizard } from "@wizardkit/core";
|
|
241
|
+
|
|
242
|
+
const wizard = createWizard<SignupData>("signup")
|
|
243
|
+
.initialStep("personal")
|
|
244
|
+
.step("personal", (s) =>
|
|
245
|
+
s.title("Personal Info").required("name").next("contact"),
|
|
246
|
+
)
|
|
247
|
+
.step("contact", (s) =>
|
|
248
|
+
s
|
|
249
|
+
.title("Contact Info")
|
|
250
|
+
.required("email")
|
|
251
|
+
.previous("personal")
|
|
252
|
+
.onSubmit(async (data) => {
|
|
253
|
+
await api.submit(data);
|
|
254
|
+
}),
|
|
255
|
+
)
|
|
256
|
+
.onComplete(async (data) => {
|
|
257
|
+
console.log("Done!");
|
|
258
|
+
})
|
|
259
|
+
.build();
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
### With Raw Definitions
|
|
263
|
+
|
|
264
|
+
For complex logic or server-side generation:
|
|
265
|
+
|
|
266
|
+
```typescript
|
|
267
|
+
const wizard: WizardDefinition<SignupData> = {
|
|
268
|
+
id: "signup",
|
|
269
|
+
initialStepId: "personal",
|
|
270
|
+
steps: {
|
|
271
|
+
personal: {
|
|
272
|
+
id: "personal",
|
|
273
|
+
validate: (data) => ({
|
|
274
|
+
valid: Boolean(data.name),
|
|
275
|
+
errors: data.name ? undefined : { name: "Required" },
|
|
276
|
+
}),
|
|
277
|
+
next: { type: "static", to: "contact" },
|
|
278
|
+
},
|
|
279
|
+
contact: {
|
|
280
|
+
id: "contact",
|
|
281
|
+
validate: (data) => ({
|
|
282
|
+
valid: data.email?.includes("@") ?? false,
|
|
283
|
+
errors: data.email?.includes("@")
|
|
284
|
+
? undefined
|
|
285
|
+
: { email: "Invalid email" },
|
|
286
|
+
}),
|
|
287
|
+
previous: { type: "static", to: "personal" },
|
|
288
|
+
},
|
|
289
|
+
},
|
|
290
|
+
};
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
### For Simple Linear Flows
|
|
294
|
+
|
|
295
|
+
Use the linear helper:
|
|
296
|
+
|
|
297
|
+
```typescript
|
|
298
|
+
const wizard = createLinearWizard<SignupData>({
|
|
299
|
+
id: "signup",
|
|
300
|
+
steps: [
|
|
301
|
+
{ id: "personal", title: "Name", validate: (d) => ({ valid: !!d.name }) },
|
|
302
|
+
{ id: "contact", title: "Email", validate: (d) => ({ valid: !!d.email }) },
|
|
303
|
+
{ id: "review", title: "Review" },
|
|
304
|
+
],
|
|
305
|
+
onComplete: (data) => console.log("Done!", data),
|
|
306
|
+
});
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
## Validation
|
|
310
|
+
|
|
311
|
+
### Simple Validators
|
|
312
|
+
|
|
313
|
+
```typescript
|
|
314
|
+
validate: (data) => ({
|
|
315
|
+
valid: data.name?.length > 0,
|
|
316
|
+
errors: data.name?.length > 0 ? undefined : { name: "Required" },
|
|
317
|
+
});
|
|
318
|
+
```
|
|
319
|
+
|
|
320
|
+
### Using Utilities
|
|
321
|
+
|
|
322
|
+
```typescript
|
|
323
|
+
import {
|
|
324
|
+
combineValidators,
|
|
325
|
+
requiredFields,
|
|
326
|
+
createValidator,
|
|
327
|
+
} from "@wizardkit/core";
|
|
328
|
+
|
|
329
|
+
const emailValidator = createValidator(
|
|
330
|
+
(data) => data.email?.includes("@"),
|
|
331
|
+
"Invalid email",
|
|
332
|
+
"email",
|
|
333
|
+
);
|
|
334
|
+
|
|
335
|
+
step.validate = combineValidators(
|
|
336
|
+
requiredFields("name", "email"),
|
|
337
|
+
emailValidator,
|
|
338
|
+
);
|
|
339
|
+
```
|
|
340
|
+
|
|
341
|
+
### Schema Validation
|
|
342
|
+
|
|
343
|
+
```typescript
|
|
344
|
+
import { createStandardSchemaValidator } from "@wizardkit/core";
|
|
345
|
+
|
|
346
|
+
step.validate = createStandardSchemaValidator(myValibotSchema);
|
|
347
|
+
```
|
|
348
|
+
|
|
349
|
+
### Async Validation
|
|
350
|
+
|
|
351
|
+
```typescript
|
|
352
|
+
validate: async (data, ctx) => {
|
|
353
|
+
const isAvailable = await ctx.api.checkEmail(data.email);
|
|
354
|
+
return {
|
|
355
|
+
valid: isAvailable,
|
|
356
|
+
errors: isAvailable ? undefined : { email: "Already taken" },
|
|
357
|
+
};
|
|
358
|
+
};
|
|
359
|
+
```
|
|
360
|
+
|
|
361
|
+
## Navigation
|
|
362
|
+
|
|
363
|
+
```typescript
|
|
364
|
+
const machine = new WizardMachine(definition, context, initialData);
|
|
365
|
+
|
|
366
|
+
// Move forward
|
|
367
|
+
await machine.goNext();
|
|
368
|
+
|
|
369
|
+
// Move backward (uses history stack — always returns to actual previous step)
|
|
370
|
+
await machine.goPrevious();
|
|
371
|
+
|
|
372
|
+
// Jump to specific step (validates current step, checks target guards)
|
|
373
|
+
await machine.goTo("step-id");
|
|
374
|
+
|
|
375
|
+
// Jump without validation
|
|
376
|
+
await machine.goTo("step-id", { skipValidation: true });
|
|
377
|
+
|
|
378
|
+
// @deprecated — use goTo() instead
|
|
379
|
+
await machine.goToStep("step-id");
|
|
380
|
+
|
|
381
|
+
// Navigation history
|
|
382
|
+
console.log(machine.snapshot.canGoBack); // true if history has entries
|
|
383
|
+
console.log(machine.history); // full history stack
|
|
384
|
+
machine.clearHistory(); // reset history to current step only
|
|
385
|
+
|
|
386
|
+
// Query navigation
|
|
387
|
+
const canGo = await machine.canNavigateToStep("step-id");
|
|
388
|
+
const nextId = await machine.getNextStepId();
|
|
389
|
+
const available = await machine.getAvailableSteps();
|
|
390
|
+
```
|
|
391
|
+
|
|
392
|
+
## Context and Dependencies
|
|
393
|
+
|
|
394
|
+
Pass utilities to validators and hooks via context:
|
|
395
|
+
|
|
396
|
+
```typescript
|
|
397
|
+
interface MyContext extends WizardContext {
|
|
398
|
+
api: ApiClient;
|
|
399
|
+
logger: Logger;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
const context: MyContext = {
|
|
403
|
+
api: new ApiClient(),
|
|
404
|
+
logger: console,
|
|
405
|
+
};
|
|
406
|
+
|
|
407
|
+
const machine = new WizardMachine(definition, context, initialData);
|
|
408
|
+
|
|
409
|
+
// Use in validators
|
|
410
|
+
const step = {
|
|
411
|
+
validate: async (data, ctx) => {
|
|
412
|
+
const myCtx = ctx as MyContext;
|
|
413
|
+
const isValid = await myCtx.api.validate(data);
|
|
414
|
+
return { valid: isValid };
|
|
415
|
+
},
|
|
416
|
+
};
|
|
417
|
+
```
|
|
418
|
+
|
|
419
|
+
## Events and Reactivity
|
|
420
|
+
|
|
421
|
+
Subscribe to state changes:
|
|
422
|
+
|
|
423
|
+
```typescript
|
|
424
|
+
const machine = new WizardMachine(definition, context, initialData, {
|
|
425
|
+
onStateChange: (state) => {
|
|
426
|
+
// Update UI
|
|
427
|
+
},
|
|
428
|
+
onStepEnter: (stepId, data) => {
|
|
429
|
+
// Load data for step
|
|
430
|
+
},
|
|
431
|
+
onValidation: (result) => {
|
|
432
|
+
// Show validation errors
|
|
433
|
+
},
|
|
434
|
+
onComplete: (data) => {
|
|
435
|
+
// Handle completion
|
|
436
|
+
},
|
|
437
|
+
onError: (error) => {
|
|
438
|
+
// Handle errors
|
|
439
|
+
},
|
|
440
|
+
});
|
|
441
|
+
```
|
|
442
|
+
|
|
443
|
+
## TypeScript Support
|
|
444
|
+
|
|
445
|
+
Everything is generic over your data type:
|
|
446
|
+
|
|
447
|
+
```typescript
|
|
448
|
+
// Define once
|
|
449
|
+
type SignupData = {
|
|
450
|
+
name: string;
|
|
451
|
+
email: string;
|
|
452
|
+
plan: "basic" | "pro";
|
|
453
|
+
};
|
|
454
|
+
|
|
455
|
+
// Use everywhere
|
|
456
|
+
const wizard: WizardDefinition<SignupData> = { /* ... */ };
|
|
457
|
+
const machine: WizardMachine<SignupData> = new WizardMachine(wizard, ...);
|
|
458
|
+
|
|
459
|
+
// Types are inferred
|
|
460
|
+
machine.snapshot.data.name; // ✅ Works
|
|
461
|
+
machine.snapshot.data.unknown; // ❌ TypeScript error
|
|
462
|
+
```
|
|
463
|
+
|
|
464
|
+
## Documentation
|
|
465
|
+
|
|
466
|
+
- [Getting Started](https://wizardkit.dev/guide/getting-started) - Quick introduction
|
|
467
|
+
- [Core Concepts](https://wizardkit.dev/guide/core-concepts) - Understand the fundamentals
|
|
468
|
+
- [Defining Wizards](https://wizardkit.dev/guide/defining-wizards) - Different definition patterns
|
|
469
|
+
- [API Reference](https://wizardkit.dev/guide/api/core) - Complete API documentation
|
|
470
|
+
|
|
471
|
+
## No Dependencies
|
|
472
|
+
|
|
473
|
+
The core library has **zero dependencies**. Optional schema validation requires Standard Schema implementations like:
|
|
474
|
+
|
|
475
|
+
- Valibot
|
|
476
|
+
- ArkType
|
|
477
|
+
- Zod (with adapter)
|
|
478
|
+
|
|
479
|
+
## License
|
|
480
|
+
|
|
481
|
+
MIT
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { StandardSchemaV1 } from '@standard-schema/spec';
|
|
2
|
+
import { RequiredFieldsOptions, StandardSchemaValidatorOptions } from '../machine/validators';
|
|
3
|
+
import { StepId, WizardContext, WizardData } from '../types/base';
|
|
4
|
+
import { LifecycleHook, StepLoader, StepMeta, SubmitHandler, Validator, WizardStepDefinition } from '../types/step';
|
|
5
|
+
import { StepGuard, StepTransition } from '../types/transitions';
|
|
6
|
+
/**
|
|
7
|
+
* Fluent builder for creating wizard steps
|
|
8
|
+
*/
|
|
9
|
+
export declare class StepBuilder<T extends WizardData> {
|
|
10
|
+
private step;
|
|
11
|
+
constructor(id: StepId);
|
|
12
|
+
/**
|
|
13
|
+
* Sets the previous step transition
|
|
14
|
+
*/
|
|
15
|
+
previous(transition: StepTransition<T> | StepId): this;
|
|
16
|
+
/**
|
|
17
|
+
* Sets the next step transition
|
|
18
|
+
*/
|
|
19
|
+
next(transition: StepTransition<T> | StepId): this;
|
|
20
|
+
/**
|
|
21
|
+
* Sets conditional next step
|
|
22
|
+
*/
|
|
23
|
+
nextWhen(branches: Array<{
|
|
24
|
+
when: StepGuard<T>;
|
|
25
|
+
to: StepId;
|
|
26
|
+
}>): this;
|
|
27
|
+
/**
|
|
28
|
+
* Sets dynamic next step resolver
|
|
29
|
+
*/
|
|
30
|
+
nextResolver(resolver: (data: T, ctx: WizardContext) => Promise<StepId | null> | StepId | null): this;
|
|
31
|
+
/**
|
|
32
|
+
* Sets step enabled state or guard
|
|
33
|
+
*/
|
|
34
|
+
enabled(value: boolean | StepGuard<T>): this;
|
|
35
|
+
/**
|
|
36
|
+
* Sets step validator
|
|
37
|
+
*/
|
|
38
|
+
validate(validator: Validator<T>): this;
|
|
39
|
+
/**
|
|
40
|
+
* Sets validator using a Standard Schema definition
|
|
41
|
+
*/
|
|
42
|
+
validateWithSchema<Schema extends StandardSchemaV1>(schema: Schema, options?: StandardSchemaValidatorOptions): this;
|
|
43
|
+
/**
|
|
44
|
+
* Adds validation for required fields (composes with existing validators)
|
|
45
|
+
* @example
|
|
46
|
+
* step.required('firstName', 'lastName')
|
|
47
|
+
* step.required('email', { messages: { email: 'Please enter your email' } })
|
|
48
|
+
*/
|
|
49
|
+
required(...args: Array<keyof T | RequiredFieldsOptions<T>>): this;
|
|
50
|
+
/**
|
|
51
|
+
* Sets onEnter lifecycle hook
|
|
52
|
+
*/
|
|
53
|
+
onEnter(hook: LifecycleHook<T>): this;
|
|
54
|
+
/**
|
|
55
|
+
* Sets onLeave lifecycle hook
|
|
56
|
+
*/
|
|
57
|
+
onLeave(hook: LifecycleHook<T>): this;
|
|
58
|
+
/**
|
|
59
|
+
* Sets submit handler
|
|
60
|
+
*/
|
|
61
|
+
onSubmit(handler: SubmitHandler<T>): this;
|
|
62
|
+
/**
|
|
63
|
+
* Loads this step's implementation (validate / onEnter / onLeave /
|
|
64
|
+
* onSubmit) lazily on first use (WIZ-013), e.g.
|
|
65
|
+
* `.lazy(() => import("./steps/documents"))`. Transitions, `enabled` and
|
|
66
|
+
* meta set on this builder stay eager. Hooks set on this builder are
|
|
67
|
+
* composed with the loaded ones, not replaced: e.g. `.required(...)` still
|
|
68
|
+
* applies alongside a loaded `validate` (both must pass), and an eager
|
|
69
|
+
* `onEnter` runs before the loaded `onEnter`.
|
|
70
|
+
*/
|
|
71
|
+
lazy(loader: StepLoader<T>): this;
|
|
72
|
+
/**
|
|
73
|
+
* Sets step metadata
|
|
74
|
+
*/
|
|
75
|
+
meta(meta: StepMeta): this;
|
|
76
|
+
/**
|
|
77
|
+
* Sets step title
|
|
78
|
+
*/
|
|
79
|
+
title(title: string): this;
|
|
80
|
+
/**
|
|
81
|
+
* Sets step description
|
|
82
|
+
*/
|
|
83
|
+
description(description: string): this;
|
|
84
|
+
/**
|
|
85
|
+
* Sets step icon
|
|
86
|
+
*/
|
|
87
|
+
icon(icon: string): this;
|
|
88
|
+
/**
|
|
89
|
+
* Builds the final step definition
|
|
90
|
+
*/
|
|
91
|
+
build(): WizardStepDefinition<T>;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Creates a new step builder
|
|
95
|
+
*/
|
|
96
|
+
export declare function createStep<T extends WizardData>(id: StepId): StepBuilder<T>;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { StepId, WizardData } from '../types/base';
|
|
2
|
+
import { CompleteHandler, WizardDefinition } from '../types/definition';
|
|
3
|
+
import { SubmitHandler, Validator, WizardStepDefinition } from '../types/step';
|
|
4
|
+
import { StepBuilder } from './create-step';
|
|
5
|
+
/**
|
|
6
|
+
* Fluent builder for creating wizards
|
|
7
|
+
*/
|
|
8
|
+
export declare class WizardBuilder<T extends WizardData> {
|
|
9
|
+
private id;
|
|
10
|
+
private initialStepId?;
|
|
11
|
+
private steps;
|
|
12
|
+
private completeHandler?;
|
|
13
|
+
private cancelHandler?;
|
|
14
|
+
constructor(id: string);
|
|
15
|
+
/**
|
|
16
|
+
* Sets the initial step
|
|
17
|
+
*/
|
|
18
|
+
initialStep(stepId: StepId): this;
|
|
19
|
+
/**
|
|
20
|
+
* Adds a step to the wizard
|
|
21
|
+
*/
|
|
22
|
+
addStep(step: WizardStepDefinition<T>): this;
|
|
23
|
+
/**
|
|
24
|
+
* Adds a step using a builder
|
|
25
|
+
*/
|
|
26
|
+
step(id: StepId, configure: (builder: StepBuilder<T>) => void): this;
|
|
27
|
+
/**
|
|
28
|
+
* Creates a linear sequence of steps
|
|
29
|
+
*/
|
|
30
|
+
sequence(stepConfigs: Array<{
|
|
31
|
+
id: StepId;
|
|
32
|
+
configure?: (builder: StepBuilder<T>) => void;
|
|
33
|
+
}>): this;
|
|
34
|
+
/**
|
|
35
|
+
* Sets the completion handler
|
|
36
|
+
*/
|
|
37
|
+
onComplete(handler: CompleteHandler<T>): this;
|
|
38
|
+
/**
|
|
39
|
+
* Sets the cancel handler. Called by `WizardMachine.cancel()` before reset.
|
|
40
|
+
*/
|
|
41
|
+
onCancel(handler: CompleteHandler<T>): this;
|
|
42
|
+
/**
|
|
43
|
+
* Builds the final wizard definition
|
|
44
|
+
*/
|
|
45
|
+
build(): WizardDefinition<T>;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Creates a new wizard builder
|
|
49
|
+
*/
|
|
50
|
+
export declare function createWizard<T extends WizardData>(id: string): WizardBuilder<T>;
|
|
51
|
+
/**
|
|
52
|
+
* Helper to create a simple linear wizard
|
|
53
|
+
*/
|
|
54
|
+
export declare function createLinearWizard<T extends WizardData>(config: {
|
|
55
|
+
id: string;
|
|
56
|
+
steps: Array<{
|
|
57
|
+
id: StepId;
|
|
58
|
+
title?: string;
|
|
59
|
+
description?: string;
|
|
60
|
+
validate?: Validator<T>;
|
|
61
|
+
onSubmit?: SubmitHandler<T>;
|
|
62
|
+
}>;
|
|
63
|
+
onComplete?: CompleteHandler<T>;
|
|
64
|
+
}): WizardDefinition<T>;
|