| 1 |
import { css } from '@patternfly/react-styles'; |
| 2 |
import styles from '@patternfly/react-styles/css/components/NotificationDrawer/notification-drawer'; |
| 3 |
|
| 4 |
export interface NotificationDrawerListItemProps extends React.HTMLProps<HTMLLIElement> { |
| 5 |
/** Content rendered inside the list item */ |
| 6 |
children?: React.ReactNode; |
| 7 |
/** Additional classes added to the list item */ |
| 8 |
className?: string; |
| 9 |
/** Modifies the list item to include hover styles on :hover */ |
| 10 |
isHoverable?: boolean; |
| 11 |
/** Adds styling to the list item to indicate it has been read */ |
| 12 |
isRead?: boolean; |
| 13 |
/** Callback for when a list item is clicked */ |
| 14 |
onClick?: (event: any) => void; |
| 15 |
/** Visually hidden text that conveys the current read state of the notification list item */ |
| 16 |
readStateScreenReaderText?: string; |
| 17 |
/** Tab index for the list item */ |
| 18 |
tabIndex?: number; |
| 19 |
/** Variant indicates the severity level */ |
| 20 |
variant?: 'custom' | 'success' | 'danger' | 'warning' | 'info'; |
| 21 |
} |
| 22 |
|
| 23 |
export const NotificationDrawerListItem: React.FunctionComponent<NotificationDrawerListItemProps> = ({ |
| 24 |
children = null, |
| 25 |
className = '', |
| 26 |
isHoverable = true, |
| 27 |
isRead = false, |
| 28 |
// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 29 |
onClick = (event: React.MouseEvent) => undefined as any, |
| 30 |
readStateScreenReaderText, |
| 31 |
tabIndex = 0, |
| 32 |
variant = 'custom', |
| 33 |
...props |
| 34 |
}: NotificationDrawerListItemProps) => { |
| 35 |
const onKeyDown = (event: React.KeyboardEvent) => { |
| 36 |
if (!(event.target as HTMLElement).parentElement.classList.contains(styles.notificationDrawerListItemAction)) { |
| 37 |
// Accessibility function. Click on the list item when pressing Enter or Space on it. |
| 38 |
if (event.key === 'Enter' || event.key === ' ') { |
| 39 |
(event.target as HTMLElement).click(); |
| 40 |
} |
| 41 |
} |
| 42 |
}; |
| 43 |
|
| 44 |
let readStateSRText; |
| 45 |
if (readStateScreenReaderText) { |
| 46 |
readStateSRText = readStateScreenReaderText; |
| 47 |
} else { |
| 48 |
readStateSRText = isRead ? 'read' : 'unread'; |
| 49 |
} |
| 50 |
|
| 51 |
return ( |
| 52 |
<li |
| 53 |
{...props} |
| 54 |
className={css( |
| 55 |
styles.notificationDrawerListItem, |
| 56 |
isHoverable && styles.modifiers.hoverable, |
| 57 |
styles.modifiers[variant], |
| 58 |
isRead && styles.modifiers.read, |
| 59 |
className |
| 60 |
)} |
| 61 |
tabIndex={tabIndex} |
| 62 |
onClick={(e) => onClick(e)} |
| 63 |
onKeyDown={onKeyDown} |
| 64 |
> |
| 65 |
<span className="pf-v6-screen-reader">{readStateSRText}</span> |
| 66 |
{children} |
| 67 |
</li> |
| 68 |
); |
| 69 |
}; |
| 70 |
NotificationDrawerListItem.displayName = 'NotificationDrawerListItem'; |