From 9a6452030fb4aecdc3dcb936a63b422efa34a552 Mon Sep 17 00:00:00 2001 From: Mehmet Yarar Date: Mon, 6 Mar 2023 19:51:30 +0600 Subject: [PATCH 1/3] fix: close notification with Enter key --- .eslintrc.js | 7 +++++++ src/Notice.tsx | 10 ++++++++++ tests/index.test.tsx | 23 +++++++++++++++++++++-- tsconfig.test.json | 4 ++++ 4 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 tsconfig.test.json 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..dd7e4a0 100644 --- a/src/Notice.tsx +++ b/src/Notice.tsx @@ -49,6 +49,14 @@ const Notify = React.forwardRef = closable + ? (e) => { + if (e.key === 'Enter' || e.code === 'Enter' || e.keyCode === 13) { + onInternalClose(); + } + } + : undefined; + // ======================== Effect ======================== React.useEffect(() => { if (!hovering && duration > 0) { @@ -60,6 +68,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"] +} From 1d9a9cdbe04460ee519056c3bd0f1eb18ae3b874 Mon Sep 17 00:00:00 2001 From: Mehmet Yarar Date: Tue, 7 Mar 2023 15:48:40 +0600 Subject: [PATCH 2/3] fix: use KeyCode from rc-util --- src/Notice.tsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Notice.tsx b/src/Notice.tsx index dd7e4a0..5411d84 100644 --- a/src/Notice.tsx +++ b/src/Notice.tsx @@ -1,4 +1,5 @@ import * as React from 'react'; +import KeyCode from 'rc-util/lib/KeyCode'; import classNames from 'classnames'; export interface NoticeConfig { @@ -51,7 +52,7 @@ const Notify = React.forwardRef = closable ? (e) => { - if (e.key === 'Enter' || e.code === 'Enter' || e.keyCode === 13) { + if (e.key === 'Enter' || e.code === 'Enter' || e.keyCode === KeyCode.ENTER) { onInternalClose(); } } From 9476372a458e8820d28eb75dd87844faebfb17be Mon Sep 17 00:00:00 2001 From: Mehmet Yarar Date: Thu, 9 Mar 2023 17:47:39 +0600 Subject: [PATCH 3/3] fix: code review fixes --- src/Notice.tsx | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/Notice.tsx b/src/Notice.tsx index 5411d84..5bfe373 100644 --- a/src/Notice.tsx +++ b/src/Notice.tsx @@ -1,6 +1,6 @@ -import * as React from 'react'; -import KeyCode from 'rc-util/lib/KeyCode'; import classNames from 'classnames'; +import KeyCode from 'rc-util/lib/KeyCode'; +import * as React from 'react'; export interface NoticeConfig { content?: React.ReactNode; @@ -50,13 +50,11 @@ const Notify = React.forwardRef = closable - ? (e) => { - if (e.key === 'Enter' || e.code === 'Enter' || e.keyCode === KeyCode.ENTER) { - onInternalClose(); - } - } - : undefined; + const onCloseKeyDown: React.KeyboardEventHandler = (e) => { + if (e.key === 'Enter' || e.code === 'Enter' || e.keyCode === KeyCode.ENTER) { + onInternalClose(); + } + }; // ======================== Effect ======================== React.useEffect(() => { @@ -99,7 +97,7 @@ const Notify = React.forwardRef { e.preventDefault(); e.stopPropagation();