| 1 |
import { useContext } from 'react'; |
| 2 |
import { css } from '@patternfly/react-styles'; |
| 3 |
import styles from '@patternfly/react-styles/css/components/Card/card'; |
| 4 |
import { CardContext } from './Card'; |
| 5 |
import { CardSubtitle } from './CardSubtitle'; |
| 6 |
|
| 7 |
export interface CardTitleProps extends React.HTMLProps<HTMLDivElement> { |
| 8 |
/** Content rendered inside the CardTitle */ |
| 9 |
children?: React.ReactNode; |
| 10 |
/** Additional classes added to the CardTitle */ |
| 11 |
className?: string; |
| 12 |
/** Sets the base component to render. defaults to div */ |
| 13 |
component?: keyof React.JSX.IntrinsicElements; |
| 14 |
/** @beta Subtitle of the card title */ |
| 15 |
subtitle?: React.ReactNode; |
| 16 |
} |
| 17 |
|
| 18 |
export const CardTitle: React.FunctionComponent<CardTitleProps> = ({ |
| 19 |
children, |
| 20 |
className, |
| 21 |
component = 'div', |
| 22 |
subtitle, |
| 23 |
...props |
| 24 |
}: CardTitleProps) => { |
| 25 |
const { cardId } = useContext(CardContext); |
| 26 |
const Component = component as any; |
| 27 |
const titleId = cardId ? `${cardId}-title` : ''; |
| 28 |
const subtitleId = cardId ? `${cardId}-subtitle` : ''; |
| 29 |
|
| 30 |
return ( |
| 31 |
<div className={css(styles.cardTitle)}> |
| 32 |
<Component className={css(styles.cardTitleText, className)} id={titleId || undefined} {...props}> |
| 33 |
{children} |
| 34 |
</Component> |
| 35 |
{subtitle && <CardSubtitle id={subtitleId}>{subtitle}</CardSubtitle>} |
| 36 |
</div> |
| 37 |
); |
| 38 |
}; |
| 39 |
CardTitle.displayName = 'CardTitle'; |