| 1 |
import { createContext, forwardRef } from 'react'; |
| 2 |
import styles from '@patternfly/react-styles/css/components/Card/card'; |
| 3 |
import { css } from '@patternfly/react-styles'; |
| 4 |
import { useOUIAProps, OUIAProps } from '../../helpers'; |
| 5 |
|
| 6 |
export interface CardProps extends React.HTMLProps<HTMLElement>, OUIAProps { |
| 7 |
/** Content rendered inside the Card */ |
| 8 |
children?: React.ReactNode; |
| 9 |
/** ID of the Card. Also passed back in the CardHeader onExpand callback. */ |
| 10 |
id?: string; |
| 11 |
/** Additional classes added to the Card */ |
| 12 |
className?: string; |
| 13 |
/** Sets the base component to render. defaults to div */ |
| 14 |
component?: keyof React.JSX.IntrinsicElements; |
| 15 |
/** Modifies the card to include compact styling. Should not be used with isLarge. */ |
| 16 |
isCompact?: boolean; |
| 17 |
/** Flag indicating that the card is selectable. */ |
| 18 |
isSelectable?: boolean; |
| 19 |
/** Flag indicating that the card is clickable and contains some action that triggers on click. */ |
| 20 |
isClickable?: boolean; |
| 21 |
/** Flag indicating whether a card that is either selectable only or both clickable and selectable is |
| 22 |
* currently selected and has selected styling. |
| 23 |
*/ |
| 24 |
isSelected?: boolean; |
| 25 |
/** Flag indicating whether a card that is either only clickable or that is both clickable and selectable |
| 26 |
* is currently clicked and has clicked styling. |
| 27 |
*/ |
| 28 |
isClicked?: boolean; |
| 29 |
/** Flag indicating that a clickable or selectable card is disabled. */ |
| 30 |
isDisabled?: boolean; |
| 31 |
/** Modifies the card to be large. Should not be used with isCompact. */ |
| 32 |
isLarge?: boolean; |
| 33 |
/** Cause component to consume the available height of its container */ |
| 34 |
isFullHeight?: boolean; |
| 35 |
/** Modifies the card to include plain styling; this removes border and background */ |
| 36 |
isPlain?: boolean; |
| 37 |
/** Modifies the card to include glass styling when glass theme is enabled */ |
| 38 |
isGlass?: boolean; |
| 39 |
/** Flag indicating if a card is expanded. Modifies the card to be expandable. */ |
| 40 |
isExpanded?: boolean; |
| 41 |
/** Card background color variant */ |
| 42 |
variant?: 'default' | 'secondary'; |
| 43 |
/** Value to overwrite the randomly generated data-ouia-component-id.*/ |
| 44 |
ouiaId?: number | string; |
| 45 |
/** Set the value of data-ouia-safe. Only set to true when the component is in a static state, i.e. no animations are occurring. At all other times, this value must be false. */ |
| 46 |
ouiaSafe?: boolean; |
| 47 |
/** @hide Forwarded ref */ |
| 48 |
innerRef?: React.Ref<any>; |
| 49 |
} |
| 50 |
|
| 51 |
interface CardContextProps { |
| 52 |
cardId: string; |
| 53 |
isExpanded: boolean; |
| 54 |
isClickable: boolean; |
| 55 |
isSelectable: boolean; |
| 56 |
isSelected: boolean; |
| 57 |
isClicked: boolean; |
| 58 |
isDisabled: boolean; |
| 59 |
} |
| 60 |
|
| 61 |
export const CardContext = createContext<Partial<CardContextProps>>({ |
| 62 |
cardId: '', |
| 63 |
isExpanded: false, |
| 64 |
isClickable: false, |
| 65 |
isSelectable: false, |
| 66 |
isSelected: false, |
| 67 |
isClicked: false, |
| 68 |
isDisabled: false |
| 69 |
}); |
| 70 |
|
| 71 |
const CardBase: React.FunctionComponent<CardProps> = ({ |
| 72 |
children, |
| 73 |
id = '', |
| 74 |
className, |
| 75 |
component = 'div', |
| 76 |
isCompact = false, |
| 77 |
isSelectable = false, |
| 78 |
isClickable = false, |
| 79 |
isDisabled = false, |
| 80 |
isSelected = false, |
| 81 |
isClicked = false, |
| 82 |
isExpanded = false, |
| 83 |
isLarge = false, |
| 84 |
isFullHeight = false, |
| 85 |
isPlain = false, |
| 86 |
isGlass = false, |
| 87 |
variant = 'default', |
| 88 |
ouiaId, |
| 89 |
ouiaSafe = true, |
| 90 |
innerRef, |
| 91 |
...props |
| 92 |
}: CardProps) => { |
| 93 |
const Component = component as any; |
| 94 |
const ouiaProps = useOUIAProps(Card.displayName, ouiaId, ouiaSafe); |
| 95 |
|
| 96 |
if (isCompact && isLarge) { |
| 97 |
// eslint-disable-next-line no-console |
| 98 |
console.warn('Card: Cannot use isCompact with isLarge. Defaulting to isCompact'); |
| 99 |
isLarge = false; |
| 100 |
} |
| 101 |
|
| 102 |
const getSelectableModifiers = () => { |
| 103 |
if (isSelectable && isClickable) { |
| 104 |
return css( |
| 105 |
styles.modifiers.selectable, |
| 106 |
styles.modifiers.clickable, |
| 107 |
(isSelected || isClicked) && styles.modifiers.current |
| 108 |
); |
| 109 |
} |
| 110 |
|
| 111 |
if (isSelectable) { |
| 112 |
return css(styles.modifiers.selectable, isSelected && styles.modifiers.selected); |
| 113 |
} |
| 114 |
|
| 115 |
if (isClickable) { |
| 116 |
return css(styles.modifiers.clickable, isClicked && styles.modifiers.current); |
| 117 |
} |
| 118 |
|
| 119 |
return ''; |
| 120 |
}; |
| 121 |
|
| 122 |
return ( |
| 123 |
<CardContext.Provider |
| 124 |
value={{ |
| 125 |
cardId: id, |
| 126 |
isExpanded, |
| 127 |
isClickable, |
| 128 |
isSelectable, |
| 129 |
isSelected, |
| 130 |
isClicked, |
| 131 |
isDisabled |
| 132 |
}} |
| 133 |
> |
| 134 |
<Component |
| 135 |
ref={innerRef} |
| 136 |
id={id} |
| 137 |
className={css( |
| 138 |
styles.card, |
| 139 |
isCompact && styles.modifiers.compact, |
| 140 |
isExpanded && styles.modifiers.expanded, |
| 141 |
isLarge && styles.modifiers.displayLg, |
| 142 |
isFullHeight && styles.modifiers.fullHeight, |
| 143 |
isPlain && styles.modifiers.plain, |
| 144 |
isGlass && styles.modifiers.glass, |
| 145 |
variant === 'secondary' && styles.modifiers.secondary, |
| 146 |
getSelectableModifiers(), |
| 147 |
isDisabled && styles.modifiers.disabled, |
| 148 |
className |
| 149 |
)} |
| 150 |
{...props} |
| 151 |
{...ouiaProps} |
| 152 |
> |
| 153 |
{children} |
| 154 |
</Component> |
| 155 |
</CardContext.Provider> |
| 156 |
); |
| 157 |
}; |
| 158 |
|
| 159 |
export const Card = forwardRef((props: CardProps, ref: React.Ref<any>) => <CardBase {...props} innerRef={ref} />); |
| 160 |
|
| 161 |
Card.displayName = 'Card'; |