@ttoss/react-wizard 0.12.0 → 0.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/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 steps to navigate back. |
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` | 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. |
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
@@ -203,6 +203,21 @@ var getWizardStepSeparatorSx = ({
203
203
  opacity: isCompleted ? 1 : .4
204
204
  };
205
205
  };
206
+ /**
207
+ * In a vertical list the separator is absolute, as tall as its step's item
208
+ * less the indicator and its gutters, and the items share whatever height the
209
+ * content leaves: next to a short step they shrink to the indicator and the
210
+ * line disappears. Every item but the last keeps room for a visible line, and
211
+ * the wizard grows instead.
212
+ */
213
+ var getWizardStepItemSx = ({
214
+ isVertical,
215
+ isLast
216
+ }) => {
217
+ return isVertical && !isLast ? {
218
+ minHeight: "calc(var(--steps-size) + var(--steps-gutter) * 2 + 1.5rem)"
219
+ } : {};
220
+ };
206
221
  var getWizardStepTitleSx = ({
207
222
  status,
208
223
  variant = "spotlight-accent"
@@ -250,6 +265,8 @@ var useWizard = () => {
250
265
 
251
266
  //#endregion
252
267
  //#region src/WizardStepList.tsx
268
+ /** A completed step's indicator: a symbol, not copy. */
269
+ var COMPLETED_MARK = "✓";
253
270
  var messages$1 = (0, _ttoss_react_i18n.defineMessages)({
254
271
  stepListLabel: {
255
272
  defaultMessage: "Wizard steps",
@@ -363,43 +380,40 @@ var WizardStepList = ({
363
380
  const isClickable = allowStepClick && status === "completed" && index !== currentStep;
364
381
  return /* @__PURE__ */(0, react_jsx_runtime.jsxs)(_chakra_ui_react.Steps.Item, {
365
382
  index,
383
+ css: getWizardStepItemSx({
384
+ isVertical: !isHorizontal,
385
+ isLast: index === steps.length - 1
386
+ }),
366
387
  children: [/* @__PURE__ */(0, react_jsx_runtime.jsxs)(_ttoss_ui.Flex, {
388
+ role: "button",
389
+ tabIndex: isClickable ? 0 : -1,
390
+ onClick: () => {
391
+ if (isClickable) onStepClick({
392
+ stepIndex: index
393
+ });
394
+ },
395
+ onKeyDown: e => {
396
+ if (isClickable && (e.key === "Enter" || e.key === " ")) {
397
+ e.preventDefault();
398
+ onStepClick({
399
+ stepIndex: index
400
+ });
401
+ }
402
+ },
403
+ "aria-current": status === "active" ? "step" : void 0,
367
404
  sx: {
368
405
  flexDirection: isHorizontal ? "column" : "row",
369
406
  alignItems: "center",
370
- gap: "2"
407
+ gap: "2",
408
+ cursor: isClickable ? "pointer" : "default"
371
409
  },
372
- children: [/* @__PURE__ */(0, react_jsx_runtime.jsx)(_ttoss_ui.Box, {
373
- role: "button",
374
- tabIndex: isClickable ? 0 : -1,
375
- onClick: () => {
376
- if (isClickable) onStepClick({
377
- stepIndex: index
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
- })
410
+ children: [/* @__PURE__ */(0, react_jsx_runtime.jsx)(_chakra_ui_react.Steps.Indicator, {
411
+ css: getWizardStepIndicatorSx({
412
+ status,
413
+ variant,
414
+ isClickable
415
+ }),
416
+ children: status === "completed" ? COMPLETED_MARK : /* @__PURE__ */(0, react_jsx_runtime.jsx)(_chakra_ui_react.Steps.Number, {})
403
417
  }), /* @__PURE__ */(0, react_jsx_runtime.jsx)(WizardStepText, {
404
418
  step,
405
419
  status,
@@ -408,7 +422,9 @@ var WizardStepList = ({
408
422
  })]
409
423
  }), index < steps.length - 1 && /* @__PURE__ */(0, react_jsx_runtime.jsx)(_chakra_ui_react.Steps.Separator, {
410
424
  css: getWizardStepSeparatorSx({
411
- isCompleted: index < currentStep,
425
+ isCompleted: getStepStatus({
426
+ stepIndex: index + 1
427
+ }) !== "upcoming",
412
428
  variant
413
429
  })
414
430
  })]
@@ -475,6 +491,12 @@ var useWizardNavigation = ({
475
491
  onStepChange
476
492
  }) => {
477
493
  const [currentStep, setCurrentStep] = react.useState(initialStep);
494
+ /**
495
+ * The furthest step the user has reached (the initial one counts): going
496
+ * back keeps every step up to it completed, so the user can return to it
497
+ * without clicking "Next" through each one.
498
+ */
499
+ const [furthestStep, setFurthestStep] = react.useState(initialStep);
478
500
  const [isNavigating, setIsNavigating] = react.useState(false);
479
501
  /**
480
502
  * The state above re-renders the buttons; this ref is what refuses a second
@@ -490,8 +512,20 @@ var useWizardNavigation = ({
490
512
  }) => {
491
513
  if (stepIndex < currentStep) return "completed";
492
514
  if (stepIndex === currentStep) return "active";
493
- return "upcoming";
494
- }, [currentStep]);
515
+ return stepIndex <= furthestStep ? "completed" : "upcoming";
516
+ }, [currentStep, furthestStep]);
517
+ const moveTo = react.useCallback(({
518
+ stepIndex
519
+ }) => {
520
+ stepValidationRef.current = null;
521
+ setCurrentStep(stepIndex);
522
+ setFurthestStep(furthest => {
523
+ return Math.max(furthest, stepIndex);
524
+ });
525
+ onStepChange?.({
526
+ stepIndex
527
+ });
528
+ }, [onStepChange]);
495
529
  const canLeaveStep = react.useCallback(async () => {
496
530
  if (stepValidationRef.current && !(await stepValidationRef.current())) return false;
497
531
  const {
@@ -505,40 +539,65 @@ var useWizardNavigation = ({
505
539
  setIsNavigating(true);
506
540
  try {
507
541
  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
- }
542
+ if (isLastStep) onComplete?.();else moveTo({
543
+ stepIndex: currentStep + 1
544
+ });
516
545
  } finally {
517
546
  navigatingRef.current = false;
518
547
  setIsNavigating(false);
519
548
  }
520
- }, [canLeaveStep, currentStep, isLastStep, onComplete, onStepChange]);
549
+ }, [canLeaveStep, currentStep, isLastStep, onComplete, moveTo]);
521
550
  const goToPrevious = react.useCallback(() => {
522
- if (!isFirstStep && !navigatingRef.current) {
523
- const prevStep = currentStep - 1;
524
- stepValidationRef.current = null;
525
- setCurrentStep(prevStep);
526
- onStepChange?.({
527
- stepIndex: prevStep
551
+ if (!isFirstStep && !navigatingRef.current) moveTo({
552
+ stepIndex: currentStep - 1
553
+ });
554
+ }, [currentStep, isFirstStep, moveTo]);
555
+ /**
556
+ * Forward to a step already reached, as if clicking "Next" through each
557
+ * step on the way: the current step's validation and `onNext`, then each
558
+ * step's `onNext` in order. The first refusal stops there, on the step that
559
+ * refused, so the user sees why.
560
+ */
561
+ const goForwardTo = react.useCallback(async ({
562
+ stepIndex
563
+ }) => {
564
+ navigatingRef.current = true;
565
+ setIsNavigating(true);
566
+ try {
567
+ if (!(await canLeaveStep())) return;
568
+ for (let index = currentStep + 1; index < stepIndex; index += 1) {
569
+ const {
570
+ onNext
571
+ } = steps[index];
572
+ if (onNext && !(await onNext())) {
573
+ moveTo({
574
+ stepIndex: index
575
+ });
576
+ return;
577
+ }
578
+ }
579
+ moveTo({
580
+ stepIndex
528
581
  });
582
+ } finally {
583
+ navigatingRef.current = false;
584
+ setIsNavigating(false);
529
585
  }
530
- }, [currentStep, isFirstStep, onStepChange]);
531
- const goToStep = react.useCallback(({
586
+ }, [canLeaveStep, currentStep, steps, moveTo]);
587
+ const goToStep = react.useCallback(async ({
532
588
  stepIndex
533
589
  }) => {
534
- if (!navigatingRef.current && stepIndex >= 0 && stepIndex < totalSteps && stepIndex <= currentStep) {
535
- stepValidationRef.current = null;
536
- setCurrentStep(stepIndex);
537
- onStepChange?.({
590
+ if (navigatingRef.current || stepIndex < 0 || stepIndex >= totalSteps) return;
591
+ if (stepIndex <= currentStep) {
592
+ moveTo({
538
593
  stepIndex
539
594
  });
595
+ return;
540
596
  }
541
- }, [currentStep, totalSteps, onStepChange]);
597
+ if (stepIndex <= furthestStep) await goForwardTo({
598
+ stepIndex
599
+ });
600
+ }, [currentStep, furthestStep, totalSteps, moveTo, goForwardTo]);
542
601
  const setStepValidation = react.useCallback(validate => {
543
602
  stepValidationRef.current = validate;
544
603
  }, []);
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 specific step (only if it's completed or active).
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 specific step (only if it's completed or active).
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
@@ -172,6 +172,21 @@ var getWizardStepSeparatorSx = ({
172
172
  opacity: isCompleted ? 1 : .4
173
173
  };
174
174
  };
175
+ /**
176
+ * In a vertical list the separator is absolute, as tall as its step's item
177
+ * less the indicator and its gutters, and the items share whatever height the
178
+ * content leaves: next to a short step they shrink to the indicator and the
179
+ * line disappears. Every item but the last keeps room for a visible line, and
180
+ * the wizard grows instead.
181
+ */
182
+ var getWizardStepItemSx = ({
183
+ isVertical,
184
+ isLast
185
+ }) => {
186
+ return isVertical && !isLast ? {
187
+ minHeight: "calc(var(--steps-size) + var(--steps-gutter) * 2 + 1.5rem)"
188
+ } : {};
189
+ };
175
190
  var getWizardStepTitleSx = ({
176
191
  status,
177
192
  variant = "spotlight-accent"
@@ -219,6 +234,8 @@ var useWizard = () => {
219
234
 
220
235
  //#endregion
221
236
  //#region src/WizardStepList.tsx
237
+ /** A completed step's indicator: a symbol, not copy. */
238
+ var COMPLETED_MARK = "✓";
222
239
  var messages$1 = defineMessages({
223
240
  stepListLabel: {
224
241
  id: "mVLnY8",
@@ -335,43 +352,40 @@ var WizardStepList = ({
335
352
  const isClickable = allowStepClick && status === "completed" && index !== currentStep;
336
353
  return /* @__PURE__ */jsxs(Steps.Item, {
337
354
  index,
355
+ css: getWizardStepItemSx({
356
+ isVertical: !isHorizontal,
357
+ isLast: index === steps.length - 1
358
+ }),
338
359
  children: [/* @__PURE__ */jsxs(Flex, {
360
+ role: "button",
361
+ tabIndex: isClickable ? 0 : -1,
362
+ onClick: () => {
363
+ if (isClickable) onStepClick({
364
+ stepIndex: index
365
+ });
366
+ },
367
+ onKeyDown: e => {
368
+ if (isClickable && (e.key === "Enter" || e.key === " ")) {
369
+ e.preventDefault();
370
+ onStepClick({
371
+ stepIndex: index
372
+ });
373
+ }
374
+ },
375
+ "aria-current": status === "active" ? "step" : void 0,
339
376
  sx: {
340
377
  flexDirection: isHorizontal ? "column" : "row",
341
378
  alignItems: "center",
342
- gap: "2"
379
+ gap: "2",
380
+ cursor: isClickable ? "pointer" : "default"
343
381
  },
344
- children: [/* @__PURE__ */jsx(Box, {
345
- role: "button",
346
- tabIndex: isClickable ? 0 : -1,
347
- onClick: () => {
348
- if (isClickable) onStepClick({
349
- stepIndex: index
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
- })
382
+ children: [/* @__PURE__ */jsx(Steps.Indicator, {
383
+ css: getWizardStepIndicatorSx({
384
+ status,
385
+ variant,
386
+ isClickable
387
+ }),
388
+ children: status === "completed" ? COMPLETED_MARK : /* @__PURE__ */jsx(Steps.Number, {})
375
389
  }), /* @__PURE__ */jsx(WizardStepText, {
376
390
  step,
377
391
  status,
@@ -380,7 +394,9 @@ var WizardStepList = ({
380
394
  })]
381
395
  }), index < steps.length - 1 && /* @__PURE__ */jsx(Steps.Separator, {
382
396
  css: getWizardStepSeparatorSx({
383
- isCompleted: index < currentStep,
397
+ isCompleted: getStepStatus({
398
+ stepIndex: index + 1
399
+ }) !== "upcoming",
384
400
  variant
385
401
  })
386
402
  })]
@@ -459,6 +475,12 @@ var useWizardNavigation = ({
459
475
  onStepChange
460
476
  }) => {
461
477
  const [currentStep, setCurrentStep] = React.useState(initialStep);
478
+ /**
479
+ * The furthest step the user has reached (the initial one counts): going
480
+ * back keeps every step up to it completed, so the user can return to it
481
+ * without clicking "Next" through each one.
482
+ */
483
+ const [furthestStep, setFurthestStep] = React.useState(initialStep);
462
484
  const [isNavigating, setIsNavigating] = React.useState(false);
463
485
  /**
464
486
  * The state above re-renders the buttons; this ref is what refuses a second
@@ -474,8 +496,20 @@ var useWizardNavigation = ({
474
496
  }) => {
475
497
  if (stepIndex < currentStep) return "completed";
476
498
  if (stepIndex === currentStep) return "active";
477
- return "upcoming";
478
- }, [currentStep]);
499
+ return stepIndex <= furthestStep ? "completed" : "upcoming";
500
+ }, [currentStep, furthestStep]);
501
+ const moveTo = React.useCallback(({
502
+ stepIndex
503
+ }) => {
504
+ stepValidationRef.current = null;
505
+ setCurrentStep(stepIndex);
506
+ setFurthestStep(furthest => {
507
+ return Math.max(furthest, stepIndex);
508
+ });
509
+ onStepChange?.({
510
+ stepIndex
511
+ });
512
+ }, [onStepChange]);
479
513
  const canLeaveStep = React.useCallback(async () => {
480
514
  if (stepValidationRef.current && !(await stepValidationRef.current())) return false;
481
515
  const {
@@ -489,40 +523,65 @@ var useWizardNavigation = ({
489
523
  setIsNavigating(true);
490
524
  try {
491
525
  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
- }
526
+ if (isLastStep) onComplete?.();else moveTo({
527
+ stepIndex: currentStep + 1
528
+ });
500
529
  } finally {
501
530
  navigatingRef.current = false;
502
531
  setIsNavigating(false);
503
532
  }
504
- }, [canLeaveStep, currentStep, isLastStep, onComplete, onStepChange]);
533
+ }, [canLeaveStep, currentStep, isLastStep, onComplete, moveTo]);
505
534
  const goToPrevious = React.useCallback(() => {
506
- if (!isFirstStep && !navigatingRef.current) {
507
- const prevStep = currentStep - 1;
508
- stepValidationRef.current = null;
509
- setCurrentStep(prevStep);
510
- onStepChange?.({
511
- stepIndex: prevStep
535
+ if (!isFirstStep && !navigatingRef.current) moveTo({
536
+ stepIndex: currentStep - 1
537
+ });
538
+ }, [currentStep, isFirstStep, moveTo]);
539
+ /**
540
+ * Forward to a step already reached, as if clicking "Next" through each
541
+ * step on the way: the current step's validation and `onNext`, then each
542
+ * step's `onNext` in order. The first refusal stops there, on the step that
543
+ * refused, so the user sees why.
544
+ */
545
+ const goForwardTo = React.useCallback(async ({
546
+ stepIndex
547
+ }) => {
548
+ navigatingRef.current = true;
549
+ setIsNavigating(true);
550
+ try {
551
+ if (!(await canLeaveStep())) return;
552
+ for (let index = currentStep + 1; index < stepIndex; index += 1) {
553
+ const {
554
+ onNext
555
+ } = steps[index];
556
+ if (onNext && !(await onNext())) {
557
+ moveTo({
558
+ stepIndex: index
559
+ });
560
+ return;
561
+ }
562
+ }
563
+ moveTo({
564
+ stepIndex
512
565
  });
566
+ } finally {
567
+ navigatingRef.current = false;
568
+ setIsNavigating(false);
513
569
  }
514
- }, [currentStep, isFirstStep, onStepChange]);
515
- const goToStep = React.useCallback(({
570
+ }, [canLeaveStep, currentStep, steps, moveTo]);
571
+ const goToStep = React.useCallback(async ({
516
572
  stepIndex
517
573
  }) => {
518
- if (!navigatingRef.current && stepIndex >= 0 && stepIndex < totalSteps && stepIndex <= currentStep) {
519
- stepValidationRef.current = null;
520
- setCurrentStep(stepIndex);
521
- onStepChange?.({
574
+ if (navigatingRef.current || stepIndex < 0 || stepIndex >= totalSteps) return;
575
+ if (stepIndex <= currentStep) {
576
+ moveTo({
522
577
  stepIndex
523
578
  });
579
+ return;
524
580
  }
525
- }, [currentStep, totalSteps, onStepChange]);
581
+ if (stepIndex <= furthestStep) await goForwardTo({
582
+ stepIndex
583
+ });
584
+ }, [currentStep, furthestStep, totalSteps, moveTo, goForwardTo]);
526
585
  const setStepValidation = React.useCallback(validate => {
527
586
  stepValidationRef.current = validate;
528
587
  }, []);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ttoss/react-wizard",
3
- "version": "0.12.0",
3
+ "version": "0.13.1",
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,12 +40,12 @@
40
40
  "jest": "^30.5.2",
41
41
  "react": "^19.3.0",
42
42
  "tsdown": "^0.23.1",
43
- "@ttoss/config": "^4.0.1",
44
43
  "@ttoss/i18n-cli": "^0.16.1",
45
44
  "@ttoss/react-i18n": "^4.0.2",
46
45
  "@ttoss/ui": "^9.0.2",
47
- "@ttoss/forms": "^0.53.3",
48
- "@ttoss/test-utils": "^7.1.1"
46
+ "@ttoss/config": "^4.0.1",
47
+ "@ttoss/test-utils": "^7.1.1",
48
+ "@ttoss/forms": "^0.53.3"
49
49
  },
50
50
  "peerDependencies": {
51
51
  "@chakra-ui/react": "^3",