StatisticItem.tsx 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* eslint-disable react/no-unused-prop-types */
  2. /* eslint-disable react/no-unstable-nested-components */
  3. // TODO: This component should be cleaned up and usage should be re-examined. The types should be reconsidered as well.
  4. import { Typography, Statistic, Card, Progress } from 'antd';
  5. import { FC } from 'react';
  6. const { Text } = Typography;
  7. export type StatisticItemProps = {
  8. title?: string;
  9. value?: any;
  10. prefix?: any;
  11. suffix?: string;
  12. color?: string;
  13. progress?: boolean;
  14. centered?: boolean;
  15. formatter?: any;
  16. };
  17. const defaultProps = {
  18. title: '',
  19. value: 0,
  20. prefix: null,
  21. suffix: null,
  22. color: '',
  23. progress: false,
  24. centered: false,
  25. formatter: null,
  26. };
  27. export type ContentProps = {
  28. prefix: string;
  29. value: any;
  30. suffix: string;
  31. title: string;
  32. };
  33. const Content: FC<ContentProps> = ({ prefix, value, suffix, title }) => (
  34. <div>
  35. {prefix}
  36. <div>
  37. <Text type="secondary">{title}</Text>
  38. </div>
  39. <div>
  40. <Text type="secondary">
  41. {value}
  42. {suffix || '%'}
  43. </Text>
  44. </div>
  45. </div>
  46. );
  47. const ProgressView: FC<StatisticItemProps> = ({ title, value, prefix, suffix, color }) => {
  48. const endColor = value > 90 ? 'red' : color;
  49. const content = <Content prefix={prefix} value={value} suffix={suffix} title={title} />;
  50. return (
  51. <Progress
  52. type="dashboard"
  53. percent={value}
  54. width={120}
  55. strokeColor={{
  56. '0%': color,
  57. '90%': endColor,
  58. }}
  59. format={() => content}
  60. />
  61. );
  62. };
  63. ProgressView.defaultProps = defaultProps;
  64. const StatisticView: FC<StatisticItemProps> = ({ title, value, prefix, formatter }) => (
  65. <Statistic title={title} value={value} prefix={prefix} formatter={formatter} />
  66. );
  67. StatisticView.defaultProps = defaultProps;
  68. export const StatisticItem: FC<StatisticItemProps> = props => {
  69. const { progress, centered } = props;
  70. const View = progress ? ProgressView : StatisticView;
  71. const style = centered ? { display: 'flex', alignItems: 'center', justifyContent: 'center' } : {};
  72. return (
  73. <Card type="inner">
  74. <div style={style}>
  75. <View {...props} />
  76. </div>
  77. </Card>
  78. );
  79. };
  80. StatisticItem.defaultProps = defaultProps;