Skip to content

Commit

Permalink
fix(tree): allow deselection in single selectionMode (#9363)
Browse files Browse the repository at this point in the history
**Related Issue:** #7900

## Summary
Made sure an item can be selected and deselected in single selection mode.
  • Loading branch information
josercarcamo authored May 23, 2024
1 parent d294d1d commit cb6ef73
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
46 changes: 39 additions & 7 deletions packages/calcite-components/src/components/tree/tree.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1231,11 +1231,11 @@ describe("calcite-tree", () => {
Child 2
<calcite-tree slot="children">
<calcite-tree-item id="${expandableItemChildId}">Grandchild 1</calcite-tree-item>
<calcite-tree-item>Grandchild 1</calcite-tree-item>
<calcite-tree-item>Grandchild 2</calcite-tree-item>
<calcite-tree-item>
<calcite-tree-item id="${expandableItemChildId}">
Grandchild 3
<calcite-tree slot="children">
<calcite-tree-item>Great-Grandchild 1</calcite-tree-item>
Expand Down Expand Up @@ -1292,10 +1292,11 @@ describe("calcite-tree", () => {
? 0
: selectionMode === "ancestors" && !childToggleTraversesParent
? 7
: !childToggleTraversesParent &&
(selectionMode === "multiple" || selectionMode === "single" || selectionMode === "single-persist")
: selectionMode === "multiple" || selectionMode === "single" || selectionMode === "single-persist"
? 0
: 1,
: selectionMode === "children" || selectionMode === "multichildren"
? 1
: 4,
);

await selectItemChild(page, expandableChildItem);
Expand All @@ -1307,14 +1308,45 @@ describe("calcite-tree", () => {
selectionMode === "none"
? 0
: !childToggleTraversesParent && selectionMode === "multiple"
? 1
: canDeselect.child
? 0
: canDeselect.child || selectionMode === "single" || selectionMode === "single-persist"
? 0
: 1,
);
});
},
);
}

it("selects/deselects in single selection", async () => {
const page = await newE2EPage();
await page.setContent(html`
<calcite-tree selection-mode="single">
<calcite-tree-item id="child1">Child 1</calcite-tree-item>
<calcite-tree-item id="sub1">
Child 2
<calcite-tree slot="children">
<calcite-tree-item id="gc1">Grandchild 1</calcite-tree-item>
<calcite-tree-item>Grandchild 2</calcite-tree-item>
</calcite-tree>
</calcite-tree-item>
</calcite-tree>
`);
const tree = await page.find("calcite-tree");
expect(await tree.getProperty("selectedItems")).toHaveLength(0);
const child1 = await page.find("#child1");
await directItemClick(page, child1);
expect(await tree.getProperty("selectedItems")).toHaveLength(1);
await directItemClick(page, child1);
expect(await tree.getProperty("selectedItems")).toHaveLength(0);
const sub1 = await page.find("#sub1");
await directItemClick(page, sub1);
expect(await tree.getProperty("selectedItems")).toHaveLength(0);
const gc1 = await page.find("#gc1");
await directItemClick(page, gc1);
expect(await tree.getProperty("selectedItems")).toHaveLength(1);
await directItemClick(page, gc1);
expect(await tree.getProperty("selectedItems")).toHaveLength(0);
});
});
});
2 changes: 1 addition & 1 deletion packages/calcite-components/src/components/tree/tree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ export class Tree {
} else if (!isNoneSelectionMode) {
targetItems.forEach((treeItem) => {
if (!treeItem.disabled) {
treeItem.selected = true;
treeItem.selected = this.selectionMode !== "single" || !treeItem.selected;
}
});
}
Expand Down

0 comments on commit cb6ef73

Please sign in to comment.