|
| 1 | +/* @author Rohil Visariya |
| 2 | + @description ProgressBar Indicator |
| 3 | + @created 10 Aug 2020 |
| 4 | + */ |
| 5 | +import React from 'react'; |
| 6 | + |
| 7 | +interface ifPercentage { |
| 8 | + percentage: string | number |
| 9 | + totalValue: never |
| 10 | + completedValue: never |
| 11 | +} |
| 12 | + |
| 13 | +interface ifOutOf { |
| 14 | + percentage: never |
| 15 | + totalValue: number |
| 16 | + completedValue: number |
| 17 | +} |
| 18 | + |
| 19 | +type Props = { |
| 20 | + completedValue: number, |
| 21 | + totalValue: number, |
| 22 | + percentage: string | number, |
| 23 | + height?: number, |
| 24 | + color?: string, |
| 25 | + labelColor?: string, |
| 26 | + prefixLabelText?: string | any, |
| 27 | + suffixLabelText?: string | any |
| 28 | +} |
| 29 | + |
| 30 | +export default function Progressbar( |
| 31 | + { |
| 32 | + height = 20, |
| 33 | + color, |
| 34 | + labelColor = '#ffffff', |
| 35 | + percentage, |
| 36 | + completedValue, |
| 37 | + totalValue, |
| 38 | + prefixLabelText, |
| 39 | + suffixLabelText |
| 40 | + }: Props |
| 41 | +) { |
| 42 | + |
| 43 | + let percent = percentage ? percentage : ((completedValue * 100) / totalValue); |
| 44 | + |
| 45 | + const containerStyles = { |
| 46 | + height: height, |
| 47 | + width: '100%', |
| 48 | + backgroundColor: "#dddddd", |
| 49 | + textAlign: 'left' as const |
| 50 | + } |
| 51 | + |
| 52 | + const fillerStyles = { |
| 53 | + height: '100%', |
| 54 | + width: `${percent}%`, |
| 55 | + backgroundColor: color, |
| 56 | + textAlign: 'center' as const, |
| 57 | + transition: 'width 1s ease-in-out', |
| 58 | + } |
| 59 | + |
| 60 | + const labelStyles = { |
| 61 | + padding: 5, |
| 62 | + display: 'inline-flex', |
| 63 | + alignItems: 'middle', |
| 64 | + height: height, |
| 65 | + color: `${labelColor}`, |
| 66 | + fontWeight: 700, |
| 67 | + fontSize: '100%', |
| 68 | + lineHeight: '1.2em' |
| 69 | + } |
| 70 | + return ( |
| 71 | + <div className="ProgressBar" style={containerStyles}> |
| 72 | + <div style={fillerStyles}> |
| 73 | + <span style={labelStyles}> |
| 74 | + {prefixLabelText ? prefixLabelText : null} |
| 75 | + {percentage ? ` ${percent}% ` : ` ${completedValue} / ${totalValue} `} |
| 76 | + {suffixLabelText ? suffixLabelText : null} |
| 77 | + </span> |
| 78 | + </div> |
| 79 | + </div> |
| 80 | + ); |
| 81 | +} |
0 commit comments