Skip to content

Commit 28d5508

Browse files
committed
feat(ui): support Progress custom format
1 parent c69d578 commit 28d5508

1 file changed

Lines changed: 9 additions & 6 deletions

File tree

packages/ui/src/components/data/Progress/Progress.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { forwardRef, type HTMLAttributes } from 'react'
1+
import { forwardRef, type HTMLAttributes, type ReactNode } from 'react'
22
import { cn } from '../../../utils/cn'
33

44
export interface ProgressProps extends HTMLAttributes<HTMLDivElement> {
55
percent: number
66
status?: 'normal' | 'success' | 'exception'
77
showInfo?: boolean
8+
format?: (percent: number) => ReactNode
89
}
910

1011
export const Progress = forwardRef<HTMLDivElement, ProgressProps>(function Progress(
@@ -13,6 +14,7 @@ export const Progress = forwardRef<HTMLDivElement, ProgressProps>(function Progr
1314
percent,
1415
status = 'normal',
1516
showInfo = true,
17+
format,
1618
'aria-label': ariaLabel,
1719
'aria-labelledby': ariaLabelledBy,
1820
'aria-valuetext': ariaValueText,
@@ -22,6 +24,9 @@ export const Progress = forwardRef<HTMLDivElement, ProgressProps>(function Progr
2224
) {
2325
const safePercent = Math.max(0, Math.min(100, percent))
2426
const accessibleLabel = ariaLabel ?? (ariaLabelledBy ? undefined : 'Progress')
27+
const info = format ? format(safePercent) : `${safePercent}%`
28+
const formattedValueText =
29+
typeof info === 'string' || typeof info === 'number' ? String(info) : `${safePercent}%`
2530

2631
return (
2732
<div
@@ -34,7 +39,7 @@ export const Progress = forwardRef<HTMLDivElement, ProgressProps>(function Progr
3439
aria-valuemin={0}
3540
aria-valuemax={100}
3641
aria-valuenow={safePercent}
37-
aria-valuetext={ariaValueText ?? `${safePercent}%`}
42+
aria-valuetext={ariaValueText ?? formattedValueText}
3843
>
3944
<div className="h-2 w-full overflow-hidden rounded-full bg-slate-200 dark:bg-slate-700">
4045
<div
@@ -46,9 +51,7 @@ export const Progress = forwardRef<HTMLDivElement, ProgressProps>(function Progr
4651
style={{ width: `${safePercent}%` }}
4752
/>
4853
</div>
49-
{showInfo ? (
50-
<div className="mt-1 text-right text-xs text-slate-500">{safePercent}%</div>
51-
) : null}
54+
{showInfo ? <div className="mt-1 text-right text-xs text-slate-500">{info}</div> : null}
5255
</div>
5356
)
54-
})
57+
})

0 commit comments

Comments
 (0)