Line.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import _extends from "@babel/runtime/helpers/esm/extends";
  2. import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
  3. import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
  4. var _excluded = ["className", "percent", "prefixCls", "strokeColor", "strokeLinecap", "strokeWidth", "style", "trailColor", "trailWidth", "transition"];
  5. import * as React from 'react';
  6. import classNames from 'classnames';
  7. import { useTransitionDuration, defaultProps } from "./common";
  8. var Line = function Line(props) {
  9. var _defaultProps$props = _objectSpread(_objectSpread({}, defaultProps), props),
  10. className = _defaultProps$props.className,
  11. percent = _defaultProps$props.percent,
  12. prefixCls = _defaultProps$props.prefixCls,
  13. strokeColor = _defaultProps$props.strokeColor,
  14. strokeLinecap = _defaultProps$props.strokeLinecap,
  15. strokeWidth = _defaultProps$props.strokeWidth,
  16. style = _defaultProps$props.style,
  17. trailColor = _defaultProps$props.trailColor,
  18. trailWidth = _defaultProps$props.trailWidth,
  19. transition = _defaultProps$props.transition,
  20. restProps = _objectWithoutProperties(_defaultProps$props, _excluded);
  21. // eslint-disable-next-line no-param-reassign
  22. delete restProps.gapPosition;
  23. var percentList = Array.isArray(percent) ? percent : [percent];
  24. var strokeColorList = Array.isArray(strokeColor) ? strokeColor : [strokeColor];
  25. var paths = useTransitionDuration();
  26. var center = strokeWidth / 2;
  27. var right = 100 - strokeWidth / 2;
  28. var pathString = "M ".concat(strokeLinecap === 'round' ? center : 0, ",").concat(center, "\n L ").concat(strokeLinecap === 'round' ? right : 100, ",").concat(center);
  29. var viewBoxString = "0 0 100 ".concat(strokeWidth);
  30. var stackPtg = 0;
  31. return /*#__PURE__*/React.createElement("svg", _extends({
  32. className: classNames("".concat(prefixCls, "-line"), className),
  33. viewBox: viewBoxString,
  34. preserveAspectRatio: "none",
  35. style: style
  36. }, restProps), /*#__PURE__*/React.createElement("path", {
  37. className: "".concat(prefixCls, "-line-trail"),
  38. d: pathString,
  39. strokeLinecap: strokeLinecap,
  40. stroke: trailColor,
  41. strokeWidth: trailWidth || strokeWidth,
  42. fillOpacity: "0"
  43. }), percentList.map(function (ptg, index) {
  44. var dashPercent = 1;
  45. switch (strokeLinecap) {
  46. case 'round':
  47. dashPercent = 1 - strokeWidth / 100;
  48. break;
  49. case 'square':
  50. dashPercent = 1 - strokeWidth / 2 / 100;
  51. break;
  52. default:
  53. dashPercent = 1;
  54. break;
  55. }
  56. var pathStyle = {
  57. strokeDasharray: "".concat(ptg * dashPercent, "px, 100px"),
  58. strokeDashoffset: "-".concat(stackPtg, "px"),
  59. transition: transition || 'stroke-dashoffset 0.3s ease 0s, stroke-dasharray .3s ease 0s, stroke 0.3s linear'
  60. };
  61. var color = strokeColorList[index] || strokeColorList[strokeColorList.length - 1];
  62. stackPtg += ptg;
  63. return /*#__PURE__*/React.createElement("path", {
  64. key: index,
  65. className: "".concat(prefixCls, "-line-path"),
  66. d: pathString,
  67. strokeLinecap: strokeLinecap,
  68. stroke: color,
  69. strokeWidth: strokeWidth,
  70. fillOpacity: "0",
  71. ref: function ref(elem) {
  72. // https://reactjs.org/docs/refs-and-the-dom.html#callback-refs
  73. // React will call the ref callback with the DOM element when the component mounts,
  74. // and call it with `null` when it unmounts.
  75. // Refs are guaranteed to be up-to-date before componentDidMount or componentDidUpdate fires.
  76. paths[index] = elem;
  77. },
  78. style: pathStyle
  79. });
  80. }));
  81. };
  82. if (process.env.NODE_ENV !== 'production') {
  83. Line.displayName = 'Line';
  84. }
  85. export default Line;