diff --git a/.eslintrc.js b/.eslintrc.js index 1979492..d0598e2 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -9,4 +9,11 @@ module.exports = { 'react/require-default-props': 0, 'jsx-a11y/no-noninteractive-tabindex': 0, }, + overrides: [ + { + // 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 + files: ['tests/*.test.tsx'], + parserOptions: { project: './tsconfig.test.json' }, + }, + ], }; diff --git a/src/Notice.tsx b/src/Notice.tsx index d279f8d..5bfe373 100644 --- a/src/Notice.tsx +++ b/src/Notice.tsx @@ -1,5 +1,6 @@ -import * as React from 'react'; import classNames from 'classnames'; +import KeyCode from 'rc-util/lib/KeyCode'; +import * as React from 'react'; export interface NoticeConfig { content?: React.ReactNode; @@ -49,6 +50,12 @@ const Notify = React.forwardRef = (e) => { + if (e.key === 'Enter' || e.code === 'Enter' || e.keyCode === KeyCode.ENTER) { + onInternalClose(); + } + }; + // ======================== Effect ======================== React.useEffect(() => { if (!hovering && duration > 0) { @@ -60,6 +67,7 @@ const Notify = React.forwardRef { e.preventDefault(); e.stopPropagation(); diff --git a/tests/index.test.tsx b/tests/index.test.tsx index f865fc8..1922846 100644 --- a/tests/index.test.tsx +++ b/tests/index.test.tsx @@ -1,8 +1,8 @@ +import { fireEvent, render } from '@testing-library/react'; import React from 'react'; -import { render, fireEvent } from '@testing-library/react'; import { act } from 'react-dom/test-utils'; -import { useNotification } from '../src'; import type { NotificationAPI, NotificationConfig } from '../src'; +import { useNotification } from '../src'; require('../assets/index.less'); @@ -587,6 +587,7 @@ describe('Notification.Basic', () => { }); expect(onAllRemoved).toHaveBeenCalled(); }); + it('when the same key message is closing, dont open new until it closed', () => { const onClose = jest.fn(); const Demo = () => { @@ -629,4 +630,22 @@ describe('Notification.Basic', () => { unmount(); }); + + it('closes via keyboard Enter key', () => { + const { instance } = renderDemo(); + let closeCount = 0; + + act(() => { + instance.open({ + content:

1

, + closable: true, + onClose: () => { + closeCount += 1; + }, + }); + }); + + fireEvent.keyDown(document.querySelector('.rc-notification-notice-close'), { key: 'Enter' }); // origin latest + expect(closeCount).toEqual(1); + }); }); diff --git a/tsconfig.test.json b/tsconfig.test.json new file mode 100644 index 0000000..15c0e03 --- /dev/null +++ b/tsconfig.test.json @@ -0,0 +1,4 @@ +{ + "extends": "./tsconfig.json", + "include": ["./tests"] +}