Skip to content

Commit 29eb11b

Browse files
authored
fix(alert): use context to set label (#3771)
1 parent cf3559e commit 29eb11b

File tree

9 files changed

+121
-274
lines changed

9 files changed

+121
-274
lines changed

packages/react-core/src/components/Alert/Alert.tsx

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ export interface AlertProps extends Omit<React.HTMLProps<HTMLDivElement>, 'actio
3535
isLiveRegion?: boolean;
3636
}
3737

38+
interface AlertContext {
39+
title: React.ReactNode;
40+
variantLabel?: string;
41+
}
42+
43+
export const AlertContext = React.createContext<AlertContext>(null);
44+
3845
const Alert: React.FunctionComponent<AlertProps & InjectedOuiaProps> = ({
3946
variant = AlertVariant.info,
4047
isInline = false,
@@ -49,7 +56,7 @@ const Alert: React.FunctionComponent<AlertProps & InjectedOuiaProps> = ({
4956
ouiaId = null,
5057
...props
5158
}: AlertProps & InjectedOuiaProps) => {
52-
const readerTitle = (
59+
const getHeadingContent = (
5360
<React.Fragment>
5461
<span className={css(accessibleStyles.screenReader)}>{variantLabel}</span>
5562
{title}
@@ -78,11 +85,13 @@ const Alert: React.FunctionComponent<AlertProps & InjectedOuiaProps> = ({
7885
})}
7986
>
8087
<AlertIcon variant={variant} />
81-
<h4 className={css(styles.alertTitle)}>{readerTitle}</h4>
88+
<h4 className={css(styles.alertTitle)}>{getHeadingContent}</h4>
8289
{children && <div className={css(styles.alertDescription)}>{children}</div>}
83-
{action && (
84-
<div className={css(styles.alertAction)}>{React.cloneElement(action as any, { title, variantLabel })}</div>
85-
)}
90+
<AlertContext.Provider value={{ title, variantLabel }}>
91+
{action && (typeof action === 'object' || typeof action === 'string') && (
92+
<div className={css(styles.alertAction)}>{action}</div>
93+
)}
94+
</AlertContext.Provider>
8695
</div>
8796
);
8897
};
Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as React from 'react';
22
import { Button, ButtonVariant, ButtonProps } from '../Button';
33
import TimesIcon from '@patternfly/react-icons/dist/js/icons/times-icon';
4+
import { AlertContext } from '..';
45

56
interface AlertActionCloseButtonProps extends ButtonProps {
67
/** Additional classes added to the AlertActionCloseButton */
@@ -18,16 +19,19 @@ export const AlertActionCloseButton = ({
1819
className = '',
1920
onClose = () => undefined as any,
2021
'aria-label': ariaLabel = '',
21-
title,
22-
variantLabel = '',
22+
variantLabel,
2323
...props
2424
}: AlertActionCloseButtonProps) => (
25-
<Button
26-
variant={ButtonVariant.plain}
27-
onClick={onClose}
28-
aria-label={ariaLabel === '' ? `Close ${variantLabel} alert: ${title}` : ariaLabel}
29-
{...props}
30-
>
31-
<TimesIcon />
32-
</Button>
25+
<AlertContext.Consumer>
26+
{({ title, variantLabel: alertVariantLabel }) => (
27+
<Button
28+
variant={ButtonVariant.plain}
29+
onClick={onClose}
30+
aria-label={ariaLabel === '' ? `Close ${variantLabel || alertVariantLabel} alert: ${title}` : ariaLabel}
31+
{...props}
32+
>
33+
<TimesIcon />
34+
</Button>
35+
)}
36+
</AlertContext.Consumer>
3337
);

packages/react-core/src/components/Alert/AlertActionLink.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export interface AlertActionLinkProps extends ButtonProps {
1010

1111
export const AlertActionLink: React.FunctionComponent<AlertActionLinkProps> = ({
1212
className = '',
13-
children = '',
13+
children,
1414
...props
1515
}: AlertActionLinkProps) => (
1616
<Button variant={ButtonVariant.link} className={className} {...props}>

packages/react-core/src/components/Alert/__tests__/Alert.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Object.values(AlertVariant).forEach(variant => {
4747
test('Action Close Button', () => {
4848
const onClose = jest.fn();
4949
const view = mount(
50-
<Alert variant={variant} action={<AlertActionCloseButton aria-label="Close" onClose={onClose} />} title="">
50+
<Alert variant={variant} action={<AlertActionCloseButton aria-label="Close" onClose={onClose} />} title={`Sample ${variant} alert`}>
5151
Some alert
5252
</Alert>
5353
);
Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,7 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`AlertActionCloseButton should match snapshot (auto-generated) 1`] = `
4-
<Component
5-
aria-label="''"
6-
onClick={[Function]}
7-
variant="plain"
8-
>
9-
<TimesIcon
10-
color="currentColor"
11-
noVerticalAlign={false}
12-
size="sm"
13-
title={null}
14-
/>
15-
</Component>
4+
<ContextConsumer>
5+
<Component />
6+
</ContextConsumer>
167
`;

0 commit comments

Comments
 (0)