Skip to content

Commit

Permalink
[TreeView] Improve Enter key behavior (#3221)
Browse files Browse the repository at this point in the history
  • Loading branch information
imnasnainaec authored Jul 10, 2024
1 parent 8b152c2 commit b06fac6
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 30 deletions.
1 change: 1 addition & 0 deletions src/components/TreeView/TreeDepiction/DomainTileButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export default function DomainTileButton(
margin: "2.5%",
padding: "5px",
}}
tabIndex={-1}
variant={"outlined"}
>
<DomainTile {...props} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ exports[`DomainTileButton renders tile and matches the latest snapshot 1`] = `
"width": "95%",
}
}
tabIndex={0}
tabIndex={-1}
type="button"
>
<div
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Array [
"width": "95%",
}
}
tabIndex={0}
tabIndex={-1}
type="button"
>
<div>
Expand Down Expand Up @@ -680,7 +680,7 @@ Array [
"width": "95%",
}
}
tabIndex={0}
tabIndex={-1}
type="button"
>
<div>
Expand Down Expand Up @@ -770,7 +770,7 @@ Array [
"width": "95%",
}
}
tabIndex={0}
tabIndex={-1}
type="button"
>
<div>
Expand Down Expand Up @@ -860,7 +860,7 @@ Array [
"width": "95%",
}
}
tabIndex={0}
tabIndex={-1}
type="button"
>
<div>
Expand Down Expand Up @@ -936,7 +936,7 @@ Array [
"width": "95%",
}
}
tabIndex={0}
tabIndex={-1}
type="button"
>
<div>
Expand Down Expand Up @@ -1039,7 +1039,7 @@ Array [
"width": "95%",
}
}
tabIndex={0}
tabIndex={-1}
type="button"
>
<div
Expand Down Expand Up @@ -1310,7 +1310,7 @@ Array [
"width": "95%",
}
}
tabIndex={0}
tabIndex={-1}
type="button"
>
<div>
Expand Down Expand Up @@ -1386,7 +1386,7 @@ Array [
"width": "95%",
}
}
tabIndex={0}
tabIndex={-1}
type="button"
>
<div>
Expand Down Expand Up @@ -1684,7 +1684,7 @@ Array [
"width": "95%",
}
}
tabIndex={0}
tabIndex={-1}
type="button"
>
<div>
Expand Down Expand Up @@ -1760,7 +1760,7 @@ Array [
"width": "95%",
}
}
tabIndex={0}
tabIndex={-1}
type="button"
>
<div>
Expand Down Expand Up @@ -1936,7 +1936,7 @@ Array [
"width": "95%",
}
}
tabIndex={0}
tabIndex={-1}
type="button"
>
<div
Expand Down Expand Up @@ -2662,7 +2662,7 @@ Array [
"width": "95%",
}
}
tabIndex={0}
tabIndex={-1}
type="button"
>
<div>
Expand Down Expand Up @@ -2752,7 +2752,7 @@ Array [
"width": "95%",
}
}
tabIndex={0}
tabIndex={-1}
type="button"
>
<div>
Expand Down Expand Up @@ -2842,7 +2842,7 @@ Array [
"width": "95%",
}
}
tabIndex={0}
tabIndex={-1}
type="button"
>
<div>
Expand Down Expand Up @@ -2932,7 +2932,7 @@ Array [
"width": "95%",
}
}
tabIndex={0}
tabIndex={-1}
type="button"
>
<div>
Expand Down Expand Up @@ -3008,7 +3008,7 @@ Array [
"width": "95%",
}
}
tabIndex={0}
tabIndex={-1}
type="button"
>
<div>
Expand Down Expand Up @@ -3197,7 +3197,7 @@ Array [
"width": "95%",
}
}
tabIndex={0}
tabIndex={-1}
type="button"
>
<div>
Expand Down Expand Up @@ -3300,7 +3300,7 @@ Array [
"width": "95%",
}
}
tabIndex={0}
tabIndex={-1}
type="button"
>
<div
Expand Down Expand Up @@ -3449,7 +3449,7 @@ Array [
"width": "95%",
}
}
tabIndex={0}
tabIndex={-1}
type="button"
>
<div
Expand Down Expand Up @@ -3999,7 +3999,7 @@ Array [
"width": "95%",
}
}
tabIndex={0}
tabIndex={-1}
type="button"
>
<div>
Expand Down Expand Up @@ -4089,7 +4089,7 @@ Array [
"width": "95%",
}
}
tabIndex={0}
tabIndex={-1}
type="button"
>
<div>
Expand Down Expand Up @@ -4179,7 +4179,7 @@ Array [
"width": "95%",
}
}
tabIndex={0}
tabIndex={-1}
type="button"
>
<div>
Expand Down
23 changes: 16 additions & 7 deletions src/components/TreeView/TreeNavigator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@ export interface TreeNavigatorProps {
}

export default function TreeNavigator(props: TreeNavigatorProps): ReactElement {
const { getFirstChild, getNextSibling, getParent, getPrevSibling } =
useTreeNavigation(props);
const {
getCurrent,
getFirstChild,
getNextSibling,
getParent,
getPrevSibling,
} = useTreeNavigation(props);

// Navigate tree via arrow keys.
const getArrowKeyDomain = (e: KeyboardEvent): SemanticDomain | undefined => {
const getKeyDomain = (e: KeyboardEvent): SemanticDomain | undefined => {
const rtl = document.body.dir === "rtl";
switch (e.key) {
case Key.ArrowLeft:
Expand All @@ -24,25 +29,28 @@ export default function TreeNavigator(props: TreeNavigatorProps): ReactElement {
return getParent();
case Key.ArrowDown:
return getFirstChild();
case Key.Enter:
return getCurrent();
}
};

const navigateDomainArrowKeys = async (e: KeyboardEvent): Promise<void> => {
const domain = getArrowKeyDomain(e);
const navigateDomainByKey = async (e: KeyboardEvent): Promise<void> => {
const domain = getKeyDomain(e);
if (domain) {
await props.animate(domain);
}
};

useEffect(() => {
window.addEventListener("keydown", navigateDomainArrowKeys);
return () => window.removeEventListener("keydown", navigateDomainArrowKeys);
window.addEventListener("keydown", navigateDomainByKey);
return () => window.removeEventListener("keydown", navigateDomainByKey);
});

return <Fragment />;
}

interface TreeNavigation {
getCurrent: () => SemanticDomain | undefined;
getFirstChild: () => SemanticDomain | undefined;
getNextSibling: () => SemanticDomain | undefined;
getParent: () => SemanticDomain | undefined;
Expand All @@ -53,6 +61,7 @@ interface TreeNavigation {
export function useTreeNavigation(props: TreeNavigatorProps): TreeNavigation {
const dom = props.currentDomain;
return {
getCurrent: () => dom,
getFirstChild: () => (dom.children.length ? dom.children[0] : undefined),
getNextSibling: () => dom.next,
getParent: () => dom.parent,
Expand Down

0 comments on commit b06fac6

Please sign in to comment.