Skip to content
Open
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
20 changes: 10 additions & 10 deletions src/NotificationList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -128,13 +128,13 @@ const NotificationListItem: React.FC<NotificationListItemProps> = (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);

Expand Down Expand Up @@ -184,7 +184,7 @@ const NotificationList: React.FC<NotificationListProps> = (props) => {
() =>
configList.map((config) => ({
config,
key: String(config.key),
key: getNoticeKey(config.key),
})),
[configList],
);
Expand Down Expand Up @@ -278,14 +278,14 @@ const NotificationList: React.FC<NotificationListProps> = (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 (
<NotificationListItem
key={key}
key={noticeKey}
config={config}
components={components}
contextClassNames={contextClassNames}
Expand All @@ -295,7 +295,7 @@ const NotificationList: React.FC<NotificationListProps> = (props) => {
style={motionStyle}
nodeRef={nodeRef}
prefixCls={prefixCls}
offset={notificationPosition.get(strKey)}
offset={notificationPosition.get(noticeKey)}
notificationIndex={notificationIndex}
stackInThreshold={stackInThreshold}
listHovering={listHovering}
Expand Down
6 changes: 5 additions & 1 deletion src/hooks/useListPosition/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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;

Expand Down
52 changes: 52 additions & 0 deletions tests/hooks.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<HTMLElement>('.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<HTMLElement>('.rc-notification-notice'),
);
expect(remaining).toHaveLength(1);
expect(remaining[0].textContent).toBe('String 1');

unmount();
} finally {
offsetHeightSpy.mockRestore();
errorSpy.mockRestore();
}
});
});
Loading