ant-design/components/progress/Circle.tsx
kiner-tang(文辉) da3babbe39
refactor(progress): progress size (#40903)
* feat: progress size

* feat: update snapshots

* feat: update demo

* feat: update demo

* docs: fix lint

* feat: update demo

* feat: update demo

* docs: update doc

* feat: update snapshots

* docs: update doc

* docs: update doc

* docs: update doc

* Update components/progress/index.en-US.md

Co-authored-by: lijianan <574980606@qq.com>

* Update components/progress/index.zh-CN.md

Co-authored-by: lijianan <574980606@qq.com>

* feat: optimize code

* feat: optimize code

* feat: optimize code

* feat: optimize code

* feat: optimize code

* Update components/progress/utils.ts

Co-authored-by: MadCcc <1075746765@qq.com>

* feat: optimize code

* feat: optimize code

* feat: optimize code

* Update components/progress/Circle.tsx

Co-authored-by: MadCcc <1075746765@qq.com>

* feat: optimize code

* feat: optimize code

* docs: update doc

---------

Co-authored-by: 二货机器人 <smith3816@gmail.com>
Co-authored-by: lijianan <574980606@qq.com>
Co-authored-by: MadCcc <1075746765@qq.com>
2023-03-01 11:49:42 +08:00

97 lines
2.6 KiB
TypeScript

import classNames from 'classnames';
import type { ProgressProps as RcProgressProps } from 'rc-progress';
import { Circle as RCCircle } from 'rc-progress';
import * as React from 'react';
import Tooltip from '../tooltip';
import type { ProgressGradient, ProgressProps } from './progress';
import { getPercentage, getSize, 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,
trailColor = null as unknown as string,
strokeLinecap = 'round',
gapPosition,
gapDegree,
width: originWidth = 120,
type,
children,
success,
size,
} = props;
const mergedSize = size ?? [originWidth, originWidth];
const [width, height] = getSize(mergedSize, 'circle');
let { strokeWidth } = props;
if (strokeWidth === undefined) {
strokeWidth = Math.max(getMinPercent(width), 6);
}
const circleStyle: React.CSSProperties = { width, height, fontSize: width * 0.15 + 6 };
const realGapDegree = React.useMemo<RcProgressProps['gapDegree']>(() => {
// 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;
// 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,
});
const circleContent = (
<RCCircle
percent={getPercentage(props)}
strokeWidth={strokeWidth}
trailWidth={strokeWidth}
strokeColor={strokeColor}
strokeLinecap={strokeLinecap}
trailColor={trailColor}
prefixCls={prefixCls}
gapDegree={realGapDegree}
gapPosition={gapPos}
/>
);
return (
<div className={wrapperClassName} style={circleStyle}>
{width <= 20 ? (
<Tooltip title={children}>
<span>{circleContent}</span>
</Tooltip>
) : (
<>
{circleContent}
{children}
</>
)}
</div>
);
};
export default Circle;