Skip to content

Commit

Permalink
fix(ToastNotification): fix typo in prop name
Browse files Browse the repository at this point in the history
- add in dismissType
- add in tests and console messages so make sure the new field falls back
  • Loading branch information
booc0mtaco committed Jan 3, 2025
1 parent d77ae83 commit 8f75d15
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const NotDismissable: Story = {
export const AutoDismiss: Story = {
args: {
...Default.args,
dissmissType: 'auto',
dismissType: 'auto',
timeout: 500,
onDismiss: () => console.log('trigger onDismiss'),
},
Expand Down Expand Up @@ -111,7 +111,7 @@ const ToastNotificationManager = (args: Args) => {
>
<ToastNotification
{...args}
dissmissType="auto"
dismissType="auto"
onDismiss={() => {
setToasts(
toasts.map((thisToast) => {
Expand Down
14 changes: 13 additions & 1 deletion src/components/ToastNotification/ToastNotification.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,22 @@ describe('<ToastNotification />', () => {
it('generates an error when onDissmiss and type=auto are misused', async () => {
// One must use onDismiss if auto is used
render(
<ToastNotification dissmissType="auto" timeout={50} title="test" />,
<ToastNotification dismissType="auto" timeout={50} title="test" />,
);

await waitFor(() => expect(consoleErrorMock).toHaveBeenCalledTimes(1));
});

it('[EDS-1453] generates an error message when using the dissmissType prop', async () => {
render(<ToastNotification dissmissType="manual" title="test" />);

await waitFor(() => expect(consoleErrorMock).toHaveBeenCalledTimes(1));
});

it('[EDS-1453] generates no error when using the correct dismissType prop', async () => {
render(<ToastNotification dismissType="manual" title="test" />);

await waitFor(() => expect(consoleErrorMock).toHaveBeenCalledTimes(0));
});
});
});
27 changes: 22 additions & 5 deletions src/components/ToastNotification/ToastNotification.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ export type ToastNotificationProps = {
*
* **Default is `"manual"`**.
*/
dissmissType?: 'manual' | 'auto';
dismissType?: 'manual' | 'auto';
dissmissType?: 'manual' | 'auto'; // TODO(next-major): remove this misspelled prop at next major release
/**
* Keyword to characterize the state of the notification
*/
Expand All @@ -49,7 +50,8 @@ export type ToastNotificationProps = {
*/
export const ToastNotification = ({
className,
dissmissType = 'manual',
dismissType = 'manual',
dissmissType,
onDismiss,
status = 'favorable',
timeout = 8000,
Expand All @@ -62,22 +64,37 @@ export const ToastNotification = ({
className,
);

// if both are defined, temporarily prefer the original (misspelled) prop for now, but throw a warning
const tempDismissType =
dissmissType && dismissType ? dissmissType : dismissType;

assertEdsUsage(
[!!timeout && typeof onDismiss === 'undefined' && dissmissType === 'auto'],
[
!!timeout &&
typeof onDismiss === 'undefined' &&
tempDismissType === 'auto',
],
'When using dismissType=auto, an onDismiss method must be defined',
'error',
);

// TODO(next-major): remove this misspelled prop at next major release
assertEdsUsage(
[typeof dissmissType !== 'undefined'],
'`dissmissType` should not be used. Use `dismissType` instead',
'error',
);

useEffect(() => {
const expireId =
dissmissType === 'auto'
tempDismissType === 'auto'
? setTimeout(() => {
onDismiss && onDismiss();
}, timeout)
: undefined;

return () => clearTimeout(expireId);
}, [onDismiss, dissmissType, timeout]);
}, [onDismiss, tempDismissType, timeout]);

return (
<div className={componentClassName} {...other}>
Expand Down

0 comments on commit 8f75d15

Please sign in to comment.