progress.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. "use client";
  2. var __rest = this && this.__rest || function (s, e) {
  3. var t = {};
  4. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  5. if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
  6. if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  7. }
  8. return t;
  9. };
  10. import * as React from 'react';
  11. import { FastColor } from '@ant-design/fast-color';
  12. import CheckCircleFilled from "@ant-design/icons/es/icons/CheckCircleFilled";
  13. import CheckOutlined from "@ant-design/icons/es/icons/CheckOutlined";
  14. import CloseCircleFilled from "@ant-design/icons/es/icons/CloseCircleFilled";
  15. import CloseOutlined from "@ant-design/icons/es/icons/CloseOutlined";
  16. import classNames from 'classnames';
  17. import omit from "rc-util/es/omit";
  18. import { devUseWarning } from '../_util/warning';
  19. import { ConfigContext } from '../config-provider';
  20. import Circle from './Circle';
  21. import Line from './Line';
  22. import Steps from './Steps';
  23. import useStyle from './style';
  24. import { getSize, getSuccessPercent, validProgress } from './utils';
  25. export const ProgressTypes = ['line', 'circle', 'dashboard'];
  26. const ProgressStatuses = ['normal', 'exception', 'active', 'success'];
  27. const Progress = /*#__PURE__*/React.forwardRef((props, ref) => {
  28. const {
  29. prefixCls: customizePrefixCls,
  30. className,
  31. rootClassName,
  32. steps,
  33. strokeColor,
  34. percent = 0,
  35. size = 'default',
  36. showInfo = true,
  37. type = 'line',
  38. status,
  39. format,
  40. style,
  41. percentPosition = {}
  42. } = props,
  43. restProps = __rest(props, ["prefixCls", "className", "rootClassName", "steps", "strokeColor", "percent", "size", "showInfo", "type", "status", "format", "style", "percentPosition"]);
  44. const {
  45. align: infoAlign = 'end',
  46. type: infoPosition = 'outer'
  47. } = percentPosition;
  48. const strokeColorNotArray = Array.isArray(strokeColor) ? strokeColor[0] : strokeColor;
  49. const strokeColorNotGradient = typeof strokeColor === 'string' || Array.isArray(strokeColor) ? strokeColor : undefined;
  50. const strokeColorIsBright = React.useMemo(() => {
  51. if (strokeColorNotArray) {
  52. const color = typeof strokeColorNotArray === 'string' ? strokeColorNotArray : Object.values(strokeColorNotArray)[0];
  53. return new FastColor(color).isLight();
  54. }
  55. return false;
  56. }, [strokeColor]);
  57. const percentNumber = React.useMemo(() => {
  58. var _a, _b;
  59. const successPercent = getSuccessPercent(props);
  60. return parseInt(successPercent !== undefined ? (_a = successPercent !== null && successPercent !== void 0 ? successPercent : 0) === null || _a === void 0 ? void 0 : _a.toString() : (_b = percent !== null && percent !== void 0 ? percent : 0) === null || _b === void 0 ? void 0 : _b.toString(), 10);
  61. }, [percent, props.success, props.successPercent]);
  62. const progressStatus = React.useMemo(() => {
  63. if (!ProgressStatuses.includes(status) && percentNumber >= 100) {
  64. return 'success';
  65. }
  66. return status || 'normal';
  67. }, [status, percentNumber]);
  68. const {
  69. getPrefixCls,
  70. direction,
  71. progress: progressStyle
  72. } = React.useContext(ConfigContext);
  73. const prefixCls = getPrefixCls('progress', customizePrefixCls);
  74. const [wrapCSSVar, hashId, cssVarCls] = useStyle(prefixCls);
  75. const isLineType = type === 'line';
  76. const isPureLineType = isLineType && !steps;
  77. const progressInfo = React.useMemo(() => {
  78. if (!showInfo) {
  79. return null;
  80. }
  81. const successPercent = getSuccessPercent(props);
  82. let text;
  83. const textFormatter = format || (number => `${number}%`);
  84. const isBrightInnerColor = isLineType && strokeColorIsBright && infoPosition === 'inner';
  85. if (infoPosition === 'inner' || format || progressStatus !== 'exception' && progressStatus !== 'success') {
  86. text = textFormatter(validProgress(percent), validProgress(successPercent));
  87. } else if (progressStatus === 'exception') {
  88. text = isLineType ? /*#__PURE__*/React.createElement(CloseCircleFilled, null) : /*#__PURE__*/React.createElement(CloseOutlined, null);
  89. } else if (progressStatus === 'success') {
  90. text = isLineType ? /*#__PURE__*/React.createElement(CheckCircleFilled, null) : /*#__PURE__*/React.createElement(CheckOutlined, null);
  91. }
  92. return /*#__PURE__*/React.createElement("span", {
  93. className: classNames(`${prefixCls}-text`, {
  94. [`${prefixCls}-text-bright`]: isBrightInnerColor,
  95. [`${prefixCls}-text-${infoAlign}`]: isPureLineType,
  96. [`${prefixCls}-text-${infoPosition}`]: isPureLineType
  97. }),
  98. title: typeof text === 'string' ? text : undefined
  99. }, text);
  100. }, [showInfo, percent, percentNumber, progressStatus, type, prefixCls, format]);
  101. if (process.env.NODE_ENV !== 'production') {
  102. const warning = devUseWarning('Progress');
  103. warning.deprecated(!('successPercent' in props), 'successPercent', 'success.percent');
  104. warning.deprecated(!('width' in props), 'width', 'size');
  105. if (type === 'circle' || type === 'dashboard') {
  106. if (Array.isArray(size)) {
  107. process.env.NODE_ENV !== "production" ? warning(false, 'usage', 'Type "circle" and "dashboard" do not accept array as `size`, please use number or preset size instead.') : void 0;
  108. } else if (typeof size === 'object') {
  109. process.env.NODE_ENV !== "production" ? warning(false, 'usage', 'Type "circle" and "dashboard" do not accept object as `size`, please use number or preset size instead.') : void 0;
  110. }
  111. }
  112. if (props.success && 'progress' in props.success) {
  113. warning.deprecated(false, 'success.progress', 'success.percent');
  114. }
  115. }
  116. let progress;
  117. // Render progress shape
  118. if (type === 'line') {
  119. progress = steps ? (/*#__PURE__*/React.createElement(Steps, Object.assign({}, props, {
  120. strokeColor: strokeColorNotGradient,
  121. prefixCls: prefixCls,
  122. steps: typeof steps === 'object' ? steps.count : steps
  123. }), progressInfo)) : (/*#__PURE__*/React.createElement(Line, Object.assign({}, props, {
  124. strokeColor: strokeColorNotArray,
  125. prefixCls: prefixCls,
  126. direction: direction,
  127. percentPosition: {
  128. align: infoAlign,
  129. type: infoPosition
  130. }
  131. }), progressInfo));
  132. } else if (type === 'circle' || type === 'dashboard') {
  133. progress = /*#__PURE__*/React.createElement(Circle, Object.assign({}, props, {
  134. strokeColor: strokeColorNotArray,
  135. prefixCls: prefixCls,
  136. progressStatus: progressStatus
  137. }), progressInfo);
  138. }
  139. const classString = classNames(prefixCls, `${prefixCls}-status-${progressStatus}`, {
  140. [`${prefixCls}-${type === 'dashboard' && 'circle' || type}`]: type !== 'line',
  141. [`${prefixCls}-inline-circle`]: type === 'circle' && getSize(size, 'circle')[0] <= 20,
  142. [`${prefixCls}-line`]: isPureLineType,
  143. [`${prefixCls}-line-align-${infoAlign}`]: isPureLineType,
  144. [`${prefixCls}-line-position-${infoPosition}`]: isPureLineType,
  145. [`${prefixCls}-steps`]: steps,
  146. [`${prefixCls}-show-info`]: showInfo,
  147. [`${prefixCls}-${size}`]: typeof size === 'string',
  148. [`${prefixCls}-rtl`]: direction === 'rtl'
  149. }, progressStyle === null || progressStyle === void 0 ? void 0 : progressStyle.className, className, rootClassName, hashId, cssVarCls);
  150. return wrapCSSVar(/*#__PURE__*/React.createElement("div", Object.assign({
  151. ref: ref,
  152. style: Object.assign(Object.assign({}, progressStyle === null || progressStyle === void 0 ? void 0 : progressStyle.style), style),
  153. className: classString,
  154. role: "progressbar",
  155. "aria-valuenow": percentNumber,
  156. "aria-valuemin": 0,
  157. "aria-valuemax": 100
  158. }, omit(restProps, ['trailColor', 'strokeWidth', 'width', 'gapDegree', 'gapPosition', 'strokeLinecap', 'success', 'successPercent'])), progress));
  159. });
  160. if (process.env.NODE_ENV !== 'production') {
  161. Progress.displayName = 'Progress';
  162. }
  163. export default Progress;