Skip to content

Commit c387633

Browse files
committed
feat(progress): format options
1 parent 5287fd2 commit c387633

File tree

5 files changed

+31
-22
lines changed

5 files changed

+31
-22
lines changed

.changeset/spotty-lines-return.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@zag-js/progress": minor
3+
---
4+
5+
Add support for `locale` and `formatOptions` to properly format the `api.percentAsString` result

.xstate/progress.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ const fetchMachine = createMachine({
2121
min,
2222
defaultValue: props.defaultValue ?? midValue(min, max),
2323
orientation: "horizontal",
24+
formatOptions: {
25+
style: "percent",
26+
...props.formatOptions
27+
},
2428
translations: {
2529
value: ({
2630
percent

packages/machines/progress/src/progress.connect.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type { ProgressApi, ProgressService, ProgressState } from "./progress.typ
66
export function connect<T extends PropTypes>(service: ProgressService, normalize: NormalizeProps<T>): ProgressApi<T> {
77
const { context, computed, prop, send, scope } = service
88
const percent = computed("percent")
9-
const percentAsString = computed("isIndeterminate") ? "" : `${percent}%`
9+
const percentAsString = computed("isIndeterminate") ? "" : computed("formatter").format(computed("percent") / 100)
1010

1111
const max = prop("max")
1212
const min = prop("min")

packages/machines/progress/src/progress.machine.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createMachine } from "@zag-js/core"
1+
import { createMachine, memo } from "@zag-js/core"
22
import { getValuePercent, isNumber } from "@zag-js/utils"
33
import type { ProgressSchema } from "./progress.types"
44

@@ -12,6 +12,10 @@ export const machine = createMachine<ProgressSchema>({
1212
min,
1313
defaultValue: props.defaultValue ?? midValue(min, max),
1414
orientation: "horizontal",
15+
formatOptions: {
16+
style: "percent",
17+
...props.formatOptions,
18+
},
1519
translations: {
1620
value: ({ percent }) => (percent === -1 ? "loading..." : `${percent} percent`),
1721
...props.translations,
@@ -44,9 +48,11 @@ export const machine = createMachine<ProgressSchema>({
4448
if (!isNumber(value)) return -1
4549
return getValuePercent(value, prop("min"), prop("max")) * 100
4650
},
47-
isAtMax: ({ context, prop }) => context.get("value") === prop("max"),
51+
formatter: memo(
52+
({ prop }) => [prop("locale"), prop("formatOptions")],
53+
(locale, formatOptions) => new Intl.NumberFormat(locale, formatOptions),
54+
),
4855
isHorizontal: ({ prop }) => prop("orientation") === "horizontal",
49-
isRtl: ({ prop }) => prop("dir") === "rtl",
5056
},
5157

5258
states: {

packages/machines/progress/src/progress.types.ts

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -73,31 +73,25 @@ export interface ProgressProps extends DirectionProperty, CommonProperties, Orie
7373
* Callback fired when the value changes.
7474
*/
7575
onValueChange?: ((details: ValueChangeDetails) => void) | undefined
76+
/**
77+
* The options to use for formatting the value.
78+
* @default { style: "percent" }
79+
*/
80+
formatOptions?: Intl.NumberFormatOptions | undefined
81+
/**
82+
* The locale to use for formatting the value.
83+
* @default "en-US"
84+
*/
85+
locale?: string | undefined
7686
}
7787

78-
type PropsWithDefault = "orientation" | "translations" | "min" | "max"
88+
type PropsWithDefault = "orientation" | "translations" | "min" | "max" | "formatOptions"
7989

8090
type Computed = Readonly<{
81-
/**
82-
* Whether the progress bar is indeterminate.
83-
*/
8491
isIndeterminate: boolean
85-
/**
86-
* The percentage of the progress bar's value.
87-
*/
8892
percent: number
89-
/**
90-
* Whether the progress bar is at its minimum value.
91-
*/
92-
isAtMax: boolean
93-
/**
94-
* Whether the progress bar is horizontal.
95-
*/
9693
isHorizontal: boolean
97-
/**
98-
* Whether the progress bar is in RTL mode.
99-
*/
100-
isRtl: boolean
94+
formatter: Intl.NumberFormat
10195
}>
10296

10397
export interface ProgressSchema {

0 commit comments

Comments
 (0)