Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: notification support duration #47141

Merged
merged 2 commits into from
Jan 24, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 48 additions & 2 deletions components/notification/__tests__/hooks.test.tsx
Original file line number Diff line number Diff line change
@@ -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();
});

Expand Down Expand Up @@ -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);
});
});
1 change: 1 addition & 0 deletions components/notification/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,5 @@ export interface NotificationConfig {
maxCount?: number;
rtl?: boolean;
stack?: boolean | { threshold?: number };
duration?: number;
}
3 changes: 2 additions & 1 deletion components/notification/useNotification.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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,
Expand Down