|
| 1 | +import type { CSSProperties, ExtractPropTypes, PropType } from 'vue'; |
| 2 | +import { computed, defineComponent } from 'vue'; |
| 3 | +import type { Direction } from '../config-provider'; |
| 4 | +import PropTypes from '../_util/vue-types'; |
| 5 | +import type { StringGradients, ProgressGradient } from './props'; |
| 6 | +import { progressProps } from './props'; |
| 7 | +import { getSuccessPercent, validProgress } from './utils'; |
| 8 | + |
| 9 | +const lineProps = { |
| 10 | + ...progressProps(), |
| 11 | + prefixCls: PropTypes.string, |
| 12 | + direction: { |
| 13 | + type: String as PropType<Direction>, |
| 14 | + }, |
| 15 | +}; |
| 16 | + |
| 17 | +export type LineProps = Partial<ExtractPropTypes<typeof lineProps>>; |
| 18 | + |
| 19 | +/** |
| 20 | + * { |
| 21 | + * '0%': '#afc163', |
| 22 | + * '75%': '#009900', |
| 23 | + * '50%': 'green', ====> '#afc163 0%, #66FF00 25%, #00CC00 50%, #009900 75%, #ffffff 100%' |
| 24 | + * '25%': '#66FF00', |
| 25 | + * '100%': '#ffffff' |
| 26 | + * } |
| 27 | + */ |
| 28 | +export const sortGradient = (gradients: StringGradients) => { |
| 29 | + let tempArr = []; |
| 30 | + Object.keys(gradients).forEach(key => { |
| 31 | + const formattedKey = parseFloat(key.replace(/%/g, '')); |
| 32 | + if (!isNaN(formattedKey)) { |
| 33 | + tempArr.push({ |
| 34 | + key: formattedKey, |
| 35 | + value: gradients[key], |
| 36 | + }); |
| 37 | + } |
| 38 | + }); |
| 39 | + tempArr = tempArr.sort((a, b) => a.key - b.key); |
| 40 | + return tempArr.map(({ key, value }) => `${value} ${key}%`).join(', '); |
| 41 | +}; |
| 42 | + |
| 43 | +/** |
| 44 | + * Then this man came to realize the truth: Besides six pence, there is the moon. Besides bread and |
| 45 | + * butter, there is the bug. And... Besides women, there is the code. |
| 46 | + * |
| 47 | + * @example |
| 48 | + * { |
| 49 | + * "0%": "#afc163", |
| 50 | + * "25%": "#66FF00", |
| 51 | + * "50%": "#00CC00", // ====> linear-gradient(to right, #afc163 0%, #66FF00 25%, |
| 52 | + * "75%": "#009900", // #00CC00 50%, #009900 75%, #ffffff 100%) |
| 53 | + * "100%": "#ffffff" |
| 54 | + * } |
| 55 | + */ |
| 56 | +export const handleGradient = (strokeColor: ProgressGradient, directionConfig: Direction) => { |
| 57 | + const { |
| 58 | + from = '#1890ff', |
| 59 | + to = '#1890ff', |
| 60 | + direction = directionConfig === 'rtl' ? 'to left' : 'to right', |
| 61 | + ...rest |
| 62 | + } = strokeColor; |
| 63 | + if (Object.keys(rest).length !== 0) { |
| 64 | + const sortedGradients = sortGradient(rest as StringGradients); |
| 65 | + return { backgroundImage: `linear-gradient(${direction}, ${sortedGradients})` }; |
| 66 | + } |
| 67 | + return { backgroundImage: `linear-gradient(${direction}, ${from}, ${to})` }; |
| 68 | +}; |
| 69 | + |
| 70 | +export default defineComponent({ |
| 71 | + name: 'Line', |
| 72 | + props: lineProps, |
| 73 | + setup(props, { slots }) { |
| 74 | + const backgroundProps = computed(() => { |
| 75 | + const { strokeColor, direction } = props; |
| 76 | + return strokeColor && typeof strokeColor !== 'string' |
| 77 | + ? handleGradient(strokeColor, direction) |
| 78 | + : { |
| 79 | + background: strokeColor, |
| 80 | + }; |
| 81 | + }); |
| 82 | + |
| 83 | + const trailStyle = computed(() => |
| 84 | + props.trailColor |
| 85 | + ? { |
| 86 | + backgroundColor: props.trailColor, |
| 87 | + } |
| 88 | + : undefined, |
| 89 | + ); |
| 90 | + |
| 91 | + const percentStyle = computed<CSSProperties>(() => { |
| 92 | + const { percent, strokeWidth, strokeLinecap, size } = props; |
| 93 | + return { |
| 94 | + width: `${validProgress(percent)}%`, |
| 95 | + height: `${strokeWidth || (size === 'small' ? 6 : 8)}px`, |
| 96 | + borderRadius: strokeLinecap === 'square' ? 0 : '', |
| 97 | + ...(backgroundProps.value as any), |
| 98 | + }; |
| 99 | + }); |
| 100 | + |
| 101 | + const successPercent = computed(() => { |
| 102 | + return getSuccessPercent(props); |
| 103 | + }); |
| 104 | + const successPercentStyle = computed<CSSProperties>(() => { |
| 105 | + const { strokeWidth, size, strokeLinecap, success } = props; |
| 106 | + return { |
| 107 | + width: `${validProgress(successPercent.value)}%`, |
| 108 | + height: `${strokeWidth || (size === 'small' ? 6 : 8)}px`, |
| 109 | + borderRadius: strokeLinecap === 'square' ? 0 : '', |
| 110 | + backgroundColor: success?.strokeColor, |
| 111 | + }; |
| 112 | + }); |
| 113 | + |
| 114 | + return () => ( |
| 115 | + <> |
| 116 | + <div class={`${props.prefixCls}-outer`}> |
| 117 | + <div class={`${props.prefixCls}-inner`} style={trailStyle.value}> |
| 118 | + <div class={`${props.prefixCls}-bg`} style={percentStyle.value} /> |
| 119 | + {successPercent.value !== undefined ? ( |
| 120 | + <div class={`${props.prefixCls}-success-bg`} style={successPercentStyle.value} /> |
| 121 | + ) : null} |
| 122 | + </div> |
| 123 | + </div> |
| 124 | + {slots.default?.()} |
| 125 | + </> |
| 126 | + ); |
| 127 | + }, |
| 128 | +}); |
0 commit comments