UNPKG

6.26 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
5var _typeof = require("@babel/runtime/helpers/typeof");
6
7Object.defineProperty(exports, "__esModule", {
8 value: true
9});
10exports["default"] = exports.handleGradient = exports.sortGradient = void 0;
11
12var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
13
14var React = _interopRequireWildcard(require("react"));
15
16var _colors = require("@ant-design/colors");
17
18var _utils = require("./utils");
19
20function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
21
22function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
23
24var __rest = void 0 && (void 0).__rest || function (s, e) {
25 var t = {};
26
27 for (var p in s) {
28 if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
29 }
30
31 if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
32 if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
33 }
34 return t;
35};
36
37/**
38 * @example
39 * {
40 * "0%": "#afc163",
41 * "75%": "#009900",
42 * "50%": "green", // ====> '#afc163 0%, #66FF00 25%, #00CC00 50%, #009900 75%, #ffffff 100%'
43 * "25%": "#66FF00",
44 * "100%": "#ffffff"
45 * }
46 */
47var sortGradient = function sortGradient(gradients) {
48 var tempArr = [];
49 Object.keys(gradients).forEach(function (key) {
50 var formattedKey = parseFloat(key.replace(/%/g, ''));
51
52 if (!isNaN(formattedKey)) {
53 tempArr.push({
54 key: formattedKey,
55 value: gradients[key]
56 });
57 }
58 });
59 tempArr = tempArr.sort(function (a, b) {
60 return a.key - b.key;
61 });
62 return tempArr.map(function (_ref) {
63 var key = _ref.key,
64 value = _ref.value;
65 return "".concat(value, " ").concat(key, "%");
66 }).join(', ');
67};
68/**
69 * Then this man came to realize the truth: Besides six pence, there is the moon. Besides bread and
70 * butter, there is the bug. And... Besides women, there is the code.
71 *
72 * @example
73 * {
74 * "0%": "#afc163",
75 * "25%": "#66FF00",
76 * "50%": "#00CC00", // ====> linear-gradient(to right, #afc163 0%, #66FF00 25%,
77 * "75%": "#009900", // #00CC00 50%, #009900 75%, #ffffff 100%)
78 * "100%": "#ffffff"
79 * }
80 */
81
82
83exports.sortGradient = sortGradient;
84
85var handleGradient = function handleGradient(strokeColor, directionConfig) {
86 var _strokeColor$from = strokeColor.from,
87 from = _strokeColor$from === void 0 ? _colors.presetPrimaryColors.blue : _strokeColor$from,
88 _strokeColor$to = strokeColor.to,
89 to = _strokeColor$to === void 0 ? _colors.presetPrimaryColors.blue : _strokeColor$to,
90 _strokeColor$directio = strokeColor.direction,
91 direction = _strokeColor$directio === void 0 ? directionConfig === 'rtl' ? 'to left' : 'to right' : _strokeColor$directio,
92 rest = __rest(strokeColor, ["from", "to", "direction"]);
93
94 if (Object.keys(rest).length !== 0) {
95 var sortedGradients = sortGradient(rest);
96 return {
97 backgroundImage: "linear-gradient(".concat(direction, ", ").concat(sortedGradients, ")")
98 };
99 }
100
101 return {
102 backgroundImage: "linear-gradient(".concat(direction, ", ").concat(from, ", ").concat(to, ")")
103 };
104};
105
106exports.handleGradient = handleGradient;
107
108var Line = function Line(props) {
109 var prefixCls = props.prefixCls,
110 directionConfig = props.direction,
111 percent = props.percent,
112 strokeWidth = props.strokeWidth,
113 size = props.size,
114 strokeColor = props.strokeColor,
115 strokeLinecap = props.strokeLinecap,
116 children = props.children,
117 trailColor = props.trailColor,
118 success = props.success;
119 var backgroundProps = strokeColor && typeof strokeColor !== 'string' ? handleGradient(strokeColor, directionConfig) : {
120 background: strokeColor
121 };
122 var trailStyle = trailColor ? {
123 backgroundColor: trailColor
124 } : undefined;
125 var percentStyle = (0, _extends2["default"])({
126 width: "".concat((0, _utils.validProgress)(percent), "%"),
127 height: strokeWidth || (size === 'small' ? 6 : 8),
128 borderRadius: strokeLinecap === 'square' ? 0 : ''
129 }, backgroundProps);
130 var successPercent = (0, _utils.getSuccessPercent)(props);
131 var successPercentStyle = {
132 width: "".concat((0, _utils.validProgress)(successPercent), "%"),
133 height: strokeWidth || (size === 'small' ? 6 : 8),
134 borderRadius: strokeLinecap === 'square' ? 0 : '',
135 backgroundColor: success === null || success === void 0 ? void 0 : success.strokeColor
136 };
137 var successSegment = successPercent !== undefined ? /*#__PURE__*/React.createElement("div", {
138 className: "".concat(prefixCls, "-success-bg"),
139 style: successPercentStyle
140 }) : null;
141 return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
142 className: "".concat(prefixCls, "-outer")
143 }, /*#__PURE__*/React.createElement("div", {
144 className: "".concat(prefixCls, "-inner"),
145 style: trailStyle
146 }, /*#__PURE__*/React.createElement("div", {
147 className: "".concat(prefixCls, "-bg"),
148 style: percentStyle
149 }), successSegment)), children);
150};
151
152var _default = Line;
153exports["default"] = _default;
\No newline at end of file