From 01c4dc5db1b4d64babbeb33e6a2d2be043fabeeb Mon Sep 17 00:00:00 2001 From: Mathieu Suen Date: Fri, 11 Nov 2022 09:26:59 +0100 Subject: [PATCH] SONAR-17591 Fix keyboard arrow behavior in multi-line input --- .../security-hotspots/SecurityHotspotsApp.tsx | 4 +++ .../controls/UpDownKeyboardHandler.tsx | 6 ++-- .../controls/__tests__/Toggler-test.tsx | 34 ++++++++++++++----- .../main/js/helpers/keyboardEventHelpers.ts | 4 +++ 4 files changed, 37 insertions(+), 11 deletions(-) diff --git a/server/sonar-web/src/main/js/apps/security-hotspots/SecurityHotspotsApp.tsx b/server/sonar-web/src/main/js/apps/security-hotspots/SecurityHotspotsApp.tsx index beaccb76e96..e309c9a4af8 100644 --- a/server/sonar-web/src/main/js/apps/security-hotspots/SecurityHotspotsApp.tsx +++ b/server/sonar-web/src/main/js/apps/security-hotspots/SecurityHotspotsApp.tsx @@ -27,6 +27,7 @@ import withCurrentUserContext from '../../app/components/current-user/withCurren import { Location, Router, withRouter } from '../../components/hoc/withRouter'; import { getLeakValue } from '../../components/measure/utils'; import { getBranchLikeQuery, isPullRequest, isSameBranchLike } from '../../helpers/branch-like'; +import { isInput } from '../../helpers/keyboardEventHelpers'; import { KeyboardKeys } from '../../helpers/keycodes'; import { getStandards } from '../../helpers/security-standard'; import { BranchLike } from '../../types/branch-like'; @@ -147,6 +148,9 @@ export class SecurityHotspotsApp extends React.PureComponent { } handleKeyDown = (event: KeyboardEvent) => { + if (isInput(event)) { + return; + } if (event.key === KeyboardKeys.Alt) { event.preventDefault(); return; diff --git a/server/sonar-web/src/main/js/components/controls/UpDownKeyboardHandler.tsx b/server/sonar-web/src/main/js/components/controls/UpDownKeyboardHandler.tsx index faf70fc0ce6..874843ba02b 100644 --- a/server/sonar-web/src/main/js/components/controls/UpDownKeyboardHandler.tsx +++ b/server/sonar-web/src/main/js/components/controls/UpDownKeyboardHandler.tsx @@ -18,7 +18,7 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ import * as React from 'react'; -import { isShortcut } from '../../helpers/keyboardEventHelpers'; +import { isShortcut, isTextarea } from '../../helpers/keyboardEventHelpers'; import { KeyboardKeys } from '../../helpers/keycodes'; interface Props { @@ -47,7 +47,7 @@ export default class UpDownKeyboardHanlder extends React.PureComponent< } handleKeyboard = (event: KeyboardEvent) => { - if (isShortcut(event)) { + if (isShortcut(event) || isTextarea(event)) { return true; } switch (event.key) { @@ -67,7 +67,7 @@ export default class UpDownKeyboardHanlder extends React.PureComponent< getFocusableElement() { const { containerClass = 'popup' } = this.props; - const focussableElements = `.${containerClass} a,.${containerClass} button,.${containerClass} input[type=text]`; + const focussableElements = `.${containerClass} a,.${containerClass} button,.${containerClass} input[type=text],.${containerClass} textarea`; return document.querySelectorAll(focussableElements); } diff --git a/server/sonar-web/src/main/js/components/controls/__tests__/Toggler-test.tsx b/server/sonar-web/src/main/js/components/controls/__tests__/Toggler-test.tsx index 3b476d48c72..b21c0d013f4 100644 --- a/server/sonar-web/src/main/js/components/controls/__tests__/Toggler-test.tsx +++ b/server/sonar-web/src/main/js/components/controls/__tests__/Toggler-test.tsx @@ -36,6 +36,8 @@ const ui = { outButton: byRole('button', { name: 'out' }), overlayButton: byRole('button', { name: 'overlay' }), nextOverlayButton: byRole('button', { name: 'next overlay' }), + overlayTextarea: byRole('textbox'), + overlayLatButton: byRole('button', { name: 'last' }), }; async function openToggler(user: UserEvent) { @@ -55,25 +57,40 @@ function focusOut() { it('should handle key up/down', async () => { const user = userEvent.setup({ delay: null }); - const rerender = renderToggler(); + const rerender = renderToggler( + {}, + <> +