Skip to content

Commit

Permalink
fix(steps): invoke step complete
Browse files Browse the repository at this point in the history
  • Loading branch information
segunadebayo committed Jan 10, 2025
1 parent d5f4b6f commit 5492945
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/sharp-peaches-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@zag-js/steps": patch
---

Fix issue where `onStepComplete` is not implemented
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ See the [Changesets](./.changeset) for the latest changes.

- **Tour**: Remove `zIndex` from elements in favor of userland control.

- **Steps**: Fix issue where `onStepComplete` is not implemented

## [0.81.0](./#0.81.0) - 2025-01-07

### Fixed
Expand Down
1 change: 1 addition & 0 deletions packages/machines/steps/src/steps.connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export function connect<T extends PropTypes>(state: State, send: Send, normalize
percent,
hasNextStep,
hasPrevStep,
isCompleted: state.context.completed,
goToNextStep,
goToPrevStep,
resetStep,
Expand Down
22 changes: 16 additions & 6 deletions packages/machines/steps/src/steps.machine.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createMachine } from "@zag-js/core"
import { compact, isEqual } from "@zag-js/utils"
import { compact, isEqual, isValueWithinRange } from "@zag-js/utils"
import type { MachineContext, MachineState, UserDefinedContext } from "./steps.types"

export function machine(userContext: UserDefinedContext) {
Expand All @@ -21,6 +21,7 @@ export function machine(userContext: UserDefinedContext) {
percent: (ctx) => (ctx.step / ctx.count) * 100,
hasNextStep: (ctx) => ctx.step < ctx.count,
hasPrevStep: (ctx) => ctx.step > 0,
completed: (ctx) => ctx.step === ctx.count,
},

states: {
Expand Down Expand Up @@ -55,21 +56,30 @@ export function machine(userContext: UserDefinedContext) {
resetStep(ctx) {
set.value(ctx, 0)
},
setStep(ctx, event) {
const value = event.value
const inRange = value >= 0 && value < ctx.count
if (!inRange) throw new RangeError(`Index ${value} is out of bounds`)
set.value(ctx, value)
setStep(ctx, evt) {
set.value(ctx, evt.value)
},
},
},
)
}

const validateStep = (ctx: MachineContext, step: number) => {
if (!isValueWithinRange(step, 0, ctx.count)) {
throw new RangeError(`[zag-js/steps] step index ${step} is out of bounds`)
}
}

const set = {
value(ctx: MachineContext, step: number) {
if (isEqual(ctx.step, step)) return
validateStep(ctx, step)

ctx.step = step
ctx.onStepChange?.({ step })

if (ctx.completed) {
ctx.onStepComplete?.()
}
},
}
5 changes: 5 additions & 0 deletions packages/machines/steps/src/steps.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type ComputedContext = Readonly<{
percent: number
hasNextStep: boolean
hasPrevStep: boolean
completed: boolean
}>

export type UserDefinedContext = RequiredBy<PublicContext, "id">
Expand Down Expand Up @@ -113,6 +114,10 @@ export interface MachineApi<T extends PropTypes = PropTypes> {
* Whether the stepper has a previous step.
*/
hasPrevStep: boolean
/**
* Whether the stepper is completed.
*/
isCompleted: boolean
/**
* Function to set the value of the stepper.
*/
Expand Down

0 comments on commit 5492945

Please sign in to comment.