Line.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
  2. import _extends from "@babel/runtime/helpers/esm/extends";
  3. import { createVNode as _createVNode, Fragment as _Fragment } from "vue";
  4. var __rest = this && this.__rest || function (s, e) {
  5. var t = {};
  6. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  7. if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
  8. if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  9. }
  10. return t;
  11. };
  12. import { presetPrimaryColors } from '@ant-design/colors';
  13. import { computed, defineComponent } from 'vue';
  14. import { progressProps } from './props';
  15. import { getSize, getSuccessPercent, validProgress } from './utils';
  16. import devWarning from '../vc-util/devWarning';
  17. import { anyType, stringType } from '../_util/type';
  18. export const lineProps = () => _extends(_extends({}, progressProps()), {
  19. strokeColor: anyType(),
  20. direction: stringType()
  21. });
  22. /**
  23. * {
  24. * '0%': '#afc163',
  25. * '75%': '#009900',
  26. * '50%': 'green', ====> '#afc163 0%, #66FF00 25%, #00CC00 50%, #009900 75%, #ffffff 100%'
  27. * '25%': '#66FF00',
  28. * '100%': '#ffffff'
  29. * }
  30. */
  31. export const sortGradient = gradients => {
  32. let tempArr = [];
  33. Object.keys(gradients).forEach(key => {
  34. const formattedKey = parseFloat(key.replace(/%/g, ''));
  35. if (!isNaN(formattedKey)) {
  36. tempArr.push({
  37. key: formattedKey,
  38. value: gradients[key]
  39. });
  40. }
  41. });
  42. tempArr = tempArr.sort((a, b) => a.key - b.key);
  43. return tempArr.map(_ref => {
  44. let {
  45. key,
  46. value
  47. } = _ref;
  48. return `${value} ${key}%`;
  49. }).join(', ');
  50. };
  51. /**
  52. * Then this man came to realize the truth: Besides six pence, there is the moon. Besides bread and
  53. * butter, there is the bug. And... Besides women, there is the code.
  54. *
  55. * @example
  56. * {
  57. * "0%": "#afc163",
  58. * "25%": "#66FF00",
  59. * "50%": "#00CC00", // ====> linear-gradient(to right, #afc163 0%, #66FF00 25%,
  60. * "75%": "#009900", // #00CC00 50%, #009900 75%, #ffffff 100%)
  61. * "100%": "#ffffff"
  62. * }
  63. */
  64. export const handleGradient = (strokeColor, directionConfig) => {
  65. const {
  66. from = presetPrimaryColors.blue,
  67. to = presetPrimaryColors.blue,
  68. direction = directionConfig === 'rtl' ? 'to left' : 'to right'
  69. } = strokeColor,
  70. rest = __rest(strokeColor, ["from", "to", "direction"]);
  71. if (Object.keys(rest).length !== 0) {
  72. const sortedGradients = sortGradient(rest);
  73. return {
  74. backgroundImage: `linear-gradient(${direction}, ${sortedGradients})`
  75. };
  76. }
  77. return {
  78. backgroundImage: `linear-gradient(${direction}, ${from}, ${to})`
  79. };
  80. };
  81. export default defineComponent({
  82. compatConfig: {
  83. MODE: 3
  84. },
  85. name: 'ProgressLine',
  86. inheritAttrs: false,
  87. props: lineProps(),
  88. setup(props, _ref2) {
  89. let {
  90. slots,
  91. attrs
  92. } = _ref2;
  93. const backgroundProps = computed(() => {
  94. const {
  95. strokeColor,
  96. direction
  97. } = props;
  98. return strokeColor && typeof strokeColor !== 'string' ? handleGradient(strokeColor, direction) : {
  99. backgroundColor: strokeColor
  100. };
  101. });
  102. const borderRadius = computed(() => props.strokeLinecap === 'square' || props.strokeLinecap === 'butt' ? 0 : undefined);
  103. const trailStyle = computed(() => props.trailColor ? {
  104. backgroundColor: props.trailColor
  105. } : undefined);
  106. const mergedSize = computed(() => {
  107. var _a;
  108. return (_a = props.size) !== null && _a !== void 0 ? _a : [-1, props.strokeWidth || (props.size === 'small' ? 6 : 8)];
  109. });
  110. const sizeRef = computed(() => getSize(mergedSize.value, 'line', {
  111. strokeWidth: props.strokeWidth
  112. }));
  113. if (process.env.NODE_ENV !== 'production') {
  114. devWarning('strokeWidth' in props, 'Progress', '`strokeWidth` is deprecated. Please use `size` instead.');
  115. }
  116. const percentStyle = computed(() => {
  117. const {
  118. percent
  119. } = props;
  120. return _extends({
  121. width: `${validProgress(percent)}%`,
  122. height: `${sizeRef.value.height}px`,
  123. borderRadius: borderRadius.value
  124. }, backgroundProps.value);
  125. });
  126. const successPercent = computed(() => {
  127. return getSuccessPercent(props);
  128. });
  129. const successPercentStyle = computed(() => {
  130. const {
  131. success
  132. } = props;
  133. return {
  134. width: `${validProgress(successPercent.value)}%`,
  135. height: `${sizeRef.value.height}px`,
  136. borderRadius: borderRadius.value,
  137. backgroundColor: success === null || success === void 0 ? void 0 : success.strokeColor
  138. };
  139. });
  140. const outerStyle = {
  141. width: sizeRef.value.width < 0 ? '100%' : sizeRef.value.width,
  142. height: `${sizeRef.value.height}px`
  143. };
  144. return () => {
  145. var _a;
  146. return _createVNode(_Fragment, null, [_createVNode("div", _objectSpread(_objectSpread({}, attrs), {}, {
  147. "class": [`${props.prefixCls}-outer`, attrs.class],
  148. "style": [attrs.style, outerStyle]
  149. }), [_createVNode("div", {
  150. "class": `${props.prefixCls}-inner`,
  151. "style": trailStyle.value
  152. }, [_createVNode("div", {
  153. "class": `${props.prefixCls}-bg`,
  154. "style": percentStyle.value
  155. }, null), successPercent.value !== undefined ? _createVNode("div", {
  156. "class": `${props.prefixCls}-success-bg`,
  157. "style": successPercentStyle.value
  158. }, null) : null])]), (_a = slots.default) === null || _a === void 0 ? void 0 : _a.call(slots)]);
  159. };
  160. }
  161. });