import { forwardRef, Fragment } from 'react'; import type { FC } from 'react'; import clsx from 'clsx'; import { CardGridCellParams } from '../card-grid'; import { StyledCard, StyledColumn, StyledDivider } from './styles'; import { CardProps, CardColumnProps } from './types'; import { useToggleOnFocus } from '../../../utils'; /** * Cards display content and actions in columns separated by a vertical divider. */ export const Card: FC = forwardRef(function Card(props, ref) { const { children, active = false, hovered = false, disabled = false, indicator = true, columns = [], rowIndex = 0, dividerProps, onCellClick, classes, className, ...otherProps } = props; if (children) { columns.push({ field: '_children_', children }); } const { currentState: focused, eventHandlers } = useToggleOnFocus(); const cardFocusHandler: CardProps['onFocus'] = event => { eventHandlers.onFocus(); otherProps?.onFocus?.(event); }; const cardBlurHandler: CardProps['onBlur'] = event => { eventHandlers.onBlur(); otherProps?.onBlur?.(event); }; return ( {columns?.map((column: CardColumnProps, colIndex) => { const { field, children, hideColumn = false, hideDivider = false, onClick, ...otherColumnProps } = column; const cellParams: CardGridCellParams = { colIndex, field }; const cellClickHandler: CardColumnProps['onClick'] = event => { onCellClick?.(event, cellParams); onClick?.(event); }; return !hideColumn ? ( {!hideDivider && colIndex > 0 && ( )} {children} ) : null; })} ); }); export default Card;