| 1 |
import { css } from '@patternfly/react-styles'; |
| 2 |
import styles from '@patternfly/react-styles/css/components/Card/card'; |
| 3 |
import { CardContext } from './Card'; |
| 4 |
import { CardHeaderMain } from './CardHeaderMain'; |
| 5 |
import { CardActions } from './CardActions'; |
| 6 |
import { CardSelectableActions } from './CardSelectableActions'; |
| 7 |
import { Button } from '../Button'; |
| 8 |
import RhMicronsCaretDownIcon from '@patternfly/react-icons/dist/esm/icons/rh-microns-caret-down-icon'; |
| 9 |
import { Radio } from '../Radio'; |
| 10 |
import { Checkbox } from '../Checkbox'; |
| 11 |
import { useSSRSafeId } from '../../helpers'; |
| 12 |
|
| 13 |
export interface CardHeaderActionsObject { |
| 14 |
/** Actions of the card header */ |
| 15 |
actions: React.ReactNode; |
| 16 |
/** Flag indicating that the actions have no offset */ |
| 17 |
hasNoOffset?: boolean; |
| 18 |
/** Additional classes added to the actions wrapper */ |
| 19 |
className?: string; |
| 20 |
} |
| 21 |
|
| 22 |
export interface CardHeaderSelectableActionsObject { |
| 23 |
/** Determines the type of input to be used for a selectable card. */ |
| 24 |
variant?: 'single' | 'multiple'; |
| 25 |
/** Flag indicating that the actions have no offset */ |
| 26 |
hasNoOffset?: boolean; |
| 27 |
/** Additional classes added to the selectable actions wrapper */ |
| 28 |
className?: string; |
| 29 |
/** Custom ID passed to the selectable card's input or a clickable-only card's button/anchor. |
| 30 |
* If omitted, a random unique ID will be assigned to a selectable card's input. */ |
| 31 |
selectableActionId?: string; |
| 32 |
/** Adds an accessible name to the input of a selectable card or clickable button/anchor of a clickable-only card. |
| 33 |
* This or selectableActionAriaLabelledby is required for clickable-only cards. |
| 34 |
*/ |
| 35 |
selectableActionAriaLabel?: string; |
| 36 |
/** A single or list of space-delimited ID's that provide an accessible name to the input of a selectable card |
| 37 |
* or clickable button/anchor of a clickable-only card. This or selectableActionAriaLabelledby is required |
| 38 |
* for clickable-only cards. |
| 39 |
*/ |
| 40 |
selectableActionAriaLabelledby?: string; |
| 41 |
/** Callback for when a selectable card input changes */ |
| 42 |
onChange?: (event: React.FormEvent<HTMLInputElement>, checked: boolean) => void; |
| 43 |
/** Action to call when a clickable-only card is clicked. This cannot be combined with the to prop. */ |
| 44 |
onClickAction?: (event: React.MouseEvent) => void; |
| 45 |
/** Link to navigate to when a clickable-only card is clicked. This cannot be combined with the onClickAction prop. */ |
| 46 |
to?: string; |
| 47 |
/** Additional props spread to a selectable card input or clickable-only card's button/anchor. */ |
| 48 |
selectableActionProps?: any; |
| 49 |
/** Flag to indicate whether a clickable-only card's link should open in a new tab/window. */ |
| 50 |
isExternalLink?: boolean; |
| 51 |
/** Name for the input element of a selectable card. */ |
| 52 |
name?: string; |
| 53 |
/** @deprecated Flag indicating whether the selectable card input is checked. We recommend using |
| 54 |
* the isSelected prop on the card component instead. |
| 55 |
*/ |
| 56 |
isChecked?: boolean; |
| 57 |
/** Flag indicating the action is hidden */ |
| 58 |
isHidden?: boolean; |
| 59 |
} |
| 60 |
|
| 61 |
export interface CardHeaderProps extends React.HTMLProps<HTMLDivElement> { |
| 62 |
/** Content rendered inside the card header */ |
| 63 |
children?: React.ReactNode; |
| 64 |
/** Additional classes added to the card header */ |
| 65 |
className?: string; |
| 66 |
/** Actions of the card header */ |
| 67 |
actions?: CardHeaderActionsObject; |
| 68 |
/** Selectable actions of the card header */ |
| 69 |
selectableActions?: CardHeaderSelectableActionsObject; |
| 70 |
/** ID of the card header. */ |
| 71 |
id?: string; |
| 72 |
/** Callback expandable card */ |
| 73 |
onExpand?: (event: React.MouseEvent, id: string) => void; |
| 74 |
/** Additional props for expandable toggle button */ |
| 75 |
toggleButtonProps?: any; |
| 76 |
/** Whether to right-align expandable toggle button */ |
| 77 |
isToggleRightAligned?: boolean; |
| 78 |
/** Flag indicating that header wrapping is enabled */ |
| 79 |
hasWrap?: boolean; |
| 80 |
} |
| 81 |
|
| 82 |
export const CardHeader: React.FunctionComponent<CardHeaderProps> = ({ |
| 83 |
children, |
| 84 |
className, |
| 85 |
actions, |
| 86 |
selectableActions, |
| 87 |
id, |
| 88 |
onExpand, |
| 89 |
toggleButtonProps, |
| 90 |
isToggleRightAligned, |
| 91 |
hasWrap, |
| 92 |
...props |
| 93 |
}: CardHeaderProps) => { |
| 94 |
const randomId = useSSRSafeId(); |
| 95 |
|
| 96 |
return ( |
| 97 |
<CardContext.Consumer> |
| 98 |
{({ cardId, isClickable, isSelectable, isSelected, isDisabled: isCardDisabled }) => { |
| 99 |
const cardHeaderToggle = ( |
| 100 |
<div className={css(styles.cardHeaderToggle)}> |
| 101 |
<Button |
| 102 |
variant="plain" |
| 103 |
type="button" |
| 104 |
onClick={(evt) => { |
| 105 |
onExpand(evt, cardId); |
| 106 |
}} |
| 107 |
{...toggleButtonProps} |
| 108 |
icon={ |
| 109 |
<span className={css(styles.cardHeaderToggleIcon)}> |
| 110 |
<RhMicronsCaretDownIcon /> |
| 111 |
</span> |
| 112 |
} |
| 113 |
/> |
| 114 |
</div> |
| 115 |
); |
| 116 |
|
| 117 |
const isClickableOrSelectableOnly = (isClickable && !isSelectable) || (isSelectable && !isClickable); |
| 118 |
if (actions?.actions && isClickableOrSelectableOnly) { |
| 119 |
// eslint-disable-next-line no-console |
| 120 |
console.error( |
| 121 |
`Card: ${ |
| 122 |
isClickable ? 'Clickable' : 'Selectable' |
| 123 |
} only cards should not contain any other actions. If you wish to include additional actions, use a clickable and selectable card.` |
| 124 |
); |
| 125 |
} |
| 126 |
|
| 127 |
const isClickableOnlyCard = isClickable && !isSelectable; |
| 128 |
if ( |
| 129 |
(isClickableOnlyCard || isSelectable) && |
| 130 |
!selectableActions?.selectableActionAriaLabel && |
| 131 |
!selectableActions?.selectableActionAriaLabelledby |
| 132 |
) { |
| 133 |
// eslint-disable-next-line no-console |
| 134 |
console.error( |
| 135 |
`Card: ${isClickableOnlyCard ? 'Clickable-only cards' : 'Cards with a selectable input'} must have either the selectableActions.selectableActionAriaLabel or selectableActions.selectableActionAriaLabelledby prop passed in order to provide an accessible name to the clickable element.` |
| 136 |
); |
| 137 |
} |
| 138 |
|
| 139 |
const SelectableCardInput = selectableActions?.variant === 'single' ? Radio : Checkbox; |
| 140 |
const getSelectableProps = () => ({ |
| 141 |
className: css('pf-m-standalone'), |
| 142 |
inputClassName: css(selectableActions?.isHidden && 'pf-v6-screen-reader'), |
| 143 |
label: <></>, |
| 144 |
'aria-label': selectableActions.selectableActionAriaLabel, |
| 145 |
'aria-labelledby': selectableActions.selectableActionAriaLabelledby, |
| 146 |
id: selectableActions.selectableActionId ?? `card-selectable-${randomId}`, |
| 147 |
name: selectableActions.name, |
| 148 |
isDisabled: isCardDisabled, |
| 149 |
onChange: selectableActions.onChange, |
| 150 |
isChecked: selectableActions.isChecked ?? isSelected, |
| 151 |
...selectableActions.selectableActionProps |
| 152 |
}); |
| 153 |
|
| 154 |
const isClickableLinkCard = selectableActions?.to !== undefined; |
| 155 |
const ClickableCardComponent = isClickableLinkCard ? 'a' : 'button'; |
| 156 |
const getClickableProps = () => { |
| 157 |
const isDisabledLinkCard = isCardDisabled && isClickableLinkCard; |
| 158 |
const baseProps = { |
| 159 |
className: css( |
| 160 |
'pf-v6-c-card__clickable-action', |
| 161 |
isDisabledLinkCard && styles.modifiers.disabled, |
| 162 |
selectableActions?.isHidden && 'pf-v6-screen-reader' |
| 163 |
), |
| 164 |
id: selectableActions.selectableActionId, |
| 165 |
'aria-label': selectableActions.selectableActionAriaLabel, |
| 166 |
'aria-labelledby': selectableActions.selectableActionAriaLabelledby, |
| 167 |
...selectableActions.selectableActionProps |
| 168 |
}; |
| 169 |
|
| 170 |
if (isClickableLinkCard) { |
| 171 |
return { |
| 172 |
...baseProps, |
| 173 |
href: selectableActions.to, |
| 174 |
...(isCardDisabled && { tabIndex: -1, 'aria-disabled': true }), |
| 175 |
...(selectableActions.isExternalLink && { target: '_blank' }) |
| 176 |
}; |
| 177 |
} |
| 178 |
|
| 179 |
return { ...baseProps, type: 'button', disabled: isCardDisabled, onClick: selectableActions.onClickAction }; |
| 180 |
}; |
| 181 |
|
| 182 |
return ( |
| 183 |
<div |
| 184 |
className={css( |
| 185 |
styles.cardHeader, |
| 186 |
isToggleRightAligned && styles.modifiers.toggleRight, |
| 187 |
hasWrap && styles.modifiers.wrap, |
| 188 |
className |
| 189 |
)} |
| 190 |
id={id} |
| 191 |
{...props} |
| 192 |
> |
| 193 |
{onExpand && !isToggleRightAligned && cardHeaderToggle} |
| 194 |
{(actions || (selectableActions && (isClickable || isSelectable))) && ( |
| 195 |
<CardActions |
| 196 |
className={actions?.className} |
| 197 |
hasNoOffset={actions?.hasNoOffset || selectableActions?.hasNoOffset} |
| 198 |
> |
| 199 |
{actions?.actions} |
| 200 |
{selectableActions && (isClickable || isSelectable) && ( |
| 201 |
<CardSelectableActions className={selectableActions?.className}> |
| 202 |
{isSelectable && <SelectableCardInput {...getSelectableProps()} />} |
| 203 |
{isClickableOnlyCard && <ClickableCardComponent {...getClickableProps()} />} |
| 204 |
</CardSelectableActions> |
| 205 |
)} |
| 206 |
</CardActions> |
| 207 |
)} |
| 208 |
{children && <CardHeaderMain>{children}</CardHeaderMain>} |
| 209 |
{onExpand && isToggleRightAligned && cardHeaderToggle} |
| 210 |
</div> |
| 211 |
); |
| 212 |
}} |
| 213 |
</CardContext.Consumer> |
| 214 |
); |
| 215 |
}; |
| 216 |
CardHeader.displayName = 'CardHeader'; |