You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
153 lines
5.4 KiB
153 lines
5.4 KiB
import _extends from "@babel/runtime/helpers/esm/extends";
|
|
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
|
|
import _typeof from "@babel/runtime/helpers/esm/typeof";
|
|
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
|
|
var _excluded = ["id", "prefixCls", "strokeWidth", "trailWidth", "gapDegree", "gapPosition", "trailColor", "strokeLinecap", "style", "className", "strokeColor", "percent"];
|
|
import * as React from 'react';
|
|
import classNames from 'classnames';
|
|
import { useTransitionDuration, defaultProps } from './common';
|
|
import useId from './hooks/useId';
|
|
|
|
function stripPercentToNumber(percent) {
|
|
return +percent.replace('%', '');
|
|
}
|
|
|
|
function toArray(value) {
|
|
var mergedValue = value !== null && value !== void 0 ? value : [];
|
|
return Array.isArray(mergedValue) ? mergedValue : [mergedValue];
|
|
}
|
|
|
|
function getPathStyles(offset, percent, strokeColor, strokeWidth) {
|
|
var gapDegree = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 0;
|
|
var gapPosition = arguments.length > 5 ? arguments[5] : undefined;
|
|
var radius = 50 - strokeWidth / 2;
|
|
var beginPositionX = 0;
|
|
var beginPositionY = -radius;
|
|
var endPositionX = 0;
|
|
var endPositionY = -2 * radius;
|
|
|
|
switch (gapPosition) {
|
|
case 'left':
|
|
beginPositionX = -radius;
|
|
beginPositionY = 0;
|
|
endPositionX = 2 * radius;
|
|
endPositionY = 0;
|
|
break;
|
|
|
|
case 'right':
|
|
beginPositionX = radius;
|
|
beginPositionY = 0;
|
|
endPositionX = -2 * radius;
|
|
endPositionY = 0;
|
|
break;
|
|
|
|
case 'bottom':
|
|
beginPositionY = radius;
|
|
endPositionY = 2 * radius;
|
|
break;
|
|
|
|
default:
|
|
}
|
|
|
|
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);
|
|
var len = Math.PI * 2 * radius;
|
|
var pathStyle = {
|
|
stroke: typeof strokeColor === 'string' ? strokeColor : undefined,
|
|
strokeDasharray: "".concat(percent / 100 * (len - gapDegree), "px ").concat(len, "px"),
|
|
strokeDashoffset: "-".concat(gapDegree / 2 + offset / 100 * (len - gapDegree), "px"),
|
|
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
|
|
|
|
};
|
|
return {
|
|
pathString: pathString,
|
|
pathStyle: pathStyle
|
|
};
|
|
}
|
|
|
|
var Circle = function Circle(_ref) {
|
|
var id = _ref.id,
|
|
prefixCls = _ref.prefixCls,
|
|
strokeWidth = _ref.strokeWidth,
|
|
trailWidth = _ref.trailWidth,
|
|
gapDegree = _ref.gapDegree,
|
|
gapPosition = _ref.gapPosition,
|
|
trailColor = _ref.trailColor,
|
|
strokeLinecap = _ref.strokeLinecap,
|
|
style = _ref.style,
|
|
className = _ref.className,
|
|
strokeColor = _ref.strokeColor,
|
|
percent = _ref.percent,
|
|
restProps = _objectWithoutProperties(_ref, _excluded);
|
|
|
|
var mergedId = useId(id);
|
|
var gradientId = "".concat(mergedId, "-gradient");
|
|
|
|
var _getPathStyles = getPathStyles(0, 100, trailColor, strokeWidth, gapDegree, gapPosition),
|
|
pathString = _getPathStyles.pathString,
|
|
pathStyle = _getPathStyles.pathStyle;
|
|
|
|
var percentList = toArray(percent);
|
|
var strokeColorList = toArray(strokeColor);
|
|
var gradient = strokeColorList.find(function (color) {
|
|
return color && _typeof(color) === 'object';
|
|
});
|
|
|
|
var _useTransitionDuratio = useTransitionDuration(percentList),
|
|
_useTransitionDuratio2 = _slicedToArray(_useTransitionDuratio, 1),
|
|
paths = _useTransitionDuratio2[0];
|
|
|
|
var getStokeList = function getStokeList() {
|
|
var stackPtg = 0;
|
|
return percentList.map(function (ptg, index) {
|
|
var color = strokeColorList[index] || strokeColorList[strokeColorList.length - 1];
|
|
var stroke = color && _typeof(color) === 'object' ? "url(#".concat(gradientId, ")") : '';
|
|
var pathStyles = getPathStyles(stackPtg, ptg, color, strokeWidth, gapDegree, gapPosition);
|
|
stackPtg += ptg;
|
|
return /*#__PURE__*/React.createElement("path", {
|
|
key: index,
|
|
className: "".concat(prefixCls, "-circle-path"),
|
|
d: pathStyles.pathString,
|
|
stroke: stroke,
|
|
strokeLinecap: strokeLinecap,
|
|
strokeWidth: strokeWidth,
|
|
opacity: ptg === 0 ? 0 : 1,
|
|
fillOpacity: "0",
|
|
style: pathStyles.pathStyle,
|
|
ref: paths[index]
|
|
});
|
|
});
|
|
};
|
|
|
|
return /*#__PURE__*/React.createElement("svg", _extends({
|
|
className: classNames("".concat(prefixCls, "-circle"), className),
|
|
viewBox: "0 0 100 100",
|
|
style: style,
|
|
id: id
|
|
}, restProps), gradient && /*#__PURE__*/React.createElement("defs", null, /*#__PURE__*/React.createElement("linearGradient", {
|
|
id: gradientId,
|
|
x1: "100%",
|
|
y1: "0%",
|
|
x2: "0%",
|
|
y2: "0%"
|
|
}, Object.keys(gradient).sort(function (a, b) {
|
|
return stripPercentToNumber(a) - stripPercentToNumber(b);
|
|
}).map(function (key, index) {
|
|
return /*#__PURE__*/React.createElement("stop", {
|
|
key: index,
|
|
offset: key,
|
|
stopColor: gradient[key]
|
|
});
|
|
}))), /*#__PURE__*/React.createElement("path", {
|
|
className: "".concat(prefixCls, "-circle-trail"),
|
|
d: pathString,
|
|
stroke: trailColor,
|
|
strokeLinecap: strokeLinecap,
|
|
strokeWidth: trailWidth || strokeWidth,
|
|
fillOpacity: "0",
|
|
style: pathStyle
|
|
}), getStokeList().reverse());
|
|
};
|
|
|
|
Circle.defaultProps = defaultProps;
|
|
Circle.displayName = 'Circle';
|
|
export default Circle; |