Skip to content

Commit 89890c4

Browse files
Merge pull request #107 from damianricobelli/add-before-after-go-to
feat: add before and after go to
2 parents 8930785 + 8360286 commit 89890c4

File tree

5 files changed

+89
-19
lines changed

5 files changed

+89
-19
lines changed

.changeset/great-baboons-argue.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@stepperize/react": minor
3+
"@stepperize/core": minor
4+
"docs": minor
5+
---
6+
7+
Add before and after go to feature

apps/docs/.source/index.js

Lines changed: 19 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apps/docs/content/docs/react/api-references/hook.mdx

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ That is why there are the following functions:
142142
- `afterNext`
143143
- `beforePrev`
144144
- `afterPrev`
145+
- `beforeGoTo`
146+
- `afterGoTo`
145147

146148
### beforeNext
147149

@@ -249,6 +251,55 @@ const MyStepperComponent = () => {
249251
};
250252
```
251253

254+
### beforeGoTo
255+
256+
The `beforeGoTo` function allows you to execute a function before moving to a specific step.
257+
It returns a promise that resolves to a boolean value. If the promise resolves to true,
258+
the stepper will move to the specific step. If the promise resolves to false, the stepper
259+
will not move to the specific step.
260+
261+
```tsx
262+
const MyStepperComponent = () => {
263+
const stepper = useStepper();
264+
265+
return (
266+
<button
267+
onClick={() =>
268+
stepper.beforeGoTo("first", () => {
269+
// Your logic here
270+
return true; // or false
271+
})
272+
}
273+
>
274+
Go to first step
275+
</button>
276+
);
277+
};
278+
```
279+
280+
### afterGoTo
281+
282+
The `afterGoTo` function allows you to execute a function after moving to a specific step.
283+
It returns a promise that resolves to a void value.
284+
285+
```tsx
286+
const MyStepperComponent = () => {
287+
const stepper = useStepper();
288+
289+
return (
290+
<button
291+
onClick={() =>
292+
stepper.afterGoTo("first", () => {
293+
// Your logic here
294+
})
295+
}
296+
>
297+
Go to first step
298+
</button>
299+
);
300+
};
301+
```
302+
252303
## Metadata
253304

254305
The metadata is a way to add custom data to a step. It is useful for adding dynamic values to a step, such as a value fetched from a server or any dynamic state in your application.

packages/core/src/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ export type Stepper<Steps extends Step[] = Step[]> = {
2828
beforePrev: (callback: () => Promise<boolean> | boolean) => Promise<void> | void;
2929
/** Executes a function after navigating to the previous step. */
3030
afterPrev: (callback: () => Promise<void> | void) => Promise<void> | void;
31+
/** Executes a function before navigating to a specific step. */
32+
beforeGoTo: (id: Get.Id<Steps>, callback: () => Promise<boolean> | boolean) => Promise<void> | void;
33+
/** Executes a function after navigating to a specific step. */
34+
afterGoTo: (id: Get.Id<Steps>, callback: () => Promise<void> | void) => Promise<void> | void;
3135
/**
3236
* Executes a function based on the current step ID.
3337
* @param id - The ID of the step to check.

packages/react/src/define-stepper.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ export const defineStepper = <const Steps extends Step[]>(...steps: Steps): Step
7979
get(id) {
8080
return steps.find((step) => step.id === id);
8181
},
82+
async beforeGoTo(id, callback) {
83+
const shouldProceed = await executeStepCallback(callback, true);
84+
if (shouldProceed) this.goTo(id);
85+
},
86+
async afterGoTo(id, callback) {
87+
this.goTo(id);
88+
await executeStepCallback(callback, false);
89+
},
8290
goTo(id) {
8391
const index = steps.findIndex((s) => s.id === id);
8492
setStepIndex(index);

0 commit comments

Comments
 (0)