2019-01-18 15:19:36 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
import { Circle as RCCircle } from 'rc-progress';
|
2019-06-26 17:31:54 +08:00
|
|
|
import classNames from 'classnames';
|
2019-01-18 15:19:36 +08:00
|
|
|
import { validProgress } from './utils';
|
|
|
|
import { ProgressProps } from './progress';
|
|
|
|
|
|
|
|
interface CircleProps extends ProgressProps {
|
|
|
|
prefixCls: string;
|
|
|
|
children: React.ReactNode;
|
|
|
|
progressStatus: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getPercentage({ percent, successPercent }: CircleProps) {
|
|
|
|
const ptg = validProgress(percent);
|
2019-06-19 19:41:37 +08:00
|
|
|
if (!successPercent) {
|
|
|
|
return ptg;
|
|
|
|
}
|
2019-01-18 15:19:36 +08:00
|
|
|
|
|
|
|
const successPtg = validProgress(successPercent);
|
|
|
|
return [successPercent, validProgress(ptg - successPtg)];
|
|
|
|
}
|
|
|
|
|
2019-12-04 20:45:12 +08:00
|
|
|
function getStrokeColor({ successPercent, strokeColor }: CircleProps) {
|
|
|
|
const color = strokeColor || null;
|
2019-06-19 19:41:37 +08:00
|
|
|
if (!successPercent) {
|
|
|
|
return color;
|
|
|
|
}
|
2019-12-04 20:45:12 +08:00
|
|
|
return [null, color];
|
2019-01-18 15:19:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const Circle: React.SFC<CircleProps> = props => {
|
|
|
|
const {
|
|
|
|
prefixCls,
|
|
|
|
width,
|
|
|
|
strokeWidth,
|
|
|
|
trailColor,
|
|
|
|
strokeLinecap,
|
|
|
|
gapPosition,
|
|
|
|
gapDegree,
|
|
|
|
type,
|
|
|
|
children,
|
|
|
|
} = props;
|
|
|
|
const circleSize = width || 120;
|
|
|
|
const circleStyle = {
|
|
|
|
width: circleSize,
|
|
|
|
height: circleSize,
|
|
|
|
fontSize: circleSize * 0.15 + 6,
|
|
|
|
};
|
|
|
|
const circleWidth = strokeWidth || 6;
|
|
|
|
const gapPos = gapPosition || (type === 'dashboard' && 'bottom') || 'top';
|
2020-03-22 17:59:43 +08:00
|
|
|
// Support gapDeg = 0 when type = 'dashboard'
|
|
|
|
let gapDeg;
|
|
|
|
if (gapDegree || gapDegree === 0) {
|
|
|
|
gapDeg = gapDegree;
|
|
|
|
}
|
|
|
|
else if (type === 'dashboard') {
|
|
|
|
gapDeg = 75;
|
|
|
|
}
|
|
|
|
|
2019-12-04 21:01:55 +08:00
|
|
|
// using className to style stroke color
|
|
|
|
const strokeColor = getStrokeColor(props) as string | string[] | object;
|
2019-06-26 17:31:54 +08:00
|
|
|
const isGradient = Object.prototype.toString.call(strokeColor) === '[object Object]';
|
|
|
|
|
|
|
|
const wrapperClassName = classNames(`${prefixCls}-inner`, {
|
|
|
|
[`${prefixCls}-circle-gradient`]: isGradient,
|
|
|
|
});
|
2019-01-18 15:19:36 +08:00
|
|
|
|
|
|
|
return (
|
2019-06-26 17:31:54 +08:00
|
|
|
<div className={wrapperClassName} style={circleStyle}>
|
2019-01-18 15:19:36 +08:00
|
|
|
<RCCircle
|
|
|
|
percent={getPercentage(props)}
|
|
|
|
strokeWidth={circleWidth}
|
|
|
|
trailWidth={circleWidth}
|
2019-06-26 17:31:54 +08:00
|
|
|
strokeColor={strokeColor}
|
2019-01-18 15:19:36 +08:00
|
|
|
strokeLinecap={strokeLinecap}
|
|
|
|
trailColor={trailColor}
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
gapDegree={gapDeg}
|
|
|
|
gapPosition={gapPos}
|
|
|
|
/>
|
|
|
|
{children}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Circle;
|