Skip to content

Commit 5b12c7a

Browse files
committed
fix: close notification with Enter key
1 parent 30782cc commit 5b12c7a

File tree

4 files changed

+42
-3
lines changed

4 files changed

+42
-3
lines changed

.eslintrc.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,11 @@ module.exports = {
99
'react/require-default-props': 0,
1010
'jsx-a11y/no-noninteractive-tabindex': 0,
1111
},
12+
overrides: [
13+
{
14+
// https://typescript-eslint.io/linting/troubleshooting/#i-get-errors-telling-me-eslint-was-configured-to-run--however-that-tsconfig-does-not--none-of-those-tsconfigs-include-this-file
15+
files: ['tests/*.test.tsx'],
16+
parserOptions: { project: './tsconfig.test.json' },
17+
},
18+
],
1219
};

src/Notice.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import * as React from 'react';
2-
import classNames from 'classnames';
32

43
export interface NoticeConfig {
54
content?: React.ReactNode;
@@ -49,6 +48,14 @@ const Notify = React.forwardRef<HTMLDivElement, NoticeProps & { times?: number }
4948
onNoticeClose(eventKey);
5049
};
5150

51+
const handleCloseKeydown: React.KeyboardEventHandler<HTMLAnchorElement> = closable
52+
? (e) => {
53+
if (e.key === 'Enter' || e.code === 'Enter' || e.keyCode === 13) {
54+
onInternalClose();
55+
}
56+
}
57+
: undefined;
58+
5259
// ======================== Effect ========================
5360
React.useEffect(() => {
5461
if (!hovering && duration > 0) {
@@ -60,6 +67,7 @@ const Notify = React.forwardRef<HTMLDivElement, NoticeProps & { times?: number }
6067
clearTimeout(timeout);
6168
};
6269
}
70+
// eslint-disable-next-line react-hooks/exhaustive-deps
6371
}, [duration, hovering, times]);
6472

6573
// ======================== Render ========================
@@ -89,6 +97,7 @@ const Notify = React.forwardRef<HTMLDivElement, NoticeProps & { times?: number }
8997
<a
9098
tabIndex={0}
9199
className={`${noticePrefixCls}-close`}
100+
onKeyDown={handleCloseKeydown}
92101
onClick={(e) => {
93102
e.preventDefault();
94103
e.stopPropagation();

tests/index.test.tsx

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import { fireEvent, render } from '@testing-library/react';
12
import React from 'react';
2-
import { render, fireEvent } from '@testing-library/react';
33
import { act } from 'react-dom/test-utils';
4-
import { useNotification } from '../src';
54
import type { NotificationAPI, NotificationConfig } from '../src';
5+
import { useNotification } from '../src';
66

77
require('../assets/index.less');
88

@@ -587,6 +587,7 @@ describe('Notification.Basic', () => {
587587
});
588588
expect(onAllRemoved).toHaveBeenCalled();
589589
});
590+
590591
it('when the same key message is closing, dont open new until it closed', () => {
591592
const onClose = jest.fn();
592593
const Demo = () => {
@@ -629,4 +630,22 @@ describe('Notification.Basic', () => {
629630

630631
unmount();
631632
});
633+
634+
it('closes via keyboard Enter key', () => {
635+
const { instance } = renderDemo();
636+
let closeCount = 0;
637+
638+
act(() => {
639+
instance.open({
640+
content: <p className="test">1</p>,
641+
closable: true,
642+
onClose: () => {
643+
closeCount += 1;
644+
},
645+
});
646+
});
647+
648+
fireEvent.keyDown(document.querySelector('.rc-notification-notice-close'), { key: 'Enter' }); // origin latest
649+
expect(closeCount).toEqual(1);
650+
});
632651
});

tsconfig.test.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"include": ["./tests"]
4+
}

0 commit comments

Comments
 (0)