Skip to content

Commit

Permalink
feat: add useKeyDown hook (#584)
Browse files Browse the repository at this point in the history
Resolves: #568

---------

Co-authored-by: Amir Blum <amirgiraffe@gmail.com>
  • Loading branch information
Kritik-J and blumamir authored Oct 18, 2023
1 parent d5274bd commit f443273
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { SETUP } from '@/utils/constants';
import { DestinationBody } from '@/containers/setup/connection/connection.section';
import { Field } from '@/types/destinations';
import theme from '@/styles/palette';
import { useKeyDown } from '@/hooks';

interface CreateConnectionFormProps {
fields: Field[];
Expand Down Expand Up @@ -64,15 +65,10 @@ export function CreateConnectionForm({
filterSupportedMonitors();
}, [supportedSignals]);

useEffect(() => {
window.addEventListener('keypress', handleKeyPress);
return () => {
window.removeEventListener('keypress', handleKeyPress);
};
}, []);
useKeyDown('Enter', handleKeyPress);

function handleKeyPress(e: any) {
if (e.key === 'Enter' && !isCreateButtonDisabled) {
if (!isCreateButtonDisabled) {
onCreateClick();
}
}
Expand Down
15 changes: 5 additions & 10 deletions frontend/webapp/containers/overview/sources/update.source.form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
} from '@/design.system';
import { DeleteSource } from '@/components/overview';
import { deleteSource, getSource, patchSources } from '@/services/sources';
import { useNotification } from '@/hooks';
import { useKeyDown, useNotification } from '@/hooks';
import theme from '@/styles/palette';
import { ManagedSource } from '@/types/sources';

Expand Down Expand Up @@ -65,18 +65,13 @@ export function UpdateSourceForm() {
useEffect(() => {
setInputValue(currentSource?.reported_name || '');
}, [currentSource]);
useEffect(() => {
window.addEventListener('keypress', handleKeyPress);
return () => {
window.removeEventListener('keypress', handleKeyPress);
};
}, []);

useKeyDown('Enter', handleKeyPress);

function handleKeyPress(e: any) {
if (e.key === 'Enter') {
onSaveClick();
}
onSaveClick();
}

async function onPageLoad() {
const name = searchParams.get(NAME) || '';
const kind = searchParams.get(KIND) || '';
Expand Down
1 change: 1 addition & 0 deletions frontend/webapp/hooks/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { useSectionData } from "./useSectionData";
export { useNotification } from "./useNotification";
export { useOnClickOutside } from "./useOnClickOutside";
export { useKeyDown } from "./useKeyDown";
19 changes: 19 additions & 0 deletions frontend/webapp/hooks/useKeyDown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { useEffect } from "react";

export function useKeyDown(key, callback) {
const handleKeyDown = (event) => {
if (key === event.key) {
callback(event);
}
};

useEffect(() => {
window.addEventListener("keydown", handleKeyDown);

return () => {
window.removeEventListener("keydown", handleKeyDown);
};
}, [key, callback]);

return handleKeyDown;
}

0 comments on commit f443273

Please sign in to comment.