UNPKG

5.51 kBJavaScriptView Raw
1import _extends from "@babel/runtime/helpers/esm/extends";
2import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
3import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
4import * as React from 'react';
5import classNames from 'classnames';
6import { useTransitionDuration, defaultProps } from './common';
7var gradientSeed = 0;
8
9function stripPercentToNumber(percent) {
10 return +percent.replace('%', '');
11}
12
13function toArray(symArray) {
14 return Array.isArray(symArray) ? symArray : [symArray];
15}
16
17function getPathStyles(offset, percent, strokeColor, strokeWidth) {
18 var gapDegree = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 0;
19 var gapPosition = arguments.length > 5 ? arguments[5] : undefined;
20 var radius = 50 - strokeWidth / 2;
21 var beginPositionX = 0;
22 var beginPositionY = -radius;
23 var endPositionX = 0;
24 var endPositionY = -2 * radius;
25
26 switch (gapPosition) {
27 case 'left':
28 beginPositionX = -radius;
29 beginPositionY = 0;
30 endPositionX = 2 * radius;
31 endPositionY = 0;
32 break;
33
34 case 'right':
35 beginPositionX = radius;
36 beginPositionY = 0;
37 endPositionX = -2 * radius;
38 endPositionY = 0;
39 break;
40
41 case 'bottom':
42 beginPositionY = radius;
43 endPositionY = 2 * radius;
44 break;
45
46 default:
47 }
48
49 var pathString = "M 50,50 m ".concat(beginPositionX, ",").concat(beginPositionY, "\n a ").concat(radius, ",").concat(radius, " 0 1 1 ").concat(endPositionX, ",").concat(-endPositionY, "\n a ").concat(radius, ",").concat(radius, " 0 1 1 ").concat(-endPositionX, ",").concat(endPositionY);
50 var len = Math.PI * 2 * radius;
51 var pathStyle = {
52 stroke: typeof strokeColor === 'string' ? strokeColor : undefined,
53 strokeDasharray: "".concat(percent / 100 * (len - gapDegree), "px ").concat(len, "px"),
54 strokeDashoffset: "-".concat(gapDegree / 2 + offset / 100 * (len - gapDegree), "px"),
55 transition: 'stroke-dashoffset .3s ease 0s, stroke-dasharray .3s ease 0s, stroke .3s, stroke-width .06s ease .3s, opacity .3s ease 0s' // eslint-disable-line
56
57 };
58 return {
59 pathString: pathString,
60 pathStyle: pathStyle
61 };
62}
63
64var Circle = function Circle(_ref) {
65 var prefixCls = _ref.prefixCls,
66 strokeWidth = _ref.strokeWidth,
67 trailWidth = _ref.trailWidth,
68 gapDegree = _ref.gapDegree,
69 gapPosition = _ref.gapPosition,
70 trailColor = _ref.trailColor,
71 strokeLinecap = _ref.strokeLinecap,
72 style = _ref.style,
73 className = _ref.className,
74 strokeColor = _ref.strokeColor,
75 percent = _ref.percent,
76 restProps = _objectWithoutProperties(_ref, ["prefixCls", "strokeWidth", "trailWidth", "gapDegree", "gapPosition", "trailColor", "strokeLinecap", "style", "className", "strokeColor", "percent"]);
77
78 var gradientId = React.useMemo(function () {
79 gradientSeed += 1;
80 return gradientSeed;
81 }, []);
82
83 var _getPathStyles = getPathStyles(0, 100, trailColor, strokeWidth, gapDegree, gapPosition),
84 pathString = _getPathStyles.pathString,
85 pathStyle = _getPathStyles.pathStyle;
86
87 var percentList = toArray(percent);
88 var strokeColorList = toArray(strokeColor);
89 var gradient = strokeColorList.find(function (color) {
90 return Object.prototype.toString.call(color) === '[object Object]';
91 });
92
93 var _useTransitionDuratio = useTransitionDuration(percentList),
94 _useTransitionDuratio2 = _slicedToArray(_useTransitionDuratio, 1),
95 paths = _useTransitionDuratio2[0];
96
97 var getStokeList = function getStokeList() {
98 var stackPtg = 0;
99 return percentList.map(function (ptg, index) {
100 var color = strokeColorList[index] || strokeColorList[strokeColorList.length - 1];
101 var stroke = Object.prototype.toString.call(color) === '[object Object]' ? "url(#".concat(prefixCls, "-gradient-").concat(gradientId, ")") : '';
102 var pathStyles = getPathStyles(stackPtg, ptg, color, strokeWidth, gapDegree, gapPosition);
103 stackPtg += ptg;
104 return /*#__PURE__*/React.createElement("path", {
105 key: index,
106 className: "".concat(prefixCls, "-circle-path"),
107 d: pathStyles.pathString,
108 stroke: stroke,
109 strokeLinecap: strokeLinecap,
110 strokeWidth: strokeWidth,
111 opacity: ptg === 0 ? 0 : 1,
112 fillOpacity: "0",
113 style: pathStyles.pathStyle,
114 ref: paths[index]
115 });
116 });
117 };
118
119 return /*#__PURE__*/React.createElement("svg", _extends({
120 className: classNames("".concat(prefixCls, "-circle"), className),
121 viewBox: "0 0 100 100",
122 style: style
123 }, restProps), gradient && /*#__PURE__*/React.createElement("defs", null, /*#__PURE__*/React.createElement("linearGradient", {
124 id: "".concat(prefixCls, "-gradient-").concat(gradientId),
125 x1: "100%",
126 y1: "0%",
127 x2: "0%",
128 y2: "0%"
129 }, Object.keys(gradient).sort(function (a, b) {
130 return stripPercentToNumber(a) - stripPercentToNumber(b);
131 }).map(function (key, index) {
132 return /*#__PURE__*/React.createElement("stop", {
133 key: index,
134 offset: key,
135 stopColor: gradient[key]
136 });
137 }))), /*#__PURE__*/React.createElement("path", {
138 className: "".concat(prefixCls, "-circle-trail"),
139 d: pathString,
140 stroke: trailColor,
141 strokeLinecap: strokeLinecap,
142 strokeWidth: trailWidth || strokeWidth,
143 fillOpacity: "0",
144 style: pathStyle
145 }), getStokeList().reverse());
146};
147
148Circle.defaultProps = defaultProps;
149Circle.displayName = 'Circle';
150export default Circle;
\No newline at end of file