diff --git a/ui/src/components/Filters.tsx b/ui/src/components/Filters.tsx index 9124c9512..fef26bbb8 100644 --- a/ui/src/components/Filters.tsx +++ b/ui/src/components/Filters.tsx @@ -9,6 +9,8 @@ import filterUIExample2 from "./assets/filter-ui-example-2.png" import variables from '../variables.module.scss'; import {useRecoilState} from "recoil"; import queryAtom from "../recoil/query"; +import useKeyPress from "../hooks/useKeyPress" +import shortcutsKeyboard from "../configs/shortcutsKeyboard" interface FiltersProps { backgroundColor: string @@ -60,6 +62,8 @@ export const QueryForm: React.FC = ({backgroundColor, ws, openWe setQuery(e.target.value); } + + const handleSubmit = (e) => { ws.close(); if (query) { @@ -70,6 +74,8 @@ export const QueryForm: React.FC = ({backgroundColor, ws, openWe e.preventDefault(); } + useKeyPress(shortcutsKeyboard.ctrlEnter, handleSubmit, formRef.current); + return <>
= ({onFirstLogin}) => { + const formRef = useRef(null); const classes = useCommonStyles(); const [isLoading, setIsLoading] = useState(false); const [password, setPassword] = useState(""); @@ -54,13 +58,9 @@ export const InstallPage: React.FC = ({onFirstLogin}) => { } - const handleFormOnKeyPress = (e: React.KeyboardEvent) => { - if (e.key === "Enter") { - onFormSubmit(); - } - }; + useKeyPress(shortcutsKeyboard.enter, onFormSubmit, formRef.current); - return
+ return
{isLoading && }
Setup
Welcome to Mizu, please set up the admin user to continue diff --git a/ui/src/components/LoginPage.tsx b/ui/src/components/LoginPage.tsx index 5b7dfebda..f6e8537de 100644 --- a/ui/src/components/LoginPage.tsx +++ b/ui/src/components/LoginPage.tsx @@ -1,11 +1,14 @@ import { Button } from "@material-ui/core"; -import React, { useState } from "react"; +import React, { useState,useRef } from "react"; import { toast } from "react-toastify"; import Api from "../helpers/api"; import { useCommonStyles } from "../helpers/commonStyle"; import LoadingOverlay from "./LoadingOverlay"; import entPageAtom, {Page} from "../recoil/entPage"; import {useSetRecoilState} from "recoil"; +import useKeyPress from "../hooks/useKeyPress" +import shortcutsKeyboard from "../configs/shortcutsKeyboard" + const api = Api.getInstance(); @@ -15,6 +18,7 @@ const LoginPage: React.FC = () => { const [isLoading, setIsLoading] = useState(false); const [username, setUsername] = useState(""); const [password, setPassword] = useState(""); + const formRef = useRef(null); const setEntPage = useSetRecoilState(entPageAtom); @@ -36,13 +40,9 @@ const LoginPage: React.FC = () => { } } - const handleFormOnKeyPress = (e: React.KeyboardEvent) => { - if (e.key === "Enter") { - onFormSubmit(); - } - }; + useKeyPress(shortcutsKeyboard.enter, onFormSubmit, formRef.current); - return
+ return
{isLoading && }
Login
diff --git a/ui/src/configs/shortcutsKeyboard.ts b/ui/src/configs/shortcutsKeyboard.ts new file mode 100644 index 000000000..b5f8fcad0 --- /dev/null +++ b/ui/src/configs/shortcutsKeyboard.ts @@ -0,0 +1,7 @@ +const dictionary = { + ctrlEnter : [{metaKey : true, code:"Enter"}, {ctrlKey:true, code:"Enter"}], // support Ctrl/command + enter : [{code:"Enter"}] +}; + + +export default dictionary; \ No newline at end of file diff --git a/ui/src/hooks/useKeyPress.ts b/ui/src/hooks/useKeyPress.ts new file mode 100644 index 000000000..4f14fd2c7 --- /dev/null +++ b/ui/src/hooks/useKeyPress.ts @@ -0,0 +1,35 @@ +import { useCallback, useEffect, useLayoutEffect, useRef } from 'react'; + +const useKeyPress = (eventConfigs, callback, node = null) => { + // implement the callback ref pattern + const callbackRef = useRef(callback); + useLayoutEffect(() => { + callbackRef.current = callback; + }); + + // handle what happens on key press + const handleKeyPress = useCallback( + (event) => { + // check if one of the key is part of the ones we want + if (eventConfigs.some((eventConfig) => Object.keys(eventConfig).every(nameKey => eventConfig[nameKey] === event[nameKey]))) { + callbackRef.current(event); + } + }, + [eventConfigs] + ); + + useEffect(() => { + // target is either the provided node or the document + const targetNode = node ?? document; + // attach the event listener + targetNode && + targetNode.addEventListener("keydown", handleKeyPress); + + // remove the event listener + return () => + targetNode && + targetNode.removeEventListener("keydown", handleKeyPress); + }, [handleKeyPress, node]); +}; + +export default useKeyPress; \ No newline at end of file