Skip to content

Commit

Permalink
Simplify code for shotcut handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristophLabacher committed Aug 22, 2019
1 parent ac1be52 commit bb0f4f6
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 39 deletions.
15 changes: 1 addition & 14 deletions frontend/src/Search/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,30 +85,17 @@ function Search(props) {
hideSearch();
}

if (event.key === 's' && !searchIsFocused) {
if (event.key === 's' && event.target.nodeName !== "INPUT") {
event.preventDefault();
focus();
}
};

const localShortcutHandler = event => {
if (event.key === 'f' && searchIsFocused) {
event.stopPropagation();
}
};

useEffect(() => {
document.addEventListener('keydown', shortcutHandler);

// We do this so we can type "f" in the search, even though it is
// the global shortcut to focus filter
searchInputRef.current.addEventListener('keydown', localShortcutHandler);

return () => {
document.removeEventListener('keydown', shortcutHandler);
if (searchInputRef.current) {
searchInputRef.current.removeEventListener('keydown', localShortcutHandler);
}
};
});

Expand Down
26 changes: 1 addition & 25 deletions frontend/src/TreeNavigation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import './TreeNavigation.css';
function TreeNavigation(props) {
const [filterTerm, setFilterTerm] = useGlobal('filterTerm');
const [filteredTree, setFilteredTree] = useState(null);
const [filterIsFocused, setFilterIsFocused] = useState(false);

const filterInputRef = React.createRef();

Expand All @@ -24,30 +23,17 @@ function TreeNavigation(props) {
blurFilter();
}

if (event.key === 'f' && !filterIsFocused) {
if (event.key === 'f' && event.target.nodeName !== "INPUT") {
event.preventDefault();
focusFilter();
}
};

// We do this so we can type "s" in the filter, even though it is
// the global shortcut to focus search
const localShortcutHandler = event => {
if (event.key === 's' && filterIsFocused) {
event.stopPropagation();
}
};

useEffect(() => {
document.addEventListener('keydown', shortcutHandler);
filterInputRef.current.addEventListener('keydown', localShortcutHandler);

return () => {
document.removeEventListener('keydown', shortcutHandler);

if (filterInputRef.current) {
filterInputRef.current.removeEventListener('keydown', localShortcutHandler);
}
};
});

Expand Down Expand Up @@ -120,14 +106,6 @@ function TreeNavigation(props) {
return <li key={node.title}>{content}</li>;
}

function onFocusFilter() {
setFilterIsFocused(true);
}

function onBlurFilter() {
setFilterIsFocused(false);
}

function blurFilter() {
if (filterInputRef.current) {
filterInputRef.current.blur();
Expand Down Expand Up @@ -162,8 +140,6 @@ function TreeNavigation(props) {
placeholder="Filter Aspects"
value={filterTerm}
onChange={onFilterTermChange}
onFocus={onFocusFilter}
onBlur={onBlurFilter}
ref={filterInputRef}
/>
</div>
Expand Down

0 comments on commit bb0f4f6

Please sign in to comment.