@ttoss/react-wizard 0.11.4 → 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 +30 -28
- package/dist/index.cjs +137 -65
- package/dist/index.d.cts +16 -4
- package/dist/index.d.mts +16 -4
- package/dist/index.mjs +137 -65
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -53,26 +53,26 @@ 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
|
|
|
70
|
-
| Property | Type | Description
|
|
71
|
-
| ------------- | ----------------------------------- |
|
|
72
|
-
| `title` | `string` | Title displayed in the step list.
|
|
73
|
-
| `description` | `string` | Optional description below the title.
|
|
74
|
-
| `content` | `ReactNode` | Content rendered when the step is active.
|
|
75
|
-
| `onNext` | `() => boolean \| Promise<boolean>` | Validation callback. Return `false` to prevent advancing. |
|
|
70
|
+
| Property | Type | Description |
|
|
71
|
+
| ------------- | ----------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
72
|
+
| `title` | `string` | Title displayed in the step list. |
|
|
73
|
+
| `description` | `string` | Optional description below the title. |
|
|
74
|
+
| `content` | `ReactNode` | Content rendered when the step is active. |
|
|
75
|
+
| `onNext` | `() => boolean \| Promise<boolean>` | Validation callback. Return `false` to prevent advancing. While a returned Promise is pending, the next/finish button shows its loading state and the wizard refuses any other navigation. |
|
|
76
76
|
|
|
77
77
|
## Layouts
|
|
78
78
|
|
|
@@ -97,6 +97,7 @@ const StepContent = () => {
|
|
|
97
97
|
goToNext,
|
|
98
98
|
goToPrevious,
|
|
99
99
|
isLastStep,
|
|
100
|
+
isNavigating,
|
|
100
101
|
setStepValidation,
|
|
101
102
|
} = useWizard();
|
|
102
103
|
|
|
@@ -110,17 +111,18 @@ const StepContent = () => {
|
|
|
110
111
|
|
|
111
112
|
### useWizard Return Value
|
|
112
113
|
|
|
113
|
-
| Property | Type | Description
|
|
114
|
-
| ------------------- | ------------------------------------------------------- |
|
|
115
|
-
| `currentStep` | `number` | Current active step index.
|
|
116
|
-
| `totalSteps` | `number` | Total number of steps.
|
|
117
|
-
| `goToNext` | `() => Promise<void>` | Navigate to the next step.
|
|
118
|
-
| `goToPrevious` | `() => void` | Navigate to the previous step.
|
|
119
|
-
| `goToStep` | `(params: { stepIndex: number }) => void
|
|
120
|
-
| `isFirstStep` | `boolean` | Whether the wizard is on the first step.
|
|
121
|
-
| `isLastStep` | `boolean` | Whether the wizard is on the last step.
|
|
122
|
-
| `
|
|
123
|
-
| `
|
|
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. |
|
|
124
126
|
|
|
125
127
|
## Example: Form with Step-by-Step Validation
|
|
126
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,18 @@ 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);
|
|
492
|
+
const [isNavigating, setIsNavigating] = react.useState(false);
|
|
493
|
+
/**
|
|
494
|
+
* The state above re-renders the buttons; this ref is what refuses a second
|
|
495
|
+
* click that lands before that render.
|
|
496
|
+
*/
|
|
497
|
+
const navigatingRef = react.useRef(false);
|
|
478
498
|
const stepValidationRef = react.useRef(null);
|
|
479
499
|
const totalSteps = steps.length;
|
|
480
500
|
const isFirstStep = currentStep === 0;
|
|
@@ -484,46 +504,92 @@ var useWizardNavigation = ({
|
|
|
484
504
|
}) => {
|
|
485
505
|
if (stepIndex < currentStep) return "completed";
|
|
486
506
|
if (stepIndex === currentStep) return "active";
|
|
487
|
-
return "upcoming";
|
|
488
|
-
}, [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]);
|
|
521
|
+
const canLeaveStep = react.useCallback(async () => {
|
|
522
|
+
if (stepValidationRef.current && !(await stepValidationRef.current())) return false;
|
|
523
|
+
const {
|
|
524
|
+
onNext
|
|
525
|
+
} = steps[currentStep];
|
|
526
|
+
return !onNext || (await onNext());
|
|
527
|
+
}, [currentStep, steps]);
|
|
489
528
|
const goToNext = react.useCallback(async () => {
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
if (
|
|
496
|
-
|
|
497
|
-
if (isLastStep) onComplete?.();else {
|
|
498
|
-
const nextStep = currentStep + 1;
|
|
499
|
-
stepValidationRef.current = null;
|
|
500
|
-
setCurrentStep(nextStep);
|
|
501
|
-
onStepChange?.({
|
|
502
|
-
stepIndex: nextStep
|
|
529
|
+
if (navigatingRef.current) return;
|
|
530
|
+
navigatingRef.current = true;
|
|
531
|
+
setIsNavigating(true);
|
|
532
|
+
try {
|
|
533
|
+
if (!(await canLeaveStep())) return;
|
|
534
|
+
if (isLastStep) onComplete?.();else moveTo({
|
|
535
|
+
stepIndex: currentStep + 1
|
|
503
536
|
});
|
|
537
|
+
} finally {
|
|
538
|
+
navigatingRef.current = false;
|
|
539
|
+
setIsNavigating(false);
|
|
504
540
|
}
|
|
505
|
-
}, [
|
|
541
|
+
}, [canLeaveStep, currentStep, isLastStep, onComplete, moveTo]);
|
|
506
542
|
const goToPrevious = react.useCallback(() => {
|
|
507
|
-
if (!isFirstStep) {
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
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
|
|
513
573
|
});
|
|
574
|
+
} finally {
|
|
575
|
+
navigatingRef.current = false;
|
|
576
|
+
setIsNavigating(false);
|
|
514
577
|
}
|
|
515
|
-
}, [currentStep,
|
|
516
|
-
const goToStep = react.useCallback(({
|
|
578
|
+
}, [canLeaveStep, currentStep, steps, moveTo]);
|
|
579
|
+
const goToStep = react.useCallback(async ({
|
|
517
580
|
stepIndex
|
|
518
581
|
}) => {
|
|
519
|
-
if (
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
onStepChange?.({
|
|
582
|
+
if (navigatingRef.current || stepIndex < 0 || stepIndex >= totalSteps) return;
|
|
583
|
+
if (stepIndex <= currentStep) {
|
|
584
|
+
moveTo({
|
|
523
585
|
stepIndex
|
|
524
586
|
});
|
|
587
|
+
return;
|
|
525
588
|
}
|
|
526
|
-
|
|
589
|
+
if (stepIndex <= furthestStep) await goForwardTo({
|
|
590
|
+
stepIndex
|
|
591
|
+
});
|
|
592
|
+
}, [currentStep, furthestStep, totalSteps, moveTo, goForwardTo]);
|
|
527
593
|
const setStepValidation = react.useCallback(validate => {
|
|
528
594
|
stepValidationRef.current = validate;
|
|
529
595
|
}, []);
|
|
@@ -536,10 +602,11 @@ var useWizardNavigation = ({
|
|
|
536
602
|
goToStep,
|
|
537
603
|
isFirstStep,
|
|
538
604
|
isLastStep,
|
|
605
|
+
isNavigating,
|
|
539
606
|
getStepStatus,
|
|
540
607
|
setStepValidation
|
|
541
608
|
};
|
|
542
|
-
}, [currentStep, totalSteps, goToNext, goToPrevious, goToStep, isFirstStep, isLastStep, getStepStatus, setStepValidation]);
|
|
609
|
+
}, [currentStep, totalSteps, goToNext, goToPrevious, goToStep, isFirstStep, isLastStep, isNavigating, getStepStatus, setStepValidation]);
|
|
543
610
|
};
|
|
544
611
|
var WizardNavigation = ({
|
|
545
612
|
variant,
|
|
@@ -547,6 +614,7 @@ var WizardNavigation = ({
|
|
|
547
614
|
labels,
|
|
548
615
|
isFirstStep,
|
|
549
616
|
isLastStep,
|
|
617
|
+
isNavigating,
|
|
550
618
|
goToPrevious,
|
|
551
619
|
goToNext
|
|
552
620
|
}) => {
|
|
@@ -574,12 +642,15 @@ var WizardNavigation = ({
|
|
|
574
642
|
children: [/* @__PURE__ */(0, react_jsx_runtime.jsx)(_ttoss_ui.Button, {
|
|
575
643
|
variant: "secondary",
|
|
576
644
|
onClick: goToPrevious,
|
|
577
|
-
disabled: isFirstStep,
|
|
645
|
+
disabled: isFirstStep || isNavigating,
|
|
578
646
|
"aria-label": buttonLabels.previous,
|
|
579
647
|
children: buttonLabels.previous
|
|
580
648
|
}), /* @__PURE__ */(0, react_jsx_runtime.jsx)(_ttoss_ui.Button, {
|
|
581
649
|
variant: getWizardPrimaryButtonVariant(variant),
|
|
582
650
|
onClick: goToNext,
|
|
651
|
+
loading: isNavigating,
|
|
652
|
+
disabled: isNavigating,
|
|
653
|
+
"aria-busy": isNavigating,
|
|
583
654
|
"aria-label": isLastStep ? buttonLabels.finish : buttonLabels.next,
|
|
584
655
|
children: isLastStep ? buttonLabels.finish : buttonLabels.next
|
|
585
656
|
})]
|
|
@@ -652,6 +723,7 @@ var Wizard = ({
|
|
|
652
723
|
labels,
|
|
653
724
|
isFirstStep: contextValue.isFirstStep,
|
|
654
725
|
isLastStep: contextValue.isLastStep,
|
|
726
|
+
isNavigating: contextValue.isNavigating,
|
|
655
727
|
goToPrevious: contextValue.goToPrevious,
|
|
656
728
|
goToNext: contextValue.goToNext
|
|
657
729
|
})]
|
package/dist/index.d.cts
CHANGED
|
@@ -31,7 +31,9 @@ interface WizardStep {
|
|
|
31
31
|
content: React.ReactNode;
|
|
32
32
|
/**
|
|
33
33
|
* Called when the user clicks "Next". If it returns false or
|
|
34
|
-
* a Promise that resolves to false, the wizard will not advance.
|
|
34
|
+
* a Promise that resolves to false, the wizard will not advance. While a
|
|
35
|
+
* returned Promise is pending, the next/finish button shows its loading
|
|
36
|
+
* state and the wizard refuses any other navigation (`isNavigating`).
|
|
35
37
|
*/
|
|
36
38
|
onNext?: () => boolean | Promise<boolean>;
|
|
37
39
|
}
|
|
@@ -72,7 +74,8 @@ interface WizardProps {
|
|
|
72
74
|
*/
|
|
73
75
|
initialStep?: number;
|
|
74
76
|
/**
|
|
75
|
-
* 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.
|
|
76
79
|
* Defaults to true.
|
|
77
80
|
*/
|
|
78
81
|
allowStepClick?: boolean;
|
|
@@ -111,11 +114,14 @@ interface WizardContextValue {
|
|
|
111
114
|
*/
|
|
112
115
|
goToPrevious: () => void;
|
|
113
116
|
/**
|
|
114
|
-
* 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.
|
|
115
121
|
*/
|
|
116
122
|
goToStep: (params: {
|
|
117
123
|
stepIndex: number;
|
|
118
|
-
}) => void
|
|
124
|
+
}) => Promise<void>;
|
|
119
125
|
/**
|
|
120
126
|
* Whether the wizard is on the first step.
|
|
121
127
|
*/
|
|
@@ -124,6 +130,12 @@ interface WizardContextValue {
|
|
|
124
130
|
* Whether the wizard is on the last step.
|
|
125
131
|
*/
|
|
126
132
|
isLastStep: boolean;
|
|
133
|
+
/**
|
|
134
|
+
* Whether `goToNext` is waiting on the step's validation or `onNext`. While
|
|
135
|
+
* true, the next/finish button shows its loading state, and every
|
|
136
|
+
* navigation (next, previous, a step click) is refused.
|
|
137
|
+
*/
|
|
138
|
+
isNavigating: boolean;
|
|
127
139
|
/**
|
|
128
140
|
* Get the status of a step by index.
|
|
129
141
|
*/
|
package/dist/index.d.mts
CHANGED
|
@@ -31,7 +31,9 @@ interface WizardStep {
|
|
|
31
31
|
content: React.ReactNode;
|
|
32
32
|
/**
|
|
33
33
|
* Called when the user clicks "Next". If it returns false or
|
|
34
|
-
* a Promise that resolves to false, the wizard will not advance.
|
|
34
|
+
* a Promise that resolves to false, the wizard will not advance. While a
|
|
35
|
+
* returned Promise is pending, the next/finish button shows its loading
|
|
36
|
+
* state and the wizard refuses any other navigation (`isNavigating`).
|
|
35
37
|
*/
|
|
36
38
|
onNext?: () => boolean | Promise<boolean>;
|
|
37
39
|
}
|
|
@@ -72,7 +74,8 @@ interface WizardProps {
|
|
|
72
74
|
*/
|
|
73
75
|
initialStep?: number;
|
|
74
76
|
/**
|
|
75
|
-
* 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.
|
|
76
79
|
* Defaults to true.
|
|
77
80
|
*/
|
|
78
81
|
allowStepClick?: boolean;
|
|
@@ -111,11 +114,14 @@ interface WizardContextValue {
|
|
|
111
114
|
*/
|
|
112
115
|
goToPrevious: () => void;
|
|
113
116
|
/**
|
|
114
|
-
* 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.
|
|
115
121
|
*/
|
|
116
122
|
goToStep: (params: {
|
|
117
123
|
stepIndex: number;
|
|
118
|
-
}) => void
|
|
124
|
+
}) => Promise<void>;
|
|
119
125
|
/**
|
|
120
126
|
* Whether the wizard is on the first step.
|
|
121
127
|
*/
|
|
@@ -124,6 +130,12 @@ interface WizardContextValue {
|
|
|
124
130
|
* Whether the wizard is on the last step.
|
|
125
131
|
*/
|
|
126
132
|
isLastStep: boolean;
|
|
133
|
+
/**
|
|
134
|
+
* Whether `goToNext` is waiting on the step's validation or `onNext`. While
|
|
135
|
+
* true, the next/finish button shows its loading state, and every
|
|
136
|
+
* navigation (next, previous, a step click) is refused.
|
|
137
|
+
*/
|
|
138
|
+
isNavigating: boolean;
|
|
127
139
|
/**
|
|
128
140
|
* Get the status of a step by index.
|
|
129
141
|
*/
|
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,18 @@ 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);
|
|
476
|
+
const [isNavigating, setIsNavigating] = React.useState(false);
|
|
477
|
+
/**
|
|
478
|
+
* The state above re-renders the buttons; this ref is what refuses a second
|
|
479
|
+
* click that lands before that render.
|
|
480
|
+
*/
|
|
481
|
+
const navigatingRef = React.useRef(false);
|
|
462
482
|
const stepValidationRef = React.useRef(null);
|
|
463
483
|
const totalSteps = steps.length;
|
|
464
484
|
const isFirstStep = currentStep === 0;
|
|
@@ -468,46 +488,92 @@ var useWizardNavigation = ({
|
|
|
468
488
|
}) => {
|
|
469
489
|
if (stepIndex < currentStep) return "completed";
|
|
470
490
|
if (stepIndex === currentStep) return "active";
|
|
471
|
-
return "upcoming";
|
|
472
|
-
}, [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]);
|
|
505
|
+
const canLeaveStep = React.useCallback(async () => {
|
|
506
|
+
if (stepValidationRef.current && !(await stepValidationRef.current())) return false;
|
|
507
|
+
const {
|
|
508
|
+
onNext
|
|
509
|
+
} = steps[currentStep];
|
|
510
|
+
return !onNext || (await onNext());
|
|
511
|
+
}, [currentStep, steps]);
|
|
473
512
|
const goToNext = React.useCallback(async () => {
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
if (
|
|
480
|
-
|
|
481
|
-
if (isLastStep) onComplete?.();else {
|
|
482
|
-
const nextStep = currentStep + 1;
|
|
483
|
-
stepValidationRef.current = null;
|
|
484
|
-
setCurrentStep(nextStep);
|
|
485
|
-
onStepChange?.({
|
|
486
|
-
stepIndex: nextStep
|
|
513
|
+
if (navigatingRef.current) return;
|
|
514
|
+
navigatingRef.current = true;
|
|
515
|
+
setIsNavigating(true);
|
|
516
|
+
try {
|
|
517
|
+
if (!(await canLeaveStep())) return;
|
|
518
|
+
if (isLastStep) onComplete?.();else moveTo({
|
|
519
|
+
stepIndex: currentStep + 1
|
|
487
520
|
});
|
|
521
|
+
} finally {
|
|
522
|
+
navigatingRef.current = false;
|
|
523
|
+
setIsNavigating(false);
|
|
488
524
|
}
|
|
489
|
-
}, [
|
|
525
|
+
}, [canLeaveStep, currentStep, isLastStep, onComplete, moveTo]);
|
|
490
526
|
const goToPrevious = React.useCallback(() => {
|
|
491
|
-
if (!isFirstStep) {
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
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
|
|
497
557
|
});
|
|
558
|
+
} finally {
|
|
559
|
+
navigatingRef.current = false;
|
|
560
|
+
setIsNavigating(false);
|
|
498
561
|
}
|
|
499
|
-
}, [currentStep,
|
|
500
|
-
const goToStep = React.useCallback(({
|
|
562
|
+
}, [canLeaveStep, currentStep, steps, moveTo]);
|
|
563
|
+
const goToStep = React.useCallback(async ({
|
|
501
564
|
stepIndex
|
|
502
565
|
}) => {
|
|
503
|
-
if (
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
onStepChange?.({
|
|
566
|
+
if (navigatingRef.current || stepIndex < 0 || stepIndex >= totalSteps) return;
|
|
567
|
+
if (stepIndex <= currentStep) {
|
|
568
|
+
moveTo({
|
|
507
569
|
stepIndex
|
|
508
570
|
});
|
|
571
|
+
return;
|
|
509
572
|
}
|
|
510
|
-
|
|
573
|
+
if (stepIndex <= furthestStep) await goForwardTo({
|
|
574
|
+
stepIndex
|
|
575
|
+
});
|
|
576
|
+
}, [currentStep, furthestStep, totalSteps, moveTo, goForwardTo]);
|
|
511
577
|
const setStepValidation = React.useCallback(validate => {
|
|
512
578
|
stepValidationRef.current = validate;
|
|
513
579
|
}, []);
|
|
@@ -520,10 +586,11 @@ var useWizardNavigation = ({
|
|
|
520
586
|
goToStep,
|
|
521
587
|
isFirstStep,
|
|
522
588
|
isLastStep,
|
|
589
|
+
isNavigating,
|
|
523
590
|
getStepStatus,
|
|
524
591
|
setStepValidation
|
|
525
592
|
};
|
|
526
|
-
}, [currentStep, totalSteps, goToNext, goToPrevious, goToStep, isFirstStep, isLastStep, getStepStatus, setStepValidation]);
|
|
593
|
+
}, [currentStep, totalSteps, goToNext, goToPrevious, goToStep, isFirstStep, isLastStep, isNavigating, getStepStatus, setStepValidation]);
|
|
527
594
|
};
|
|
528
595
|
var WizardNavigation = ({
|
|
529
596
|
variant,
|
|
@@ -531,6 +598,7 @@ var WizardNavigation = ({
|
|
|
531
598
|
labels,
|
|
532
599
|
isFirstStep,
|
|
533
600
|
isLastStep,
|
|
601
|
+
isNavigating,
|
|
534
602
|
goToPrevious,
|
|
535
603
|
goToNext
|
|
536
604
|
}) => {
|
|
@@ -558,12 +626,15 @@ var WizardNavigation = ({
|
|
|
558
626
|
children: [/* @__PURE__ */jsx(Button, {
|
|
559
627
|
variant: "secondary",
|
|
560
628
|
onClick: goToPrevious,
|
|
561
|
-
disabled: isFirstStep,
|
|
629
|
+
disabled: isFirstStep || isNavigating,
|
|
562
630
|
"aria-label": buttonLabels.previous,
|
|
563
631
|
children: buttonLabels.previous
|
|
564
632
|
}), /* @__PURE__ */jsx(Button, {
|
|
565
633
|
variant: getWizardPrimaryButtonVariant(variant),
|
|
566
634
|
onClick: goToNext,
|
|
635
|
+
loading: isNavigating,
|
|
636
|
+
disabled: isNavigating,
|
|
637
|
+
"aria-busy": isNavigating,
|
|
567
638
|
"aria-label": isLastStep ? buttonLabels.finish : buttonLabels.next,
|
|
568
639
|
children: isLastStep ? buttonLabels.finish : buttonLabels.next
|
|
569
640
|
})]
|
|
@@ -636,6 +707,7 @@ var Wizard = ({
|
|
|
636
707
|
labels,
|
|
637
708
|
isFirstStep: contextValue.isFirstStep,
|
|
638
709
|
isLastStep: contextValue.isLastStep,
|
|
710
|
+
isNavigating: contextValue.isNavigating,
|
|
639
711
|
goToPrevious: contextValue.goToPrevious,
|
|
640
712
|
goToNext: contextValue.goToNext
|
|
641
713
|
})]
|
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/forms": "^0.53.3",
|
|
45
44
|
"@ttoss/react-i18n": "^4.0.2",
|
|
46
|
-
"@ttoss/
|
|
45
|
+
"@ttoss/forms": "^0.53.3",
|
|
47
46
|
"@ttoss/test-utils": "^7.1.1",
|
|
48
|
-
"@ttoss/ui": "^9.0.2"
|
|
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",
|