Skip to content
Open

fix #7484

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 24 additions & 20 deletions packages/antd/src/providers/notificationProvider/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,18 @@ describe("Antd useNotificationProvider", () => {
});

const notificationOpenSpy = vi.spyOn(notification, "open");
const notificationSuccessSpy = vi.spyOn(notification, "success");
const notificationErrorSpy = vi.spyOn(notification, "error");
const notificationCloseSpy = vi.spyOn(notification, "destroy");

it("should render notification type succes notification", async () => {
const { result } = renderHook(() => useNotificationProvider(), {});

result.current.open?.(mockNotification);

expect(notificationOpenSpy).toHaveBeenCalledTimes(1);
expect(notificationOpenSpy).toHaveBeenCalledWith({
...mockNotification,
expect(notificationSuccessSpy).toHaveBeenCalledTimes(1);
expect(notificationSuccessSpy).toHaveBeenCalledWith({
key: mockNotification.key,
message: null,
description: mockNotification.message,
});
Expand All @@ -45,12 +47,11 @@ describe("Antd useNotificationProvider", () => {
type: "error",
});

expect(notificationOpenSpy).toHaveBeenCalledTimes(1);
expect(notificationOpenSpy).toHaveBeenCalledWith({
...mockNotification,
expect(notificationErrorSpy).toHaveBeenCalledTimes(1);
expect(notificationErrorSpy).toHaveBeenCalledWith({
key: mockNotification.key,
message: null,
description: mockNotification.message,
type: "error",
});
});

Expand All @@ -62,9 +63,9 @@ describe("Antd useNotificationProvider", () => {
description: "Notification Description",
});

expect(notificationOpenSpy).toHaveBeenCalledTimes(1);
expect(notificationOpenSpy).toHaveBeenCalledWith({
...mockNotification,
expect(notificationSuccessSpy).toHaveBeenCalledTimes(1);
expect(notificationSuccessSpy).toHaveBeenCalledWith({
key: mockNotification.key,
message: "Notification Description",
description: "Test Notification Message",
});
Expand Down Expand Up @@ -109,12 +110,16 @@ describe("Antd useNotificationProvider", () => {

describe("using with Ant design's App component", () => {
const openFn = vi.fn();
const successFn = vi.fn();
const errorFn = vi.fn();
const destroyFn = vi.fn();

beforeAll(() => {
vi.spyOn(App, "useApp").mockReturnValue({
notification: {
open: openFn,
success: successFn,
error: errorFn,
destroy: destroyFn,
},
} as unknown as ReturnType<typeof App.useApp>);
Expand All @@ -132,9 +137,9 @@ describe("Antd useNotificationProvider", () => {
});

await waitFor(() => {
expect(openFn).toHaveBeenCalledTimes(1);
expect(openFn).toHaveBeenCalledWith({
...mockNotification,
expect(successFn).toHaveBeenCalledTimes(1);
expect(successFn).toHaveBeenCalledWith({
key: mockNotification.key,
message: null,
description: mockNotification.message,
});
Expand All @@ -152,12 +157,11 @@ describe("Antd useNotificationProvider", () => {
});

await waitFor(() => {
expect(openFn).toHaveBeenCalledTimes(1);
expect(openFn).toHaveBeenCalledWith({
...mockNotification,
expect(errorFn).toHaveBeenCalledTimes(1);
expect(errorFn).toHaveBeenCalledWith({
key: mockNotification.key,
message: null,
description: mockNotification.message,
type: "error",
});
});
});
Expand All @@ -173,9 +177,9 @@ describe("Antd useNotificationProvider", () => {
});

await waitFor(() => {
expect(openFn).toHaveBeenCalledTimes(1);
expect(openFn).toHaveBeenCalledWith({
...mockNotification,
expect(successFn).toHaveBeenCalledTimes(1);
expect(successFn).toHaveBeenCalledWith({
key: mockNotification.key,
message: "Notification Description",
description: "Test Notification Message",
});
Expand Down
25 changes: 19 additions & 6 deletions packages/antd/src/providers/notificationProvider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,25 @@ export const useNotificationProvider = (): NotificationProvider => {
closeIcon: <></>,
});
} else {
notification.open({
key,
description: message,
message: description ?? null,
type,
});
if (
type === "success" ||
type === "error" ||
type === "info" ||
type === "warning"
) {
notification[type]({
key,
description: message,
message: description ?? null,
});
} else {
notification.open({
key,
description: message,
message: description ?? null,
type,
});
}
}
},
close: (key) => notification.destroy(key),
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/contexts/notification/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export type SuccessErrorNotification<
export type OpenNotificationParams = {
key?: string;
message: string;
type: "success" | "error" | "progress";
type?: "success" | "error" | "progress" | "info" | "warning";
description?: string;
cancelMutation?: () => void;
undoableTimeout?: number;
Expand Down
57 changes: 41 additions & 16 deletions packages/mantine/src/providers/notificationProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ import {
hideNotification,
} from "@mantine/notifications";
import { ActionIcon, Box, Group, Text } from "@mantine/core";
import { IconCheck, IconRotate2, IconX } from "@tabler/icons-react";

import {
IconCheck,
IconRotate2,
IconX,
IconInfoCircle,
IconAlertCircle,
} from "@tabler/icons-react";
import { RingCountdown } from "@components";

export const useNotificationProvider = (): NotificationProvider => {
Expand Down Expand Up @@ -123,16 +128,41 @@ export const useNotificationProvider = (): NotificationProvider => {
});
}
} else {
const getIcon = (type?: "success" | "error" | "progress" | "info" | "warning") => {
switch (type) {
case "success":
return <IconCheck size={18} />;
case "error":
return <IconX size={18} />;
case "info":
return <IconInfoCircle size={18} />;
case "warning":
return <IconAlertCircle size={18} />;
default:
return undefined;
}
};

const getColor = (type?: "success" | "error" | "progress" | "info" | "warning") => {
switch (type) {
case "success":
return "primary";
case "error":
return "red";
case "info":
return "blue";
case "warning":
return "orange";
default:
return undefined;
}
};

if (isNotificationActive(key)) {
updateNotification({
id: key!,
color: type === "success" ? "primary" : "red",
icon:
type === "success" ? (
<IconCheck size={18} />
) : (
<IconX size={18} />
),
color: getColor(type),
icon: getIcon(type),
message,
title: description,
autoClose: 5000,
Expand All @@ -141,13 +171,8 @@ export const useNotificationProvider = (): NotificationProvider => {
addNotification(key);
showNotification({
id: key!,
color: type === "success" ? "primary" : "red",
icon:
type === "success" ? (
<IconCheck size={18} />
) : (
<IconX size={18} />
),
color: getColor(type),
icon: getIcon(type),
message,
title: description,
onClose: () => {
Expand Down