Skip to content
This repository has been archived by the owner on Jan 20, 2024. It is now read-only.

Commit

Permalink
Add more hotkeys to main view (see #14)
Browse files Browse the repository at this point in the history
  • Loading branch information
tdemin committed Oct 9, 2019
1 parent e5f5d34 commit 27cc96c
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/views/mainView.tsx
Expand Up @@ -52,7 +52,7 @@ class MainView extends React.Component<Props, State> {
toNewTask = () => this.props.history.push("/task/new");
componentDidMount = () => {
this.refetch();
document.addEventListener("keydown", this.handleCtrlF);
document.addEventListener("keydown", this.handleHotkeys);
};
componentDidUpdate = (prevProps: Props) => {
if (prevProps.tasks !== this.props.tasks) {
Expand All @@ -61,11 +61,25 @@ class MainView extends React.Component<Props, State> {
});
}
};
handleCtrlF = (event: KeyboardEvent) => {
handleHotkeys = (event: KeyboardEvent) => {
const search = document.getElementById("searchInput");
const searchFocused = document.activeElement === search;
if ((event.ctrlKey || event.metaKey) && event.key === "f") {
event.preventDefault();
const search = document.getElementById("searchInput");
(search as HTMLElement).focus();
} else if (event.key === "n" && !searchFocused) {
event.preventDefault();
this.toNewTask();
} else if (event.key === "u" && !searchFocused) {
event.preventDefault();
this.refetch();
} else if (event.keyCode === 27 && searchFocused) {
// unfocus search on Esc press
(document.activeElement as HTMLElement).blur();
event.preventDefault();
this.setState({
search: "",
});
}
};
render = () => {
Expand Down

0 comments on commit 27cc96c

Please sign in to comment.