2020-03-02 12:09:38 +08:00
|
|
|
import CheckCircleFilled from '@ant-design/icons/CheckCircleFilled';
|
2022-06-22 14:57:09 +08:00
|
|
|
import CheckOutlined from '@ant-design/icons/CheckOutlined';
|
2020-03-02 12:09:38 +08:00
|
|
|
import CloseCircleFilled from '@ant-design/icons/CloseCircleFilled';
|
2022-06-22 14:57:09 +08:00
|
|
|
import CloseOutlined from '@ant-design/icons/CloseOutlined';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import omit from 'rc-util/lib/omit';
|
|
|
|
import * as React from 'react';
|
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';
|
2022-05-10 15:43:29 +08:00
|
|
|
import warning from '../_util/warning';
|
2019-01-18 15:19:36 +08:00
|
|
|
import Circle from './Circle';
|
2022-06-22 14:57:09 +08:00
|
|
|
import Line from './Line';
|
2019-11-22 10:46:33 +08:00
|
|
|
import Steps from './Steps';
|
2022-06-22 14:57:09 +08:00
|
|
|
import { getSuccessPercent, validProgress } from './utils';
|
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';
|
2022-06-01 23:00:03 +08:00
|
|
|
strokeColor?: string | 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) => {
|
2022-06-01 23:00:03 +08:00
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
className,
|
|
|
|
steps,
|
|
|
|
strokeColor,
|
|
|
|
percent = 0,
|
|
|
|
size = 'default',
|
|
|
|
showInfo = true,
|
|
|
|
type = 'line',
|
|
|
|
...restProps
|
|
|
|
} = 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;
|
2022-10-21 11:45:55 +08:00
|
|
|
if (!ProgressStatuses.includes(status!) && 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
|
|
|
}
|
|
|
|
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 = getPrefixCls('progress', customizePrefixCls);
|
|
|
|
const progressStatus = getProgressStatus();
|
|
|
|
const progressInfo = renderProcessInfo(prefixCls, progressStatus);
|
2016-04-05 16:51:26 +08:00
|
|
|
|
2022-05-12 15:56:18 +08:00
|
|
|
warning(
|
2022-05-06 18:34:45 +08:00
|
|
|
!('successPercent' in props),
|
|
|
|
'Progress',
|
|
|
|
'`successPercent` is deprecated. Please use `success.percent` instead.',
|
|
|
|
);
|
2016-04-05 16:51:26 +08:00
|
|
|
|
2022-06-01 23:00:03 +08:00
|
|
|
const strokeColorNotArray = Array.isArray(strokeColor) ? strokeColor[0] : strokeColor;
|
|
|
|
const strokeColorNotGradient =
|
|
|
|
typeof strokeColor === 'string' || Array.isArray(strokeColor) ? strokeColor : undefined;
|
2022-10-21 11:45:55 +08:00
|
|
|
let progress: React.ReactNode;
|
2022-05-06 18:34:45 +08:00
|
|
|
// Render progress shape
|
|
|
|
if (type === 'line') {
|
|
|
|
progress = steps ? (
|
2022-06-01 23:00:03 +08:00
|
|
|
<Steps {...props} strokeColor={strokeColorNotGradient} prefixCls={prefixCls} steps={steps}>
|
2022-05-06 18:34:45 +08:00
|
|
|
{progressInfo}
|
|
|
|
</Steps>
|
|
|
|
) : (
|
2022-06-01 23:00:03 +08:00
|
|
|
<Line
|
|
|
|
{...props}
|
|
|
|
strokeColor={strokeColorNotArray}
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
direction={direction}
|
|
|
|
>
|
2022-05-06 18:34:45 +08:00
|
|
|
{progressInfo}
|
|
|
|
</Line>
|
|
|
|
);
|
|
|
|
} else if (type === 'circle' || type === 'dashboard') {
|
|
|
|
progress = (
|
2022-06-01 23:00:03 +08:00
|
|
|
<Circle
|
|
|
|
{...props}
|
|
|
|
strokeColor={strokeColorNotArray}
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
progressStatus={progressStatus}
|
|
|
|
>
|
2022-05-06 18:34:45 +08:00
|
|
|
{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,
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
{...omit(restProps, [
|
|
|
|
'status',
|
|
|
|
'format',
|
|
|
|
'trailColor',
|
|
|
|
'strokeWidth',
|
|
|
|
'width',
|
|
|
|
'gapDegree',
|
|
|
|
'gapPosition',
|
|
|
|
'strokeLinecap',
|
|
|
|
'success',
|
|
|
|
'successPercent',
|
|
|
|
])}
|
|
|
|
className={classString}
|
2022-11-09 09:42:10 +08:00
|
|
|
role="progressbar"
|
2022-05-06 18:34:45 +08:00
|
|
|
>
|
|
|
|
{progress}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Progress;
|