@ttoss/react-wizard 0.12.0 → 0.13.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 +23 -23
- package/dist/index.cjs +109 -58
- package/dist/index.d.cts +7 -3
- package/dist/index.d.mts +7 -3
- package/dist/index.mjs +109 -58
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -53,17 +53,17 @@ Labels are customizable, so applications can localize them as needed.
|
|
|
53
53
|
|
|
54
54
|
## Props
|
|
55
55
|
|
|
56
|
-
| Prop | Type | Default | Description
|
|
57
|
-
| ---------------- | ----------------------------------------------------------------------------------- | -------------------- |
|
|
58
|
-
| `steps` | `WizardStep[]` | — | Array of step definitions.
|
|
59
|
-
| `layout` | `'top' \| 'right' \| 'bottom' \| 'left'` | `'top'` | Position of the step list relative to content.
|
|
60
|
-
| `variant` | `'spotlight-accent' \| 'spotlight-primary' \| 'primary' \| 'secondary' \| 'accent'` | `'spotlight-accent'` | Visual variant for the shell, step accents, and primary action.
|
|
61
|
-
| `onComplete` | `() => void` | — | Called when the user finishes the last step.
|
|
62
|
-
| `onCancel` | `() => void` | — | Called on cancel. If omitted, cancel button is hidden.
|
|
63
|
-
| `onStepChange` | `(params: { stepIndex: number }) => void` | — | Called when the active step changes.
|
|
64
|
-
| `initialStep` | `number` | `0` | The initially active step index.
|
|
65
|
-
| `allowStepClick` | `boolean` | `true` | Allow clicking completed
|
|
66
|
-
| `labels` | `Partial<{ previous: string; next: string; finish: string; cancel: string }>` | — | Override the built-in navigation labels.
|
|
56
|
+
| Prop | Type | Default | Description |
|
|
57
|
+
| ---------------- | ----------------------------------------------------------------------------------- | -------------------- | --------------------------------------------------------------------------------------------------------------- |
|
|
58
|
+
| `steps` | `WizardStep[]` | — | Array of step definitions. |
|
|
59
|
+
| `layout` | `'top' \| 'right' \| 'bottom' \| 'left'` | `'top'` | Position of the step list relative to content. |
|
|
60
|
+
| `variant` | `'spotlight-accent' \| 'spotlight-primary' \| 'primary' \| 'secondary' \| 'accent'` | `'spotlight-accent'` | Visual variant for the shell, step accents, and primary action. |
|
|
61
|
+
| `onComplete` | `() => void` | — | Called when the user finishes the last step. |
|
|
62
|
+
| `onCancel` | `() => void` | — | Called on cancel. If omitted, cancel button is hidden. |
|
|
63
|
+
| `onStepChange` | `(params: { stepIndex: number }) => void` | — | Called when the active step changes. |
|
|
64
|
+
| `initialStep` | `number` | `0` | The initially active step index. |
|
|
65
|
+
| `allowStepClick` | `boolean` | `true` | Allow clicking a completed step (its number or its name): back, or forward to a step reached before going back. |
|
|
66
|
+
| `labels` | `Partial<{ previous: string; next: string; finish: string; cancel: string }>` | — | Override the built-in navigation labels. |
|
|
67
67
|
|
|
68
68
|
## WizardStep
|
|
69
69
|
|
|
@@ -111,18 +111,18 @@ const StepContent = () => {
|
|
|
111
111
|
|
|
112
112
|
### useWizard Return Value
|
|
113
113
|
|
|
114
|
-
| Property | Type | Description
|
|
115
|
-
| ------------------- | ------------------------------------------------------- |
|
|
116
|
-
| `currentStep` | `number` | Current active step index.
|
|
117
|
-
| `totalSteps` | `number` | Total number of steps.
|
|
118
|
-
| `goToNext` | `() => Promise<void>` | Navigate to the next step.
|
|
119
|
-
| `goToPrevious` | `() => void` | Navigate to the previous step.
|
|
120
|
-
| `goToStep` | `(params: { stepIndex: number }) => void
|
|
121
|
-
| `isFirstStep` | `boolean` | Whether the wizard is on the first step.
|
|
122
|
-
| `isLastStep` | `boolean` | Whether the wizard is on the last step.
|
|
123
|
-
| `isNavigating` | `boolean` | Whether `goToNext` is waiting on the step's validation or `onNext`; while true the next/finish button loads and every navigation is refused.
|
|
124
|
-
| `getStepStatus` | `(params: { stepIndex: number }) => WizardStepStatus` | Get the status of a step by index.
|
|
125
|
-
| `setStepValidation` | `(validate: () => boolean \| Promise<boolean>) => void` | Register a validation function for the current step content.
|
|
114
|
+
| Property | Type | Description |
|
|
115
|
+
| ------------------- | ------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
116
|
+
| `currentStep` | `number` | Current active step index. |
|
|
117
|
+
| `totalSteps` | `number` | Total number of steps. |
|
|
118
|
+
| `goToNext` | `() => Promise<void>` | Navigate to the next step. |
|
|
119
|
+
| `goToPrevious` | `() => void` | Navigate to the previous step. |
|
|
120
|
+
| `goToStep` | `(params: { stepIndex: number }) => Promise<void>` | Navigate to a specific step: back, or forward to a step reached before going back, running each step's validation and `onNext` on the way and stopping on the first refusal. |
|
|
121
|
+
| `isFirstStep` | `boolean` | Whether the wizard is on the first step. |
|
|
122
|
+
| `isLastStep` | `boolean` | Whether the wizard is on the last step. |
|
|
123
|
+
| `isNavigating` | `boolean` | Whether `goToNext` is waiting on the step's validation or `onNext`; while true the next/finish button loads and every navigation is refused. |
|
|
124
|
+
| `getStepStatus` | `(params: { stepIndex: number }) => WizardStepStatus` | Get the status of a step by index. |
|
|
125
|
+
| `setStepValidation` | `(validate: () => boolean \| Promise<boolean>) => void` | Register a validation function for the current step content. |
|
|
126
126
|
|
|
127
127
|
## Example: Form with Step-by-Step Validation
|
|
128
128
|
|
package/dist/index.cjs
CHANGED
|
@@ -194,13 +194,23 @@ var getWizardStepIndicatorSx = ({
|
|
|
194
194
|
} : {})
|
|
195
195
|
};
|
|
196
196
|
};
|
|
197
|
+
/**
|
|
198
|
+
* A vertical list's separators share whatever height the content leaves, so
|
|
199
|
+
* next to a short step they would shrink to nothing and the steps would touch:
|
|
200
|
+
* each keeps at least this much, and the wizard grows instead.
|
|
201
|
+
*/
|
|
202
|
+
var VERTICAL_SEPARATOR_MIN_HEIGHT = "1.5rem";
|
|
197
203
|
var getWizardStepSeparatorSx = ({
|
|
198
204
|
isCompleted,
|
|
205
|
+
isVertical = false,
|
|
199
206
|
variant = "spotlight-accent"
|
|
200
207
|
}) => {
|
|
201
208
|
return {
|
|
202
209
|
backgroundColor: getVariantStyles(variant).accentTextColor,
|
|
203
|
-
opacity: isCompleted ? 1 : .4
|
|
210
|
+
opacity: isCompleted ? 1 : .4,
|
|
211
|
+
...(isVertical ? {
|
|
212
|
+
minHeight: VERTICAL_SEPARATOR_MIN_HEIGHT
|
|
213
|
+
} : {})
|
|
204
214
|
};
|
|
205
215
|
};
|
|
206
216
|
var getWizardStepTitleSx = ({
|
|
@@ -250,6 +260,8 @@ var useWizard = () => {
|
|
|
250
260
|
|
|
251
261
|
//#endregion
|
|
252
262
|
//#region src/WizardStepList.tsx
|
|
263
|
+
/** A completed step's indicator: a symbol, not copy. */
|
|
264
|
+
var COMPLETED_MARK = "✓";
|
|
253
265
|
var messages$1 = (0, _ttoss_react_i18n.defineMessages)({
|
|
254
266
|
stepListLabel: {
|
|
255
267
|
defaultMessage: "Wizard steps",
|
|
@@ -364,42 +376,35 @@ var WizardStepList = ({
|
|
|
364
376
|
return /* @__PURE__ */(0, react_jsx_runtime.jsxs)(_chakra_ui_react.Steps.Item, {
|
|
365
377
|
index,
|
|
366
378
|
children: [/* @__PURE__ */(0, react_jsx_runtime.jsxs)(_ttoss_ui.Flex, {
|
|
379
|
+
role: "button",
|
|
380
|
+
tabIndex: isClickable ? 0 : -1,
|
|
381
|
+
onClick: () => {
|
|
382
|
+
if (isClickable) onStepClick({
|
|
383
|
+
stepIndex: index
|
|
384
|
+
});
|
|
385
|
+
},
|
|
386
|
+
onKeyDown: e => {
|
|
387
|
+
if (isClickable && (e.key === "Enter" || e.key === " ")) {
|
|
388
|
+
e.preventDefault();
|
|
389
|
+
onStepClick({
|
|
390
|
+
stepIndex: index
|
|
391
|
+
});
|
|
392
|
+
}
|
|
393
|
+
},
|
|
394
|
+
"aria-current": status === "active" ? "step" : void 0,
|
|
367
395
|
sx: {
|
|
368
396
|
flexDirection: isHorizontal ? "column" : "row",
|
|
369
397
|
alignItems: "center",
|
|
370
|
-
gap: "2"
|
|
398
|
+
gap: "2",
|
|
399
|
+
cursor: isClickable ? "pointer" : "default"
|
|
371
400
|
},
|
|
372
|
-
children: [/* @__PURE__ */(0, react_jsx_runtime.jsx)(
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
},
|
|
380
|
-
onKeyDown: e => {
|
|
381
|
-
if (isClickable && (e.key === "Enter" || e.key === " ")) {
|
|
382
|
-
e.preventDefault();
|
|
383
|
-
onStepClick({
|
|
384
|
-
stepIndex: index
|
|
385
|
-
});
|
|
386
|
-
}
|
|
387
|
-
},
|
|
388
|
-
"aria-current": status === "active" ? "step" : void 0,
|
|
389
|
-
sx: {
|
|
390
|
-
cursor: isClickable ? "pointer" : "default"
|
|
391
|
-
},
|
|
392
|
-
children: /* @__PURE__ */(0, react_jsx_runtime.jsx)(_chakra_ui_react.Steps.Indicator, {
|
|
393
|
-
css: getWizardStepIndicatorSx({
|
|
394
|
-
status,
|
|
395
|
-
variant,
|
|
396
|
-
isClickable
|
|
397
|
-
}),
|
|
398
|
-
children: /* @__PURE__ */(0, react_jsx_runtime.jsx)(_chakra_ui_react.Steps.Status, {
|
|
399
|
-
complete: "✓",
|
|
400
|
-
incomplete: /* @__PURE__ */(0, react_jsx_runtime.jsx)(_chakra_ui_react.Steps.Number, {})
|
|
401
|
-
})
|
|
402
|
-
})
|
|
401
|
+
children: [/* @__PURE__ */(0, react_jsx_runtime.jsx)(_chakra_ui_react.Steps.Indicator, {
|
|
402
|
+
css: getWizardStepIndicatorSx({
|
|
403
|
+
status,
|
|
404
|
+
variant,
|
|
405
|
+
isClickable
|
|
406
|
+
}),
|
|
407
|
+
children: status === "completed" ? COMPLETED_MARK : /* @__PURE__ */(0, react_jsx_runtime.jsx)(_chakra_ui_react.Steps.Number, {})
|
|
403
408
|
}), /* @__PURE__ */(0, react_jsx_runtime.jsx)(WizardStepText, {
|
|
404
409
|
step,
|
|
405
410
|
status,
|
|
@@ -408,7 +413,10 @@ var WizardStepList = ({
|
|
|
408
413
|
})]
|
|
409
414
|
}), index < steps.length - 1 && /* @__PURE__ */(0, react_jsx_runtime.jsx)(_chakra_ui_react.Steps.Separator, {
|
|
410
415
|
css: getWizardStepSeparatorSx({
|
|
411
|
-
isCompleted:
|
|
416
|
+
isCompleted: getStepStatus({
|
|
417
|
+
stepIndex: index + 1
|
|
418
|
+
}) !== "upcoming",
|
|
419
|
+
isVertical: !isHorizontal,
|
|
412
420
|
variant
|
|
413
421
|
})
|
|
414
422
|
})]
|
|
@@ -475,6 +483,12 @@ var useWizardNavigation = ({
|
|
|
475
483
|
onStepChange
|
|
476
484
|
}) => {
|
|
477
485
|
const [currentStep, setCurrentStep] = react.useState(initialStep);
|
|
486
|
+
/**
|
|
487
|
+
* The furthest step the user has reached (the initial one counts): going
|
|
488
|
+
* back keeps every step up to it completed, so the user can return to it
|
|
489
|
+
* without clicking "Next" through each one.
|
|
490
|
+
*/
|
|
491
|
+
const [furthestStep, setFurthestStep] = react.useState(initialStep);
|
|
478
492
|
const [isNavigating, setIsNavigating] = react.useState(false);
|
|
479
493
|
/**
|
|
480
494
|
* The state above re-renders the buttons; this ref is what refuses a second
|
|
@@ -490,8 +504,20 @@ var useWizardNavigation = ({
|
|
|
490
504
|
}) => {
|
|
491
505
|
if (stepIndex < currentStep) return "completed";
|
|
492
506
|
if (stepIndex === currentStep) return "active";
|
|
493
|
-
return "upcoming";
|
|
494
|
-
}, [currentStep]);
|
|
507
|
+
return stepIndex <= furthestStep ? "completed" : "upcoming";
|
|
508
|
+
}, [currentStep, furthestStep]);
|
|
509
|
+
const moveTo = react.useCallback(({
|
|
510
|
+
stepIndex
|
|
511
|
+
}) => {
|
|
512
|
+
stepValidationRef.current = null;
|
|
513
|
+
setCurrentStep(stepIndex);
|
|
514
|
+
setFurthestStep(furthest => {
|
|
515
|
+
return Math.max(furthest, stepIndex);
|
|
516
|
+
});
|
|
517
|
+
onStepChange?.({
|
|
518
|
+
stepIndex
|
|
519
|
+
});
|
|
520
|
+
}, [onStepChange]);
|
|
495
521
|
const canLeaveStep = react.useCallback(async () => {
|
|
496
522
|
if (stepValidationRef.current && !(await stepValidationRef.current())) return false;
|
|
497
523
|
const {
|
|
@@ -505,40 +531,65 @@ var useWizardNavigation = ({
|
|
|
505
531
|
setIsNavigating(true);
|
|
506
532
|
try {
|
|
507
533
|
if (!(await canLeaveStep())) return;
|
|
508
|
-
if (isLastStep) onComplete?.();else {
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
setCurrentStep(nextStep);
|
|
512
|
-
onStepChange?.({
|
|
513
|
-
stepIndex: nextStep
|
|
514
|
-
});
|
|
515
|
-
}
|
|
534
|
+
if (isLastStep) onComplete?.();else moveTo({
|
|
535
|
+
stepIndex: currentStep + 1
|
|
536
|
+
});
|
|
516
537
|
} finally {
|
|
517
538
|
navigatingRef.current = false;
|
|
518
539
|
setIsNavigating(false);
|
|
519
540
|
}
|
|
520
|
-
}, [canLeaveStep, currentStep, isLastStep, onComplete,
|
|
541
|
+
}, [canLeaveStep, currentStep, isLastStep, onComplete, moveTo]);
|
|
521
542
|
const goToPrevious = react.useCallback(() => {
|
|
522
|
-
if (!isFirstStep && !navigatingRef.current) {
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
543
|
+
if (!isFirstStep && !navigatingRef.current) moveTo({
|
|
544
|
+
stepIndex: currentStep - 1
|
|
545
|
+
});
|
|
546
|
+
}, [currentStep, isFirstStep, moveTo]);
|
|
547
|
+
/**
|
|
548
|
+
* Forward to a step already reached, as if clicking "Next" through each
|
|
549
|
+
* step on the way: the current step's validation and `onNext`, then each
|
|
550
|
+
* step's `onNext` in order. The first refusal stops there, on the step that
|
|
551
|
+
* refused, so the user sees why.
|
|
552
|
+
*/
|
|
553
|
+
const goForwardTo = react.useCallback(async ({
|
|
554
|
+
stepIndex
|
|
555
|
+
}) => {
|
|
556
|
+
navigatingRef.current = true;
|
|
557
|
+
setIsNavigating(true);
|
|
558
|
+
try {
|
|
559
|
+
if (!(await canLeaveStep())) return;
|
|
560
|
+
for (let index = currentStep + 1; index < stepIndex; index += 1) {
|
|
561
|
+
const {
|
|
562
|
+
onNext
|
|
563
|
+
} = steps[index];
|
|
564
|
+
if (onNext && !(await onNext())) {
|
|
565
|
+
moveTo({
|
|
566
|
+
stepIndex: index
|
|
567
|
+
});
|
|
568
|
+
return;
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
moveTo({
|
|
572
|
+
stepIndex
|
|
528
573
|
});
|
|
574
|
+
} finally {
|
|
575
|
+
navigatingRef.current = false;
|
|
576
|
+
setIsNavigating(false);
|
|
529
577
|
}
|
|
530
|
-
}, [currentStep,
|
|
531
|
-
const goToStep = react.useCallback(({
|
|
578
|
+
}, [canLeaveStep, currentStep, steps, moveTo]);
|
|
579
|
+
const goToStep = react.useCallback(async ({
|
|
532
580
|
stepIndex
|
|
533
581
|
}) => {
|
|
534
|
-
if (
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
onStepChange?.({
|
|
582
|
+
if (navigatingRef.current || stepIndex < 0 || stepIndex >= totalSteps) return;
|
|
583
|
+
if (stepIndex <= currentStep) {
|
|
584
|
+
moveTo({
|
|
538
585
|
stepIndex
|
|
539
586
|
});
|
|
587
|
+
return;
|
|
540
588
|
}
|
|
541
|
-
|
|
589
|
+
if (stepIndex <= furthestStep) await goForwardTo({
|
|
590
|
+
stepIndex
|
|
591
|
+
});
|
|
592
|
+
}, [currentStep, furthestStep, totalSteps, moveTo, goForwardTo]);
|
|
542
593
|
const setStepValidation = react.useCallback(validate => {
|
|
543
594
|
stepValidationRef.current = validate;
|
|
544
595
|
}, []);
|
package/dist/index.d.cts
CHANGED
|
@@ -74,7 +74,8 @@ interface WizardProps {
|
|
|
74
74
|
*/
|
|
75
75
|
initialStep?: number;
|
|
76
76
|
/**
|
|
77
|
-
* Allow clicking on completed steps to navigate back
|
|
77
|
+
* Allow clicking on completed steps to navigate to them: back, or forward
|
|
78
|
+
* to a step already reached before going back.
|
|
78
79
|
* Defaults to true.
|
|
79
80
|
*/
|
|
80
81
|
allowStepClick?: boolean;
|
|
@@ -113,11 +114,14 @@ interface WizardContextValue {
|
|
|
113
114
|
*/
|
|
114
115
|
goToPrevious: () => void;
|
|
115
116
|
/**
|
|
116
|
-
* Navigate to a
|
|
117
|
+
* Navigate to a completed or active step. A step after the current one
|
|
118
|
+
* (reached before, then left by going back) is reached as if clicking
|
|
119
|
+
* "Next" through each step on the way: the first validation or `onNext`
|
|
120
|
+
* that refuses stops there.
|
|
117
121
|
*/
|
|
118
122
|
goToStep: (params: {
|
|
119
123
|
stepIndex: number;
|
|
120
|
-
}) => void
|
|
124
|
+
}) => Promise<void>;
|
|
121
125
|
/**
|
|
122
126
|
* Whether the wizard is on the first step.
|
|
123
127
|
*/
|
package/dist/index.d.mts
CHANGED
|
@@ -74,7 +74,8 @@ interface WizardProps {
|
|
|
74
74
|
*/
|
|
75
75
|
initialStep?: number;
|
|
76
76
|
/**
|
|
77
|
-
* Allow clicking on completed steps to navigate back
|
|
77
|
+
* Allow clicking on completed steps to navigate to them: back, or forward
|
|
78
|
+
* to a step already reached before going back.
|
|
78
79
|
* Defaults to true.
|
|
79
80
|
*/
|
|
80
81
|
allowStepClick?: boolean;
|
|
@@ -113,11 +114,14 @@ interface WizardContextValue {
|
|
|
113
114
|
*/
|
|
114
115
|
goToPrevious: () => void;
|
|
115
116
|
/**
|
|
116
|
-
* Navigate to a
|
|
117
|
+
* Navigate to a completed or active step. A step after the current one
|
|
118
|
+
* (reached before, then left by going back) is reached as if clicking
|
|
119
|
+
* "Next" through each step on the way: the first validation or `onNext`
|
|
120
|
+
* that refuses stops there.
|
|
117
121
|
*/
|
|
118
122
|
goToStep: (params: {
|
|
119
123
|
stepIndex: number;
|
|
120
|
-
}) => void
|
|
124
|
+
}) => Promise<void>;
|
|
121
125
|
/**
|
|
122
126
|
* Whether the wizard is on the first step.
|
|
123
127
|
*/
|
package/dist/index.mjs
CHANGED
|
@@ -163,13 +163,23 @@ var getWizardStepIndicatorSx = ({
|
|
|
163
163
|
} : {})
|
|
164
164
|
};
|
|
165
165
|
};
|
|
166
|
+
/**
|
|
167
|
+
* A vertical list's separators share whatever height the content leaves, so
|
|
168
|
+
* next to a short step they would shrink to nothing and the steps would touch:
|
|
169
|
+
* each keeps at least this much, and the wizard grows instead.
|
|
170
|
+
*/
|
|
171
|
+
var VERTICAL_SEPARATOR_MIN_HEIGHT = "1.5rem";
|
|
166
172
|
var getWizardStepSeparatorSx = ({
|
|
167
173
|
isCompleted,
|
|
174
|
+
isVertical = false,
|
|
168
175
|
variant = "spotlight-accent"
|
|
169
176
|
}) => {
|
|
170
177
|
return {
|
|
171
178
|
backgroundColor: getVariantStyles(variant).accentTextColor,
|
|
172
|
-
opacity: isCompleted ? 1 : .4
|
|
179
|
+
opacity: isCompleted ? 1 : .4,
|
|
180
|
+
...(isVertical ? {
|
|
181
|
+
minHeight: VERTICAL_SEPARATOR_MIN_HEIGHT
|
|
182
|
+
} : {})
|
|
173
183
|
};
|
|
174
184
|
};
|
|
175
185
|
var getWizardStepTitleSx = ({
|
|
@@ -219,6 +229,8 @@ var useWizard = () => {
|
|
|
219
229
|
|
|
220
230
|
//#endregion
|
|
221
231
|
//#region src/WizardStepList.tsx
|
|
232
|
+
/** A completed step's indicator: a symbol, not copy. */
|
|
233
|
+
var COMPLETED_MARK = "✓";
|
|
222
234
|
var messages$1 = defineMessages({
|
|
223
235
|
stepListLabel: {
|
|
224
236
|
id: "mVLnY8",
|
|
@@ -336,42 +348,35 @@ var WizardStepList = ({
|
|
|
336
348
|
return /* @__PURE__ */jsxs(Steps.Item, {
|
|
337
349
|
index,
|
|
338
350
|
children: [/* @__PURE__ */jsxs(Flex, {
|
|
351
|
+
role: "button",
|
|
352
|
+
tabIndex: isClickable ? 0 : -1,
|
|
353
|
+
onClick: () => {
|
|
354
|
+
if (isClickable) onStepClick({
|
|
355
|
+
stepIndex: index
|
|
356
|
+
});
|
|
357
|
+
},
|
|
358
|
+
onKeyDown: e => {
|
|
359
|
+
if (isClickable && (e.key === "Enter" || e.key === " ")) {
|
|
360
|
+
e.preventDefault();
|
|
361
|
+
onStepClick({
|
|
362
|
+
stepIndex: index
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
},
|
|
366
|
+
"aria-current": status === "active" ? "step" : void 0,
|
|
339
367
|
sx: {
|
|
340
368
|
flexDirection: isHorizontal ? "column" : "row",
|
|
341
369
|
alignItems: "center",
|
|
342
|
-
gap: "2"
|
|
370
|
+
gap: "2",
|
|
371
|
+
cursor: isClickable ? "pointer" : "default"
|
|
343
372
|
},
|
|
344
|
-
children: [/* @__PURE__ */jsx(
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
},
|
|
352
|
-
onKeyDown: e => {
|
|
353
|
-
if (isClickable && (e.key === "Enter" || e.key === " ")) {
|
|
354
|
-
e.preventDefault();
|
|
355
|
-
onStepClick({
|
|
356
|
-
stepIndex: index
|
|
357
|
-
});
|
|
358
|
-
}
|
|
359
|
-
},
|
|
360
|
-
"aria-current": status === "active" ? "step" : void 0,
|
|
361
|
-
sx: {
|
|
362
|
-
cursor: isClickable ? "pointer" : "default"
|
|
363
|
-
},
|
|
364
|
-
children: /* @__PURE__ */jsx(Steps.Indicator, {
|
|
365
|
-
css: getWizardStepIndicatorSx({
|
|
366
|
-
status,
|
|
367
|
-
variant,
|
|
368
|
-
isClickable
|
|
369
|
-
}),
|
|
370
|
-
children: /* @__PURE__ */jsx(Steps.Status, {
|
|
371
|
-
complete: "✓",
|
|
372
|
-
incomplete: /* @__PURE__ */jsx(Steps.Number, {})
|
|
373
|
-
})
|
|
374
|
-
})
|
|
373
|
+
children: [/* @__PURE__ */jsx(Steps.Indicator, {
|
|
374
|
+
css: getWizardStepIndicatorSx({
|
|
375
|
+
status,
|
|
376
|
+
variant,
|
|
377
|
+
isClickable
|
|
378
|
+
}),
|
|
379
|
+
children: status === "completed" ? COMPLETED_MARK : /* @__PURE__ */jsx(Steps.Number, {})
|
|
375
380
|
}), /* @__PURE__ */jsx(WizardStepText, {
|
|
376
381
|
step,
|
|
377
382
|
status,
|
|
@@ -380,7 +385,10 @@ var WizardStepList = ({
|
|
|
380
385
|
})]
|
|
381
386
|
}), index < steps.length - 1 && /* @__PURE__ */jsx(Steps.Separator, {
|
|
382
387
|
css: getWizardStepSeparatorSx({
|
|
383
|
-
isCompleted:
|
|
388
|
+
isCompleted: getStepStatus({
|
|
389
|
+
stepIndex: index + 1
|
|
390
|
+
}) !== "upcoming",
|
|
391
|
+
isVertical: !isHorizontal,
|
|
384
392
|
variant
|
|
385
393
|
})
|
|
386
394
|
})]
|
|
@@ -459,6 +467,12 @@ var useWizardNavigation = ({
|
|
|
459
467
|
onStepChange
|
|
460
468
|
}) => {
|
|
461
469
|
const [currentStep, setCurrentStep] = React.useState(initialStep);
|
|
470
|
+
/**
|
|
471
|
+
* The furthest step the user has reached (the initial one counts): going
|
|
472
|
+
* back keeps every step up to it completed, so the user can return to it
|
|
473
|
+
* without clicking "Next" through each one.
|
|
474
|
+
*/
|
|
475
|
+
const [furthestStep, setFurthestStep] = React.useState(initialStep);
|
|
462
476
|
const [isNavigating, setIsNavigating] = React.useState(false);
|
|
463
477
|
/**
|
|
464
478
|
* The state above re-renders the buttons; this ref is what refuses a second
|
|
@@ -474,8 +488,20 @@ var useWizardNavigation = ({
|
|
|
474
488
|
}) => {
|
|
475
489
|
if (stepIndex < currentStep) return "completed";
|
|
476
490
|
if (stepIndex === currentStep) return "active";
|
|
477
|
-
return "upcoming";
|
|
478
|
-
}, [currentStep]);
|
|
491
|
+
return stepIndex <= furthestStep ? "completed" : "upcoming";
|
|
492
|
+
}, [currentStep, furthestStep]);
|
|
493
|
+
const moveTo = React.useCallback(({
|
|
494
|
+
stepIndex
|
|
495
|
+
}) => {
|
|
496
|
+
stepValidationRef.current = null;
|
|
497
|
+
setCurrentStep(stepIndex);
|
|
498
|
+
setFurthestStep(furthest => {
|
|
499
|
+
return Math.max(furthest, stepIndex);
|
|
500
|
+
});
|
|
501
|
+
onStepChange?.({
|
|
502
|
+
stepIndex
|
|
503
|
+
});
|
|
504
|
+
}, [onStepChange]);
|
|
479
505
|
const canLeaveStep = React.useCallback(async () => {
|
|
480
506
|
if (stepValidationRef.current && !(await stepValidationRef.current())) return false;
|
|
481
507
|
const {
|
|
@@ -489,40 +515,65 @@ var useWizardNavigation = ({
|
|
|
489
515
|
setIsNavigating(true);
|
|
490
516
|
try {
|
|
491
517
|
if (!(await canLeaveStep())) return;
|
|
492
|
-
if (isLastStep) onComplete?.();else {
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
setCurrentStep(nextStep);
|
|
496
|
-
onStepChange?.({
|
|
497
|
-
stepIndex: nextStep
|
|
498
|
-
});
|
|
499
|
-
}
|
|
518
|
+
if (isLastStep) onComplete?.();else moveTo({
|
|
519
|
+
stepIndex: currentStep + 1
|
|
520
|
+
});
|
|
500
521
|
} finally {
|
|
501
522
|
navigatingRef.current = false;
|
|
502
523
|
setIsNavigating(false);
|
|
503
524
|
}
|
|
504
|
-
}, [canLeaveStep, currentStep, isLastStep, onComplete,
|
|
525
|
+
}, [canLeaveStep, currentStep, isLastStep, onComplete, moveTo]);
|
|
505
526
|
const goToPrevious = React.useCallback(() => {
|
|
506
|
-
if (!isFirstStep && !navigatingRef.current) {
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
527
|
+
if (!isFirstStep && !navigatingRef.current) moveTo({
|
|
528
|
+
stepIndex: currentStep - 1
|
|
529
|
+
});
|
|
530
|
+
}, [currentStep, isFirstStep, moveTo]);
|
|
531
|
+
/**
|
|
532
|
+
* Forward to a step already reached, as if clicking "Next" through each
|
|
533
|
+
* step on the way: the current step's validation and `onNext`, then each
|
|
534
|
+
* step's `onNext` in order. The first refusal stops there, on the step that
|
|
535
|
+
* refused, so the user sees why.
|
|
536
|
+
*/
|
|
537
|
+
const goForwardTo = React.useCallback(async ({
|
|
538
|
+
stepIndex
|
|
539
|
+
}) => {
|
|
540
|
+
navigatingRef.current = true;
|
|
541
|
+
setIsNavigating(true);
|
|
542
|
+
try {
|
|
543
|
+
if (!(await canLeaveStep())) return;
|
|
544
|
+
for (let index = currentStep + 1; index < stepIndex; index += 1) {
|
|
545
|
+
const {
|
|
546
|
+
onNext
|
|
547
|
+
} = steps[index];
|
|
548
|
+
if (onNext && !(await onNext())) {
|
|
549
|
+
moveTo({
|
|
550
|
+
stepIndex: index
|
|
551
|
+
});
|
|
552
|
+
return;
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
moveTo({
|
|
556
|
+
stepIndex
|
|
512
557
|
});
|
|
558
|
+
} finally {
|
|
559
|
+
navigatingRef.current = false;
|
|
560
|
+
setIsNavigating(false);
|
|
513
561
|
}
|
|
514
|
-
}, [currentStep,
|
|
515
|
-
const goToStep = React.useCallback(({
|
|
562
|
+
}, [canLeaveStep, currentStep, steps, moveTo]);
|
|
563
|
+
const goToStep = React.useCallback(async ({
|
|
516
564
|
stepIndex
|
|
517
565
|
}) => {
|
|
518
|
-
if (
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
onStepChange?.({
|
|
566
|
+
if (navigatingRef.current || stepIndex < 0 || stepIndex >= totalSteps) return;
|
|
567
|
+
if (stepIndex <= currentStep) {
|
|
568
|
+
moveTo({
|
|
522
569
|
stepIndex
|
|
523
570
|
});
|
|
571
|
+
return;
|
|
524
572
|
}
|
|
525
|
-
|
|
573
|
+
if (stepIndex <= furthestStep) await goForwardTo({
|
|
574
|
+
stepIndex
|
|
575
|
+
});
|
|
576
|
+
}, [currentStep, furthestStep, totalSteps, moveTo, goForwardTo]);
|
|
526
577
|
const setStepValidation = React.useCallback(validate => {
|
|
527
578
|
stepValidationRef.current = validate;
|
|
528
579
|
}, []);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ttoss/react-wizard",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.13.0",
|
|
4
4
|
"description": "A React wizard component for guiding users through multi-step flows with configurable step list layouts.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"React",
|
|
@@ -41,18 +41,18 @@
|
|
|
41
41
|
"react": "^19.3.0",
|
|
42
42
|
"tsdown": "^0.23.1",
|
|
43
43
|
"@ttoss/config": "^4.0.1",
|
|
44
|
-
"@ttoss/i18n-cli": "^0.16.1",
|
|
45
44
|
"@ttoss/react-i18n": "^4.0.2",
|
|
46
|
-
"@ttoss/ui": "^9.0.2",
|
|
47
45
|
"@ttoss/forms": "^0.53.3",
|
|
48
|
-
"@ttoss/test-utils": "^7.1.1"
|
|
46
|
+
"@ttoss/test-utils": "^7.1.1",
|
|
47
|
+
"@ttoss/ui": "^9.0.2",
|
|
48
|
+
"@ttoss/i18n-cli": "^0.16.1"
|
|
49
49
|
},
|
|
50
50
|
"peerDependencies": {
|
|
51
51
|
"@chakra-ui/react": "^3",
|
|
52
52
|
"react": ">=16.8.0",
|
|
53
53
|
"react-icons": "^5",
|
|
54
|
-
"@ttoss/
|
|
55
|
-
"@ttoss/
|
|
54
|
+
"@ttoss/ui": "^9.0.2",
|
|
55
|
+
"@ttoss/react-i18n": "^4.0.2"
|
|
56
56
|
},
|
|
57
57
|
"publishConfig": {
|
|
58
58
|
"access": "public",
|