ant-design/components/progress/Circle.tsx
二货机器人 2df11184f4
test: Demo check popup content (#33378)
* auto demo

* chore: update demo snapshot

* fix: trigger mock ref logic

* chore: try 2 test case

* chore: more snapshot

* chore: update snapshot

* chore: fix ts

* chore: split test

* test: Update test case

* chore: fix test order
2021-12-23 11:51:10 +08:00

87 lines
2.4 KiB
TypeScript

import * as React from 'react';
import { Circle as RCCircle } from 'rc-progress';
import { presetPrimaryColors } from '@ant-design/colors';
import classNames from 'classnames';
import { validProgress, getSuccessPercent } from './utils';
import { ProgressProps } from './progress';
interface CircleProps extends ProgressProps {
prefixCls: string;
children: React.ReactNode;
progressStatus: string;
}
function getPercentage({ percent, success, successPercent }: CircleProps) {
const realSuccessPercent = validProgress(getSuccessPercent({ success, successPercent }));
return [realSuccessPercent, validProgress(validProgress(percent) - realSuccessPercent)];
}
function getStrokeColor({
success = {},
strokeColor,
}: Partial<CircleProps>): (string | Record<string, string>)[] {
const { strokeColor: successColor } = success;
return [successColor || presetPrimaryColors.green, strokeColor || null!];
}
const Circle: React.FC<CircleProps> = props => {
const {
prefixCls,
width,
strokeWidth,
trailColor,
strokeLinecap,
gapPosition,
gapDegree,
type,
children,
success,
} = props;
const circleSize = width || 120;
const circleStyle = {
width: circleSize,
height: circleSize,
fontSize: circleSize * 0.15 + 6,
} as React.CSSProperties;
const circleWidth = strokeWidth || 6;
const gapPos = gapPosition || (type === 'dashboard' && 'bottom') || 'top';
const getGapDegree = () => {
// Support gapDeg = 0 when type = 'dashboard'
if (gapDegree || gapDegree === 0) {
return gapDegree;
}
if (type === 'dashboard') {
return 75;
}
return undefined;
};
// using className to style stroke color
const isGradient = Object.prototype.toString.call(props.strokeColor) === '[object Object]';
const strokeColor = getStrokeColor({ success, strokeColor: props.strokeColor });
const wrapperClassName = classNames(`${prefixCls}-inner`, {
[`${prefixCls}-circle-gradient`]: isGradient,
});
return (
<div className={wrapperClassName} style={circleStyle}>
<RCCircle
percent={getPercentage(props)}
strokeWidth={circleWidth}
trailWidth={circleWidth}
strokeColor={strokeColor}
strokeLinecap={strokeLinecap}
trailColor={trailColor}
prefixCls={prefixCls}
gapDegree={getGapDegree()}
gapPosition={gapPos}
/>
{children}
</div>
);
};
export default Circle;