ant-design/components/progress/Circle.tsx

98 lines
2.5 KiB
TypeScript
Raw Normal View History

2019-06-26 17:31:54 +08:00
import classNames from 'classnames';
2022-06-22 14:57:09 +08:00
import { Circle as RCCircle } from 'rc-progress';
import * as React from 'react';
import Tooltip from '../tooltip';
2022-06-22 14:57:09 +08:00
import type { ProgressGradient, ProgressProps } from './progress';
import { getPercentage, getStrokeColor } from './utils';
const CIRCLE_MIN_STROKE_WIDTH = 3;
const getMinPercent = (width: number): number => (CIRCLE_MIN_STROKE_WIDTH / width) * 100;
export interface CircleProps extends ProgressProps {
prefixCls: string;
children: React.ReactNode;
progressStatus: string;
strokeColor?: string | ProgressGradient;
}
const Circle: React.FC<CircleProps> = props => {
const {
prefixCls,
width,
strokeWidth,
trailColor = null as unknown as string,
strokeLinecap = 'round',
gapPosition,
gapDegree,
type,
children,
success,
} = props;
const circleSize = width || 120;
const circleStyle: React.CSSProperties = {
width: circleSize,
height: circleSize,
fontSize: circleSize * 0.15 + 6,
};
const memoizedStrokeWidth = React.useMemo<number>(() => {
if (!strokeWidth) {
return Math.max(getMinPercent(circleSize), 6);
}
return strokeWidth;
}, [circleSize, strokeWidth]);
const getGapDegree = React.useMemo<number | undefined>(() => {
// Support gapDeg = 0 when type = 'dashboard'
if (gapDegree || gapDegree === 0) {
return gapDegree;
}
if (type === 'dashboard') {
return 75;
}
return undefined;
}, [gapDegree, type]);
const gapPos = gapPosition || (type === 'dashboard' && 'bottom') || undefined;
2019-12-04 21:01:55 +08:00
// using className to style stroke color
const isGradient = Object.prototype.toString.call(props.strokeColor) === '[object Object]';
const strokeColor = getStrokeColor({ success, strokeColor: props.strokeColor });
2019-06-26 17:31:54 +08:00
const wrapperClassName = classNames(`${prefixCls}-inner`, {
[`${prefixCls}-circle-gradient`]: isGradient,
});
const circleContent = (
<RCCircle
percent={getPercentage(props)}
strokeWidth={memoizedStrokeWidth}
trailWidth={memoizedStrokeWidth}
strokeColor={strokeColor}
strokeLinecap={strokeLinecap}
trailColor={trailColor}
prefixCls={prefixCls}
gapDegree={getGapDegree}
gapPosition={gapPos}
/>
);
return (
2019-06-26 17:31:54 +08:00
<div className={wrapperClassName} style={circleStyle}>
{circleSize <= 20 ? (
<Tooltip title={children}>{circleContent}</Tooltip>
) : (
<>
{circleContent}
{children}
</>
)}
</div>
);
};
export default Circle;