Skip to content

Commit

Permalink
fix(steps): update api and data-attr
Browse files Browse the repository at this point in the history
  • Loading branch information
segunadebayo committed Sep 23, 2024
1 parent b1cf407 commit f685a58
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
7 changes: 7 additions & 0 deletions .changeset/little-adults-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@zag-js/steps": minor
---

- Fix issue where past step item had `data-incomplete` on it.
- Rename `api.value` to `api.step`
- Rename `api.setValue` to `api.setStep`
19 changes: 10 additions & 9 deletions packages/machines/steps/src/steps.connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { dom } from "./steps.dom"
import type { ItemProps, ItemState, MachineApi, Send, State } from "./steps.types"

export function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): MachineApi<T> {
const value = state.context.step
const step = state.context.step
const count = state.context.count
const percent = state.context.percent
const hasNextStep = state.context.hasNextStep
Expand All @@ -15,8 +15,9 @@ export function connect<T extends PropTypes>(state: State, send: Send, normalize
const getItemState = (props: ItemProps): ItemState => ({
triggerId: dom.getTriggerId(state.context, props.index),
contentId: dom.getContentId(state.context, props.index),
current: props.index === value,
completed: props.index < value,
current: props.index === step,
completed: props.index < step,
incomplete: props.index > step,
index: props.index,
first: props.index === 0,
last: props.index === count - 1,
Expand All @@ -34,12 +35,12 @@ export function connect<T extends PropTypes>(state: State, send: Send, normalize
send({ type: "STEP.RESET", src: "reset.trigger.click" })
}

const setValue = (value: number) => {
const setStep = (value: number) => {
send({ type: "STEP.SET", value, src: "api.setValue" })
}

return {
value,
value: step,
count,
percent,
hasNextStep,
Expand All @@ -48,7 +49,7 @@ export function connect<T extends PropTypes>(state: State, send: Send, normalize
goToPrevStep,
resetStep,
getItemState,
setValue,
setStep,

getRootProps() {
return normalize.element({
Expand Down Expand Up @@ -100,7 +101,7 @@ export function connect<T extends PropTypes>(state: State, send: Send, normalize
"data-orientation": state.context.orientation,
"data-complete": dataAttr(itemState.completed),
"data-current": dataAttr(itemState.current),
"data-incomplete": dataAttr(!itemState.current),
"data-incomplete": dataAttr(itemState.incomplete),
onClick(event) {
if (event.defaultPrevented) return
if (state.context.linear) return
Expand Down Expand Up @@ -132,7 +133,7 @@ export function connect<T extends PropTypes>(state: State, send: Send, normalize
"aria-hidden": true,
"data-complete": dataAttr(itemState.completed),
"data-current": dataAttr(itemState.current),
"data-incomplete": dataAttr(!itemState.current),
"data-incomplete": dataAttr(itemState.incomplete),
})
},

Expand All @@ -144,7 +145,7 @@ export function connect<T extends PropTypes>(state: State, send: Send, normalize
"data-orientation": state.context.orientation,
"data-complete": dataAttr(itemState.completed),
"data-current": dataAttr(itemState.current),
"data-incomplete": dataAttr(!itemState.current),
"data-incomplete": dataAttr(itemState.incomplete),
})
},

Expand Down
3 changes: 2 additions & 1 deletion packages/machines/steps/src/steps.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export interface ItemState {
contentId: string
current: boolean
completed: boolean
incomplete: boolean
last: boolean
first: boolean
}
Expand Down Expand Up @@ -115,7 +116,7 @@ export interface MachineApi<T extends PropTypes = PropTypes> {
/**
* Function to set the value of the stepper.
*/
setValue(value: number): void
setStep(step: number): void
/**
* Function to go to the next step.
*/
Expand Down

0 comments on commit f685a58

Please sign in to comment.