Skip to content

Commit

Permalink
feat: Add Space/Enter keyboard functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
yusuf-musleh committed Mar 12, 2024
1 parent 7556823 commit 2b35fb8
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/content-tags-drawer/ContentTagsDropDownSelector.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ const ContentTagsDropDownSelector = ({
setNumPages((x) => x + 1);
}, []);

const handleKeyBoardNav = (e) => {
const keyPressed = e.key;
const handleKeyBoardNav = (e, hasChildren) => {
const keyPressed = e.code;
const currentElement = e.target;
const encapsulator = currentElement.closest('.dropdown-selector-tag-encapsulator');

Expand All @@ -130,14 +130,15 @@ const ContentTagsDropDownSelector = ({
tagValue = tagValue ? decodeURIComponent(tagValue) : tagValue;

if (keyPressed === 'ArrowRight') {
e.preventDefault();
if (tagValue && !isOpen(tagValue)) {
clickAndEnterHandler(tagValue);
}
} else if (keyPressed === 'ArrowLeft') {
e.preventDefault();
if (tagValue && isOpen(tagValue)) {
clickAndEnterHandler(tagValue);
}
// TODO: Implement remaining cases when left key pressed
} else if (keyPressed === 'ArrowUp') {
const prevSubTags = encapsulator?.previousElementSibling?.querySelectorAll('.dropdown-selector-tag-actions');
const prevSubTag = prevSubTags && prevSubTags[prevSubTags.length - 1];
Expand All @@ -153,7 +154,6 @@ const ContentTagsDropDownSelector = ({
// Handles case of jumping out of subtags to previous parent tag
const prevParentTagEncapsulator = encapsulator?.parentNode.closest('.dropdown-selector-tag-encapsulator');
const prevParentTag = prevParentTagEncapsulator?.querySelector('.dropdown-selector-tag-actions');

prevParentTag?.focus();
}
} else if (keyPressed === 'ArrowDown') {
Expand Down Expand Up @@ -185,9 +185,17 @@ const ContentTagsDropDownSelector = ({
}
}
} else if (keyPressed === 'Enter') {
// TODO: Implement
e.preventDefault();
if (hasChildren && tagValue) {
clickAndEnterHandler(tagValue);
} else {
const checkbox = currentElement.querySelector('.taxonomy-tags-selectable-box');
checkbox.click();
}
} else if (keyPressed === 'Space') {
// TODO: Implmenet
e.preventDefault();
const checkbox = currentElement.querySelector('.taxonomy-tags-selectable-box');
checkbox.click();
}
};

Expand Down Expand Up @@ -216,7 +224,7 @@ const ContentTagsDropDownSelector = ({
<div
className="d-flex dropdown-selector-tag-actions"
tabIndex={i === 0 && level === 0 ? 0 : -1} // Only enable tab into top of dropdown to set focus
onKeyDown={handleKeyBoardNav}
onKeyDown={(e) => handleKeyBoardNav(e, tagData.childCount > 0)}
>
<SelectableBox
inputHidden={false}
Expand Down

0 comments on commit 2b35fb8

Please sign in to comment.