Skip to content

Commit

Permalink
fix(tree-item): Allow space and enter key events when selectionMode i…
Browse files Browse the repository at this point in the history
…s "none" (#5800)

**Related Issue:** #5735

## Summary

fix(tree-item): Allow space and enter key events when selectionMode is
"none". #5735
  • Loading branch information
driskull committed Nov 23, 2022
1 parent 7fe6467 commit 2fa483b
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/components/tree-item/tree-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -309,13 +309,19 @@ export class TreeItem implements ConditionalSlotComponent, InteractiveComponent

switch (event.key) {
case " ":
if (this.selectionMode === "none") {
return;
}
this.calciteInternalTreeItemSelect.emit({
modifyCurrentSelection: this.isSelectionMultiLike,
forceToggle: false
});
event.preventDefault();
break;
case "Enter":
if (this.selectionMode === "none") {
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) =>
el.matches("a")
Expand Down
56 changes: 56 additions & 0 deletions src/components/tree/tree.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,62 @@ describe("calcite-tree", () => {
expect(keydownSpy).toHaveReceivedEventTimes(14);
});

it("does prevent space/enter keyboard event on actions with selectionMode of single", async () => {
const page = await newE2EPage();
await page.setContent(html`<div id="container">
<calcite-tree selection-mode="single">
<calcite-tree-item>
<button>My button</button>
</calcite-tree-item>
</calcite-tree>
</div>`);

const container = await page.find("#container");
const button = await page.find("button");
const keydownSpy = await container.spyOnEvent("keydown");

expect(keydownSpy).toHaveReceivedEventTimes(0);

await button.focus();
await page.keyboard.press("Enter");

expect(keydownSpy).toHaveReceivedEventTimes(1);
expect(keydownSpy.lastEvent.defaultPrevented).toBe(true);

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

expect(keydownSpy).toHaveReceivedEventTimes(2);
expect(keydownSpy.lastEvent.defaultPrevented).toBe(true);
});

it("does not prevent space/enter keyboard event on actions with selectionMode of none", async () => {
const page = await newE2EPage();
await page.setContent(html`<div id="container">
<calcite-tree selection-mode="none">
<calcite-tree-item>
<button>My button</button>
</calcite-tree-item>
</calcite-tree>
</div>`);

const container = await page.find("#container");
const button = await page.find("button");
const keydownSpy = await container.spyOnEvent("keydown");

expect(keydownSpy).toHaveReceivedEventTimes(0);

await button.focus();
await page.keyboard.press("Enter");

expect(keydownSpy).toHaveReceivedEventTimes(1);
expect(keydownSpy.lastEvent.defaultPrevented).toBe(false);

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

expect(keydownSpy).toHaveReceivedEventTimes(2);
expect(keydownSpy.lastEvent.defaultPrevented).toBe(false);
});

it("honors disabled items when navigating the tree", async () => {
const page = await newE2EPage();
await page.setContent(
Expand Down

0 comments on commit 2fa483b

Please sign in to comment.