2024-04-08 14:04:08 +08:00
|
|
|
import * as React from 'react';
|
2019-06-26 17:31:54 +08:00
|
|
|
import classNames from 'classnames';
|
2022-11-19 18:27:48 +08:00
|
|
|
import type { ProgressProps as RcProgressProps } from 'rc-progress';
|
2022-06-22 14:57:09 +08:00
|
|
|
import { Circle as RCCircle } from 'rc-progress';
|
2024-04-08 14:04:08 +08:00
|
|
|
|
2022-10-31 10:23:26 +08:00
|
|
|
import Tooltip from '../tooltip';
|
2022-06-22 14:57:09 +08:00
|
|
|
import type { ProgressGradient, ProgressProps } from './progress';
|
2023-03-01 11:49:42 +08:00
|
|
|
import { getPercentage, getSize, getStrokeColor } from './utils';
|
2019-01-18 15:19:36 +08:00
|
|
|
|
2022-10-31 10:23:26 +08:00
|
|
|
const CIRCLE_MIN_STROKE_WIDTH = 3;
|
|
|
|
|
|
|
|
const getMinPercent = (width: number): number => (CIRCLE_MIN_STROKE_WIDTH / width) * 100;
|
|
|
|
|
|
|
|
export interface CircleProps extends ProgressProps {
|
2019-01-18 15:19:36 +08:00
|
|
|
prefixCls: string;
|
|
|
|
children: React.ReactNode;
|
|
|
|
progressStatus: string;
|
2022-06-01 23:00:03 +08:00
|
|
|
strokeColor?: string | ProgressGradient;
|
2019-01-18 15:19:36 +08:00
|
|
|
}
|
|
|
|
|
2022-11-19 13:47:33 +08:00
|
|
|
const Circle: React.FC<CircleProps> = (props) => {
|
2019-01-18 15:19:36 +08:00
|
|
|
const {
|
|
|
|
prefixCls,
|
2022-10-31 10:23:26 +08:00
|
|
|
trailColor = null as unknown as string,
|
2022-05-06 18:34:45 +08:00
|
|
|
strokeLinecap = 'round',
|
2019-01-18 15:19:36 +08:00
|
|
|
gapPosition,
|
|
|
|
gapDegree,
|
2023-03-01 11:49:42 +08:00
|
|
|
width: originWidth = 120,
|
2019-01-18 15:19:36 +08:00
|
|
|
type,
|
|
|
|
children,
|
2021-07-30 13:17:57 +08:00
|
|
|
success,
|
2023-05-13 16:42:38 +08:00
|
|
|
size = originWidth,
|
2024-03-24 18:30:53 +08:00
|
|
|
steps,
|
2019-01-18 15:19:36 +08:00
|
|
|
} = props;
|
2022-10-31 10:23:26 +08:00
|
|
|
|
2023-05-13 16:42:38 +08:00
|
|
|
const [width, height] = getSize(size, 'circle');
|
2023-03-01 11:49:42 +08:00
|
|
|
|
|
|
|
let { strokeWidth } = props;
|
|
|
|
if (strokeWidth === undefined) {
|
|
|
|
strokeWidth = Math.max(getMinPercent(width), 6);
|
|
|
|
}
|
|
|
|
|
|
|
|
const circleStyle: React.CSSProperties = { width, height, fontSize: width * 0.15 + 6 };
|
2022-10-31 10:23:26 +08:00
|
|
|
|
2022-11-19 18:27:48 +08:00
|
|
|
const realGapDegree = React.useMemo<RcProgressProps['gapDegree']>(() => {
|
2020-08-20 17:15:40 +08:00
|
|
|
// Support gapDeg = 0 when type = 'dashboard'
|
|
|
|
if (gapDegree || gapDegree === 0) {
|
|
|
|
return gapDegree;
|
|
|
|
}
|
|
|
|
if (type === 'dashboard') {
|
|
|
|
return 75;
|
|
|
|
}
|
|
|
|
return undefined;
|
2022-10-31 10:23:26 +08:00
|
|
|
}, [gapDegree, type]);
|
|
|
|
|
2024-03-24 18:30:53 +08:00
|
|
|
const percentArray = getPercentage(props);
|
2022-10-31 10:23:26 +08:00
|
|
|
const gapPos = gapPosition || (type === 'dashboard' && 'bottom') || undefined;
|
2020-03-22 17:59:43 +08:00
|
|
|
|
2019-12-04 21:01:55 +08:00
|
|
|
// using className to style stroke color
|
2021-06-05 13:25:19 +08:00
|
|
|
const isGradient = Object.prototype.toString.call(props.strokeColor) === '[object Object]';
|
2021-07-30 13:17:57 +08:00
|
|
|
const strokeColor = getStrokeColor({ success, strokeColor: props.strokeColor });
|
2019-06-26 17:31:54 +08:00
|
|
|
|
|
|
|
const wrapperClassName = classNames(`${prefixCls}-inner`, {
|
|
|
|
[`${prefixCls}-circle-gradient`]: isGradient,
|
|
|
|
});
|
2019-01-18 15:19:36 +08:00
|
|
|
|
2022-10-31 10:23:26 +08:00
|
|
|
const circleContent = (
|
|
|
|
<RCCircle
|
2024-03-24 18:30:53 +08:00
|
|
|
steps={steps}
|
|
|
|
percent={steps ? percentArray[1] : percentArray}
|
2022-11-19 18:27:48 +08:00
|
|
|
strokeWidth={strokeWidth}
|
|
|
|
trailWidth={strokeWidth}
|
2024-03-24 18:30:53 +08:00
|
|
|
strokeColor={steps ? strokeColor[1] : strokeColor}
|
2022-10-31 10:23:26 +08:00
|
|
|
strokeLinecap={strokeLinecap}
|
|
|
|
trailColor={trailColor}
|
|
|
|
prefixCls={prefixCls}
|
2022-11-19 18:27:48 +08:00
|
|
|
gapDegree={realGapDegree}
|
2022-10-31 10:23:26 +08:00
|
|
|
gapPosition={gapPos}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
2024-05-18 15:07:26 +08:00
|
|
|
const smallCircle = width <= 20;
|
|
|
|
const node = (
|
2019-06-26 17:31:54 +08:00
|
|
|
<div className={wrapperClassName} style={circleStyle}>
|
2024-05-18 15:07:26 +08:00
|
|
|
{circleContent}
|
|
|
|
{!smallCircle && children}
|
2019-01-18 15:19:36 +08:00
|
|
|
</div>
|
|
|
|
);
|
2024-05-18 15:07:26 +08:00
|
|
|
|
|
|
|
if (smallCircle) {
|
|
|
|
return <Tooltip title={children}>{node}</Tooltip>;
|
|
|
|
}
|
|
|
|
|
|
|
|
return node;
|
2019-01-18 15:19:36 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
export default Circle;
|