import { createContext, useContext } from 'react'; import ConditionalWrapper from "../../common/ConditionalWrapper"; import WrapperTag from "../../common/WrapperTag"; import AspectBox from '../AspectBox'; import Metadata from '../PageMetadata'; import Tag from '../Tag'; import { CardProps, CardHeaderProps, CardMediaProps, CardStatusTagProps } from "./types"; import { HeadingLevel, LinkComponent } from '../../shared-types'; import clsx from 'clsx'; type CardSettingsContextProps = { headingLevel: HeadingLevel href?: string linkComponent?: LinkComponent }; const CardSettingsContext = createContext({ headingLevel: 'h2', href: undefined, linkComponent: undefined }); const CardContent = ({ children }: React.AllHTMLAttributes) => { return (
{children}
) }; const CardContentHeader = ({ children, description, title }: CardHeaderProps) => { const headingLevel = useContext(CardSettingsContext).headingLevel; const href = useContext(CardSettingsContext).href; const linkComponent = useContext(CardSettingsContext).linkComponent; const LINK_CLASS = 'ds_card__link ds_card__link--cover'; function getLinkElement(children: React.ReactNode) { let linkElement; if (linkComponent) { linkElement = linkComponent({ className: LINK_CLASS, href, children }); } else { linkElement = {children}; } return linkElement as React.JSX.Element; } return (
{children} getLinkElement(children)} > {title} {description && , {description}}
) }; const CardStatusTag = ({ children, colour, isHidden, ...props }: CardStatusTagProps) => { return ( {children} ) }; const CardContentMain = ({ children }: React.AllHTMLAttributes) => { return (
{children}
) }; const CardContentFooter = ({ children }: React.AllHTMLAttributes) => { return (
{children}
) }; const CardMetadata = ({ children }: React.AllHTMLAttributes) => { return ( {children} ) }; const CardMedia = ({ children, isCover, mediaWidth, ratio, ...props }: CardMediaProps) => { return (
{children} } > {children}
) }; const Card = ({ children, className, hasSecondaryBackground = false, headingLevel, href, isAdaptive = false, isHorizontal = false, isHorizontalSmall = false, linkComponent, tagName = 'div', ...props }: CardProps) => { return (
{children}
} > {children}
); }; Card.Content = CardContent; Card.ContentFooter = CardContentFooter; Card.ContentHeader = CardContentHeader; Card.ContentMain = CardContentMain; Card.Metadata = CardMetadata; Card.Media = CardMedia; Card.StatusTag = CardStatusTag; Card.displayName = 'Card'; CardContent.displayName = 'Card.Content'; CardContentFooter.displayName = 'Card.ContentFooter'; CardContentHeader.displayName = 'Card.ContentHeader'; CardContentMain.displayName = 'Card.ContentMain'; CardMetadata.displayName = 'Card.Metadata'; CardMedia.displayName = 'Card.Media'; CardStatusTag.displayName = 'Card.StatusTag'; export default Card;