@@ -2,21 +2,21 @@ import * as React from 'react';
22import classNames from 'classnames' ;
33import { useTransitionDuration , defaultProps } from './common' ;
44import type { ProgressProps , GapPositionType } from './interface' ;
5-
6- let gradientSeed = 0 ;
5+ import useId from './hooks/useId' ;
76
87function stripPercentToNumber ( percent : string ) {
98 return + percent . replace ( '%' , '' ) ;
109}
1110
12- function toArray ( symArray : any ) {
13- return Array . isArray ( symArray ) ? symArray : [ symArray ] ;
11+ function toArray < T > ( value : T | T [ ] ) : T [ ] {
12+ const mergedValue = value ?? [ ] ;
13+ return Array . isArray ( mergedValue ) ? mergedValue : [ mergedValue ] ;
1414}
1515
1616function getPathStyles (
1717 offset : number ,
1818 percent : number ,
19- strokeColor : string ,
19+ strokeColor : string | Record < string , string > ,
2020 strokeWidth : number ,
2121 gapDegree = 0 ,
2222 gapPosition : GapPositionType ,
@@ -65,6 +65,7 @@ function getPathStyles(
6565}
6666
6767const Circle : React . FC < ProgressProps > = ( {
68+ id,
6869 prefixCls,
6970 strokeWidth,
7071 trailWidth,
@@ -78,10 +79,10 @@ const Circle: React.FC<ProgressProps> = ({
7879 percent,
7980 ...restProps
8081} ) => {
81- const gradientId = React . useMemo ( ( ) => {
82- gradientSeed += 1 ;
83- return gradientSeed ;
84- } , [ ] ) ;
82+ const mergedId = useId ( id ) ;
83+
84+ const gradientId = ` ${ mergedId } -gradient` ;
85+
8586 const { pathString, pathStyle } = getPathStyles (
8687 0 ,
8788 100 ,
@@ -92,20 +93,15 @@ const Circle: React.FC<ProgressProps> = ({
9293 ) ;
9394 const percentList = toArray ( percent ) ;
9495 const strokeColorList = toArray ( strokeColor ) ;
95- const gradient = strokeColorList . find (
96- ( color ) => Object . prototype . toString . call ( color ) === '[object Object]' ,
97- ) ;
96+ const gradient = strokeColorList . find ( ( color ) => color && typeof color === 'object' ) ;
9897
9998 const [ paths ] = useTransitionDuration ( percentList ) ;
10099
101100 const getStokeList = ( ) => {
102101 let stackPtg = 0 ;
103102 return percentList . map ( ( ptg , index ) => {
104103 const color = strokeColorList [ index ] || strokeColorList [ strokeColorList . length - 1 ] ;
105- const stroke =
106- Object . prototype . toString . call ( color ) === '[object Object]'
107- ? `url(#${ prefixCls } -gradient-${ gradientId } )`
108- : '' ;
104+ const stroke = color && typeof color === 'object' ? `url(#${ gradientId } )` : '' ;
109105 const pathStyles = getPathStyles ( stackPtg , ptg , color , strokeWidth , gapDegree , gapPosition ) ;
110106 stackPtg += ptg ;
111107 return (
@@ -130,17 +126,12 @@ const Circle: React.FC<ProgressProps> = ({
130126 className = { classNames ( `${ prefixCls } -circle` , className ) }
131127 viewBox = "0 0 100 100"
132128 style = { style }
129+ id = { id }
133130 { ...restProps }
134131 >
135132 { gradient && (
136133 < defs >
137- < linearGradient
138- id = { `${ prefixCls } -gradient-${ gradientId } ` }
139- x1 = "100%"
140- y1 = "0%"
141- x2 = "0%"
142- y2 = "0%"
143- >
134+ < linearGradient id = { gradientId } x1 = "100%" y1 = "0%" x2 = "0%" y2 = "0%" >
144135 { Object . keys ( gradient )
145136 . sort ( ( a , b ) => stripPercentToNumber ( a ) - stripPercentToNumber ( b ) )
146137 . map ( ( key , index ) => (
0 commit comments