diff --git a/packages/react-core/src/components/Alert/Alert.tsx b/packages/react-core/src/components/Alert/Alert.tsx index 33292e63e40..5997d355666 100644 --- a/packages/react-core/src/components/Alert/Alert.tsx +++ b/packages/react-core/src/components/Alert/Alert.tsx @@ -14,6 +14,14 @@ export enum AlertVariant { default = 'default' } +export type AlertActionRenderer = ({ + title, + variantLabel +}: { + title: React.ReactNode; + variantLabel: string; +}) => React.ReactNode; + export interface AlertProps extends Omit, 'action' | 'title'> { /** Adds Alert variant styles */ variant?: 'success' | 'danger' | 'warning' | 'info' | 'default'; @@ -21,8 +29,8 @@ export interface AlertProps extends Omit, 'actio isInline?: boolean; /** Title of the Alert */ title: React.ReactNode; - /** Action button to put in the Alert. Should be or */ - action?: React.ReactNode; + /** Action button to put in the Alert. Often or */ + action?: AlertActionRenderer | React.ReactNode; /** Content rendered inside the Alert */ children?: React.ReactNode; /** Additional classes added to the Alert */ @@ -80,6 +88,9 @@ export const Alert: React.FunctionComponent = ({ {action && (typeof action === 'object' || typeof action === 'string') && (
{action}
)} + {action && typeof action === 'function' && ( +
{action({ title, variantLabel })}
+ )} ); diff --git a/packages/react-core/src/components/Alert/AlertActionCloseButton.tsx b/packages/react-core/src/components/Alert/AlertActionCloseButton.tsx index 023edb31001..9f162a4b74e 100644 --- a/packages/react-core/src/components/Alert/AlertActionCloseButton.tsx +++ b/packages/react-core/src/components/Alert/AlertActionCloseButton.tsx @@ -10,8 +10,6 @@ interface AlertActionCloseButtonProps extends ButtonProps { onClose?: () => void; /** Aria Label for the Close button */ 'aria-label'?: string; - /** Variant Label for the Close button */ - variantLabel?: string; } export const AlertActionCloseButton = ({ @@ -19,7 +17,6 @@ export const AlertActionCloseButton = ({ className = '', onClose = () => undefined as any, 'aria-label': ariaLabel = '', - variantLabel, ...props }: AlertActionCloseButtonProps) => ( @@ -27,7 +24,7 @@ export const AlertActionCloseButton = ({ + }}>This alert uses a customized action button ); } diff --git a/packages/react-integration/cypress/integration/alert.spec.ts b/packages/react-integration/cypress/integration/alert.spec.ts index 3734a243e8c..9ddb2916e2d 100644 --- a/packages/react-integration/cypress/integration/alert.spec.ts +++ b/packages/react-integration/cypress/integration/alert.spec.ts @@ -22,4 +22,8 @@ describe('Alert Demo Test', () => { cy.get('#test-button-2').click(); cy.get('#info-alert').should('not.exist'); }); + + it('Verify action render prop', () => { + cy.get('#custom-action-alert-button').contains('Default alert: Custom action alert'); + }); }); diff --git a/packages/react-integration/demo-app-ts/src/components/demos/AlertDemo/AlertDemo.tsx b/packages/react-integration/demo-app-ts/src/components/demos/AlertDemo/AlertDemo.tsx index 6663f8c77a5..bbbcebcb7c6 100644 --- a/packages/react-integration/demo-app-ts/src/components/demos/AlertDemo/AlertDemo.tsx +++ b/packages/react-integration/demo-app-ts/src/components/demos/AlertDemo/AlertDemo.tsx @@ -1,4 +1,4 @@ -import { Alert, AlertActionCloseButton } from '@patternfly/react-core'; +import { Alert, AlertActionCloseButton, Button } from '@patternfly/react-core'; import React from 'react'; interface AlertDemoState { @@ -47,6 +47,15 @@ export class AlertDemo extends React.Component { Info alert description + ( + + )} + /> ); }