-
Notifications
You must be signed in to change notification settings - Fork 376
Flexible alert actions #3822
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Flexible alert actions #3822
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,15 +14,23 @@ export enum AlertVariant { | |
| default = 'default' | ||
| } | ||
|
|
||
| export type AlertActionRenderer = ({ | ||
| title, | ||
| variantLabel | ||
| }: { | ||
| title: React.ReactNode; | ||
| variantLabel: string; | ||
| }) => React.ReactNode; | ||
|
|
||
| export interface AlertProps extends Omit<React.HTMLProps<HTMLDivElement>, 'action' | 'title'> { | ||
| /** Adds Alert variant styles */ | ||
| variant?: 'success' | 'danger' | 'warning' | 'info' | 'default'; | ||
| /** Flag to indicate if the Alert is inline */ | ||
| isInline?: boolean; | ||
| /** Title of the Alert */ | ||
| title: React.ReactNode; | ||
| /** Action button to put in the Alert. Should be <AlertActionLink> or <AlertActionCloseButton> */ | ||
| action?: React.ReactNode; | ||
| /** Action button to put in the Alert. Often <AlertActionLink> or <AlertActionCloseButton> */ | ||
| 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<AlertProps & OUIAProps> = ({ | |
| {action && (typeof action === 'object' || typeof action === 'string') && ( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps a better API would be to leave {React.isValidElement(action) && (
<div className={css(styles.alertAction)}>
{React.cloneElement(action as React.ReactElement, {
title,
variantLabel
})}
</div>
)}Consumers would then be able to use added <Alert action={({title, variantLabel}) => <div>{title}{variantLabel}</div>} />OR like this: class MyAlertAction extends React.Component {
render() {
return <div>{this.props.title}{this.props.variantLabel}</div>;
}
}
<Alert action={<MyAction />} /> |
||
| <div className={css(styles.alertAction)}>{action}</div> | ||
| )} | ||
| {action && typeof action === 'function' && ( | ||
| <div className={css(styles.alertAction)}>{action({ title, variantLabel })}</div> | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand, couldn't this be |
||
| )} | ||
| </AlertContext.Provider> | ||
| </div> | ||
| ); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,7 @@ import { Button, ButtonVariant, ButtonProps } from '../Button'; | |
|
|
||
| export interface AlertActionLinkProps extends ButtonProps { | ||
| /** Content rendered inside the AlertLinkAction */ | ||
| children?: string; | ||
| children: string; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be ReactNode so it could work with i18n libraries for example |
||
| /** Additional classes added to the AlertActionLink */ | ||
| className?: string; | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't this really
React.FunctionComponent<{ title: React.ReactNode, variantLabel: string }>?