/* @flow */ import React from 'react' import { get } from 'lodash' import classnames from 'classnames' import { buildCss } from '../utilities/props' import { globalProps } from '../utilities/globalProps.js' type CardPropTypes = { borderNone?: boolean, borderRadius?: "xs" | "sm" | "md" | "lg" | "xl" | "none" | "rounded", children: array | React.ReactNode, className?: string, highlight?: { position?: "side" | "top", color?: string, }, padding?: string, selected?: boolean, shadow?: "none" | "deep" | "deeper" | "deepest", } type CardHeaderProps = { headerColor?: string, children: array | React.ReactNode, className?: string, padding?: string, } type CardBodyProps = { children: array | React.ReactNode | string, className?: string, padding?: string, } // Header component const Header = (props: CardHeaderProps) => { const { children, className, headerColor = 'category_1', padding = 'sm' } = props const headerCSS = buildCss('pb_card_header_kit', `${headerColor}`) const headerSpacing = globalProps(props, { padding }) return (
{children}
) } // Body component const Body = (props: CardBodyProps) => { const { children, padding = 'md', className } = props const bodyCSS = buildCss('pb_card_body_kit') const bodySpacing = globalProps(props, { padding }) return (
{children}
) } const Card = (props: CardPropTypes) => { const { borderNone = false, borderRadius = 'md', children, className, highlight = {}, selected = false, shadow = 'none', padding = 'md', } = props const borderCSS = borderNone == true ? 'border_none' : '' const cardCss = buildCss('pb_card_kit', `shadow_${shadow}`, `${borderCSS}`, `border_radius_${borderRadius}`, { selected, deselected: !selected, [`highlight_${highlight.position}`]: highlight.position, [`highlight_${highlight.color}`]: highlight.color, }) // coerce to array const cardChildren = typeof children === 'object' && children.length ? children : [children] const subComponentTags = (tagName) => { return cardChildren.filter((c) => ( get(c, 'type.displayName') === tagName )).map((child, i) => { return React.cloneElement(child, { key: `${tagName.toLowerCase()}-${i}` }) }) } const nonHeaderChildren = cardChildren.filter((child) => (get(child, 'type.displayName') !== 'Header')) return (
{subComponentTags('Header')} {nonHeaderChildren}
) } Card.Header = Header Card.Body = Body export default Card