saleor-dashboard/src/components/TableCellAvatar/TableCellAvatar.tsx

93 lines
2.3 KiB
TypeScript
Raw Normal View History

2019-06-19 14:40:52 +00:00
import Avatar from "@material-ui/core/Avatar";
2020-09-25 14:49:21 +00:00
import TableCell, { TableCellProps } from "@material-ui/core/TableCell";
2019-06-19 14:40:52 +00:00
import Cached from "@material-ui/icons/Cached";
import { makeStyles } from "@saleor/theme";
2019-08-09 11:14:35 +00:00
import classNames from "classnames";
2019-08-09 10:26:22 +00:00
import React from "react";
2019-06-19 14:40:52 +00:00
import Image from "../../icons/Image";
2019-11-04 13:36:25 +00:00
export const AVATAR_MARGIN = 32;
2019-08-09 11:14:35 +00:00
2019-12-03 15:28:40 +00:00
const useStyles = makeStyles(
theme => ({
2020-09-25 14:49:21 +00:00
alignRight: {
justifyContent: "flex-end"
},
2019-12-03 15:28:40 +00:00
avatar: {
background: "none",
border: `1px solid ${theme.palette.divider}`,
borderRadius: 2,
color: "#bdbdbd",
display: "inline-flex",
padding: theme.spacing(0.5)
2019-11-04 13:36:25 +00:00
},
2019-12-03 15:28:40 +00:00
children: {
alignSelf: "center",
marginLeft: theme.spacing(2),
width: "100%"
},
content: {
alignItems: "center",
display: "flex"
},
root: {
"&:not(first-child)": {
paddingLeft: 0
},
paddingRight: theme.spacing(3),
width: "1%"
}
}),
{ name: "TableCellAvatar" }
);
2019-06-19 14:40:52 +00:00
2020-09-25 14:49:21 +00:00
interface TableCellAvatarProps extends TableCellProps {
2019-06-19 14:40:52 +00:00
className?: string;
thumbnail?: string;
2020-09-25 14:49:21 +00:00
alignRight?: boolean;
2019-06-19 14:40:52 +00:00
avatarProps?: string;
2019-08-09 11:14:35 +00:00
children?: React.ReactNode | React.ReactNodeArray;
2019-06-19 14:40:52 +00:00
}
2019-10-30 14:34:24 +00:00
const TableCellAvatar: React.FC<TableCellAvatarProps> = props => {
2020-09-25 14:49:21 +00:00
const {
children,
className,
alignRight,
thumbnail,
avatarProps,
...rest
} = props;
2019-10-30 14:34:24 +00:00
const classes = useStyles(props);
return (
<TableCell className={classNames(classes.root, className)} {...rest}>
2020-09-25 14:49:21 +00:00
<div
className={classNames(classes.content, {
[classes.alignRight]: alignRight
})}
>
2019-08-09 11:14:35 +00:00
{thumbnail === undefined ? (
<Avatar className={classNames(classes.avatar, avatarProps)}>
<Cached color="primary" />
</Avatar>
) : thumbnail === null ? (
<Avatar className={classNames(classes.avatar, avatarProps)}>
<Image color="primary" />
</Avatar>
) : (
<Avatar
className={classNames(classes.avatar, avatarProps)}
src={thumbnail}
/>
)}
2020-09-25 14:49:21 +00:00
{!alignRight && <div className={classes.children}>{children}</div>}
2019-08-09 11:14:35 +00:00
</div>
2019-06-19 14:40:52 +00:00
</TableCell>
2019-10-30 14:34:24 +00:00
);
};
2019-06-19 14:40:52 +00:00
TableCellAvatar.displayName = "TableCellAvatar";
export default TableCellAvatar;