index.tsx
55 lines
| 1 | import {Spinner} from '@givewp/components'; |
| 2 | import {HeaderText} from '../Header'; |
| 3 | import PercentChangePill |
| 4 | from '@givewp/campaigns/admin/components/CampaignDetailsPage/Components/CampaignStats/PercentChangePill'; |
| 5 | import styles from './styles.module.scss'; |
| 6 | |
| 7 | /** |
| 8 | * @since 4.4.0 |
| 9 | */ |
| 10 | export type StatWidgetProps = { |
| 11 | label: string; |
| 12 | value: number; |
| 13 | description?: string; |
| 14 | formatter: Intl.NumberFormat; |
| 15 | loading?: boolean; |
| 16 | previousValue?: number; |
| 17 | }; |
| 18 | |
| 19 | /** |
| 20 | * @since 4.4.0 |
| 21 | */ |
| 22 | export default function StatWidget({ |
| 23 | label, |
| 24 | value, |
| 25 | description, |
| 26 | formatter = null, |
| 27 | loading = false, |
| 28 | previousValue = null, |
| 29 | }: StatWidgetProps) { |
| 30 | return ( |
| 31 | <div className={styles.statWidget}> |
| 32 | <header> |
| 33 | <HeaderText>{label}</HeaderText> |
| 34 | </header> |
| 35 | <div className={styles.statWidgetAmount}> |
| 36 | <div className={styles.statWidgetDisplay}> |
| 37 | {!loading ? ( |
| 38 | formatter?.format(value) ?? value |
| 39 | ) : ( |
| 40 | <span> |
| 41 | <Spinner size="small" /> |
| 42 | </span> |
| 43 | )} |
| 44 | </div> |
| 45 | {previousValue !== null && <PercentChangePill value={value} comparison={previousValue} />} |
| 46 | </div> |
| 47 | {description && ( |
| 48 | <footer> |
| 49 | <div>{description}</div> |
| 50 | </footer> |
| 51 | )} |
| 52 | </div> |
| 53 | ); |
| 54 | } |
| 55 |