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 1 commit
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
35 changes: 33 additions & 2 deletions components/notification/__tests__/hooks.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
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', () => {
Expand All @@ -10,6 +11,7 @@ describe('notification.hooks', () => {
});

afterEach(() => {
jest.clearAllTimers();
jest.useRealTimers();
});

Expand Down Expand Up @@ -189,4 +191,33 @@ describe('notification.hooks', () => {

expect(document.querySelector('.ant-notification-stack')).toBeFalsy();
});

it('support duration', () => {
const Demo = () => {
const [api, holder] = notification.useNotification({ duration: 1.5 });

React.useEffect(() => {
api.info({
message: null,
description: 'test',
});
}, []);

return holder;
};

render(<Demo />);

// Pass 1s
act(() => {
jest.advanceTimersByTime(1000);
});
expect(document.querySelector('.ant-notification-notice')).toBeTruthy();
zombieJ marked this conversation as resolved.
Show resolved Hide resolved

// Pass 2s
act(() => {
jest.advanceTimersByTime(1000);
});
expect(document.querySelector('.ant-notification-notice')).toBeFalsy();
});
});
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