]> source.dussan.org Git - sonarqube.git/commitdiff
[NO JIRA] Changing window to document for EventListerner
authorGuillaume Peoc'h <guillaume.peoch@sonarsource.com>
Wed, 8 Jun 2022 15:48:30 +0000 (17:48 +0200)
committersonartech <sonartech@sonarsource.com>
Tue, 14 Jun 2022 20:02:51 +0000 (20:02 +0000)
server/sonar-web/src/main/js/app/components/KeyboardShortcutsModal.tsx
server/sonar-web/src/main/js/app/components/__tests__/KeyboardShortcutsModal-test.tsx
server/sonar-web/src/main/js/apps/security-hotspots/components/HotspotViewerTabs.tsx
server/sonar-web/src/main/js/helpers/keycodes.ts

index 1a92d7b6551f0d2beea559c9c09fa51a531ab063..03b486af05433243388f035f2db7d3358bd6660b 100644 (file)
@@ -20,6 +20,8 @@
 import * as React from 'react';
 import { Button } from '../../components/controls/buttons';
 import Modal from '../../components/controls/Modal';
+import { isInput } from '../../helpers/keyboardEventHelpers';
+import { KeyboardKeys } from '../../helpers/keycodes';
 import { translate } from '../../helpers/l10n';
 
 type Shortcuts = Array<{
@@ -129,22 +131,20 @@ export default function KeyboardShortcutsModal() {
   const [display, setDisplay] = React.useState(false);
 
   React.useEffect(() => {
-    const handleKeyPress = (event: KeyboardEvent) => {
-      const { tagName } = event.target as HTMLElement;
-
-      if (['INPUT', 'SELECT', 'TEXTAREA'].includes(tagName)) {
-        return; // Ignore keys when typed in an input
+    const handleKeyDown = (event: KeyboardEvent) => {
+      if (isInput(event)) {
+        return true;
       }
 
-      if (event.key === '?') {
+      if (event.key === KeyboardKeys.KeyQuestionMark) {
         setDisplay(d => !d);
       }
     };
 
-    window.addEventListener('keypress', handleKeyPress);
+    document.addEventListener('keydown', handleKeyDown);
 
     return () => {
-      window.removeEventListener('keypress', handleKeyPress);
+      document.removeEventListener('keydown', handleKeyDown);
     };
   }, [setDisplay]);
 
index 5c3967754d8cbf0651ac22cf549e6e3557aaf818..4727c3deb92c05a7dcc6bbbc1fa47781347f48dd 100644 (file)
@@ -46,36 +46,36 @@ it('should render correctly', () => {
   const wrapper = shallowRender();
   expect(wrapper).toMatchSnapshot('hidden');
 
-  window.dispatchEvent(new KeyboardEvent('keypress', { key: '?' }));
+  document.dispatchEvent(new KeyboardEvent('keydown', { key: '?' }));
 
   expect(wrapper).toMatchSnapshot('visible');
 });
 
 it('should close correctly', () => {
   const wrapper = shallowRender();
-  window.dispatchEvent(new KeyboardEvent('keypress', { key: '?' }));
+  document.dispatchEvent(new KeyboardEvent('keydown', { key: '?' }));
 
   wrapper.find(Modal).props().onRequestClose!(mockEvent());
 
   expect(wrapper.type()).toBeNull();
 });
 
-it('should ignore other keypresses', () => {
+it('should ignore other keydownes', () => {
   const wrapper = shallowRender();
-  window.dispatchEvent(new KeyboardEvent('keypress', { key: '!' }));
+  document.dispatchEvent(new KeyboardEvent('keydown', { key: '!' }));
   expect(wrapper.type()).toBeNull();
 });
 
 it.each([['input'], ['select'], ['textarea']])('should ignore events on a %s', type => {
   const wrapper = shallowRender();
 
-  const fakeEvent = new KeyboardEvent('keypress', { key: '!' });
+  const fakeEvent = new KeyboardEvent('keydown', { key: '!' });
 
   Object.defineProperty(fakeEvent, 'target', {
     value: document.createElement(type)
   });
 
-  window.dispatchEvent(fakeEvent);
+  document.dispatchEvent(fakeEvent);
 
   expect(wrapper.type()).toBeNull();
 });
index 996ea790cd4d32769105b8edd9bed9b709a4dcfe..100a42eb9de3186c63cee3700416b8cd82c3801a 100644 (file)
@@ -100,11 +100,11 @@ export default class HotspotViewerTabs extends React.PureComponent<Props, State>
   };
 
   registerKeyboardEvents() {
-    window.addEventListener('keydown', this.handleKeyboardNavigation);
+    document.addEventListener('keydown', this.handleKeyboardNavigation);
   }
 
   unregisterKeyboardEvents() {
-    window.removeEventListener('keydown', this.handleKeyboardNavigation);
+    document.removeEventListener('keydown', this.handleKeyboardNavigation);
   }
 
   handleSelectTabs = (tabKey: TabKeys) => {
index c222a202457514cdc8d86fe846a59e811f598e2d..f061e98713c5c09fe20312f87d090c9fc1e06336 100644 (file)
@@ -51,5 +51,6 @@ export enum KeyboardKeys {
   KeyI = 'i',
   KeyC = 'c',
   KeyT = 't',
-  KeyS = 's'
+  KeyS = 's',
+  KeyQuestionMark = '?'
 }