ant-design/components/progress/Line.tsx
LonelySnowman 5098e6e209
feat: progress add inside and bottom text position (#48157)
* feat: progress add inside and bottom text position

* feat: change progress percent position api

* fix: change to follow coding standards

* docs: reset i18n zh file

* fix: change to follow coding standards

* fix: change to follow coding standards

* docs: reset i18n file

* fix: change to follow coding standards

* docs: reset i18n file

* feat: progress inner text get brighten color

* feat: update progress snapshots

* fix: progress bottom layout center

* feat: update steps snapshots

* feat: update progress snapshots

* feat: update config provider snapshots

* feat: progress inner text add negate color

* feat: update progress snapshots

* feat: progress inner text add light bg color

* feat: progress inner text add light bg color

* feat: change progress percentPosition api

* feat: change progress component test

* feat: update progress line component snapshot

* feat: progress component outer start layout text adapt

* feat: progress line change to flex layout

* feat: update progress snapshots

* feat: update progress line style

* feat: update progress line style

* fix: progress line reset height use inline block

* fix: change progress is line type

* fix: change progress is line type

* fix: delete progress outer styles

* fix: change progress line type layout

* fix: progress add outer style

* feat: update progress snapshots

* fix: change progress text bright color

* fix: optimized code volume

* fix: progress delete after style on inner layout

* fix: update progress test file

---------

Co-authored-by: lijianan <574980606@qq.com>
Co-authored-by: afc163 <afc163@gmail.com>
2024-05-10 10:22:32 +08:00

171 lines
4.9 KiB
TypeScript

import * as React from 'react';
import { presetPrimaryColors } from '@ant-design/colors';
import classNames from 'classnames';
import { devUseWarning } from '../_util/warning';
import type { DirectionType } from '../config-provider';
import type {
ProgressGradient,
ProgressProps,
StringGradients,
PercentPositionType,
} from './progress';
import { LineStrokeColorVar, Percent } from './style';
import { getSize, getSuccessPercent, validProgress } from './utils';
interface LineProps extends ProgressProps {
prefixCls: string;
direction?: DirectionType;
strokeColor?: string | ProgressGradient;
percentPosition: PercentPositionType;
}
/**
* @example
* {
* "0%": "#afc163",
* "75%": "#009900",
* "50%": "green", // ====> '#afc163 0%, #66FF00 25%, #00CC00 50%, #009900 75%, #ffffff 100%'
* "25%": "#66FF00",
* "100%": "#ffffff"
* }
*/
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],
});
}
});
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"
* }
*/
export const handleGradient = (
strokeColor: ProgressGradient,
directionConfig?: DirectionType,
): React.CSSProperties => {
const {
from = presetPrimaryColors.blue,
to = presetPrimaryColors.blue,
direction = directionConfig === 'rtl' ? 'to left' : 'to right',
...rest
} = strokeColor;
if (Object.keys(rest).length !== 0) {
const sortedGradients = sortGradient(rest as StringGradients);
const background = `linear-gradient(${direction}, ${sortedGradients})`;
return { background, [LineStrokeColorVar]: background } as React.CSSProperties;
}
const background = `linear-gradient(${direction}, ${from}, ${to})`;
return { background, [LineStrokeColorVar]: background } as React.CSSProperties;
};
const Line: React.FC<LineProps> = (props) => {
const {
prefixCls,
direction: directionConfig,
percent,
size,
strokeWidth,
strokeColor,
strokeLinecap = 'round',
children,
trailColor = null,
percentPosition,
success,
} = props;
const { align: infoAlign, type: infoPosition } = percentPosition;
const backgroundProps =
strokeColor && typeof strokeColor !== 'string'
? handleGradient(strokeColor, directionConfig)
: { [LineStrokeColorVar]: strokeColor, background: strokeColor };
const borderRadius = strokeLinecap === 'square' || strokeLinecap === 'butt' ? 0 : undefined;
const mergedSize = size ?? [-1, strokeWidth || (size === 'small' ? 6 : 8)];
const [width, height] = getSize(mergedSize, 'line', { strokeWidth });
if (process.env.NODE_ENV !== 'production') {
const warning = devUseWarning('Progress');
warning.deprecated(!('strokeWidth' in props), 'strokeWidth', 'size');
}
const trailStyle: React.CSSProperties = {
backgroundColor: trailColor || undefined,
borderRadius,
};
const percentStyle = {
width: `${validProgress(percent)}%`,
height,
borderRadius,
...backgroundProps,
[Percent]: validProgress(percent) / 100,
};
const successPercent = getSuccessPercent(props);
const successPercentStyle = {
width: `${validProgress(successPercent)}%`,
height,
borderRadius,
backgroundColor: success?.strokeColor,
} as React.CSSProperties;
const outerStyle: React.CSSProperties = {
width: width < 0 ? '100%' : width,
};
const lineInner = (
<div className={`${prefixCls}-inner`} style={trailStyle}>
<div
className={classNames(`${prefixCls}-bg`, `${prefixCls}-bg-${infoPosition}`)}
style={percentStyle}
>
{infoPosition === 'inner' && children}
</div>
{successPercent !== undefined && (
<div className={`${prefixCls}-success-bg`} style={successPercentStyle} />
)}
</div>
);
const isOuterStart = infoPosition === 'outer' && infoAlign === 'start';
const isOuterEnd = infoPosition === 'outer' && infoAlign === 'end';
return infoPosition === 'outer' && infoAlign === 'center' ? (
<div className={`${prefixCls}-layout-bottom`}>
{lineInner}
{children}
</div>
) : (
<div className={`${prefixCls}-outer`} style={outerStyle}>
{isOuterStart && children}
{lineInner}
{isOuterEnd && children}
</div>
);
};
export default Line;