index.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import _extends from "@babel/runtime/helpers/esm/extends";
  2. import _typeof from "@babel/runtime/helpers/esm/typeof";
  3. import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
  4. import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
  5. var _excluded = ["id", "prefixCls", "steps", "strokeWidth", "trailWidth", "gapDegree", "gapPosition", "trailColor", "strokeLinecap", "style", "className", "strokeColor", "percent"];
  6. import * as React from 'react';
  7. import classNames from 'classnames';
  8. import { defaultProps, useTransitionDuration } from "../common";
  9. import useId from "../hooks/useId";
  10. import PtgCircle from "./PtgCircle";
  11. import { VIEW_BOX_SIZE, getCircleStyle } from "./util";
  12. function toArray(value) {
  13. var mergedValue = value !== null && value !== void 0 ? value : [];
  14. return Array.isArray(mergedValue) ? mergedValue : [mergedValue];
  15. }
  16. var Circle = function Circle(props) {
  17. var _defaultProps$props = _objectSpread(_objectSpread({}, defaultProps), props),
  18. id = _defaultProps$props.id,
  19. prefixCls = _defaultProps$props.prefixCls,
  20. steps = _defaultProps$props.steps,
  21. strokeWidth = _defaultProps$props.strokeWidth,
  22. trailWidth = _defaultProps$props.trailWidth,
  23. _defaultProps$props$g = _defaultProps$props.gapDegree,
  24. gapDegree = _defaultProps$props$g === void 0 ? 0 : _defaultProps$props$g,
  25. gapPosition = _defaultProps$props.gapPosition,
  26. trailColor = _defaultProps$props.trailColor,
  27. strokeLinecap = _defaultProps$props.strokeLinecap,
  28. style = _defaultProps$props.style,
  29. className = _defaultProps$props.className,
  30. strokeColor = _defaultProps$props.strokeColor,
  31. percent = _defaultProps$props.percent,
  32. restProps = _objectWithoutProperties(_defaultProps$props, _excluded);
  33. var halfSize = VIEW_BOX_SIZE / 2;
  34. var mergedId = useId(id);
  35. var gradientId = "".concat(mergedId, "-gradient");
  36. var radius = halfSize - strokeWidth / 2;
  37. var perimeter = Math.PI * 2 * radius;
  38. var rotateDeg = gapDegree > 0 ? 90 + gapDegree / 2 : -90;
  39. var perimeterWithoutGap = perimeter * ((360 - gapDegree) / 360);
  40. var _ref = _typeof(steps) === 'object' ? steps : {
  41. count: steps,
  42. gap: 2
  43. },
  44. stepCount = _ref.count,
  45. stepGap = _ref.gap;
  46. var percentList = toArray(percent);
  47. var strokeColorList = toArray(strokeColor);
  48. var gradient = strokeColorList.find(function (color) {
  49. return color && _typeof(color) === 'object';
  50. });
  51. var isConicGradient = gradient && _typeof(gradient) === 'object';
  52. var mergedStrokeLinecap = isConicGradient ? 'butt' : strokeLinecap;
  53. var circleStyle = getCircleStyle(perimeter, perimeterWithoutGap, 0, 100, rotateDeg, gapDegree, gapPosition, trailColor, mergedStrokeLinecap, strokeWidth);
  54. var paths = useTransitionDuration();
  55. var getStokeList = function getStokeList() {
  56. var stackPtg = 0;
  57. return percentList.map(function (ptg, index) {
  58. var color = strokeColorList[index] || strokeColorList[strokeColorList.length - 1];
  59. var circleStyleForStack = getCircleStyle(perimeter, perimeterWithoutGap, stackPtg, ptg, rotateDeg, gapDegree, gapPosition, color, mergedStrokeLinecap, strokeWidth);
  60. stackPtg += ptg;
  61. return /*#__PURE__*/React.createElement(PtgCircle, {
  62. key: index,
  63. color: color,
  64. ptg: ptg,
  65. radius: radius,
  66. prefixCls: prefixCls,
  67. gradientId: gradientId,
  68. style: circleStyleForStack,
  69. strokeLinecap: mergedStrokeLinecap,
  70. strokeWidth: strokeWidth,
  71. gapDegree: gapDegree,
  72. ref: function ref(elem) {
  73. // https://reactjs.org/docs/refs-and-the-dom.html#callback-refs
  74. // React will call the ref callback with the DOM element when the component mounts,
  75. // and call it with `null` when it unmounts.
  76. // Refs are guaranteed to be up-to-date before componentDidMount or componentDidUpdate fires.
  77. paths[index] = elem;
  78. },
  79. size: VIEW_BOX_SIZE
  80. });
  81. }).reverse();
  82. };
  83. var getStepStokeList = function getStepStokeList() {
  84. // only show the first percent when pass steps
  85. var current = Math.round(stepCount * (percentList[0] / 100));
  86. var stepPtg = 100 / stepCount;
  87. var stackPtg = 0;
  88. return new Array(stepCount).fill(null).map(function (_, index) {
  89. var color = index <= current - 1 ? strokeColorList[0] : trailColor;
  90. var stroke = color && _typeof(color) === 'object' ? "url(#".concat(gradientId, ")") : undefined;
  91. var circleStyleForStack = getCircleStyle(perimeter, perimeterWithoutGap, stackPtg, stepPtg, rotateDeg, gapDegree, gapPosition, color, 'butt', strokeWidth, stepGap);
  92. stackPtg += (perimeterWithoutGap - circleStyleForStack.strokeDashoffset + stepGap) * 100 / perimeterWithoutGap;
  93. return /*#__PURE__*/React.createElement("circle", {
  94. key: index,
  95. className: "".concat(prefixCls, "-circle-path"),
  96. r: radius,
  97. cx: halfSize,
  98. cy: halfSize,
  99. stroke: stroke,
  100. strokeWidth: strokeWidth,
  101. opacity: 1,
  102. style: circleStyleForStack,
  103. ref: function ref(elem) {
  104. paths[index] = elem;
  105. }
  106. });
  107. });
  108. };
  109. return /*#__PURE__*/React.createElement("svg", _extends({
  110. className: classNames("".concat(prefixCls, "-circle"), className),
  111. viewBox: "0 0 ".concat(VIEW_BOX_SIZE, " ").concat(VIEW_BOX_SIZE),
  112. style: style,
  113. id: id,
  114. role: "presentation"
  115. }, restProps), !stepCount && /*#__PURE__*/React.createElement("circle", {
  116. className: "".concat(prefixCls, "-circle-trail"),
  117. r: radius,
  118. cx: halfSize,
  119. cy: halfSize,
  120. stroke: trailColor,
  121. strokeLinecap: mergedStrokeLinecap,
  122. strokeWidth: trailWidth || strokeWidth,
  123. style: circleStyle
  124. }), stepCount ? getStepStokeList() : getStokeList());
  125. };
  126. if (process.env.NODE_ENV !== 'production') {
  127. Circle.displayName = 'Circle';
  128. }
  129. export default Circle;