| 1 |
import { __rest } from "tslib"; |
| 2 |
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime"; |
| 3 |
import { css } from '@patternfly/react-styles'; |
| 4 |
import styles from '@patternfly/react-styles/css/components/Card/card.mjs'; |
| 5 |
import { CardContext } from './Card'; |
| 6 |
import { CardHeaderMain } from './CardHeaderMain'; |
| 7 |
import { CardActions } from './CardActions'; |
| 8 |
import { CardSelectableActions } from './CardSelectableActions'; |
| 9 |
import { Button } from '../Button'; |
| 10 |
import RhMicronsCaretDownIcon from '@patternfly/react-icons/dist/esm/icons/rh-microns-caret-down-icon'; |
| 11 |
import { Radio } from '../Radio'; |
| 12 |
import { Checkbox } from '../Checkbox'; |
| 13 |
import { useSSRSafeId } from '../../helpers'; |
| 14 |
export const CardHeader = (_a) => { |
| 15 |
var { children, className, actions, selectableActions, id, onExpand, toggleButtonProps, isToggleRightAligned, hasWrap } = _a, props = __rest(_a, ["children", "className", "actions", "selectableActions", "id", "onExpand", "toggleButtonProps", "isToggleRightAligned", "hasWrap"]); |
| 16 |
const randomId = useSSRSafeId(); |
| 17 |
return (_jsx(CardContext.Consumer, { children: ({ cardId, isClickable, isSelectable, isSelected, isDisabled: isCardDisabled }) => { |
| 18 |
const cardHeaderToggle = (_jsx("div", { className: css(styles.cardHeaderToggle), children: _jsx(Button, Object.assign({ variant: "plain", type: "button", onClick: (evt) => { |
| 19 |
onExpand(evt, cardId); |
| 20 |
} }, toggleButtonProps, { icon: _jsx("span", { className: css(styles.cardHeaderToggleIcon), children: _jsx(RhMicronsCaretDownIcon, {}) }) })) })); |
| 21 |
const isClickableOrSelectableOnly = (isClickable && !isSelectable) || (isSelectable && !isClickable); |
| 22 |
if ((actions === null || actions === void 0 ? void 0 : actions.actions) && isClickableOrSelectableOnly) { |
| 23 |
// eslint-disable-next-line no-console |
| 24 |
console.error(`Card: ${isClickable ? 'Clickable' : 'Selectable'} only cards should not contain any other actions. If you wish to include additional actions, use a clickable and selectable card.`); |
| 25 |
} |
| 26 |
const isClickableOnlyCard = isClickable && !isSelectable; |
| 27 |
if ((isClickableOnlyCard || isSelectable) && |
| 28 |
!(selectableActions === null || selectableActions === void 0 ? void 0 : selectableActions.selectableActionAriaLabel) && |
| 29 |
!(selectableActions === null || selectableActions === void 0 ? void 0 : selectableActions.selectableActionAriaLabelledby)) { |
| 30 |
// eslint-disable-next-line no-console |
| 31 |
console.error(`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.`); |
| 32 |
} |
| 33 |
const SelectableCardInput = (selectableActions === null || selectableActions === void 0 ? void 0 : selectableActions.variant) === 'single' ? Radio : Checkbox; |
| 34 |
const getSelectableProps = () => { |
| 35 |
var _a, _b; |
| 36 |
return (Object.assign({ className: css('pf-m-standalone'), inputClassName: css((selectableActions === null || selectableActions === void 0 ? void 0 : selectableActions.isHidden) && 'pf-v6-screen-reader'), label: _jsx(_Fragment, {}), 'aria-label': selectableActions.selectableActionAriaLabel, 'aria-labelledby': selectableActions.selectableActionAriaLabelledby, id: (_a = selectableActions.selectableActionId) !== null && _a !== void 0 ? _a : `card-selectable-${randomId}`, name: selectableActions.name, isDisabled: isCardDisabled, onChange: selectableActions.onChange, isChecked: (_b = selectableActions.isChecked) !== null && _b !== void 0 ? _b : isSelected }, selectableActions.selectableActionProps)); |
| 37 |
}; |
| 38 |
const isClickableLinkCard = (selectableActions === null || selectableActions === void 0 ? void 0 : selectableActions.to) !== undefined; |
| 39 |
const ClickableCardComponent = isClickableLinkCard ? 'a' : 'button'; |
| 40 |
const getClickableProps = () => { |
| 41 |
const isDisabledLinkCard = isCardDisabled && isClickableLinkCard; |
| 42 |
const baseProps = Object.assign({ className: css('pf-v6-c-card__clickable-action', isDisabledLinkCard && styles.modifiers.disabled, (selectableActions === null || selectableActions === void 0 ? void 0 : selectableActions.isHidden) && 'pf-v6-screen-reader'), id: selectableActions.selectableActionId, 'aria-label': selectableActions.selectableActionAriaLabel, 'aria-labelledby': selectableActions.selectableActionAriaLabelledby }, selectableActions.selectableActionProps); |
| 43 |
if (isClickableLinkCard) { |
| 44 |
return Object.assign(Object.assign(Object.assign(Object.assign({}, baseProps), { href: selectableActions.to }), (isCardDisabled && { tabIndex: -1, 'aria-disabled': true })), (selectableActions.isExternalLink && { target: '_blank' })); |
| 45 |
} |
| 46 |
return Object.assign(Object.assign({}, baseProps), { type: 'button', disabled: isCardDisabled, onClick: selectableActions.onClickAction }); |
| 47 |
}; |
| 48 |
return (_jsxs("div", Object.assign({ className: css(styles.cardHeader, isToggleRightAligned && styles.modifiers.toggleRight, hasWrap && styles.modifiers.wrap, className), id: id }, props, { children: [onExpand && !isToggleRightAligned && cardHeaderToggle, (actions || (selectableActions && (isClickable || isSelectable))) && (_jsxs(CardActions, { className: actions === null || actions === void 0 ? void 0 : actions.className, hasNoOffset: (actions === null || actions === void 0 ? void 0 : actions.hasNoOffset) || (selectableActions === null || selectableActions === void 0 ? void 0 : selectableActions.hasNoOffset), children: [actions === null || actions === void 0 ? void 0 : actions.actions, selectableActions && (isClickable || isSelectable) && (_jsxs(CardSelectableActions, { className: selectableActions === null || selectableActions === void 0 ? void 0 : selectableActions.className, children: [isSelectable && _jsx(SelectableCardInput, Object.assign({}, getSelectableProps())), isClickableOnlyCard && _jsx(ClickableCardComponent, Object.assign({}, getClickableProps()))] }))] })), children && _jsx(CardHeaderMain, { children: children }), onExpand && isToggleRightAligned && cardHeaderToggle] }))); |
| 49 |
} })); |
| 50 |
}; |
| 51 |
CardHeader.displayName = 'CardHeader'; |
| 52 |
//# sourceMappingURL=CardHeader.js.map |