feat: notification support duration (#47141)

* feat: notication support duration

* test: update test
This commit is contained in:
二货爱吃白萝卜 2024-01-24 15:25:56 +08:00 committed by GitHub
parent 2b9ced595f
commit 7664f39d8b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 51 additions and 3 deletions

View File

@ -1,15 +1,18 @@
import React from 'react';
import { StyleProvider, createCache, extractStyle } from '@ant-design/cssinjs';
import { createCache, extractStyle, StyleProvider } from '@ant-design/cssinjs';
import notification from '..';
import { fireEvent, pureRender, render } from '../../../tests/utils';
import { act, fireEvent, pureRender, render } from '../../../tests/utils';
import ConfigProvider from '../../config-provider';
describe('notification.hooks', () => {
beforeEach(() => {
document.body.innerHTML = '';
jest.useFakeTimers();
});
afterEach(() => {
jest.clearAllTimers();
jest.useRealTimers();
});
@ -189,4 +192,47 @@ describe('notification.hooks', () => {
expect(document.querySelector('.ant-notification-stack')).toBeFalsy();
});
it('support duration', () => {
const Demo = () => {
const [api, holder] = notification.useNotification({ duration: 1.5 });
return (
<>
<a
onClick={() => {
api.info({
message: null,
description: 'test',
});
}}
>
Show
</a>
{holder}
</>
);
};
const { container } = render(<Demo />);
fireEvent.click(container.querySelector('a')!);
function getNoticeCount() {
return Array.from(document.querySelectorAll('.ant-notification-notice-wrapper')).filter(
(node) => !node.classList.contains('ant-notification-fade-leave'),
).length;
}
// Pass 1s
act(() => {
jest.advanceTimersByTime(1000);
});
expect(getNoticeCount()).toBe(1);
// Pass 2s
act(() => {
jest.advanceTimersByTime(1000);
});
expect(getNoticeCount()).toBe(0);
});
});

View File

@ -67,4 +67,5 @@ export interface NotificationConfig {
maxCount?: number;
rtl?: boolean;
stack?: boolean | { threshold?: number };
duration?: number;
}

View File

@ -64,6 +64,7 @@ const Holder = React.forwardRef<HolderRef, HolderProps>((props, ref) => {
rtl,
onAllRemoved,
stack,
duration,
} = props;
const { getPrefixCls, getPopupContainer, notification, direction } = useContext(ConfigContext);
const [, token] = useToken();
@ -87,7 +88,7 @@ const Holder = React.forwardRef<HolderRef, HolderProps>((props, ref) => {
motion: getNotificationMotion,
closable: true,
closeIcon: getCloseIcon(prefixCls),
duration: DEFAULT_DURATION,
duration: duration ?? DEFAULT_DURATION,
getContainer: () => staticGetContainer?.() || getPopupContainer?.() || document.body,
maxCount,
onAllRemoved,