2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2016-04-05 16:51:26 +08:00
|
|
|
import classNames from 'classnames';
|
2021-01-13 21:00:30 +08:00
|
|
|
import omit from 'rc-util/lib/omit';
|
2020-03-02 12:09:38 +08:00
|
|
|
import CloseOutlined from '@ant-design/icons/CloseOutlined';
|
|
|
|
import CheckOutlined from '@ant-design/icons/CheckOutlined';
|
|
|
|
import CheckCircleFilled from '@ant-design/icons/CheckCircleFilled';
|
|
|
|
import CloseCircleFilled from '@ant-design/icons/CloseCircleFilled';
|
2022-05-06 18:34:45 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2018-12-22 21:21:42 +08:00
|
|
|
import { tuple } from '../_util/type';
|
2020-06-28 22:41:59 +08:00
|
|
|
import devWarning from '../_util/devWarning';
|
2019-01-18 15:19:36 +08:00
|
|
|
import Line from './Line';
|
|
|
|
import Circle from './Circle';
|
2019-11-22 10:46:33 +08:00
|
|
|
import Steps from './Steps';
|
2020-08-20 15:27:16 +08:00
|
|
|
import { validProgress, getSuccessPercent } from './utils';
|
2022-05-11 10:52:10 +08:00
|
|
|
import useStyle from './style';
|
2016-03-31 17:46:35 +08:00
|
|
|
|
2018-12-22 21:21:42 +08:00
|
|
|
const ProgressTypes = tuple('line', 'circle', 'dashboard');
|
2019-11-28 12:34:33 +08:00
|
|
|
export type ProgressType = typeof ProgressTypes[number];
|
2018-12-22 21:21:42 +08:00
|
|
|
const ProgressStatuses = tuple('normal', 'exception', 'active', 'success');
|
2018-04-16 10:58:17 +08:00
|
|
|
export type ProgressSize = 'default' | 'small';
|
2019-03-24 17:05:24 +08:00
|
|
|
export type StringGradients = { [percentage: string]: string };
|
|
|
|
type FromToGradients = { from: string; to: string };
|
|
|
|
export type ProgressGradient = { direction?: string } & (StringGradients | FromToGradients);
|
2020-06-28 22:41:59 +08:00
|
|
|
|
|
|
|
export interface SuccessProps {
|
2020-07-01 23:32:22 +08:00
|
|
|
percent?: number;
|
|
|
|
/** @deprecated Use `percent` instead */
|
2020-06-28 22:41:59 +08:00
|
|
|
progress?: number;
|
|
|
|
strokeColor?: string;
|
|
|
|
}
|
|
|
|
|
2016-08-09 15:04:02 +08:00
|
|
|
export interface ProgressProps {
|
2016-12-19 15:19:15 +08:00
|
|
|
prefixCls?: string;
|
|
|
|
className?: string;
|
2018-04-16 10:58:17 +08:00
|
|
|
type?: ProgressType;
|
2016-08-09 15:04:02 +08:00
|
|
|
percent?: number;
|
2018-11-14 14:46:07 +08:00
|
|
|
format?: (percent?: number, successPercent?: number) => React.ReactNode;
|
2019-11-28 12:34:33 +08:00
|
|
|
status?: typeof ProgressStatuses[number];
|
2016-08-09 15:04:02 +08:00
|
|
|
showInfo?: boolean;
|
|
|
|
strokeWidth?: number;
|
2019-06-19 19:41:37 +08:00
|
|
|
strokeLinecap?: 'butt' | 'square' | 'round';
|
2019-03-24 17:05:24 +08:00
|
|
|
strokeColor?: string | ProgressGradient;
|
2016-12-19 15:19:15 +08:00
|
|
|
trailColor?: string;
|
2016-08-09 15:04:02 +08:00
|
|
|
width?: number;
|
2020-06-28 22:41:59 +08:00
|
|
|
success?: SuccessProps;
|
2016-08-09 15:04:02 +08:00
|
|
|
style?: React.CSSProperties;
|
2017-03-17 23:57:34 +08:00
|
|
|
gapDegree?: number;
|
|
|
|
gapPosition?: 'top' | 'bottom' | 'left' | 'right';
|
2018-04-16 10:58:17 +08:00
|
|
|
size?: ProgressSize;
|
2019-11-22 10:46:33 +08:00
|
|
|
steps?: number;
|
2020-06-28 22:41:59 +08:00
|
|
|
/** @deprecated Use `success` instead */
|
|
|
|
successPercent?: number;
|
2022-04-08 22:55:42 +08:00
|
|
|
children?: React.ReactNode;
|
2016-08-09 15:04:02 +08:00
|
|
|
}
|
|
|
|
|
2022-05-06 18:34:45 +08:00
|
|
|
const Progress: React.FC<ProgressProps> = (props: ProgressProps) => {
|
|
|
|
const { percent = 0, size = 'default', showInfo = true, type = 'line' } = props;
|
2016-03-31 17:46:35 +08:00
|
|
|
|
2022-05-06 18:34:45 +08:00
|
|
|
function getPercentNumber() {
|
|
|
|
const successPercent = getSuccessPercent(props);
|
2019-04-08 22:48:34 +08:00
|
|
|
return parseInt(
|
2019-04-08 21:43:30 +08:00
|
|
|
successPercent !== undefined ? successPercent.toString() : percent.toString(),
|
|
|
|
10,
|
|
|
|
);
|
2019-04-08 22:48:34 +08:00
|
|
|
}
|
|
|
|
|
2022-05-06 18:34:45 +08:00
|
|
|
function getProgressStatus() {
|
|
|
|
const { status } = props;
|
|
|
|
if (ProgressStatuses.indexOf(status!) < 0 && getPercentNumber() >= 100) {
|
2019-04-08 21:43:30 +08:00
|
|
|
return 'success';
|
|
|
|
}
|
|
|
|
return status || 'normal';
|
|
|
|
}
|
|
|
|
|
2022-05-06 18:34:45 +08:00
|
|
|
function renderProcessInfo(prefixCls: string, progressStatus: typeof ProgressStatuses[number]) {
|
|
|
|
const { format } = props;
|
|
|
|
const successPercent = getSuccessPercent(props);
|
2021-06-05 13:25:19 +08:00
|
|
|
if (!showInfo) {
|
|
|
|
return null;
|
|
|
|
}
|
2019-01-18 15:19:36 +08:00
|
|
|
let text;
|
|
|
|
const textFormatter = format || (percentNumber => `${percentNumber}%`);
|
2019-08-13 14:07:17 +08:00
|
|
|
const isLineType = type === 'line';
|
2019-01-18 15:19:36 +08:00
|
|
|
if (format || (progressStatus !== 'exception' && progressStatus !== 'success')) {
|
|
|
|
text = textFormatter(validProgress(percent), validProgress(successPercent));
|
|
|
|
} else if (progressStatus === 'exception') {
|
2019-11-28 12:34:33 +08:00
|
|
|
text = isLineType ? <CloseCircleFilled /> : <CloseOutlined />;
|
2019-01-18 15:19:36 +08:00
|
|
|
} else if (progressStatus === 'success') {
|
2019-11-28 12:34:33 +08:00
|
|
|
text = isLineType ? <CheckCircleFilled /> : <CheckOutlined />;
|
2019-01-18 15:19:36 +08:00
|
|
|
}
|
2022-05-11 10:52:10 +08:00
|
|
|
|
2019-01-18 15:19:36 +08:00
|
|
|
return (
|
|
|
|
<span className={`${prefixCls}-text`} title={typeof text === 'string' ? text : undefined}>
|
|
|
|
{text}
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-05-06 18:34:45 +08:00
|
|
|
const { getPrefixCls, direction } = React.useContext(ConfigContext);
|
2020-06-28 22:41:59 +08:00
|
|
|
|
2022-05-06 18:34:45 +08:00
|
|
|
const { prefixCls: customizePrefixCls, className, steps, strokeColor, ...restProps } = props;
|
|
|
|
const prefixCls = getPrefixCls('progress', customizePrefixCls);
|
2022-05-11 10:52:10 +08:00
|
|
|
const [wrapSSR, hashId] = useStyle(prefixCls);
|
2022-05-06 18:34:45 +08:00
|
|
|
const progressStatus = getProgressStatus();
|
|
|
|
const progressInfo = renderProcessInfo(prefixCls, progressStatus);
|
2016-04-05 16:51:26 +08:00
|
|
|
|
2022-05-06 18:34:45 +08:00
|
|
|
devWarning(
|
|
|
|
!('successPercent' in props),
|
|
|
|
'Progress',
|
|
|
|
'`successPercent` is deprecated. Please use `success.percent` instead.',
|
|
|
|
);
|
2016-04-05 16:51:26 +08:00
|
|
|
|
2022-05-06 18:34:45 +08:00
|
|
|
let progress;
|
|
|
|
// Render progress shape
|
|
|
|
if (type === 'line') {
|
|
|
|
progress = steps ? (
|
|
|
|
<Steps
|
|
|
|
{...props}
|
|
|
|
strokeColor={typeof strokeColor === 'string' ? strokeColor : undefined}
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
steps={steps}
|
2019-08-05 18:38:10 +08:00
|
|
|
>
|
2022-05-06 18:34:45 +08:00
|
|
|
{progressInfo}
|
|
|
|
</Steps>
|
|
|
|
) : (
|
|
|
|
<Line {...props} prefixCls={prefixCls} direction={direction}>
|
|
|
|
{progressInfo}
|
|
|
|
</Line>
|
|
|
|
);
|
|
|
|
} else if (type === 'circle' || type === 'dashboard') {
|
|
|
|
progress = (
|
|
|
|
<Circle {...props} prefixCls={prefixCls} progressStatus={progressStatus}>
|
|
|
|
{progressInfo}
|
|
|
|
</Circle>
|
2016-03-31 17:46:35 +08:00
|
|
|
);
|
2018-12-05 19:12:18 +08:00
|
|
|
}
|
2022-05-06 18:34:45 +08:00
|
|
|
|
|
|
|
const classString = classNames(
|
|
|
|
prefixCls,
|
|
|
|
{
|
|
|
|
[`${prefixCls}-${(type === 'dashboard' && 'circle') || (steps && 'steps') || type}`]: true,
|
|
|
|
[`${prefixCls}-status-${progressStatus}`]: true,
|
|
|
|
[`${prefixCls}-show-info`]: showInfo,
|
|
|
|
[`${prefixCls}-${size}`]: size,
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
},
|
|
|
|
className,
|
2022-05-11 10:52:10 +08:00
|
|
|
hashId,
|
2022-05-06 18:34:45 +08:00
|
|
|
);
|
|
|
|
|
2022-05-11 10:52:10 +08:00
|
|
|
return wrapSSR(
|
2022-05-06 18:34:45 +08:00
|
|
|
<div
|
|
|
|
{...omit(restProps, [
|
|
|
|
'status',
|
|
|
|
'format',
|
|
|
|
'trailColor',
|
|
|
|
'strokeWidth',
|
|
|
|
'width',
|
|
|
|
'gapDegree',
|
|
|
|
'gapPosition',
|
|
|
|
'strokeLinecap',
|
|
|
|
'percent',
|
|
|
|
'success',
|
|
|
|
'successPercent',
|
|
|
|
'type',
|
|
|
|
])}
|
|
|
|
className={classString}
|
|
|
|
>
|
|
|
|
{progress}
|
2022-05-11 10:52:10 +08:00
|
|
|
</div>,
|
2022-05-06 18:34:45 +08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Progress;
|