Created
August 20, 2020 06:47
-
-
Save maruware/8040e0c9dafe9a8889716b471431acc4 to your computer and use it in GitHub Desktop.
LabeledValue.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react' | |
import Typography from '@material-ui/core/Typography' | |
import { makeStyles, createStyles } from '@material-ui/core/styles' | |
import { grey } from '@material-ui/core/colors' | |
const useStyles = makeStyles((theme) => | |
createStyles({ | |
label: { | |
fontSize: 14, | |
lineHeight: 1.1, | |
color: grey[700], | |
}, | |
value: { | |
fontSize: 18, | |
lineHeight: 1.5, | |
color: theme.palette.common.black, | |
}, | |
}) | |
) | |
export const BooleanFormatter = (value: any) => { | |
if (value) { | |
return 'Yes' | |
} else { | |
return 'No' | |
} | |
} | |
export const DateStringFormatter = (value: any) => { | |
if (value) { | |
return new Date(value).toLocaleString() | |
} else { | |
return null | |
} | |
} | |
export type LabeledValueFormatter = (org: any) => string | null | |
export interface LabeledValueProps { | |
className?: string | |
label: string | |
value: string | number | boolean | null | |
formatter?: LabeledValueFormatter | |
} | |
export const LabeledValue: React.FC<LabeledValueProps> = ({ | |
className, | |
label, | |
value, | |
formatter, | |
}) => { | |
const classes = useStyles({}) | |
let v = value | |
if (formatter) { | |
v = formatter(v) | |
} | |
return ( | |
<div className={className}> | |
<Typography className={classes.label}>{label}</Typography> | |
<Typography className={classes.value}>{v || '\u00a0'}</Typography> | |
</div> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment