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

fix(tree): improve keyboard navigation #7618

Merged
merged 2 commits into from
Aug 29, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,7 @@ describe("calcite-tree-item", () => {

const item = await page.find("#newbie");
expect(item).toEqualAttribute("aria-hidden", "false");
expect(item).not.toHaveAttribute("calcite-hydrated-hidden");
expect(item.tabIndex).toBe(0);
expect(item.tabIndex).toBe(-1); // items are programmatically focused
});
});

Expand Down Expand Up @@ -362,61 +361,6 @@ describe("calcite-tree-item", () => {
expect(isVisible).toBe(true);
});

it("right arrow key expands subtree and left arrow collapses it", async () => {
const page = await newE2EPage({
html: `
<calcite-tree>
<calcite-tree-item id="cables">
Cables
<calcite-tree slot="children">
<calcite-tree-item id="xlr">XLR Cable</calcite-tree-item>
<calcite-tree-item id="instrument">Instrument Cable</calcite-tree-item>
</calcite-tree>
</calcite-tree-item>
</calcite-tree>
`,
});

await page.keyboard.press("Tab");

expect(await page.evaluate(() => document.activeElement.id)).toBe("cables");
expect(await page.evaluate(() => (document.activeElement as HTMLCalciteTreeItemElement).expanded)).toBe(false);

await page.keyboard.press("ArrowRight");

expect(await page.evaluate(() => document.activeElement.id)).toBe("cables");
expect(await page.evaluate(() => (document.activeElement as HTMLCalciteTreeItemElement).expanded)).toBe(true);

await page.keyboard.press("ArrowLeft");

expect(await page.evaluate(() => document.activeElement.id)).toBe("cables");
expect(await page.evaluate(() => (document.activeElement as HTMLCalciteTreeItemElement).expanded)).toBe(false);
});

it("right arrow key focuses first item in expanded subtree", async () => {
const page = await newE2EPage({
html: `
<calcite-tree>
<calcite-tree-item id="cables" expanded>
Cables
<calcite-tree slot="children">
<calcite-tree-item id="xlr">XLR Cable</calcite-tree-item>
<calcite-tree-item id="instrument">Instrument Cable</calcite-tree-item>
</calcite-tree>
</calcite-tree-item>
</calcite-tree>
`,
});

await page.keyboard.press("Tab");

expect(await page.evaluate(() => document.activeElement.id)).toBe("cables");

await page.keyboard.press("ArrowRight");

expect(await page.evaluate(() => document.activeElement.id)).toBe("xlr");
});

it("displaying an expanded item is visible", async () => {
const page = await newE2EPage();
await page.setContent(
Expand Down
40 changes: 8 additions & 32 deletions packages/calcite-components/src/components/tree-item/tree-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ import {
Watch,
} from "@stencil/core";
import {
slotChangeHasAssignedElement,
filterDirectChildren,
getElementDir,
getSlotted,
nodeListToArray,
slotChangeHasAssignedElement,
toAriaBoolean,
} from "../../utils/dom";
import {
Expand Down Expand Up @@ -186,7 +185,10 @@ export class TreeItem implements ConditionalSlotComponent, InteractiveComponent
}

componentDidRender(): void {
updateHostInteraction(this, () => this.parentExpanded || this.depth === 1);
updateHostInteraction(
this,
() => false // programmatically focusable
);
}

//--------------------------------------------------------------------------
Expand Down Expand Up @@ -358,8 +360,6 @@ export class TreeItem implements ConditionalSlotComponent, InteractiveComponent

@Listen("keydown")
keyDownHandler(event: KeyboardEvent): void {
let root;

if (this.isActionEndEvent(event)) {
return;
}
Expand All @@ -382,7 +382,7 @@ export class TreeItem implements ConditionalSlotComponent, InteractiveComponent
return;
}
// activates a node, i.e., performs its default action. For parent nodes, one possible default action is to open or close the node. In single-select trees where selection does not follow focus (see note below), the default action is typically to select the focused node.
const link = nodeListToArray(this.el.children).find((el) =>
const link = Array.from(this.el.children).find((el) =>
el.matches("a")
) as HTMLAnchorElement;

Expand All @@ -398,33 +398,8 @@ export class TreeItem implements ConditionalSlotComponent, InteractiveComponent
updateItem: true,
});
}

event.preventDefault();
break;
case "Home":
root = this.el.closest("calcite-tree:not([child])") as HTMLCalciteTreeElement;

const firstNode = root.querySelector("calcite-tree-item");

firstNode?.focus();

break;
case "End":
root = this.el.closest("calcite-tree:not([child])");

let currentNode = root.children[root.children.length - 1]; // last child
let currentTree = nodeListToArray(currentNode.children).find((el) =>
el.matches("calcite-tree")
);
while (currentTree) {
currentNode = currentTree.children[root.children.length - 1];
currentTree = nodeListToArray(currentNode.children).find((el) =>
el.matches("calcite-tree")
);
}
currentNode?.focus();
break;
}
}
}

//--------------------------------------------------------------------------
Expand Down Expand Up @@ -533,3 +508,4 @@ export class TreeItem implements ConditionalSlotComponent, InteractiveComponent
this.hasEndActions = slotChangeHasAssignedElement(event);
};
}

Loading