1- import { forwardRef , type HTMLAttributes } from 'react'
1+ import { forwardRef , type HTMLAttributes , type ReactNode } from 'react'
22import { cn } from '../../../utils/cn'
33
44export interface ProgressProps extends HTMLAttributes < HTMLDivElement > {
55 percent : number
66 status ?: 'normal' | 'success' | 'exception'
77 showInfo ?: boolean
8+ format ?: ( percent : number ) => ReactNode
89}
910
1011export 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