import React from 'react'; import { IProgress } from './types'; import { CircleWrapper, CircleText } from './style'; function add(prev: number, next: number) { if (!next) return prev; return prev + next; } const Circle: React.FC = ({ current, width = 120, height = 4, colors, status = 'normal', children }) => { const currents = typeof current === 'number' ? [current] : current; while (currents.length > colors!.length) { colors = [...colors!, ...colors!]; } const total = currents.reduce(add); if (total >= 100) status = 'success'; const totalPathLen = parseFloat((2 * Math.PI * 47).toFixed(2)); const pathLens: string[] = []; const offsets: number[] = []; let prevOffset = 0; currents.forEach(current => { const percent = ((totalPathLen * current) / total).toFixed(2); pathLens.push(percent); offsets.push(prevOffset); prevOffset += parseFloat(percent); }); const style = { transition: 'stroke-dashoffset 0.3s ease 0s, stroke-dasharray 0.3s ease 0s, stroke 0.3s ease 0s, stroke-width 0.06s ease 0.3s', }; return (
{pathLens.map((pathLen, index) => { return ( ); })} {children ? ( children ) : ( {`${currents[0]}%`} )}
); }; export default React.memo(Circle);