You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -64,28 +64,29 @@ A component whose each direct sibling is treated as a step. **Do not add anythin
64
64
65
65
```jsx
66
66
<Steps>
67
-
<Step1 />
68
-
<Step2 />
69
-
<NotAStep />
67
+
<Step1 />
68
+
<Step2 />
69
+
<NotAStep />
70
70
</Steps>
71
71
```
72
72
73
73
✅ Correct:
74
74
75
75
```jsx
76
76
<Steps>
77
-
<Step1 />
78
-
<Step2>
79
-
<NotAStep />
80
-
</Step2>
77
+
<Step1 />
78
+
<Step2>
79
+
<NotAStep />
80
+
</Step2>
81
81
</Steps>
82
82
```
83
83
84
84
This reason for this method is due to React's _composition over inheritance_ principle. It also allows you to manage your state easily in the parent component.
|`onStepChange`|`(context?: { from: number; to: number }) => void`| Runs on every step change. Receives the previous and next step numbers. Does not run on initial render. |
89
+
|`beforeNext`|`() => boolean \| Promise<boolean>`| A guard function called before moving to the next step. Return `false` to block navigation. Supports async validation. |
89
90
90
91
<br/>
91
92
<hr />
@@ -99,18 +100,19 @@ A special hook that accesses the state of `<Steps />` component and exposes meth
99
100
100
101
These are the properties inside `stepsState` object.
|`progress`|`number`| Progress of the current step, value between 0 and 1 |
108
+
|`next`|`() => Promise<boolean>`| Move to the next step. Returns `true` if navigation succeeded, `false` if blocked by `beforeNext`. |
109
+
|`prev`|`() => void`| Move to the previous step |
110
+
|`jump`|`(step: number) => void`| Jump to the given step |
111
+
|`reset`|`() => void`| Reset to the first step |
112
+
|`isFirst`|`boolean`| If the step is the first |
113
+
|`isLast`|`boolean`| If the step is the last |
114
+
|`hasPrev`|`boolean`| If the step has any previous step |
115
+
|`hasNext`|`boolean`| If the step has any next step |
114
116
115
117
<br/>
116
118
<hr />
@@ -120,13 +122,88 @@ These are the properties inside `stepsState` object.
120
122
121
123
The component that renders `<Steps />` should be wrapped with `StepsProvider` component. `useSteps` can only be called in a component that is rendered in the DOM tree under `StepsProvider`.
|`startsFrom`|`number`| The default step number to be rendered. |
126
128
127
129
> Step numbers start from 1 and goes up to the count of direct siblings given to the `Steps` component. If the number is out of range, first step is rendered by default.
128
130
129
131
<br />
130
132
<hr />
131
133
<br />
132
-
Example project: https://codesandbox.io/s/react-step-builder-v3-5625v?file=/src/App.tsx
134
+
135
+
### Step Change Callback
136
+
137
+
`onStepChange` runs on every step change and optionally receives a context object with `from` and `to` step numbers.
138
+
139
+
```jsx
140
+
constMySteps= () => {
141
+
const { next, prev } =useSteps();
142
+
143
+
consthandleStepChange= (context) => {
144
+
console.log(`Moved from step ${context.from} to step ${context.to}`);
145
+
};
146
+
147
+
return (
148
+
<Steps onStepChange={handleStepChange}>
149
+
<div>Step 1</div>
150
+
<div>Step 2</div>
151
+
</Steps>
152
+
);
153
+
};
154
+
```
155
+
156
+
<br />
157
+
<hr />
158
+
<br />
159
+
160
+
### Step Validation
161
+
162
+
Use `beforeNext` to validate before allowing navigation to the next step. The function can be synchronous or asynchronous.
163
+
164
+
```jsx
165
+
constMySteps= () => {
166
+
const { next } =useSteps();
167
+
168
+
// Sync validation
169
+
return (
170
+
<Steps beforeNext={() =>formIsValid()}>
171
+
<div>Step 1</div>
172
+
<div>Step 2</div>
173
+
</Steps>
174
+
);
175
+
};
176
+
```
177
+
178
+
```jsx
179
+
// Async validation
180
+
<Steps beforeNext={async () => {
181
+
constisValid=awaitvalidateOnServer();
182
+
return isValid;
183
+
}}>
184
+
```
185
+
186
+
`next()` returns a `Promise<boolean>` indicating whether navigation was allowed:
0 commit comments