Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: progress add inside and bottom text position #48157

Merged
merged 50 commits into from May 10, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
e5fd3ec
feat: progress add inside and bottom text position
LonelySnowman Mar 28, 2024
19660c9
feat: merge feature branch
LonelySnowman Mar 29, 2024
567e014
feat: change progress percent position api
LonelySnowman Mar 31, 2024
0aad2fa
fix: change to follow coding standards
LonelySnowman Mar 31, 2024
25ba39b
docs: reset i18n zh file
LonelySnowman Mar 31, 2024
819b23e
fix: change to follow coding standards
LonelySnowman Mar 31, 2024
c54307a
fix: change to follow coding standards
LonelySnowman Mar 31, 2024
4f4fa21
docs: reset i18n file
LonelySnowman Mar 31, 2024
776c64a
fix: change to follow coding standards
LonelySnowman Mar 31, 2024
f4e4cce
docs: reset i18n file
LonelySnowman Apr 1, 2024
5a595ba
feat: progress inner text get brighten color
LonelySnowman Apr 1, 2024
38cc44d
feat: update progress snapshots
LonelySnowman Apr 1, 2024
a8dd119
fix: progress bottom layout center
LonelySnowman Apr 1, 2024
697afd1
feat: update steps snapshots
LonelySnowman Apr 1, 2024
92bf8a3
feat: update progress snapshots
LonelySnowman Apr 1, 2024
df99c9e
feat: update config provider snapshots
LonelySnowman Apr 1, 2024
547c623
Merge branch 'feature' into feat-progress-info-inside
li-jia-nan Apr 2, 2024
6c63464
feat: progress inner text add negate color
LonelySnowman Apr 2, 2024
4dc2735
feat: update progress snapshots
LonelySnowman Apr 2, 2024
d87cf55
feat: progress inner text add light bg color
LonelySnowman Apr 4, 2024
c6ef904
feat: progress inner text add light bg color
LonelySnowman Apr 4, 2024
1e36439
fix: fix conflict merge diff
LonelySnowman Apr 9, 2024
565695a
Merge branch 'feature' into feat-progress-info-inside
li-jia-nan Apr 16, 2024
236638c
feat: change progress percentPosition api
LonelySnowman Apr 16, 2024
9312237
feat: change progress component test
LonelySnowman Apr 16, 2024
eb2ae50
feat: update progress line component snapshot
LonelySnowman Apr 16, 2024
8e219d4
Merge branch 'feature' into feat-progress-info-inside
afc163 Apr 17, 2024
c6a440e
feat: progress component outer start layout text adapt
LonelySnowman Apr 21, 2024
4c190dd
Merge branch 'feat-progress-info-inside' of github.com:LonelySnowman/…
LonelySnowman Apr 21, 2024
e8548e1
Merge branch 'feature' into feat-progress-info-inside
LonelySnowman Apr 21, 2024
9856c51
Merge branch 'feature' into feat-progress-info-inside
li-jia-nan Apr 22, 2024
a08bfdc
feat: progress line change to flex layout
LonelySnowman Apr 25, 2024
15d87c5
feat: update progress snapshots
LonelySnowman Apr 25, 2024
aa90f40
Merge branch 'feature' into feat-progress-info-inside
LonelySnowman Apr 25, 2024
8f90d94
feat: update progress line style
LonelySnowman Apr 26, 2024
2d6880b
feat: update progress line style
LonelySnowman Apr 26, 2024
7dd088a
fix: progress line reset height use inline block
LonelySnowman Apr 29, 2024
d361336
fix: change progress is line type
LonelySnowman Apr 29, 2024
cb8dfa9
fix: change progress is line type
LonelySnowman Apr 29, 2024
097108c
Merge branch 'feature' into feat-progress-info-inside
afc163 Apr 30, 2024
869fa50
fix: delete progress outer styles
LonelySnowman Apr 30, 2024
15647ac
fix: change progress line type layout
LonelySnowman May 5, 2024
d1f5773
Merge branch 'feature' into feat-progress-info-inside
li-jia-nan May 5, 2024
1b799e8
fix: progress add outer style
LonelySnowman May 5, 2024
72457b9
feat: update progress snapshots
LonelySnowman May 5, 2024
e4da3e1
fix: change progress text bright color
LonelySnowman May 5, 2024
33f84f4
fix: optimized code volume
LonelySnowman May 9, 2024
9f39a95
fix: progress delete after style on inner layout
LonelySnowman May 9, 2024
a8df1f0
Merge branch 'feature' into feat-progress-info-inside
LonelySnowman May 9, 2024
ca8d0ec
fix: update progress test file
LonelySnowman May 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 35 additions & 9 deletions components/progress/Line.tsx
@@ -1,9 +1,16 @@
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 } from './progress';
import type {
ProgressGradient,
ProgressProps,
StringGradients,
PercentAlignType,
PercentPositionType,
} from './progress';
import { getSize, getSuccessPercent, validProgress } from './utils';
import { LineStrokeColorVar, Percent } from './style';

Expand All @@ -12,6 +19,7 @@ interface LineProps extends ProgressProps {
direction?: DirectionType;
children: React.ReactNode;
strokeColor?: string | ProgressGradient;
percentPosition: [PercentAlignType, PercentPositionType];
}

/**
Expand Down Expand Up @@ -82,9 +90,12 @@ const Line: React.FC<LineProps> = (props) => {
strokeLinecap = 'round',
children,
trailColor = null,
percentPosition,
success,
} = props;

const [infoAlign, infoPosition] = percentPosition;

const backgroundProps =
strokeColor && typeof strokeColor !== 'string'
? handleGradient(strokeColor, directionConfig)
Expand Down Expand Up @@ -129,17 +140,32 @@ const Line: React.FC<LineProps> = (props) => {
height,
};

return (
const lineInner = (
<div className={`${prefixCls}-inner`} style={trailStyle}>
<div
className={classNames(`${prefixCls}-bg`, `${prefixCls}-bg-${infoPosition}`)}
style={percentStyle}
>
{infoPosition === 'inner' ? children : null}
</div>
{successPercent !== undefined ? (
<div className={`${prefixCls}-success-bg`} style={successPercentStyle} />
) : null}
LonelySnowman marked this conversation as resolved.
Show resolved Hide resolved
</div>
);

return infoPosition === 'outer' && infoAlign === 'center' ? (
<div className={`${prefixCls}-layout-bottom`}>
{lineInner}
{children}
</div>
) : (
<>
{infoPosition === 'outer' && infoAlign === 'start' && children}
LonelySnowman marked this conversation as resolved.
Show resolved Hide resolved
<div className={`${prefixCls}-outer`} style={outerStyle}>
LonelySnowman marked this conversation as resolved.
Show resolved Hide resolved
<div className={`${prefixCls}-inner`} style={trailStyle}>
<div className={`${prefixCls}-bg`} style={percentStyle} />
{successPercent !== undefined ? (
<div className={`${prefixCls}-success-bg`} style={successPercentStyle} />
) : null}
</div>
{lineInner}
</div>
{children}
{infoPosition === 'outer' && infoAlign === 'end' && children}
</>
);
};
Expand Down
22 changes: 22 additions & 0 deletions components/progress/__tests__/index.test.tsx
Expand Up @@ -414,4 +414,26 @@ describe('Progress', () => {
const { container } = render(<Progress percent={70} steps={5} />);
expect(container.firstChild).toMatchSnapshot();
});

it('should show inner info position', () => {
const { container: wrapper, rerender } = render(
<Progress percent={0} percentPosition={['center', 'inner']} size={[200, 20]} />,
);
expect(
wrapper.querySelectorAll('.ant-progress-line-align-center.ant-progress-line-position-inner'),
).toHaveLength(1);

rerender(<Progress percent={100} percentPosition={['center', 'inner']} size={[400, 20]} />);
expect(wrapper.querySelectorAll('.ant-progress-text-inner')).toHaveLength(1);

rerender(<Progress percent={100} percentPosition={['center', 'outer']} />);
expect(wrapper.querySelectorAll('.ant-progress-layout-bottom')).toHaveLength(1);
});

it('render inner info position', () => {
const { container } = render(
<Progress percent={100} percentPosition={['center', 'inner']} size={[400, 20]} />,
);
expect(container.firstChild).toMatchSnapshot();
});
});
7 changes: 7 additions & 0 deletions components/progress/demo/info-position.md
@@ -0,0 +1,7 @@
## zh-CN

改变进度数值位置,可使用 `percentPosition` 调整,使进度条数值在进度条内部、外部或底部。

## en-US

Change the position of the progress value, you can use `percentPosition` to adjust it so that the progress bar value is inside, outside or at the bottom of the progress bar.
18 changes: 18 additions & 0 deletions components/progress/demo/info-position.tsx
@@ -0,0 +1,18 @@
import React from 'react';
import { Flex, Progress } from 'antd';

const App: React.FC = () => (
<Flex gap="small" vertical>
<Progress percent={0} percentPosition={['center', 'inner']} size={[200, 20]} />
<Progress percent={10} percentPosition={['center', 'inner']} size={[300, 20]} />
<Progress percent={50} percentPosition={['start', 'inner']} size={[300, 20]} />
<Progress percent={60} percentPosition={['end', 'inner']} size={[300, 20]} />
<Progress percent={100} percentPosition={['center', 'inner']} size={[400, 20]} />
<Progress percent={60} percentPosition={['start', 'outer']} />
<Progress percent={100} percentPosition={['start', 'outer']} />
<Progress percent={60} percentPosition={['center', 'outer']} size="small" />
<Progress percent={100} percentPosition={['center', 'outer']} />
</Flex>
);

export default App;
2 changes: 2 additions & 0 deletions components/progress/index.en-US.md
Expand Up @@ -33,6 +33,7 @@ If it will take a long time to complete an operation, you can use `Progress` to
<code src="./demo/steps.tsx">Progress bar with steps</code>
<code src="./demo/circle-steps.tsx" version="5.16.0">Circular progress bar whit steps</code>
<code src="./demo/size.tsx">Progress size</code>
<code src="./demo/info-position.tsx" version="5.17.0">Change progress value position</code>
LonelySnowman marked this conversation as resolved.
Show resolved Hide resolved

## API

Expand All @@ -59,6 +60,7 @@ Properties that shared by all types.
| --- | --- | --- | --- | --- |
| steps | The total step count | number | - | - |
| strokeColor | The color of progress bar, render `linear-gradient` when passing an object, could accept `string[]` when has `steps`. | string \| string[] \| { from: string; to: string; direction: string } | - | 4.21.0: `string[]` |
| percentPosition | Position of the progress value is passed into the array. The first element represents the horizontal position of the value, and the second element represents whether the value is inside or outside the progress bar | \[\"start\" \| \"center\" \| \"end\", \"inner\" \| \"outer\"\] | \[\"end\", \"outer\"\] | 5.17.0 |

### `type="circle"`

Expand Down
2 changes: 2 additions & 0 deletions components/progress/index.zh-CN.md
Expand Up @@ -34,6 +34,7 @@ demo:
<code src="./demo/steps.tsx">步骤进度条</code>
<code src="./demo/circle-steps.tsx" version="5.16.0">步骤进度圈</code>
<code src="./demo/size.tsx">尺寸</code>
<code src="./demo/info-position.tsx" version="5.17.0">改变进度数值位置</code>
LonelySnowman marked this conversation as resolved.
Show resolved Hide resolved

## API

Expand All @@ -60,6 +61,7 @@ demo:
| --- | --- | --- | --- | --- |
| steps | 进度条总共步数 | number | - | - |
| strokeColor | 进度条的色彩,传入 object 时为渐变。当有 `steps` 时支持传入一个数组。 | string \| string[] \| { from: string; to: string; direction: string } | - | 4.21.0: `string[]` |
| percentPosition | 进度数值位置,传入数组,第一个元素代表数值的水平位置,第二个元素代表数值在进度条内部还是外部 | \[\"start\" \| \"center\" \| \"end\", \"inner\" \| \"outer\"\] | \[\"end\", \"outer\"\] | 5.17.0 |

### `type="circle"`

Expand Down
25 changes: 22 additions & 3 deletions components/progress/progress.tsx
Expand Up @@ -18,9 +18,10 @@ import { getSize, getSuccessPercent, validProgress } from './utils';
export const ProgressTypes = ['line', 'circle', 'dashboard'] as const;
export type ProgressType = (typeof ProgressTypes)[number];
const ProgressStatuses = ['normal', 'exception', 'active', 'success'] as const;
export type PercentPositionType = 'inner' | 'outer';
export type PercentAlignType = 'start' | 'center' | 'end';
export type ProgressSize = 'default' | 'small';
export type StringGradients = Record<string, string>;

type FromToGradients = { from: string; to: string };
export type ProgressGradient = { direction?: string } & (StringGradients | FromToGradients);

Expand Down Expand Up @@ -56,6 +57,7 @@ export interface ProgressProps extends ProgressAriaProps {
steps?: number | { count: number; gap: number };
/** @deprecated Use `success` instead */
successPercent?: number;
percentPosition?: [PercentAlignType, PercentPositionType];
LonelySnowman marked this conversation as resolved.
Show resolved Hide resolved
children?: React.ReactNode;
}

Expand All @@ -73,9 +75,12 @@ const Progress = React.forwardRef<HTMLDivElement, ProgressProps>((props, ref) =>
status,
format,
style,
percentPosition = [],
...restProps
} = props;

const [infoAlign = 'end', infoPosition = 'outer'] = percentPosition;

const percentNumber = React.useMemo<number>(() => {
const successPercent = getSuccessPercent(props);
return parseInt(
Expand Down Expand Up @@ -107,7 +112,11 @@ const Progress = React.forwardRef<HTMLDivElement, ProgressProps>((props, ref) =>
let text: React.ReactNode;
const textFormatter = format || ((number) => `${number}%`);
const isLineType = type === 'line';
if (format || (progressStatus !== 'exception' && progressStatus !== 'success')) {
if (
infoPosition === 'inner' ||
format ||
(progressStatus !== 'exception' && progressStatus !== 'success')
) {
text = textFormatter(validProgress(percent), validProgress(successPercent));
} else if (progressStatus === 'exception') {
text = isLineType ? <CloseCircleFilled /> : <CloseOutlined />;
Expand All @@ -116,7 +125,14 @@ const Progress = React.forwardRef<HTMLDivElement, ProgressProps>((props, ref) =>
}

return (
<span className={`${prefixCls}-text`} title={typeof text === 'string' ? text : undefined}>
<span
className={classNames(
LonelySnowman marked this conversation as resolved.
Show resolved Hide resolved
`${prefixCls}-text`,
`${prefixCls}-text-${infoPosition}`,
`${prefixCls}-text-${infoAlign}`,
)}
title={typeof text === 'string' ? text : undefined}
>
{text}
</span>
);
Expand Down Expand Up @@ -162,6 +178,7 @@ const Progress = React.forwardRef<HTMLDivElement, ProgressProps>((props, ref) =>
strokeColor={strokeColorNotArray}
prefixCls={prefixCls}
direction={direction}
percentPosition={[infoAlign, infoPosition]}
>
{progressInfo}
</Line>
Expand All @@ -182,6 +199,8 @@ const Progress = React.forwardRef<HTMLDivElement, ProgressProps>((props, ref) =>
const classString = classNames(
prefixCls,
`${prefixCls}-status-${progressStatus}`,
`${prefixCls}-line-align-${infoAlign}`,
`${prefixCls}-line-position-${infoPosition}`,
{
[`${prefixCls}-${(type === 'dashboard' && 'circle') || type}`]: type !== 'line',
[`${prefixCls}-inline-circle`]: type === 'circle' && getSize(size, 'circle')[0] <= 20,
Expand Down
66 changes: 62 additions & 4 deletions components/progress/style/index.ts
Expand Up @@ -36,6 +36,16 @@ export interface ComponentToken {
* @descEN Icon size of circular progress bar
*/
circleIconFontSize: string;
/**
* @desc 线形进度条内部信息内边距
* @descEN Linear progress bar internal information padding
*/
innerInfoPadding: number;
/**
* @desc 线形进度条底部信息顶部外边距
* @descEN Linear progress bar bottom information top margin
*/
bottomInfoMarginTop: number;
}

export const LineStrokeColorVar = '--progress-line-stroke-color';
Expand Down Expand Up @@ -90,9 +100,17 @@ const genBaseStyle: GenerateStyle<ProgressToken> = (token) => {
},

[`&${progressCls}-show-info`]: {
[`${progressCls}-outer`]: {
marginInlineEnd: `calc(-2em - ${unit(token.marginXS)})`,
paddingInlineEnd: `calc(2em + ${unit(token.paddingXS)})`,
[`&${progressCls}-line-align-end, &${progressCls}-line-align-center`]: {
[`${progressCls}-outer`]: {
marginInlineEnd: `calc(-2em - ${unit(token.marginXS)})`,
paddingInlineEnd: `calc(2em + ${unit(token.paddingXS)})`,
},
},
[`&${progressCls}-line-align-start`]: {
[`${progressCls}-outer`]: {
marginInlineStart: `calc(-2em - ${unit(token.marginXS)})`,
paddingInlineStart: `calc(2em + ${unit(token.paddingXS)})`,
},
},
},

Expand All @@ -119,11 +137,23 @@ const genBaseStyle: GenerateStyle<ProgressToken> = (token) => {
transition: `all ${token.motionDurationSlow} ${token.motionEaseInOutCirc}`,
},

[`${progressCls}-layout-bottom`]: {
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
justifyContent: 'center',
[`${progressCls}-text`]: {
marginInlineStart: 0,
marginTop: token.bottomInfoMarginTop,
},
},

[`${progressCls}-bg`]: {
overflow: 'hidden',

'&::after': {
content: '""',
position: 'absolute',
top: 0,
background: {
_multi_value_: true,
value: ['inherit', `var(${LineStrokeColorVar})`],
Expand All @@ -132,6 +162,9 @@ const genBaseStyle: GenerateStyle<ProgressToken> = (token) => {
width: `calc(1 / var(${Percent}) * 100%)`,
display: 'block',
},
[`&${progressCls}-bg-inner`]: {
minWidth: 'max-content',
},
},

[`${progressCls}-success-bg`]: {
Expand All @@ -154,6 +187,26 @@ const genBaseStyle: GenerateStyle<ProgressToken> = (token) => {
[iconPrefixCls]: {
fontSize: token.fontSize,
},
[`&${progressCls}-text-outer${progressCls}-text-start`]: {
marginInlineStart: 0,
marginInlineEnd: token.marginXS,
},
},

[`${progressCls}-text-inner`]: {
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
width: '100%',
height: '100%',
marginInlineStart: 0,
padding: `0 ${token.innerInfoPadding}`,
[`&${progressCls}-inner-start`]: {
justifyContent: 'left',
},
[`&${progressCls}-inner-end`]: {
justifyContent: 'right',
},
LonelySnowman marked this conversation as resolved.
Show resolved Hide resolved
},

[`&${progressCls}-status-active`]: {
Expand Down Expand Up @@ -199,6 +252,9 @@ const genBaseStyle: GenerateStyle<ProgressToken> = (token) => {
},
[`${progressCls}-text`]: {
color: token.colorSuccess,
[`&${progressCls}-text-inner`]: {
color: token.colorText,
},
},
},

Expand Down Expand Up @@ -314,6 +370,8 @@ export const prepareComponentToken: GetDefaultToken<'Progress'> = (token) => ({
lineBorderRadius: 100, // magic for capsule shape, should be a very large number
circleTextFontSize: '1em',
circleIconFontSize: `${token.fontSize / token.fontSizeSM}em`,
innerInfoPadding: 4,
bottomInfoMarginTop: 4,
LonelySnowman marked this conversation as resolved.
Show resolved Hide resolved
});

export default genStyleHooks(
Expand Down