Skip to content

Commit

Permalink
added keyboard listener
Browse files Browse the repository at this point in the history
Signed-off-by: sumukhswamy <sumukhhs@amazon.com>
  • Loading branch information
sumukhswamy committed Sep 24, 2024
1 parent 9a88bf5 commit 6d6e170
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/plugins/data/public/ui/query_editor/query_editor_top_row.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
prettyDuration,
} from '@elastic/eui';
import classNames from 'classnames';
import React, { useState } from 'react';
import React, { useEffect, useState } from 'react';
import { createPortal } from 'react-dom';
import { IDataPluginServices, IIndexPattern, Query, TimeHistoryContract, TimeRange } from '../..';
import {
Expand Down Expand Up @@ -72,6 +72,20 @@ export default function QueryEditorTopRow(props: QueryEditorTopRowProps) {
},
} = opensearchDashboards.services;

useEffect(() => {
function handleCmdEnter(event: KeyboardEvent) {
if ((event.metaKey || event.ctrlKey) && event.key === 'Enter') {
event.preventDefault();

onClickSubmitButton(event);
}
}
document.addEventListener('keydown', handleCmdEnter);
return () => {
document.removeEventListener('keydown', handleCmdEnter);
};
}, [onClickSubmitButton]);

const queryLanguage = props.query && props.query.language;
const persistedLog: PersistedLog | undefined = React.useMemo(
() =>
Expand All @@ -81,7 +95,7 @@ export default function QueryEditorTopRow(props: QueryEditorTopRowProps) {
[queryLanguage, uiSettings, storage, appName]
);

function onClickSubmitButton(event: React.MouseEvent<HTMLButtonElement>) {
function onClickSubmitButton(event: React.MouseEvent<HTMLButtonElement> | KeyboardEvent) {

Check failure on line 98 in src/plugins/data/public/ui/query_editor/query_editor_top_row.tsx

View workflow job for this annotation

GitHub Actions / Build and Verify on Linux (ciGroup1)

The 'onClickSubmitButton' function makes the dependencies of useEffect Hook (at line 87) change on every render. To fix this, wrap the definition of 'onClickSubmitButton' in its own useCallback() Hook
if (persistedLog && props.query) {
persistedLog.add(props.query.query);
}
Expand Down

0 comments on commit 6d6e170

Please sign in to comment.