Skip to content

Commit ac372a4

Browse files
authored
fix: add default data formatter to ProgressViewer (#552)
1 parent 8469307 commit ac372a4

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

src/components/ProgressViewer/ProgressViewer.tsx

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import cn from 'bem-cn-lite';
22

33
import type {ValueOf} from '../../types/common';
4+
import {isNumeric} from '../../utils/utils';
5+
import {formatNumber, roundToPrecision} from '../../utils/dataFormatters/dataFormatters';
46

57
import './ProgressViewer.scss';
68

@@ -18,6 +20,14 @@ export const PROGRESS_VIEWER_SIZE_IDS = {
1820

1921
type ProgressViewerSize = ValueOf<typeof PROGRESS_VIEWER_SIZE_IDS>;
2022

23+
const formatValue = (value?: number) => {
24+
return formatNumber(roundToPrecision(Number(value), 2));
25+
};
26+
27+
const defaultFormatValues = (value?: number, total?: number) => {
28+
return [formatValue(value), formatValue(total)];
29+
};
30+
2131
/*
2232
2333
Props description:
@@ -47,7 +57,7 @@ interface ProgressViewerProps {
4757
export function ProgressViewer({
4858
value,
4959
capacity,
50-
formatValues,
60+
formatValues = defaultFormatValues,
5161
percents,
5262
className,
5363
size = PROGRESS_VIEWER_SIZE_IDS.xs,
@@ -58,15 +68,15 @@ export function ProgressViewer({
5868
}: ProgressViewerProps) {
5969
let fillWidth = Math.round((parseFloat(String(value)) / parseFloat(String(capacity))) * 100);
6070
fillWidth = fillWidth > 100 ? 100 : fillWidth;
61-
let valueText: number | string | undefined = Math.round(Number(value)),
71+
let valueText: number | string | undefined = value,
6272
capacityText: number | string | undefined = capacity,
6373
divider = '/';
64-
if (formatValues) {
65-
[valueText, capacityText] = formatValues(Number(value), Number(capacity));
66-
} else if (percents) {
74+
if (percents) {
6775
valueText = fillWidth + '%';
6876
capacityText = '';
6977
divider = '';
78+
} else if (formatValues) {
79+
[valueText, capacityText] = formatValues(Number(value), Number(capacity));
7080
}
7181

7282
let bg = inverseColorize ? 'scarlet' : 'apple';
@@ -85,14 +95,14 @@ export function ProgressViewer({
8595
const text = fillWidth > 60 ? 'contrast0' : 'contrast70';
8696

8797
const renderContent = () => {
88-
if (capacityText) {
98+
if (isNumeric(capacity)) {
8999
return `${valueText} ${divider} ${capacityText}`;
90100
}
91101

92102
return valueText;
93103
};
94104

95-
if (!isNaN(Number(value))) {
105+
if (isNumeric(value)) {
96106
return (
97107
<div className={b({size}, className)}>
98108
<div className={b('line', {bg})} style={lineStyle}></div>

0 commit comments

Comments
 (0)