|
| 1 | +import cn from 'bem-cn-lite'; |
| 2 | + |
| 3 | +import type {ValueOf} from '../../types/common'; |
| 4 | + |
| 5 | +import './ProgressViewer.scss'; |
| 6 | + |
| 7 | +const b = cn('progress-viewer'); |
| 8 | + |
| 9 | +export const PROGRESS_VIEWER_SIZE_IDS = { |
| 10 | + xs: 'xs', |
| 11 | + s: 's', |
| 12 | + ns: 'ns', |
| 13 | + m: 'm', |
| 14 | + n: 'n', |
| 15 | + l: 'l', |
| 16 | + head: 'head', |
| 17 | +} as const; |
| 18 | + |
| 19 | +type ProgressViewerSize = ValueOf<typeof PROGRESS_VIEWER_SIZE_IDS>; |
| 20 | + |
| 21 | +/* |
| 22 | +
|
| 23 | +Props description: |
| 24 | +1) value - the amount of progress |
| 25 | +2) capacity - maximum possible progress value |
| 26 | +3) formatValues - function for formatting the value and capacity |
| 27 | +4) percents - display progress in percents |
| 28 | +5) colorizeProgress - change the color of the progress bar depending on its value |
| 29 | +6) inverseColorize - invert the colors of the progress bar |
| 30 | +*/ |
| 31 | + |
| 32 | +interface ProgressViewerProps { |
| 33 | + value?: number | string; |
| 34 | + capacity?: number | string; |
| 35 | + formatValues?: (value?: number, capacity?: number) => (string | undefined)[]; |
| 36 | + percents?: boolean; |
| 37 | + className?: string; |
| 38 | + size?: ProgressViewerSize; |
| 39 | + colorizeProgress?: boolean; |
| 40 | + inverseColorize?: boolean; |
| 41 | +} |
| 42 | + |
| 43 | +export function ProgressViewer({ |
| 44 | + value, |
| 45 | + capacity = 100, |
| 46 | + formatValues, |
| 47 | + percents, |
| 48 | + className, |
| 49 | + size = PROGRESS_VIEWER_SIZE_IDS.xs, |
| 50 | + colorizeProgress, |
| 51 | + inverseColorize, |
| 52 | +}: ProgressViewerProps) { |
| 53 | + let fillWidth = Math.round((parseFloat(String(value)) / parseFloat(String(capacity))) * 100); |
| 54 | + fillWidth = fillWidth > 100 ? 100 : fillWidth; |
| 55 | + |
| 56 | + let valueText: number | string | undefined = Math.round(Number(value)), |
| 57 | + capacityText: number | string | undefined = capacity, |
| 58 | + divider = '/'; |
| 59 | + if (formatValues) { |
| 60 | + [valueText, capacityText] = formatValues(Number(value), Number(capacity)); |
| 61 | + } else if (percents) { |
| 62 | + valueText = fillWidth + '%'; |
| 63 | + capacityText = ''; |
| 64 | + divider = ''; |
| 65 | + } |
| 66 | + |
| 67 | + let bg = inverseColorize ? 'scarlet' : 'apple'; |
| 68 | + if (colorizeProgress) { |
| 69 | + if (fillWidth > 60 && fillWidth <= 80) { |
| 70 | + bg = 'saffron'; |
| 71 | + } else if (fillWidth > 80) { |
| 72 | + bg = inverseColorize ? 'apple' : 'scarlet'; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + const lineStyle = { |
| 77 | + width: fillWidth + '%', |
| 78 | + }; |
| 79 | + |
| 80 | + const text = fillWidth > 60 ? 'contrast0' : 'contrast70'; |
| 81 | + |
| 82 | + if (!isNaN(fillWidth)) { |
| 83 | + return ( |
| 84 | + <div className={b({size}, className)}> |
| 85 | + <div className={b('line', {bg})} style={lineStyle}></div> |
| 86 | + <span |
| 87 | + className={b('text', {text})} |
| 88 | + >{`${valueText} ${divider} ${capacityText}`}</span> |
| 89 | + </div> |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + return <div className={`${b({size})} ${className} error`}>no data</div>; |
| 94 | +} |
0 commit comments