File tree Expand file tree Collapse file tree 2 files changed +33
-17
lines changed
containers/Tutorial/components Expand file tree Collapse file tree 2 files changed +33
-17
lines changed Original file line number Diff line number Diff line change 11import * as React from 'react'
22import { Progress , Icon } from '@alifd/next'
3+ import useNaturalProgress from 'services/hooks/useNaturalProgress'
34
45interface Props {
56 current : number
67 max : number
78}
89
910const ProgressPie = ( props : Props ) => {
10- const [ progress , setProgress ] = React . useState ( 0 )
11- React . useEffect ( ( ) => {
12- let timeout : any
13- let difference = ( props . current - progress ) / 4
14- // for difference>0.01 update progress or make it stop
15- let newProgress = difference > 0.01 ? progress + difference : props . current
16- if ( progress < props . current ) {
17- timeout = setTimeout ( ( ) => {
18- setProgress ( newProgress )
19- } , 100 )
20- }
21- return ( ) => {
22- if ( timeout ) {
23- clearTimeout ( timeout )
24- }
25- }
26- } , [ progress ] )
11+ const progress = useNaturalProgress ( { stop : props . current } )
2712
2813 const progressPercent = Math . floor ( ( progress / props . max ) * 100 )
2914
Original file line number Diff line number Diff line change 1+ import { useEffect , useState } from 'react'
2+
3+ interface ProgressConfig {
4+ start ?: number
5+ stop : number
6+ updateDuration ?: number
7+ }
8+
9+ const useNaturalProgress = ( config : ProgressConfig ) : number => {
10+ const { start, stop, updateDuration } = config
11+ const [ progress , setProgress ] = useState ( start || 0 )
12+ useEffect ( ( ) => {
13+ let timeout : any
14+ let difference = ( stop - progress ) / 4
15+ // for difference>0.01 update progress or make it stop
16+ let newProgress = difference > 0.01 ? progress + difference : stop
17+ if ( progress < stop ) {
18+ timeout = setTimeout ( ( ) => {
19+ setProgress ( newProgress )
20+ } , updateDuration || 100 )
21+ }
22+ return ( ) => {
23+ if ( timeout ) {
24+ clearTimeout ( timeout )
25+ }
26+ }
27+ } , [ progress , stop , updateDuration ] )
28+ return progress
29+ }
30+
31+ export default useNaturalProgress
You can’t perform that action at this time.
0 commit comments