All files / src/progress Progress.tsx

100% Statements 4/4
81.82% Branches 9/11
100% Functions 1/1
100% Lines 4/4

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144                                                                                                            6x   6x 2x                                                                                                                                 4x                                          
import React from "react";
import classNames from "classnames";
import { injectValue } from "../_util/inject-value";
import { useConfig } from "../_util/config-context";
import { StyledProps } from "../_type";
 
export interface ProgressProps extends StyledProps {
  /**
   * 进度条类型
   *
   * @default "line"
   */
  type?: "line" | "circle";
 
  /**
   * 百分比,取值 \[0, 100\]
   *
   * @default 0
   */
  percent?: number;
 
  /**
   * 描述内容
   *
   * **环形进度条中默认展示当前进度百分比**
   *
   * @docType React.ReactNode | ((percent: number) => React.ReactNode);
   */
  text?: React.ReactNode | ((percent: number) => React.ReactNode);
 
  /**
   * 进度条下提示内容
   *
   * 仅在 `type = "circle"` 时有效
   */
  tips?: React.ReactNode;
 
  /**
   * 进度条样式
   *
   * **默认将在 `percent` 小于 100 时使用 `default`,等于 100 时使用 `success`**
   */
  theme?: "default" | "success" | "danger";
}
 
export function Progress({
  type = "line",
  percent = 0,
  text,
  tips,
  theme = +percent === 100 ? "success" : "default",
  className,
  style,
}: ProgressProps) {
  const { classPrefix } = useConfig();
  // Circle
  if (type === "circle") {
    return (
      <div
        className={classNames(`${classPrefix}-progress-circle`, className, {
          "is-error": theme === "danger",
          "is-success": theme === "success",
        })}
        style={style}
      >
        <div className={`${classPrefix}-progress-circle__area`}>
          <div className={`${classPrefix}-progress-circle__svg-path`}>
            <svg
              version="1.1"
              id="图层_1"
              xmlns="http://www.w3.org/2000/svg"
              xmlnsXlink="http://www.w3.org/1999/xlink"
              x="0px"
              y="0px"
              width="160px"
              height="160px"
              viewBox="0 0 160 160"
              xmlSpace="preserve"
            >
              <circle
                className={`${classPrefix}-progress-circle__base-ring`}
                fill="none"
                strokeWidth="2"
                cx="80"
                cy="80"
                r="69"
              />
              <circle
                className={`${classPrefix}-progress-circle__current-ring`}
                fill="none"
                strokeWidth="2"
                cx="80"
                cy="80"
                r="69"
                style={{
                  strokeDasharray: `${(434 * percent) / 100}px 434px`,
                  transition: "stroke-dasharray .2s",
                }}
              />
            </svg>
          </div>
          <div
            className={`${classPrefix}-progress-circle__current-text`}
            id="progress-num"
          >
            {text ? (
              <span className="text--chinese">
                {injectValue(text)(percent)}
              </span>
            ) : (
              <>
                {percent}
                <i className="text--symbol">%</i>
              </>
            )}
          </div>
        </div>
        <div className={`${classPrefix}-progress-circle__tips`}>{tips}</div>
      </div>
    );
  }
 
  return (
    <div
      className={classNames(`${classPrefix}-progress`, className, {
        [`${classPrefix}-progress--error`]: theme === "danger",
        [`${classPrefix}-progress--succeed`]: theme === "success",
      })}
      style={style}
    >
      <div
        className={`${classPrefix}-progress__value`}
        style={{ width: `${percent}%`, transition: "width .2s" }}
      >
        {text && (
          <span className={`${classPrefix}-progress__text`}>
            {injectValue(text)(percent)}
          </span>
        )}
      </div>
    </div>
  );
}