Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/kind-things-listen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@wpmudev/sui-notification": patch
---

fix(notification): rename `id` to `count` in notification push logic
5 changes: 5 additions & 0 deletions .changeset/nice-olives-create.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@wpmudev/sui-notification": patch
---

fix remove + add support for `count` property and implement removal by count
5 changes: 3 additions & 2 deletions packages/ui/notification/src/notification.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { useNotifications } from "./use-notification"
import { useStyles } from "@wpmudev/sui-hooks"

const Notification: React.FC<NotificationProps> = ({
count,
id,
title,
message = "message",
Expand All @@ -34,10 +35,10 @@ const Notification: React.FC<NotificationProps> = ({
useEffect(() => {
if (!isInline && !isDismissible) {
setTimeout(() => {
notifications.remove(id)
notifications.removeByCount(count)
}, timeout ?? 5000)
}
}, [id, isInline, notifications, timeout, isDismissible])
}, [id, count, isInline, notifications, timeout, isDismissible])

/**
* Hide notification when click on dismiss button
Expand Down
1 change: 1 addition & 0 deletions packages/ui/notification/src/notification.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ interface NotificationProps
extends SuiStyleType,
SuiHTMLAttributes<HTMLProps<HTMLDivElement>> {
id?: string // unique ID for the notification
count?: number // notification count
title?: ReactNode // title content of the notification (can be any valid React node)
message?: ReactNode // message content of the notification (can be any valid React node)
action?: ReactNode // notification action
Expand Down
11 changes: 9 additions & 2 deletions packages/ui/notification/src/use-notification.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,19 @@ let listeners: Function[] = []
const notificationStore = {
// function to push a new notification to the store
push: (options: NotificationProps) => {
notifications = [...notifications, { ...options, id: id++ }]
notifications = [...notifications, { ...options, count: id++ }]
emitChange()
},
// function to remove a notification from the store based on its ID
remove: (idStr: string | undefined) => {
notifications = notifications.filter(
(alert: any) => alert?.id !== (idStr ?? ""),
(alert: NotificationProps) => alert?.id !== (idStr ?? ""),
)
emitChange()
},
removeByCount: (count?: number) => {
notifications = notifications.filter(
(alert: NotificationProps) => alert?.count !== count,
)
emitChange()
},
Expand Down Expand Up @@ -50,6 +56,7 @@ const useNotifications = () => {
return {
push: notificationStore.push,
remove: notificationStore.remove,
removeByCount: notificationStore.removeByCount,
queue,
}
}
Expand Down
Loading