Skip to content

Commit 902ccad

Browse files
committed
[issue-2847] use context instead of function to set aria text
1 parent ac911bb commit 902ccad

File tree

5 files changed

+62
-76
lines changed

5 files changed

+62
-76
lines changed

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

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,15 @@ export enum AlertVariant {
1515
default = 'default'
1616
}
1717

18-
export type CloseAlertAriaFormatter = (title: React.ReactNode, variantLabel: string) => React.ReactNode;
19-
2018
export interface AlertProps extends Omit<React.HTMLProps<HTMLDivElement>, 'action' | 'title'> {
2119
/** Adds Alert variant styles */
2220
variant?: 'success' | 'danger' | 'warning' | 'info' | 'default';
2321
/** Flag to indicate if the Alert is inline */
2422
isInline?: boolean;
2523
/** Title of the Alert */
2624
title: React.ReactNode;
27-
/** Action button to put in the Alert. Often <AlertActionLink> or <AlertActionCloseButton> */
28-
action?: CloseAlertAriaFormatter | React.ReactNode;
25+
/** Action button to put in the Alert. Should be <AlertActionLink> or <AlertActionCloseButton> */
26+
action?: React.ReactNode;
2927
/** Content rendered inside the Alert */
3028
children?: React.ReactNode;
3129
/** Additional classes added to the Alert */
@@ -38,22 +36,13 @@ export interface AlertProps extends Omit<React.HTMLProps<HTMLDivElement>, 'actio
3836
isLiveRegion?: boolean;
3937
}
4038

41-
export const closeBtnAriaTxt = (ariaLabel: string, variantLabel?: string, title?: string) => {
42-
if (ariaLabel) {
43-
return ariaLabel;
44-
} else if (variantLabel && title) {
45-
return `Close ${variantLabel}: ${title}`;
46-
} else if (title) {
47-
return `Close ${title}`;
48-
} else if (title === '' || !title) {
49-
// eslint-disable-next-line no-console
50-
console.warn(
51-
'Missing title prop: Please add a title prop that associates this close button to the Alert it closes.'
52-
);
53-
return `Close alert`;
54-
}
55-
return 'Close';
56-
};
39+
// eslint-disable-next-line @typescript-eslint/interface-name-prefix
40+
export interface IAlertContext {
41+
title: React.ReactNode;
42+
variantLabel?: string;
43+
}
44+
45+
export const AlertContext = React.createContext<IAlertContext>(null);
5746

5847
const Alert: React.FunctionComponent<AlertProps & InjectedOuiaProps> = ({
5948
variant = AlertVariant.info,
@@ -100,10 +89,9 @@ const Alert: React.FunctionComponent<AlertProps & InjectedOuiaProps> = ({
10089
<AlertIcon variant={variant} />
10190
<h4 className={css(styles.alertTitle)}>{getHeadingContent}</h4>
10291
{children && <div className={css(styles.alertDescription)}>{children}</div>}
103-
{action && typeof action === 'object' && <div className={css(styles.alertAction)}>{action}</div>}
104-
{action && typeof action === 'function' && (
105-
<div className={css(styles.alertAction)}>{action(title, variantLabel)}</div>
106-
)}
92+
<AlertContext.Provider value={{ title, variantLabel }}>
93+
{action && typeof action === 'object' && <div className={css(styles.alertAction)}>{action}</div>}
94+
</AlertContext.Provider>
10795
</div>
10896
);
10997
};
Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +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 { closeBtnAriaTxt } from '..';
4+
import { AlertContext } from '..';
55

66
interface AlertActionCloseButtonProps extends ButtonProps {
77
/** Additional classes added to the AlertActionCloseButton */
@@ -10,29 +10,25 @@ interface AlertActionCloseButtonProps extends ButtonProps {
1010
onClose?: () => void;
1111
/** Aria Label for the Close button */
1212
'aria-label'?: string;
13-
/** Variant Label for the Close button */
14-
variantLabel?: string;
15-
/** Accessible name of the Alert this close btn belongs to. It helps to use the title prop value of the
16-
* Alert as part of this title prop's text. This helps screen reader users associate the two when multiple
17-
* alerts are present at the same time. */
18-
title?: string;
1913
}
2014

2115
export const AlertActionCloseButton = ({
2216
// eslint-disable-next-line @typescript-eslint/no-unused-vars
2317
className = '',
2418
onClose = () => undefined as any,
2519
'aria-label': ariaLabel = '',
26-
title,
27-
variantLabel = '',
2820
...props
2921
}: AlertActionCloseButtonProps) => (
30-
<Button
31-
variant={ButtonVariant.plain}
32-
onClick={onClose}
33-
aria-label={closeBtnAriaTxt(ariaLabel, variantLabel, title)}
34-
{...props}
35-
>
36-
<TimesIcon />
37-
</Button>
22+
<AlertContext.Consumer>
23+
{({ title, variantLabel }) => (
24+
<Button
25+
variant={ButtonVariant.plain}
26+
onClick={onClose}
27+
aria-label={ariaLabel === '' ? `Close ${variantLabel} alert: ${title}` : ariaLabel}
28+
{...props}
29+
>
30+
<TimesIcon />
31+
</Button>
32+
)}
33+
</AlertContext.Consumer>
3834
);
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
`;

packages/patternfly-4/react-core/src/components/Alert/__tests__/__snapshots__/Alert.test.tsx.snap

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ exports[`Alert - danger Action Close Button 1`] = `
114114
<Component
115115
aria-label="Close"
116116
onClick={[MockFunction]}
117+
title="Sample danger alert"
117118
variant="plain"
118119
>
119120
<ComponentWithOuia
@@ -128,6 +129,7 @@ exports[`Alert - danger Action Close Button 1`] = `
128129
title={null}
129130
/>,
130131
"onClick": [MockFunction],
132+
"title": "Sample danger alert",
131133
"variant": "plain",
132134
}
133135
}
@@ -147,6 +149,7 @@ exports[`Alert - danger Action Close Button 1`] = `
147149
"ouiaId": null,
148150
}
149151
}
152+
title="Sample danger alert"
150153
variant="plain"
151154
>
152155
<button
@@ -156,6 +159,7 @@ exports[`Alert - danger Action Close Button 1`] = `
156159
disabled={false}
157160
onClick={[MockFunction]}
158161
tabIndex={null}
162+
title="Sample danger alert"
159163
type="button"
160164
>
161165
<TimesIcon
@@ -1110,6 +1114,7 @@ exports[`Alert - default Action Close Button 1`] = `
11101114
<Component
11111115
aria-label="Close"
11121116
onClick={[MockFunction]}
1117+
title="Sample default alert"
11131118
variant="plain"
11141119
>
11151120
<ComponentWithOuia
@@ -1124,6 +1129,7 @@ exports[`Alert - default Action Close Button 1`] = `
11241129
title={null}
11251130
/>,
11261131
"onClick": [MockFunction],
1132+
"title": "Sample default alert",
11271133
"variant": "plain",
11281134
}
11291135
}
@@ -1143,6 +1149,7 @@ exports[`Alert - default Action Close Button 1`] = `
11431149
"ouiaId": null,
11441150
}
11451151
}
1152+
title="Sample default alert"
11461153
variant="plain"
11471154
>
11481155
<button
@@ -1152,6 +1159,7 @@ exports[`Alert - default Action Close Button 1`] = `
11521159
disabled={false}
11531160
onClick={[MockFunction]}
11541161
tabIndex={null}
1162+
title="Sample default alert"
11551163
type="button"
11561164
>
11571165
<TimesIcon
@@ -2106,6 +2114,7 @@ exports[`Alert - info Action Close Button 1`] = `
21062114
<Component
21072115
aria-label="Close"
21082116
onClick={[MockFunction]}
2117+
title="Sample info alert"
21092118
variant="plain"
21102119
>
21112120
<ComponentWithOuia
@@ -2120,6 +2129,7 @@ exports[`Alert - info Action Close Button 1`] = `
21202129
title={null}
21212130
/>,
21222131
"onClick": [MockFunction],
2132+
"title": "Sample info alert",
21232133
"variant": "plain",
21242134
}
21252135
}
@@ -2139,6 +2149,7 @@ exports[`Alert - info Action Close Button 1`] = `
21392149
"ouiaId": null,
21402150
}
21412151
}
2152+
title="Sample info alert"
21422153
variant="plain"
21432154
>
21442155
<button
@@ -2148,6 +2159,7 @@ exports[`Alert - info Action Close Button 1`] = `
21482159
disabled={false}
21492160
onClick={[MockFunction]}
21502161
tabIndex={null}
2162+
title="Sample info alert"
21512163
type="button"
21522164
>
21532165
<TimesIcon
@@ -3102,6 +3114,7 @@ exports[`Alert - success Action Close Button 1`] = `
31023114
<Component
31033115
aria-label="Close"
31043116
onClick={[MockFunction]}
3117+
title="Sample success alert"
31053118
variant="plain"
31063119
>
31073120
<ComponentWithOuia
@@ -3116,6 +3129,7 @@ exports[`Alert - success Action Close Button 1`] = `
31163129
title={null}
31173130
/>,
31183131
"onClick": [MockFunction],
3132+
"title": "Sample success alert",
31193133
"variant": "plain",
31203134
}
31213135
}
@@ -3135,6 +3149,7 @@ exports[`Alert - success Action Close Button 1`] = `
31353149
"ouiaId": null,
31363150
}
31373151
}
3152+
title="Sample success alert"
31383153
variant="plain"
31393154
>
31403155
<button
@@ -3144,6 +3159,7 @@ exports[`Alert - success Action Close Button 1`] = `
31443159
disabled={false}
31453160
onClick={[MockFunction]}
31463161
tabIndex={null}
3162+
title="Sample success alert"
31473163
type="button"
31483164
>
31493165
<TimesIcon
@@ -4098,6 +4114,7 @@ exports[`Alert - warning Action Close Button 1`] = `
40984114
<Component
40994115
aria-label="Close"
41004116
onClick={[MockFunction]}
4117+
title="Sample warning alert"
41014118
variant="plain"
41024119
>
41034120
<ComponentWithOuia
@@ -4112,6 +4129,7 @@ exports[`Alert - warning Action Close Button 1`] = `
41124129
title={null}
41134130
/>,
41144131
"onClick": [MockFunction],
4132+
"title": "Sample warning alert",
41154133
"variant": "plain",
41164134
}
41174135
}
@@ -4131,6 +4149,7 @@ exports[`Alert - warning Action Close Button 1`] = `
41314149
"ouiaId": null,
41324150
}
41334151
}
4152+
title="Sample warning alert"
41344153
variant="plain"
41354154
>
41364155
<button
@@ -4140,6 +4159,7 @@ exports[`Alert - warning Action Close Button 1`] = `
41404159
disabled={false}
41414160
onClick={[MockFunction]}
41424161
tabIndex={null}
4162+
title="Sample warning alert"
41434163
type="button"
41444164
>
41454165
<TimesIcon

0 commit comments

Comments
 (0)