From d8cf4747b3729e4d3f6b5e207f24a22df2466381 Mon Sep 17 00:00:00 2001 From: Moksh Upadhyay Date: Wed, 2 Dec 2026 08:43:42 +0530 Subject: [PATCH] fix: distinguish numeric and string notification keys --- src/NotificationList/index.tsx | 20 ++++++------ src/hooks/useListPosition/index.ts | 6 +++- tests/hooks.test.tsx | 52 ++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 11 deletions(-) diff --git a/src/NotificationList/index.tsx b/src/NotificationList/index.tsx index 62bba2e5..e23f4abf 100644 --- a/src/NotificationList/index.tsx +++ b/src/NotificationList/index.tsx @@ -3,7 +3,7 @@ import type { CSSMotionProps } from '@rc-component/motion'; import { useComposeRef } from '@rc-component/util'; import { clsx } from 'clsx'; import * as React from 'react'; -import useListPosition from '../hooks/useListPosition'; +import useListPosition, { getNoticeKey } from '../hooks/useListPosition'; import useStack, { type StackConfig } from '../hooks/useStack'; import Notification, { type ComponentsType, @@ -80,8 +80,8 @@ function fillStyles(stylesList: (NotificationStyles | undefined)[]): Notificatio } function getIndex(keys: { key: React.Key }[], key: React.Key): number | undefined { - const strKey = String(key); - const index = keys.findIndex((item) => item.key === strKey); + const noticeKey = getNoticeKey(key); + const index = keys.findIndex((item) => item.key === noticeKey); if (index === -1) { return undefined; @@ -128,13 +128,13 @@ const NotificationListItem: React.FC = (props) => { ...restProps } = props; const { key, placement: itemPlacement, ...notificationConfig } = config; - const strKey = String(key); + const noticeKey = getNoticeKey(key); const setItemRef = React.useCallback( (node: HTMLDivElement | null) => { - setNodeSize(strKey, node); + setNodeSize(noticeKey, node); }, - [setNodeSize, strKey], + [setNodeSize, noticeKey], ); const ref = useComposeRef(nodeRef, setItemRef); @@ -184,7 +184,7 @@ const NotificationList: React.FC = (props) => { () => configList.map((config) => ({ config, - key: String(config.key), + key: getNoticeKey(config.key), })), [configList], ); @@ -278,14 +278,14 @@ const NotificationList: React.FC = (props) => { > {({ config, className: motionClassName, style: motionStyle }, nodeRef) => { const { key } = config; - const strKey = String(key); + const noticeKey = getNoticeKey(key); const notificationIndex = getIndex(keys, key); const stackInThreshold = stackEnabled && notificationIndex !== undefined && notificationIndex < threshold; return ( = (props) => { style={motionStyle} nodeRef={nodeRef} prefixCls={prefixCls} - offset={notificationPosition.get(strKey)} + offset={notificationPosition.get(noticeKey)} notificationIndex={notificationIndex} stackInThreshold={stackInThreshold} listHovering={listHovering} diff --git a/src/hooks/useListPosition/index.ts b/src/hooks/useListPosition/index.ts index 131c42f8..eeed5d9d 100644 --- a/src/hooks/useListPosition/index.ts +++ b/src/hooks/useListPosition/index.ts @@ -2,6 +2,10 @@ import * as React from 'react'; import type { StackConfig } from '../useStack'; import useSizes from './useSizes'; +export function getNoticeKey(key: React.Key): string { + return `${typeof key}-${key}`; +} + /** * Calculates each notification's position and the full list height. */ @@ -25,7 +29,7 @@ export default function useListPosition( .reverse() .forEach((config, index) => { // Walk from newest to oldest so each notice can be positioned after the ones below it. - const key = String(config.key); + const key = getNoticeKey(config.key); const height = sizeMap[key]?.height ?? 0; const y = stack && index > 0 ? offsetY + (stack.offset ?? 0) - height : offsetY; diff --git a/tests/hooks.test.tsx b/tests/hooks.test.tsx index a0bb650d..604a8f18 100644 --- a/tests/hooks.test.tsx +++ b/tests/hooks.test.tsx @@ -190,4 +190,56 @@ describe('Notification.Hooks', () => { expect(document.querySelector('.rc-notification')).toHaveClass('banana'); expect(document.querySelector('.custom-notice')).toHaveClass('apple'); }); + + it('distinguishes numeric and string keys with the same string representation', () => { + const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); + const offsetHeightSpy = vi + .spyOn(HTMLElement.prototype, 'offsetHeight', 'get') + .mockReturnValue(50); + + try { + const { instance, unmount } = renderDemo(); + + act(() => { + instance.open({ + key: 1, + duration: 0, + description: 'Numeric 1', + }); + instance.open({ + key: '1', + duration: 0, + description: 'String 1', + }); + }); + + const notices = Array.from(document.querySelectorAll('.rc-notification-notice')); + expect(notices).toHaveLength(2); + expect(notices.map((node) => node.textContent)).toEqual(['Numeric 1', 'String 1']); + expect(notices.map((node) => node.getAttribute('data-notification-index'))).toEqual([ + '1', + '0', + ]); + expect(notices.map((node) => node.style.getPropertyValue('--notification-y'))).toEqual([ + '50px', + '0px', + ]); + expect(errorSpy).not.toHaveBeenCalled(); + + act(() => { + instance.close(1); + }); + + const remaining = Array.from( + document.querySelectorAll('.rc-notification-notice'), + ); + expect(remaining).toHaveLength(1); + expect(remaining[0].textContent).toBe('String 1'); + + unmount(); + } finally { + offsetHeightSpy.mockRestore(); + errorSpy.mockRestore(); + } + }); });