Skip to content

Commit

Permalink
feat: add valuechange to qrcode and progress
Browse files Browse the repository at this point in the history
  • Loading branch information
segunadebayo committed Jan 7, 2025
1 parent 0e005a8 commit 1185523
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .changeset/smart-spies-brake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@zag-js/progress": minor
"@zag-js/qr-code": minor
---

Add support for `onValueChange` callback
1 change: 1 addition & 0 deletions packages/machines/progress/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type {
Orientation,
ProgressState,
Service,
ValueChangeDetails,
ValueTranslationDetails,
ViewProps,
} from "./progress.types"
12 changes: 10 additions & 2 deletions packages/machines/progress/src/progress.machine.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createMachine } from "@zag-js/core"
import { compact, isNumber } from "@zag-js/utils"
import { compact, isEqual, isNumber } from "@zag-js/utils"
import type { MachineContext, MachineState, UserDefinedContext } from "./progress.types"

function midValue(min: number, max: number) {
Expand Down Expand Up @@ -50,7 +50,7 @@ export function machine(userContext: UserDefinedContext) {
{
actions: {
setValue: (ctx, evt) => {
ctx.value = evt.value === null ? null : Math.max(0, Math.min(evt.value, ctx.max))
set.value(ctx, evt.value)
},
validateContext: (ctx) => {
if (ctx.value == null) return
Expand Down Expand Up @@ -83,3 +83,11 @@ function isValidMax(value: number, max: number) {
function isValidMin(value: number, min: number) {
return isValidNumber(value) && value >= min
}

const set = {
value(ctx: MachineContext, value: number | null) {
if (isEqual(ctx.value, value)) return
ctx.value = value === null ? null : Math.max(0, Math.min(value, ctx.max))
ctx.onValueChange?.({ value })
},
}
1 change: 1 addition & 0 deletions packages/machines/progress/src/progress.props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const props = createProps<UserDefinedContext>()([
"orientation",
"translations",
"value",
"onValueChange",
])

export const splitProps = createSplitProps<Partial<UserDefinedContext>>(props)
8 changes: 8 additions & 0 deletions packages/machines/progress/src/progress.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export interface ValueTranslationDetails {
percent: number
}

export interface ValueChangeDetails {
value: number | null
}

export interface IntlTranslations {
value(details: ValueTranslationDetails): string
}
Expand Down Expand Up @@ -60,6 +64,10 @@ interface PublicContext extends DirectionProperty, CommonProperties, Orientation
* The localized messages to use.
*/
translations: IntlTranslations
/**
* Callback fired when the value changes.
*/
onValueChange?: ((details: ValueChangeDetails) => void) | undefined
}

interface PrivateContext {}
Expand Down
1 change: 1 addition & 0 deletions packages/machines/qr-code/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export type {
QrCodeGenerateOptions,
QrCodeGenerateResult,
Service,
ValueChangeDetails,
} from "./qr-code.types"
14 changes: 11 additions & 3 deletions packages/machines/qr-code/src/qr-code.machine.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createMachine } from "@zag-js/core"
import { compact } from "@zag-js/utils"
import { compact, isEqual } from "@zag-js/utils"
import { memoize } from "proxy-memoize"
import { encode } from "uqr"
import type { MachineContext, MachineState, UserDefinedContext } from "./qr-code.types"
Expand Down Expand Up @@ -28,10 +28,18 @@ export function machine(userContext: UserDefinedContext) {
},
{
actions: {
setValue: (ctx, e) => {
ctx.value = e.value
setValue(ctx, evt) {
set.value(ctx, evt.value)
},
},
},
)
}

const set = {
value(ctx: MachineContext, value: string) {
if (isEqual(ctx.value, value)) return
ctx.value = value
ctx.onValueChange?.({ value })
},
}
10 changes: 9 additions & 1 deletion packages/machines/qr-code/src/qr-code.props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ import { createProps } from "@zag-js/types"
import { createSplitProps } from "@zag-js/utils"
import type { UserDefinedContext } from "./qr-code.types"

export const props = createProps<UserDefinedContext>()(["ids", "value", "id", "encoding", "dir", "getRootNode"])
export const props = createProps<UserDefinedContext>()([
"ids",
"value",
"id",
"encoding",
"dir",
"getRootNode",
"onValueChange",
])

export const splitProps = createSplitProps<Partial<UserDefinedContext>>(props)
8 changes: 8 additions & 0 deletions packages/machines/qr-code/src/qr-code.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ export type ElementIds = Partial<{
frame: string
}>

export interface ValueChangeDetails {
value: string
}

interface PublicContext extends DirectionProperty, CommonProperties {
/**
* The value to encode.
Expand All @@ -21,6 +25,10 @@ interface PublicContext extends DirectionProperty, CommonProperties {
* The qr code encoding options.
*/
encoding?: QrCodeGenerateOptions | undefined
/**
* Callback fired when the value changes.
*/
onValueChange?: ((details: ValueChangeDetails) => void) | undefined
}

interface PrivateContext {
Expand Down

0 comments on commit 1185523

Please sign in to comment.