@ttoss/react-wizard 0.11.3 → 0.12.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 +19 -17
- package/dist/index.cjs +40 -19
- package/dist/index.d.cts +9 -1
- package/dist/index.d.mts +9 -1
- package/dist/index.mjs +40 -19
- package/package.json +9 -9
package/README.md
CHANGED
|
@@ -67,12 +67,12 @@ Labels are customizable, so applications can localize them as needed.
|
|
|
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` | Navigate to a specific step.
|
|
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 }) => void` | Navigate to a specific step. |
|
|
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
|
@@ -475,6 +475,12 @@ var useWizardNavigation = ({
|
|
|
475
475
|
onStepChange
|
|
476
476
|
}) => {
|
|
477
477
|
const [currentStep, setCurrentStep] = react.useState(initialStep);
|
|
478
|
+
const [isNavigating, setIsNavigating] = react.useState(false);
|
|
479
|
+
/**
|
|
480
|
+
* The state above re-renders the buttons; this ref is what refuses a second
|
|
481
|
+
* click that lands before that render.
|
|
482
|
+
*/
|
|
483
|
+
const navigatingRef = react.useRef(false);
|
|
478
484
|
const stepValidationRef = react.useRef(null);
|
|
479
485
|
const totalSteps = steps.length;
|
|
480
486
|
const isFirstStep = currentStep === 0;
|
|
@@ -486,25 +492,34 @@ var useWizardNavigation = ({
|
|
|
486
492
|
if (stepIndex === currentStep) return "active";
|
|
487
493
|
return "upcoming";
|
|
488
494
|
}, [currentStep]);
|
|
495
|
+
const canLeaveStep = react.useCallback(async () => {
|
|
496
|
+
if (stepValidationRef.current && !(await stepValidationRef.current())) return false;
|
|
497
|
+
const {
|
|
498
|
+
onNext
|
|
499
|
+
} = steps[currentStep];
|
|
500
|
+
return !onNext || (await onNext());
|
|
501
|
+
}, [currentStep, steps]);
|
|
489
502
|
const goToNext = react.useCallback(async () => {
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
if (
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
503
|
+
if (navigatingRef.current) return;
|
|
504
|
+
navigatingRef.current = true;
|
|
505
|
+
setIsNavigating(true);
|
|
506
|
+
try {
|
|
507
|
+
if (!(await canLeaveStep())) return;
|
|
508
|
+
if (isLastStep) onComplete?.();else {
|
|
509
|
+
const nextStep = currentStep + 1;
|
|
510
|
+
stepValidationRef.current = null;
|
|
511
|
+
setCurrentStep(nextStep);
|
|
512
|
+
onStepChange?.({
|
|
513
|
+
stepIndex: nextStep
|
|
514
|
+
});
|
|
515
|
+
}
|
|
516
|
+
} finally {
|
|
517
|
+
navigatingRef.current = false;
|
|
518
|
+
setIsNavigating(false);
|
|
504
519
|
}
|
|
505
|
-
}, [
|
|
520
|
+
}, [canLeaveStep, currentStep, isLastStep, onComplete, onStepChange]);
|
|
506
521
|
const goToPrevious = react.useCallback(() => {
|
|
507
|
-
if (!isFirstStep) {
|
|
522
|
+
if (!isFirstStep && !navigatingRef.current) {
|
|
508
523
|
const prevStep = currentStep - 1;
|
|
509
524
|
stepValidationRef.current = null;
|
|
510
525
|
setCurrentStep(prevStep);
|
|
@@ -516,7 +531,7 @@ var useWizardNavigation = ({
|
|
|
516
531
|
const goToStep = react.useCallback(({
|
|
517
532
|
stepIndex
|
|
518
533
|
}) => {
|
|
519
|
-
if (stepIndex >= 0 && stepIndex < totalSteps && stepIndex <= currentStep) {
|
|
534
|
+
if (!navigatingRef.current && stepIndex >= 0 && stepIndex < totalSteps && stepIndex <= currentStep) {
|
|
520
535
|
stepValidationRef.current = null;
|
|
521
536
|
setCurrentStep(stepIndex);
|
|
522
537
|
onStepChange?.({
|
|
@@ -536,10 +551,11 @@ var useWizardNavigation = ({
|
|
|
536
551
|
goToStep,
|
|
537
552
|
isFirstStep,
|
|
538
553
|
isLastStep,
|
|
554
|
+
isNavigating,
|
|
539
555
|
getStepStatus,
|
|
540
556
|
setStepValidation
|
|
541
557
|
};
|
|
542
|
-
}, [currentStep, totalSteps, goToNext, goToPrevious, goToStep, isFirstStep, isLastStep, getStepStatus, setStepValidation]);
|
|
558
|
+
}, [currentStep, totalSteps, goToNext, goToPrevious, goToStep, isFirstStep, isLastStep, isNavigating, getStepStatus, setStepValidation]);
|
|
543
559
|
};
|
|
544
560
|
var WizardNavigation = ({
|
|
545
561
|
variant,
|
|
@@ -547,6 +563,7 @@ var WizardNavigation = ({
|
|
|
547
563
|
labels,
|
|
548
564
|
isFirstStep,
|
|
549
565
|
isLastStep,
|
|
566
|
+
isNavigating,
|
|
550
567
|
goToPrevious,
|
|
551
568
|
goToNext
|
|
552
569
|
}) => {
|
|
@@ -574,12 +591,15 @@ var WizardNavigation = ({
|
|
|
574
591
|
children: [/* @__PURE__ */(0, react_jsx_runtime.jsx)(_ttoss_ui.Button, {
|
|
575
592
|
variant: "secondary",
|
|
576
593
|
onClick: goToPrevious,
|
|
577
|
-
disabled: isFirstStep,
|
|
594
|
+
disabled: isFirstStep || isNavigating,
|
|
578
595
|
"aria-label": buttonLabels.previous,
|
|
579
596
|
children: buttonLabels.previous
|
|
580
597
|
}), /* @__PURE__ */(0, react_jsx_runtime.jsx)(_ttoss_ui.Button, {
|
|
581
598
|
variant: getWizardPrimaryButtonVariant(variant),
|
|
582
599
|
onClick: goToNext,
|
|
600
|
+
loading: isNavigating,
|
|
601
|
+
disabled: isNavigating,
|
|
602
|
+
"aria-busy": isNavigating,
|
|
583
603
|
"aria-label": isLastStep ? buttonLabels.finish : buttonLabels.next,
|
|
584
604
|
children: isLastStep ? buttonLabels.finish : buttonLabels.next
|
|
585
605
|
})]
|
|
@@ -652,6 +672,7 @@ var Wizard = ({
|
|
|
652
672
|
labels,
|
|
653
673
|
isFirstStep: contextValue.isFirstStep,
|
|
654
674
|
isLastStep: contextValue.isLastStep,
|
|
675
|
+
isNavigating: contextValue.isNavigating,
|
|
655
676
|
goToPrevious: contextValue.goToPrevious,
|
|
656
677
|
goToNext: contextValue.goToNext
|
|
657
678
|
})]
|
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
|
}
|
|
@@ -124,6 +126,12 @@ interface WizardContextValue {
|
|
|
124
126
|
* Whether the wizard is on the last step.
|
|
125
127
|
*/
|
|
126
128
|
isLastStep: boolean;
|
|
129
|
+
/**
|
|
130
|
+
* Whether `goToNext` is waiting on the step's validation or `onNext`. While
|
|
131
|
+
* true, the next/finish button shows its loading state, and every
|
|
132
|
+
* navigation (next, previous, a step click) is refused.
|
|
133
|
+
*/
|
|
134
|
+
isNavigating: boolean;
|
|
127
135
|
/**
|
|
128
136
|
* Get the status of a step by index.
|
|
129
137
|
*/
|
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
|
}
|
|
@@ -124,6 +126,12 @@ interface WizardContextValue {
|
|
|
124
126
|
* Whether the wizard is on the last step.
|
|
125
127
|
*/
|
|
126
128
|
isLastStep: boolean;
|
|
129
|
+
/**
|
|
130
|
+
* Whether `goToNext` is waiting on the step's validation or `onNext`. While
|
|
131
|
+
* true, the next/finish button shows its loading state, and every
|
|
132
|
+
* navigation (next, previous, a step click) is refused.
|
|
133
|
+
*/
|
|
134
|
+
isNavigating: boolean;
|
|
127
135
|
/**
|
|
128
136
|
* Get the status of a step by index.
|
|
129
137
|
*/
|
package/dist/index.mjs
CHANGED
|
@@ -459,6 +459,12 @@ var useWizardNavigation = ({
|
|
|
459
459
|
onStepChange
|
|
460
460
|
}) => {
|
|
461
461
|
const [currentStep, setCurrentStep] = React.useState(initialStep);
|
|
462
|
+
const [isNavigating, setIsNavigating] = React.useState(false);
|
|
463
|
+
/**
|
|
464
|
+
* The state above re-renders the buttons; this ref is what refuses a second
|
|
465
|
+
* click that lands before that render.
|
|
466
|
+
*/
|
|
467
|
+
const navigatingRef = React.useRef(false);
|
|
462
468
|
const stepValidationRef = React.useRef(null);
|
|
463
469
|
const totalSteps = steps.length;
|
|
464
470
|
const isFirstStep = currentStep === 0;
|
|
@@ -470,25 +476,34 @@ var useWizardNavigation = ({
|
|
|
470
476
|
if (stepIndex === currentStep) return "active";
|
|
471
477
|
return "upcoming";
|
|
472
478
|
}, [currentStep]);
|
|
479
|
+
const canLeaveStep = React.useCallback(async () => {
|
|
480
|
+
if (stepValidationRef.current && !(await stepValidationRef.current())) return false;
|
|
481
|
+
const {
|
|
482
|
+
onNext
|
|
483
|
+
} = steps[currentStep];
|
|
484
|
+
return !onNext || (await onNext());
|
|
485
|
+
}, [currentStep, steps]);
|
|
473
486
|
const goToNext = React.useCallback(async () => {
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
if (
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
487
|
+
if (navigatingRef.current) return;
|
|
488
|
+
navigatingRef.current = true;
|
|
489
|
+
setIsNavigating(true);
|
|
490
|
+
try {
|
|
491
|
+
if (!(await canLeaveStep())) return;
|
|
492
|
+
if (isLastStep) onComplete?.();else {
|
|
493
|
+
const nextStep = currentStep + 1;
|
|
494
|
+
stepValidationRef.current = null;
|
|
495
|
+
setCurrentStep(nextStep);
|
|
496
|
+
onStepChange?.({
|
|
497
|
+
stepIndex: nextStep
|
|
498
|
+
});
|
|
499
|
+
}
|
|
500
|
+
} finally {
|
|
501
|
+
navigatingRef.current = false;
|
|
502
|
+
setIsNavigating(false);
|
|
488
503
|
}
|
|
489
|
-
}, [
|
|
504
|
+
}, [canLeaveStep, currentStep, isLastStep, onComplete, onStepChange]);
|
|
490
505
|
const goToPrevious = React.useCallback(() => {
|
|
491
|
-
if (!isFirstStep) {
|
|
506
|
+
if (!isFirstStep && !navigatingRef.current) {
|
|
492
507
|
const prevStep = currentStep - 1;
|
|
493
508
|
stepValidationRef.current = null;
|
|
494
509
|
setCurrentStep(prevStep);
|
|
@@ -500,7 +515,7 @@ var useWizardNavigation = ({
|
|
|
500
515
|
const goToStep = React.useCallback(({
|
|
501
516
|
stepIndex
|
|
502
517
|
}) => {
|
|
503
|
-
if (stepIndex >= 0 && stepIndex < totalSteps && stepIndex <= currentStep) {
|
|
518
|
+
if (!navigatingRef.current && stepIndex >= 0 && stepIndex < totalSteps && stepIndex <= currentStep) {
|
|
504
519
|
stepValidationRef.current = null;
|
|
505
520
|
setCurrentStep(stepIndex);
|
|
506
521
|
onStepChange?.({
|
|
@@ -520,10 +535,11 @@ var useWizardNavigation = ({
|
|
|
520
535
|
goToStep,
|
|
521
536
|
isFirstStep,
|
|
522
537
|
isLastStep,
|
|
538
|
+
isNavigating,
|
|
523
539
|
getStepStatus,
|
|
524
540
|
setStepValidation
|
|
525
541
|
};
|
|
526
|
-
}, [currentStep, totalSteps, goToNext, goToPrevious, goToStep, isFirstStep, isLastStep, getStepStatus, setStepValidation]);
|
|
542
|
+
}, [currentStep, totalSteps, goToNext, goToPrevious, goToStep, isFirstStep, isLastStep, isNavigating, getStepStatus, setStepValidation]);
|
|
527
543
|
};
|
|
528
544
|
var WizardNavigation = ({
|
|
529
545
|
variant,
|
|
@@ -531,6 +547,7 @@ var WizardNavigation = ({
|
|
|
531
547
|
labels,
|
|
532
548
|
isFirstStep,
|
|
533
549
|
isLastStep,
|
|
550
|
+
isNavigating,
|
|
534
551
|
goToPrevious,
|
|
535
552
|
goToNext
|
|
536
553
|
}) => {
|
|
@@ -558,12 +575,15 @@ var WizardNavigation = ({
|
|
|
558
575
|
children: [/* @__PURE__ */jsx(Button, {
|
|
559
576
|
variant: "secondary",
|
|
560
577
|
onClick: goToPrevious,
|
|
561
|
-
disabled: isFirstStep,
|
|
578
|
+
disabled: isFirstStep || isNavigating,
|
|
562
579
|
"aria-label": buttonLabels.previous,
|
|
563
580
|
children: buttonLabels.previous
|
|
564
581
|
}), /* @__PURE__ */jsx(Button, {
|
|
565
582
|
variant: getWizardPrimaryButtonVariant(variant),
|
|
566
583
|
onClick: goToNext,
|
|
584
|
+
loading: isNavigating,
|
|
585
|
+
disabled: isNavigating,
|
|
586
|
+
"aria-busy": isNavigating,
|
|
567
587
|
"aria-label": isLastStep ? buttonLabels.finish : buttonLabels.next,
|
|
568
588
|
children: isLastStep ? buttonLabels.finish : buttonLabels.next
|
|
569
589
|
})]
|
|
@@ -636,6 +656,7 @@ var Wizard = ({
|
|
|
636
656
|
labels,
|
|
637
657
|
isFirstStep: contextValue.isFirstStep,
|
|
638
658
|
isLastStep: contextValue.isLastStep,
|
|
659
|
+
isNavigating: contextValue.isNavigating,
|
|
639
660
|
goToPrevious: contextValue.goToPrevious,
|
|
640
661
|
goToNext: contextValue.goToNext
|
|
641
662
|
})]
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ttoss/react-wizard",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.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",
|
|
@@ -40,19 +40,19 @@
|
|
|
40
40
|
"jest": "^30.5.2",
|
|
41
41
|
"react": "^19.3.0",
|
|
42
42
|
"tsdown": "^0.23.1",
|
|
43
|
-
"@ttoss/
|
|
44
|
-
"@ttoss/
|
|
45
|
-
"@ttoss/
|
|
46
|
-
"@ttoss/
|
|
47
|
-
"@ttoss/
|
|
48
|
-
"@ttoss/
|
|
43
|
+
"@ttoss/config": "^4.0.1",
|
|
44
|
+
"@ttoss/i18n-cli": "^0.16.1",
|
|
45
|
+
"@ttoss/react-i18n": "^4.0.2",
|
|
46
|
+
"@ttoss/ui": "^9.0.2",
|
|
47
|
+
"@ttoss/forms": "^0.53.3",
|
|
48
|
+
"@ttoss/test-utils": "^7.1.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/react-i18n": "^4.0.2",
|
|
55
|
+
"@ttoss/ui": "^9.0.2"
|
|
56
56
|
},
|
|
57
57
|
"publishConfig": {
|
|
58
58
|
"access": "public",
|