Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion packages/react-core/src/components/Alert/Alert.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { capitalize, useOUIAProps, OUIAProps } from '../../helpers';
import { AlertContext } from './AlertContext';
import maxLines from '@patternfly/react-tokens/dist/esm/c_alert__title_max_lines';
import { Tooltip } from '../Tooltip';
import { Button, ButtonVariant } from '../Button';
import AngleRightIcon from '@patternfly/react-icons/dist/esm/icons/angle-right-icon';

export enum AlertVariant {
success = 'success',
Expand All @@ -22,6 +24,8 @@ export interface AlertProps extends Omit<React.HTMLProps<HTMLDivElement>, 'actio
variant?: 'success' | 'danger' | 'warning' | 'info' | 'default';
/** Flag to indicate if the Alert is inline */
isInline?: boolean;
/** Flag to indicate if the Alert is plain */
Comment thread
CooperRedhat marked this conversation as resolved.
Outdated
isPlain?: boolean;
/** Title of the Alert */
title: React.ReactNode;
/** Close button; use the AlertActionCloseButton component */
Expand Down Expand Up @@ -50,11 +54,14 @@ export interface AlertProps extends Omit<React.HTMLProps<HTMLDivElement>, 'actio
tooltipPosition?: 'auto' | 'top' | 'bottom' | 'left' | 'right';
/** Set a custom icon to the Alert. If not set the icon is set according to the variant */
customIcon?: React.ReactNode;
/** Flag indicating that the Alert is expandable */
isExpandable?: boolean;
Comment thread
CooperRedhat marked this conversation as resolved.
}

export const Alert: React.FunctionComponent<AlertProps> = ({
variant = AlertVariant.default,
isInline = false,
isPlain = false,
isLiveRegion = false,
variantLabel = `${capitalize(variant)} alert:`,
'aria-label': ariaLabel = `${capitalize(variant)} Alert`,
Expand All @@ -71,6 +78,7 @@ export const Alert: React.FunctionComponent<AlertProps> = ({
truncateTitle = 0,
tooltipPosition,
customIcon,
isExpandable = false,
onMouseEnter = () => {},
onMouseLeave = () => {},
...props
Expand Down Expand Up @@ -135,6 +143,11 @@ export const Alert: React.FunctionComponent<AlertProps> = ({
dismissed && onTimeout();
}, [dismissed]);

const [isExpanded, setIsExpanded] = useState(false);
const onToggleExpand = () => {
setIsExpanded(!isExpanded);
};

const myOnMouseEnter = (ev: React.MouseEvent<HTMLDivElement>) => {
setIsMouseOver(true);
setTimedOutAnimation(false);
Expand Down Expand Up @@ -165,6 +178,9 @@ export const Alert: React.FunctionComponent<AlertProps> = ({
className={css(
styles.alert,
isInline && styles.modifiers.inline,
isPlain && styles.modifiers.plain,
isExpandable && styles.modifiers.expandable,
isExpanded && styles.modifiers.expanded,
styles.modifiers[variant as 'success' | 'danger' | 'warning' | 'info'],
className
)}
Expand All @@ -178,6 +194,20 @@ export const Alert: React.FunctionComponent<AlertProps> = ({
onMouseLeave={myOnMouseLeave}
{...props}
>
{isExpandable && (
<div className={css(styles.alertToggle)}>
<Button
variant={ButtonVariant.plain}
onClick={onToggleExpand}
aria-expanded={isExpanded}
aria-label={`Toggle ${ariaLabel} description`}
Comment thread
CooperRedhat marked this conversation as resolved.
Outdated
>
<span className={css(styles.alertToggleIcon)}>
<AngleRightIcon aria-hidden="true" />
</span>
</Button>
</div>
)}
<AlertIcon variant={variant} customIcon={customIcon} />
{isTooltipVisible ? (
<Tooltip content={getHeadingContent} position={tooltipPosition}>
Expand All @@ -191,7 +221,9 @@ export const Alert: React.FunctionComponent<AlertProps> = ({
<div className={css(styles.alertAction)}>{actionClose}</div>
</AlertContext.Provider>
)}
{children && <div className={css(styles.alertDescription)}>{children}</div>}
{children && (!isExpandable || (isExpandable && isExpanded)) && (
<div className={css(styles.alertDescription)}>{children}</div>
)}
{actionLinks && <div className={css(styles.alertActionGroup)}>{actionLinks}</div>}
</div>
);
Expand Down
19 changes: 19 additions & 0 deletions packages/react-core/src/components/Alert/__tests__/Alert.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,25 @@ Object.values(AlertVariant).forEach(variant => {
expect(view).toMatchSnapshot();
});

test('expandable variation', () => {
const view = mount(
<Alert variant={variant} title="Some title" isExpandable>
<p>Success alert description. This should tell the user more information about the alert.</p>
</Alert>
)
expect(view).toMatchSnapshot();
})

test('expandable variation description hidden', () => {
const wrapper = mount(
<Alert variant={variant} title="Some title" isExpandable>
<p>Success alert description. This should tell the user more information about the alert.</p>
</Alert>
)
const descriptionExists = wrapper.find('.pf-c-alert__description').exists();
expect(descriptionExists).toBeFalsy();
})

test('Toast alerts match snapsnot', () => {
const view = mount(
<Alert isLiveRegion={true} variant={variant} aria-label={`${variant} toast alert`} title="Some title">
Expand Down
Loading