import React, { useMemo } from 'react' import useTheme from '../use-theme' import { CardTypes } from '../utils/prop-types' import { getStyles } from './styles' import CardFooter from './card-footer' import CardContent from './card-content' import Image from '../image' import { hasChild, pickChild } from '../utils/collections' import useScale, { withScale } from '../use-scale' import useClasses from '../use-classes' interface Props { hoverable?: boolean shadow?: boolean className?: string type?: CardTypes } const defaultProps = { type: 'default' as CardTypes, hoverable: false, shadow: false, className: '' } type NativeAttrs = Omit, keyof Props> export type CardProps = Props & NativeAttrs const CardComponent: React.FC> = ({ children, hoverable, className, shadow, type, ...props }) => { const theme = useTheme() const { SCALES } = useScale() const hoverShadow = useMemo(() => { if (hoverable) { if (shadow) return theme.expressiveness.shadowMedium return theme.expressiveness.shadowSmall } else if (shadow) { return theme.expressiveness.shadowSmall } return 'none' }, [hoverable, shadow, theme.expressiveness]) const { color, bgColor, borderColor } = useMemo( () => getStyles(type || 'default', theme.palette, shadow), [type, theme.palette, shadow] ) const [withoutFooterChildren, footerChildren] = pickChild(children, CardFooter) const [withoutImageChildren, imageChildren] = pickChild(withoutFooterChildren, Image) const hasContent = hasChild(withoutImageChildren, CardContent) return (
{imageChildren} {hasContent ? withoutImageChildren : {withoutImageChildren}} {footerChildren}
) } CardComponent.defaultProps = defaultProps CardComponent.displayName = 'HuiCard' const Card = withScale(CardComponent) export default Card