Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework search suggestions navigation #542

Merged
5 commits merged into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 52 additions & 6 deletions frontend/src/components/QuickAdd.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
};
Expand Down Expand Up @@ -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 (
<div>
Expand All @@ -205,9 +239,13 @@ export const QuickAdd = ({
<div className="row">
<input
id="input-issue"
ref={issueInputRef}
autoComplete="off"
className={getSearchClasses()}
type="text"
onKeyUp={(ev) => {
handleInputToAutocompleteFocus(ev);
}}
min={0}
onChange={(e) => {
setSearch({ ...search, text: e.target.value });
Expand Down Expand Up @@ -244,10 +282,18 @@ export const QuickAdd = ({
</button>
</div>
{search.suggestions.length > 0 && isAutoCompleteVisible && (
<ul className="col-8 autocomplete-container" ref={wrapperRef}>
{search.suggestions.map((item) => (
<ul
id="suggestions-ul"
className="col-8 autocomplete-container"
ref={suggestionsRef}
>
{search.suggestions.map((item, index) => (
<li key={item.id} className="autocomplete-item">
<button
id={"suggestion-btn-" + index.toString()}
onKeyUp={(ev) => {
handleAutocompleteNavigation(ev);
}}
key={item.id}
onClick={() => suggestionSelected(item)}
className="autocomplete-button"
Expand Down
5 changes: 5 additions & 0 deletions frontend/src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,11 @@ Other button classes are defined further down together with other classes for th
padding: 0.5rem 1rem;
}

.autocomplete-button:focus {
background-color: hsl(186deg 30% 86%);
outline: none;
}

.autocomplete-button:hover {
background-color: hsl(186deg 30% 94%);
outline: none;
Expand Down