2019-01-18 15:19:36 +08:00
|
|
|
import * as React from 'react';
|
2020-08-20 17:15:40 +08:00
|
|
|
import { presetPrimaryColors } from '@ant-design/colors';
|
2020-02-02 11:12:07 +08:00
|
|
|
import { ProgressGradient, ProgressProps, StringGradients } from './progress';
|
2020-08-20 15:27:16 +08:00
|
|
|
import { validProgress, getSuccessPercent } from './utils';
|
2020-11-03 16:22:18 +08:00
|
|
|
import { DirectionType } from '../config-provider';
|
2019-01-18 15:19:36 +08:00
|
|
|
|
|
|
|
interface LineProps extends ProgressProps {
|
|
|
|
prefixCls: string;
|
2020-11-03 16:22:18 +08:00
|
|
|
direction?: DirectionType;
|
2019-01-18 15:19:36 +08:00
|
|
|
children: React.ReactNode;
|
|
|
|
}
|
|
|
|
|
2019-03-24 17:05:24 +08:00
|
|
|
/**
|
|
|
|
* {
|
|
|
|
* '0%': '#afc163',
|
|
|
|
* '75%': '#009900',
|
|
|
|
* '50%': 'green', ====> '#afc163 0%, #66FF00 25%, #00CC00 50%, #009900 75%, #ffffff 100%'
|
|
|
|
* '25%': '#66FF00',
|
|
|
|
* '100%': '#ffffff'
|
|
|
|
* }
|
|
|
|
*/
|
2020-02-25 12:21:49 +08:00
|
|
|
export const sortGradient = (gradients: StringGradients) => {
|
|
|
|
let tempArr: any[] = [];
|
|
|
|
Object.keys(gradients).forEach(key => {
|
|
|
|
const formattedKey = parseFloat(key.replace(/%/g, ''));
|
|
|
|
if (!isNaN(formattedKey)) {
|
|
|
|
tempArr.push({
|
|
|
|
key: formattedKey,
|
|
|
|
value: gradients[key],
|
|
|
|
});
|
2019-03-24 17:05:24 +08:00
|
|
|
}
|
2020-02-25 12:21:49 +08:00
|
|
|
});
|
2019-03-24 17:05:24 +08:00
|
|
|
tempArr = tempArr.sort((a, b) => a.key - b.key);
|
|
|
|
return tempArr.map(({ key, value }) => `${value} ${key}%`).join(', ');
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {
|
|
|
|
* '0%': '#afc163',
|
|
|
|
* '25%': '#66FF00',
|
|
|
|
* '50%': '#00CC00', ====> linear-gradient(to right, #afc163 0%, #66FF00 25%,
|
|
|
|
* '75%': '#009900', #00CC00 50%, #009900 75%, #ffffff 100%)
|
|
|
|
* '100%': '#ffffff'
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2020-11-03 16:22:18 +08:00
|
|
|
export const handleGradient = (strokeColor: ProgressGradient, directionConfig: DirectionType) => {
|
2020-08-20 17:15:40 +08:00
|
|
|
const {
|
|
|
|
from = presetPrimaryColors.blue,
|
|
|
|
to = presetPrimaryColors.blue,
|
2020-11-03 16:22:18 +08:00
|
|
|
direction = directionConfig === 'rtl' ? 'to left' : 'to right',
|
2020-08-20 17:15:40 +08:00
|
|
|
...rest
|
|
|
|
} = strokeColor;
|
2019-03-24 17:05:24 +08:00
|
|
|
if (Object.keys(rest).length !== 0) {
|
|
|
|
const sortedGradients = sortGradient(rest as StringGradients);
|
|
|
|
return { backgroundImage: `linear-gradient(${direction}, ${sortedGradients})` };
|
|
|
|
}
|
|
|
|
return { backgroundImage: `linear-gradient(${direction}, ${from}, ${to})` };
|
|
|
|
};
|
|
|
|
|
2020-03-28 11:56:57 +08:00
|
|
|
const Line: React.FC<LineProps> = props => {
|
2019-01-18 15:19:36 +08:00
|
|
|
const {
|
|
|
|
prefixCls,
|
2020-11-03 16:22:18 +08:00
|
|
|
direction: directionConfig,
|
2019-01-18 15:19:36 +08:00
|
|
|
percent,
|
|
|
|
strokeWidth,
|
|
|
|
size,
|
|
|
|
strokeColor,
|
|
|
|
strokeLinecap,
|
|
|
|
children,
|
2020-02-25 11:11:19 +08:00
|
|
|
trailColor,
|
2020-06-28 22:41:59 +08:00
|
|
|
success,
|
2019-01-18 15:19:36 +08:00
|
|
|
} = props;
|
2020-06-28 22:41:59 +08:00
|
|
|
|
2020-08-20 15:27:16 +08:00
|
|
|
const backgroundProps =
|
|
|
|
strokeColor && typeof strokeColor !== 'string'
|
2020-11-03 16:22:18 +08:00
|
|
|
? handleGradient(strokeColor, directionConfig)
|
2020-08-20 15:27:16 +08:00
|
|
|
: {
|
|
|
|
background: strokeColor,
|
|
|
|
};
|
2020-06-28 22:41:59 +08:00
|
|
|
|
2020-08-20 15:27:16 +08:00
|
|
|
const trailStyle = trailColor
|
|
|
|
? {
|
|
|
|
backgroundColor: trailColor,
|
|
|
|
}
|
|
|
|
: undefined;
|
2020-06-28 22:41:59 +08:00
|
|
|
|
2019-01-18 15:19:36 +08:00
|
|
|
const percentStyle = {
|
|
|
|
width: `${validProgress(percent)}%`,
|
|
|
|
height: strokeWidth || (size === 'small' ? 6 : 8),
|
2019-07-17 18:28:02 +08:00
|
|
|
borderRadius: strokeLinecap === 'square' ? 0 : '',
|
2019-03-24 17:05:24 +08:00
|
|
|
...backgroundProps,
|
2020-08-20 15:27:16 +08:00
|
|
|
} as React.CSSProperties;
|
2020-06-28 22:41:59 +08:00
|
|
|
|
2020-08-20 15:27:16 +08:00
|
|
|
const successPercent = getSuccessPercent(props);
|
2020-07-01 23:32:22 +08:00
|
|
|
|
2020-08-20 15:27:16 +08:00
|
|
|
const successPercentStyle = {
|
2019-01-18 15:19:36 +08:00
|
|
|
width: `${validProgress(successPercent)}%`,
|
|
|
|
height: strokeWidth || (size === 'small' ? 6 : 8),
|
2019-07-17 18:28:02 +08:00
|
|
|
borderRadius: strokeLinecap === 'square' ? 0 : '',
|
2020-08-20 15:27:16 +08:00
|
|
|
backgroundColor: success?.strokeColor,
|
|
|
|
} as React.CSSProperties;
|
|
|
|
|
2019-01-18 15:19:36 +08:00
|
|
|
const successSegment =
|
|
|
|
successPercent !== undefined ? (
|
|
|
|
<div className={`${prefixCls}-success-bg`} style={successPercentStyle} />
|
|
|
|
) : null;
|
2020-08-20 15:27:16 +08:00
|
|
|
|
2019-01-18 15:19:36 +08:00
|
|
|
return (
|
2020-02-02 11:12:07 +08:00
|
|
|
<>
|
2019-01-18 15:19:36 +08:00
|
|
|
<div className={`${prefixCls}-outer`}>
|
2020-02-25 11:11:19 +08:00
|
|
|
<div className={`${prefixCls}-inner`} style={trailStyle}>
|
2019-01-18 15:19:36 +08:00
|
|
|
<div className={`${prefixCls}-bg`} style={percentStyle} />
|
|
|
|
{successSegment}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
{children}
|
2020-02-02 11:12:07 +08:00
|
|
|
</>
|
2019-01-18 15:19:36 +08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Line;
|