fix(ui-components): make Alert not dismissable (#45291)

This commit is contained in:
Huyen Nguyen
2022-03-01 22:07:53 +07:00
committed by GitHub
parent e8ec209431
commit 686e9f6233
4 changed files with 7 additions and 61 deletions

View File

@ -7,8 +7,7 @@ const story = {
component: Alert, component: Alert,
argTypes: { argTypes: {
children: { control: { type: 'text' } }, children: { control: { type: 'text' } },
className: { control: { type: 'text' } }, className: { control: { type: 'text' } }
dismissLabel: { control: { type: 'text' } }
} }
}; };
@ -67,12 +66,4 @@ WithHeadingAndParagraphs.args = {
) )
}; };
export const Dismissable = Template.bind({});
Dismissable.args = {
children: 'Hello, Alert!',
variant: 'success',
dismissLabel: 'Close alert',
onDismiss: () => console.log('Close alert!')
};
export default story; export default story;

View File

@ -1,5 +1,4 @@
import { render, screen } from '@testing-library/react'; import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import React from 'react'; import React from 'react';
import { Alert } from './alert'; import { Alert } from './alert';
@ -31,41 +30,4 @@ describe('<Alert>', () => {
expect(screen.getByRole('alert')).toHaveClass(expectedClass); expect(screen.getByRole('alert')).toHaveClass(expectedClass);
}); });
it(`renders a button when "onDismiss" prop is present, and calls "onDismiss" when the button is clicked`, () => {
const onDismiss = jest.fn();
render(
<Alert onDismiss={onDismiss} variant='info'>
Hello
</Alert>
);
const closeButton = screen.getByRole('button');
expect(screen.getByRole('alert')).toBeInTheDocument();
expect(closeButton).toBeInTheDocument();
userEvent.click(closeButton);
expect(onDismiss).toHaveBeenCalledTimes(1);
});
it('does NOT render a close button, when "onDismiss" prop is NOT used', () => {
render(<Alert variant='info'>Hello</Alert>);
expect(screen.getByRole('alert')).toBeInTheDocument();
expect(screen.queryByRole('button')).not.toBeInTheDocument();
});
it('sets "aria-label" of close button to "dismissText" prop', () => {
const expectedLabel = 'custom dismiss alert message';
render(
<Alert dismissLabel={expectedLabel} onDismiss={jest.fn()} variant='info'>
Hello
</Alert>
);
expect(
screen.getByRole('button', { name: expectedLabel })
).toBeInTheDocument();
});
}); });

View File

@ -1,5 +1,4 @@
import React from 'react'; import React from 'react';
import { CloseButton } from '../close-button';
type AlertVariant = 'success' | 'info' | 'warning' | 'danger'; type AlertVariant = 'success' | 'info' | 'warning' | 'danger';
@ -7,8 +6,6 @@ export interface AlertProps {
children: React.ReactNode; children: React.ReactNode;
className?: string; className?: string;
variant: AlertVariant; variant: AlertVariant;
dismissLabel?: string;
onDismiss?: () => void;
} }
const variantClasses: Record<AlertVariant, string> = { const variantClasses: Record<AlertVariant, string> = {
@ -19,30 +16,26 @@ const variantClasses: Record<AlertVariant, string> = {
}; };
/** /**
* Basic UI component that provides contextual feedback * `Alert` is used to display a short, important message that does not interrupt the user's workflow.
*
* `Alert` is not dismissable.
*/ */
export function Alert({ export function Alert({
children, children,
className, className,
variant, variant
dismissLabel = 'Close',
onDismiss
}: AlertProps): JSX.Element { }: AlertProps): JSX.Element {
const isDismissable = !!onDismiss;
const variantClass = variantClasses[variant]; const variantClass = variantClasses[variant];
const classes = [ const classes = [
'flex items-start p-4 mb-6 border border-transparent break-words', 'p-4 mb-6 border border-transparent break-words',
variantClass, variantClass,
className className
].join(' '); ].join(' ');
return ( return (
<div className={classes} role='alert'> <div className={classes} role='alert'>
<div className='grow'>{children}</div> {children}
{isDismissable && (
<CloseButton label={dismissLabel} onClick={onDismiss} />
)}
</div> </div>
); );
} }