mirror of
https://github.com/ant-design/ant-design.git
synced 2024-11-24 02:59:58 +08:00
feat: notification support duration (#47141)
* feat: notication support duration * test: update test
This commit is contained in:
parent
2b9ced595f
commit
7664f39d8b
@ -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);
|
||||
});
|
||||
});
|
||||
|
@ -67,4 +67,5 @@ export interface NotificationConfig {
|
||||
maxCount?: number;
|
||||
rtl?: boolean;
|
||||
stack?: boolean | { threshold?: number };
|
||||
duration?: number;
|
||||
}
|
||||
|
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user