Skip to content

Commit 41ed8d4

Browse files
committed
feat(alert): allow for actions render prop, make children required for alertactionlink
1 parent fbf6e1c commit 41ed8d4

File tree

8 files changed

+525
-43
lines changed

8 files changed

+525
-43
lines changed

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,23 @@ export enum AlertVariant {
1414
default = 'default'
1515
}
1616

17+
export type AlertActionRenderer = ({
18+
title,
19+
variantLabel
20+
}: {
21+
title: React.ReactNode;
22+
variantLabel: string;
23+
}) => React.ReactNode;
24+
1725
export interface AlertProps extends Omit<React.HTMLProps<HTMLDivElement>, 'action' | 'title'> {
1826
/** Adds Alert variant styles */
1927
variant?: 'success' | 'danger' | 'warning' | 'info' | 'default';
2028
/** Flag to indicate if the Alert is inline */
2129
isInline?: boolean;
2230
/** Title of the Alert */
2331
title: React.ReactNode;
24-
/** Action button to put in the Alert. Should be <AlertActionLink> or <AlertActionCloseButton> */
25-
action?: React.ReactNode;
32+
/** Action button to put in the Alert. Often <AlertActionLink> or <AlertActionCloseButton> */
33+
action?: AlertActionRenderer | React.ReactNode;
2634
/** Content rendered inside the Alert */
2735
children?: React.ReactNode;
2836
/** Additional classes added to the Alert */
@@ -80,6 +88,9 @@ export const Alert: React.FunctionComponent<AlertProps & OUIAProps> = ({
8088
{action && (typeof action === 'object' || typeof action === 'string') && (
8189
<div className={css(styles.alertAction)}>{action}</div>
8290
)}
91+
{action && typeof action === 'function' && (
92+
<div className={css(styles.alertAction)}>{action({ title, variantLabel })}</div>
93+
)}
8394
</AlertContext.Provider>
8495
</div>
8596
);

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,21 @@ 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;
1513
}
1614

1715
export const AlertActionCloseButton = ({
1816
// eslint-disable-next-line @typescript-eslint/no-unused-vars
1917
className = '',
2018
onClose = () => undefined as any,
2119
'aria-label': ariaLabel = '',
22-
variantLabel,
2320
...props
2421
}: AlertActionCloseButtonProps) => (
2522
<AlertContext.Consumer>
2623
{({ title, variantLabel: alertVariantLabel }) => (
2724
<Button
2825
variant={ButtonVariant.plain}
2926
onClick={onClose}
30-
aria-label={ariaLabel === '' ? `Close ${variantLabel || alertVariantLabel} alert: ${title}` : ariaLabel}
27+
aria-label={ariaLabel === '' ? `Close ${alertVariantLabel} alert: ${title}` : ariaLabel}
3128
{...props}
3229
>
3330
<TimesIcon />

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Button, ButtonVariant, ButtonProps } from '../Button';
33

44
export interface AlertActionLinkProps extends ButtonProps {
55
/** Content rendered inside the AlertLinkAction */
6-
children?: string;
6+
children: string;
77
/** Additional classes added to the AlertActionLink */
88
className?: string;
99
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,20 @@ Object.values(AlertVariant).forEach(variant => {
8383
expect(view).toMatchSnapshot();
8484
});
8585

86+
test('Custom action', () => {
87+
const view = mount(
88+
<Alert
89+
variant={variant}
90+
aria-label={`Custom aria label for ${variant}`}
91+
action={({variantLabel, title}) => (<>{variantLabel} {title}</>)}
92+
title="Some title"
93+
>
94+
Some alert
95+
</Alert>
96+
);
97+
expect(view).toMatchSnapshot();
98+
});
99+
86100
test('inline variation', () => {
87101
const view = mount(
88102
<Alert variant={variant} isInline title="Some title">

0 commit comments

Comments
 (0)