2023-08-25 00:28:38 +08:00
|
|
|
import * as React from 'react';
|
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';
|
2023-08-25 00:28:38 +08:00
|
|
|
|
2023-09-11 17:28:04 +08:00
|
|
|
import { devUseWarning } from '../_util/warning';
|
2022-10-31 10:23:26 +08:00
|
|
|
import type { ConfigConsumerProps } from '../config-provider';
|
2022-05-06 18:34:45 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
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-05-11 10:52:10 +08:00
|
|
|
import useStyle from './style';
|
2023-03-01 11:49:42 +08:00
|
|
|
import { getSize, getSuccessPercent, validProgress } from './utils';
|
2016-03-31 17:46:35 +08:00
|
|
|
|
2023-02-26 10:28:30 +08:00
|
|
|
export const ProgressTypes = ['line', 'circle', 'dashboard'] as const;
|
2023-11-29 17:23:45 +08:00
|
|
|
export type ProgressType = (typeof ProgressTypes)[number];
|
2022-12-13 14:57:40 +08:00
|
|
|
const ProgressStatuses = ['normal', 'exception', 'active', 'success'] as const;
|
2018-04-16 10:58:17 +08:00
|
|
|
export type ProgressSize = 'default' | 'small';
|
2023-08-25 00:28:38 +08:00
|
|
|
export type StringGradients = Record<string, string>;
|
|
|
|
|
2019-03-24 17:05:24 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-05-31 20:36:29 +08:00
|
|
|
export type ProgressAriaProps = Pick<React.AriaAttributes, 'aria-label' | 'aria-labelledby'>;
|
|
|
|
|
|
|
|
export interface ProgressProps extends ProgressAriaProps {
|
2016-12-19 15:19:15 +08:00
|
|
|
prefixCls?: string;
|
|
|
|
className?: string;
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName?: 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;
|
2023-11-29 17:23:45 +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;
|
2023-03-01 11:49:42 +08:00
|
|
|
/** @deprecated Use `size` instead */
|
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';
|
2023-06-20 22:08:25 +08:00
|
|
|
size?: number | [number | string, number] | 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
|
|
|
}
|
|
|
|
|
2023-05-10 13:42:12 +08:00
|
|
|
const Progress = React.forwardRef<HTMLDivElement, ProgressProps>((props, ref) => {
|
2022-06-01 23:00:03 +08:00
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
className,
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName,
|
2022-06-01 23:00:03 +08:00
|
|
|
steps,
|
|
|
|
strokeColor,
|
|
|
|
percent = 0,
|
|
|
|
size = 'default',
|
|
|
|
showInfo = true,
|
|
|
|
type = 'line',
|
2022-10-31 10:23:26 +08:00
|
|
|
status,
|
|
|
|
format,
|
2023-07-05 19:21:56 +08:00
|
|
|
style,
|
2022-06-01 23:00:03 +08:00
|
|
|
...restProps
|
|
|
|
} = props;
|
2016-03-31 17:46:35 +08:00
|
|
|
|
2022-10-31 10:23:26 +08:00
|
|
|
const percentNumber = React.useMemo<number>(() => {
|
2022-05-06 18:34:45 +08:00
|
|
|
const successPercent = getSuccessPercent(props);
|
2019-04-08 22:48:34 +08:00
|
|
|
return parseInt(
|
2023-01-29 11:50:30 +08:00
|
|
|
successPercent !== undefined ? (successPercent ?? 0)?.toString() : (percent ?? 0)?.toString(),
|
2019-04-08 21:43:30 +08:00
|
|
|
10,
|
|
|
|
);
|
2022-10-31 10:23:26 +08:00
|
|
|
}, [percent, props.success, props.successPercent]);
|
2019-04-08 22:48:34 +08:00
|
|
|
|
2023-11-29 17:23:45 +08:00
|
|
|
const progressStatus = React.useMemo<(typeof ProgressStatuses)[number]>(() => {
|
2022-10-31 10:23:26 +08:00
|
|
|
if (!ProgressStatuses.includes(status!) && percentNumber >= 100) {
|
2019-04-08 21:43:30 +08:00
|
|
|
return 'success';
|
|
|
|
}
|
|
|
|
return status || 'normal';
|
2022-10-31 10:23:26 +08:00
|
|
|
}, [status, percentNumber]);
|
2019-04-08 21:43:30 +08:00
|
|
|
|
2023-07-05 19:21:56 +08:00
|
|
|
const {
|
|
|
|
getPrefixCls,
|
|
|
|
direction,
|
|
|
|
progress: progressStyle,
|
|
|
|
} = React.useContext<ConfigConsumerProps>(ConfigContext);
|
2022-10-31 10:23:26 +08:00
|
|
|
const prefixCls = getPrefixCls('progress', customizePrefixCls);
|
2023-12-14 14:58:53 +08:00
|
|
|
const [wrapCSSVar, hashId, cssVarCls] = useStyle(prefixCls);
|
2022-10-31 10:23:26 +08:00
|
|
|
|
|
|
|
const progressInfo = React.useMemo<React.ReactNode>(() => {
|
2021-06-05 13:25:19 +08:00
|
|
|
if (!showInfo) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-10-31 10:23:26 +08:00
|
|
|
const successPercent = getSuccessPercent(props);
|
|
|
|
let text: React.ReactNode;
|
2022-11-13 14:33:07 +08:00
|
|
|
const textFormatter = format || ((number) => `${number}%`);
|
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>
|
|
|
|
);
|
2023-02-26 10:28:30 +08:00
|
|
|
}, [showInfo, percent, percentNumber, progressStatus, type, prefixCls, format]);
|
2016-04-05 16:51:26 +08:00
|
|
|
|
2023-03-01 11:49:42 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
2023-09-13 22:07:33 +08:00
|
|
|
const warning = devUseWarning('Progress');
|
2023-09-11 17:28:04 +08:00
|
|
|
|
2023-09-13 22:07:33 +08:00
|
|
|
warning.deprecated(!('successPercent' in props), 'successPercent', 'success.percent');
|
|
|
|
warning.deprecated(!('width' in props), 'width', 'size');
|
2023-09-11 17:28:04 +08:00
|
|
|
|
|
|
|
if ((type === 'circle' || type === 'dashboard') && Array.isArray(size)) {
|
|
|
|
warning(
|
|
|
|
false,
|
|
|
|
'usage',
|
|
|
|
'Type "circle" and "dashboard" do not accept array as `size`, please use number or preset size instead.',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (props.success && 'progress' in props.success) {
|
2023-09-13 22:07:33 +08:00
|
|
|
warning.deprecated(false, 'success.progress', 'success.percent');
|
2023-09-11 17:28:04 +08:00
|
|
|
}
|
2023-03-01 11:49:42 +08:00
|
|
|
}
|
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,
|
2023-07-22 19:07:57 +08:00
|
|
|
`${prefixCls}-status-${progressStatus}`,
|
2023-07-23 10:12:05 +08:00
|
|
|
`${prefixCls}-${(type === 'dashboard' && 'circle') || (steps && 'steps') || type}`,
|
2022-05-06 18:34:45 +08:00
|
|
|
{
|
2023-03-01 11:49:42 +08:00
|
|
|
[`${prefixCls}-inline-circle`]: type === 'circle' && getSize(size, 'circle')[0] <= 20,
|
2022-05-06 18:34:45 +08:00
|
|
|
[`${prefixCls}-show-info`]: showInfo,
|
2023-03-01 11:49:42 +08:00
|
|
|
[`${prefixCls}-${size}`]: typeof size === 'string',
|
2022-05-06 18:34:45 +08:00
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
},
|
2023-07-05 19:21:56 +08:00
|
|
|
progressStyle?.className,
|
2022-05-06 18:34:45 +08:00
|
|
|
className,
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName,
|
2022-05-11 10:52:10 +08:00
|
|
|
hashId,
|
2023-12-14 14:58:53 +08:00
|
|
|
cssVarCls,
|
2022-05-06 18:34:45 +08:00
|
|
|
);
|
|
|
|
|
2023-11-13 16:08:48 +08:00
|
|
|
return wrapCSSVar(
|
2022-05-06 18:34:45 +08:00
|
|
|
<div
|
2023-05-10 13:42:12 +08:00
|
|
|
ref={ref}
|
2023-07-05 19:21:56 +08:00
|
|
|
style={{ ...progressStyle?.style, ...style }}
|
2022-10-31 10:23:26 +08:00
|
|
|
className={classString}
|
2022-11-13 14:33:07 +08:00
|
|
|
role="progressbar"
|
2023-05-31 20:36:29 +08:00
|
|
|
aria-valuenow={percentNumber}
|
2022-05-06 18:34:45 +08:00
|
|
|
{...omit(restProps, [
|
|
|
|
'trailColor',
|
|
|
|
'strokeWidth',
|
|
|
|
'width',
|
|
|
|
'gapDegree',
|
|
|
|
'gapPosition',
|
|
|
|
'strokeLinecap',
|
|
|
|
'success',
|
|
|
|
'successPercent',
|
|
|
|
])}
|
|
|
|
>
|
|
|
|
{progress}
|
2022-05-11 10:52:10 +08:00
|
|
|
</div>,
|
2022-05-06 18:34:45 +08:00
|
|
|
);
|
2023-05-10 13:42:12 +08:00
|
|
|
});
|
2022-05-06 18:34:45 +08:00
|
|
|
|
2023-01-08 21:30:41 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
Progress.displayName = 'Progress';
|
|
|
|
}
|
|
|
|
|
2022-05-06 18:34:45 +08:00
|
|
|
export default Progress;
|