Skip to content

Commit b6690c3

Browse files
authored
feat(ToastNotification): Add onDismiss callback to ToastNotificationProvider (#1236)
- Added `onDismiss` callback as an optional prop to ToastNotificationProvider - (driveby fix) Added `notifications` as a prop to `onDismiss` callback in `handleGroupedDismiss` - Previously, dismissing all notifications would only remove them from context, and the notifications were not provided to the `onDismiss` callback, so nothing would happen.
1 parent 20635b6 commit b6690c3

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

src/components/Notifications/ToastNotification/ToastNotification.test.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,4 +170,21 @@ describe("ToastNotification", () => {
170170
expect(screen.queryByText("Success")).not.toBeInTheDocument();
171171
expect(screen.queryByText("Failure")).not.toBeInTheDocument();
172172
});
173+
174+
it("calls onDismiss for an unfiltered list of notifications", async () => {
175+
const onDismiss = jest.fn();
176+
177+
render(
178+
<ToastNotificationList
179+
notifications={notifications}
180+
onDismiss={onDismiss}
181+
groupedCount={groupedCount}
182+
show={true}
183+
/>,
184+
);
185+
186+
await userEvent.click(screen.getByRole("button", { name: "Dismiss all" }));
187+
188+
expect(onDismiss).toHaveBeenCalledWith(notifications);
189+
});
173190
});

src/components/Notifications/ToastNotification/ToastNotificationList.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ const ToastNotificationList: FC<Props> = ({
106106
return;
107107
}
108108

109-
onDismiss();
109+
onDismiss(notifications);
110110
};
111111

112112
const getSeverityFilters = () => {

src/components/Notifications/ToastNotification/ToastNotificationProvider.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ export type ToastNotificationType = NotificationType & {
1818
id: string;
1919
};
2020

21+
interface Props {
22+
onDismiss?: (notifications?: ToastNotificationType[]) => void;
23+
}
24+
2125
interface ToastNotificationHelper {
2226
notifications: ToastNotificationType[];
2327
success: (
@@ -148,7 +152,10 @@ console.log(count.positive);
148152
Alternatively, you can use the `ToastNotification` and `ToastNotificationList` components directly, without using the provider.
149153
*/
150154

151-
const ToastNotificationProvider: FC<PropsWithChildren> = ({ children }) => {
155+
const ToastNotificationProvider: FC<PropsWithChildren<Props>> = ({
156+
children,
157+
onDismiss,
158+
}) => {
152159
const [notifications, setNotifications] = useState<ToastNotificationType[]>(
153160
[],
154161
);
@@ -210,6 +217,10 @@ const ToastNotificationProvider: FC<PropsWithChildren> = ({ children }) => {
210217
};
211218

212219
const clear = (notifications?: ToastNotificationType[]) => {
220+
if (onDismiss) {
221+
onDismiss(notifications);
222+
}
223+
213224
if (!notifications) {
214225
setNotifications([]);
215226
setShowList(false);

0 commit comments

Comments
 (0)