ant-design/components/progress/Steps.tsx
Đào Văn Hùng 3307fb8b0a
feat: Progress/Step supports round/floor/ceil for the current step calculate logic (#52017)
* Support round/floor/ceil for the current step calculate logic

Always `round` is not really good.
In my case, I really want step current to be 2 when percent is 34% with 3 steps.

Signed-off-by: Đào Văn Hùng <memsenpai3@gmail.com>

* Update Steps.tsx

Signed-off-by: Đào Văn Hùng <memsenpai3@gmail.com>

* Update docs and props

* refactor(Steps): rename stepRound to rounding and update type to function

* fix(Steps): update rounding prop type to specify argument type

* fix(Steps): update rounding prop type to specify parameter name

* docs(Progress): add rounding function description to props table

* fix(Steps): update rounding prop to use custom default function and document default value

* fix(Progress): update rounding prop type to specify parameter name in documentation

* fix(Steps): remove extra comma in default rounding prop assignment

* Update components/progress/index.en-US.md

Signed-off-by: lijianan <574980606@qq.com>

* Update components/progress/index.zh-CN.md

Signed-off-by: lijianan <574980606@qq.com>

---------

Signed-off-by: Đào Văn Hùng <memsenpai3@gmail.com>
Signed-off-by: lijianan <574980606@qq.com>
Co-authored-by: lijianan <574980606@qq.com>
2025-01-26 12:52:54 +08:00

57 lines
1.5 KiB
TypeScript

import * as React from 'react';
import classNames from 'classnames';
import type { ProgressProps } from './progress';
import { getSize } from './utils';
interface ProgressStepsProps extends ProgressProps {
steps: number;
rounding?: (step: number) => number;
strokeColor?: string | string[];
trailColor?: string;
}
const Steps: React.FC<ProgressStepsProps> = (props) => {
const {
size,
steps,
rounding: customRounding = Math.round,
percent = 0,
strokeWidth = 8,
strokeColor,
trailColor = null as any,
prefixCls,
children,
} = props;
const current = customRounding(steps * (percent / 100));
const stepWidth = size === 'small' ? 2 : 14;
const mergedSize = size ?? [stepWidth, strokeWidth];
const [width, height] = getSize(mergedSize, 'step', { steps, strokeWidth });
const unitWidth = width / steps;
const styledSteps: React.ReactNode[] = new Array(steps);
for (let i = 0; i < steps; i++) {
const color = Array.isArray(strokeColor) ? strokeColor[i] : strokeColor;
styledSteps[i] = (
<div
key={i}
className={classNames(`${prefixCls}-steps-item`, {
[`${prefixCls}-steps-item-active`]: i <= current - 1,
})}
style={{
backgroundColor: i <= current - 1 ? color : trailColor,
width: unitWidth,
height,
}}
/>
);
}
return (
<div className={`${prefixCls}-steps-outer`}>
{styledSteps}
{children}
</div>
);
};
export default Steps;