Skip to content

Instantly share code, notes, and snippets.

@brandonsueur
Created July 28, 2020 20:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brandonsueur/dc386704f1fe3fb8afce0e13e3d4aae2 to your computer and use it in GitHub Desktop.
Save brandonsueur/dc386704f1fe3fb8afce0e13e3d4aae2 to your computer and use it in GitHub Desktop.
import React from "react";
import PropTypes from "prop-types";
import clsx from "clsx";
import { Card as NativeCard, CardContent } from "@material-ui/core";
import { CardHeader, CardFooter } from "./components";
import Loader from "@components/loader";
import Button from "@components/button";
import Placeholder from "@components/placeholder";
import NoDataImg from "@assets/images/no-data.png";
import isEmpty from "@helpers/isEmpty";
import get from "@helpers/get";
import styles from "./styles.js";
/**
* Card
* @param {object} header - Header of the Card
* @param {ReactNode} children - Container of the Card
* @param {string} className - Overwrite classes
* @param {boolean} loading - Card loading
* @param {boolean} empty - Card empty
* @param {object} footer - Footer of the Card
*/
const Card = (props) => {
const {
header,
children,
className,
loading,
empty,
footer,
...rest
} = props;
const classes = styles();
const cardClassname = clsx(classes.root, className, {
[classes.loadingState]: loading,
});
return (
<NativeCard {...rest} className={cardClassname}>
{loading && <Loader cover visible size="small" />}
{!loading && (
<>
{get(header, "title", null) && <CardHeader {...header} />}
<CardContent
className={clsx(
classes.rootContent,
classes.cardContent,
className
)}
>
{empty ? <Placeholder img={NoDataImg} /> : children}
</CardContent>
{!isEmpty(footer) && <CardFooter {...footer} />}
</>
)}
</NativeCard>
);
};
Card.propTypes = {
/* Header of the Card */
header: PropTypes.shape({
/* Title content of the card */
title: PropTypes.string.isRequired,
/* Description of the card */
subtitle: PropTypes.oneOfType([PropTypes.node, PropTypes.string]),
/* Avatar to the left of the title */
avatar: PropTypes.oneOfType([
PropTypes.node,
PropTypes.shape({
src: PropTypes.string.isRequired,
size: PropTypes.string,
square: PropTypes.bool,
}),
]),
className: PropTypes.string,
}),
/* Container of the Card */
children: PropTypes.node,
/* Overwrite classes */
className: PropTypes.string,
/* Card loading */
loading: PropTypes.bool,
/* Card empty */
empty: PropTypes.bool,
/* Footer of the Card */
footer: PropTypes.shape({
primaryAction: PropTypes.shape(Button.propTypes),
/* Secondary with multiples actions */
secondaryActions: PropTypes.arrayOf(PropTypes.shape(Button.propTypes)),
/* Overwrite classes */
className: PropTypes.string,
}),
};
Card.defaultProps = {
header: null,
className: "",
loading: false,
empty: false,
footer: null,
};
export default Card;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment