linkmore-design
Version:
π πlmη»δ»ΆεΊγπ
120 lines (118 loc) β’ 3.74 kB
JavaScript
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.sortGradient = exports.handleGradient = exports.default = void 0;
var _colors = require("@ant-design/colors");
var React = _interopRequireWildcard(require("react"));
var _utils = require("./utils");
/**
* @example
* {
* "0%": "#afc163",
* "75%": "#009900",
* "50%": "green", // ====> '#afc163 0%, #66FF00 25%, #00CC00 50%, #009900 75%, #ffffff 100%'
* "25%": "#66FF00",
* "100%": "#ffffff"
* }
*/
const sortGradient = gradients => {
let tempArr = [];
Object.keys(gradients).forEach(key => {
const formattedKey = parseFloat(key.replace(/%/g, ''));
if (!isNaN(formattedKey)) {
tempArr.push({
key: formattedKey,
value: gradients[key]
});
}
});
tempArr = tempArr.sort((a, b) => a.key - b.key);
return tempArr.map(({
key,
value
}) => `${value} ${key}%`).join(', ');
};
/**
* Then this man came to realize the truth: Besides six pence, there is the moon. Besides bread and
* butter, there is the bug. And... Besides women, there is the code.
*
* @example
* {
* "0%": "#afc163",
* "25%": "#66FF00",
* "50%": "#00CC00", // ====> linear-gradient(to right, #afc163 0%, #66FF00 25%,
* "75%": "#009900", // #00CC00 50%, #009900 75%, #ffffff 100%)
* "100%": "#ffffff"
* }
*/
exports.sortGradient = sortGradient;
const handleGradient = (strokeColor, directionConfig) => {
const {
from = _colors.presetPrimaryColors.blue,
to = _colors.presetPrimaryColors.blue,
direction = directionConfig === 'rtl' ? 'to left' : 'to right',
...rest
} = strokeColor;
if (Object.keys(rest).length !== 0) {
const sortedGradients = sortGradient(rest);
return {
backgroundImage: `linear-gradient(${direction}, ${sortedGradients})`
};
}
return {
backgroundImage: `linear-gradient(${direction}, ${from}, ${to})`
};
};
exports.handleGradient = handleGradient;
const Line = props => {
const {
prefixCls,
direction: directionConfig,
percent,
strokeWidth,
size,
strokeColor,
strokeLinecap = 'round',
children,
trailColor = null,
success
} = props;
const backgroundProps = strokeColor && typeof strokeColor !== 'string' ? handleGradient(strokeColor, directionConfig) : {
background: strokeColor
};
const borderRadius = strokeLinecap === 'square' || strokeLinecap === 'butt' ? 0 : undefined;
const trailStyle = {
backgroundColor: trailColor || undefined,
borderRadius
};
const percentStyle = {
width: `${(0, _utils.validProgress)(percent)}%`,
height: strokeWidth || (size === 'small' ? 6 : 8),
borderRadius,
...backgroundProps
};
const successPercent = (0, _utils.getSuccessPercent)(props);
const successPercentStyle = {
width: `${(0, _utils.validProgress)(successPercent)}%`,
height: strokeWidth || (size === 'small' ? 6 : 8),
borderRadius,
backgroundColor: success?.strokeColor
};
const successSegment = successPercent !== undefined ? /*#__PURE__*/React.createElement("div", {
className: `${prefixCls}-success-bg`,
style: successPercentStyle
}) : null;
return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
className: `${prefixCls}-outer`
}, /*#__PURE__*/React.createElement("div", {
className: `${prefixCls}-inner`,
style: trailStyle
}, /*#__PURE__*/React.createElement("div", {
className: `${prefixCls}-bg`,
style: percentStyle
}), successSegment)), children);
};
var _default = Line;
exports.default = _default;
;