diff --git a/frontend/src/components/QuickAdd.tsx b/frontend/src/components/QuickAdd.tsx index f06e3a02..9a6124c5 100644 --- a/frontend/src/components/QuickAdd.tsx +++ b/frontend/src/components/QuickAdd.tsx @@ -6,7 +6,6 @@ import x from "../icons/x.svg"; import check from "../icons/check.svg"; import { AuthContext } from "../components/AuthProvider"; import { PUBLIC_API_URL, headers, useEscaper } from "../utils"; -import * as ReactDOM from "react-dom"; export const QuickAdd = ({ addIssueActivity, @@ -101,7 +100,7 @@ export const QuickAdd = ({ const suggestionSelected = (selection: Issue) => { setIssue(selection); // Update input box with selected issue - let element = ReactDOM.findDOMNode(document.getElementById("input-issue")); + let element = issueInputRef.current; element.value = selection.id.toString(); setIsAutoCompleteVisible(false); }; @@ -188,8 +187,43 @@ export const QuickAdd = ({ setIsAutoCompleteVisible(false); }; - const wrapperRef = useRef(null); - useEscaper(wrapperRef, handleHideAutocomplete); + const getSuggestionsLength = () => { + return suggestionsRef.current.childNodes.length; + }; + + const suggestionsRef = useRef(null); + useEscaper(suggestionsRef, handleHideAutocomplete); + const issueInputRef = useRef(null); + + const handleInputToAutocompleteFocus = (event: any) => { + event.preventDefault(); + if (event.key == "ArrowUp" || event.key == "ArrowDown") { + let sugLen = getSuggestionsLength(); + if (sugLen > 0) { + let suggestionIndex = 0; + event.key == "ArrowUp" ? sugLen - 1 : 0; + + suggestionsRef.current.childNodes[ + suggestionIndex + ].childNodes[0].focus(); + } + } + }; + + const handleAutocompleteNavigation = (event: any) => { + event.preventDefault(); + if (event.key == "ArrowUp" || event.key == "ArrowDown") { + let sugLen = getSuggestionsLength(); + if (sugLen > 0) { + let sourceIndex: number = Number(event.target.id.split("-")[2]); + let targetIndex: number = + event.key == "ArrowUp" ? sourceIndex - 1 : sourceIndex + 1; + if (targetIndex >= 0 && targetIndex < sugLen) { + suggestionsRef.current.childNodes[targetIndex].childNodes[0].focus(); + } + } + } + }; return (