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): allow deselection in single selectionMode #9363

Merged
merged 20 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
68de2ce
fix(tree): allow selection/deselection in single mode
josercarcamo May 17, 2024
4468431
Merge branch 'main' into josercarcamo/7900-tree-single-select
josercarcamo May 17, 2024
8bd48ed
Changed test to reflect change in logic
josercarcamo May 20, 2024
b5bb18f
Merge branch 'josercarcamo/7900-tree-single-select' of github.com:Esr…
josercarcamo May 20, 2024
23a258a
Changed test to reflect change in logic
josercarcamo May 20, 2024
9034b68
Changed test to reflect change in logic
josercarcamo May 20, 2024
200c791
Changed test to reflect change in logic
josercarcamo May 20, 2024
450da7e
Changed test to reflect change in logic
josercarcamo May 21, 2024
4ce7028
Changed test to reflect change in logic
josercarcamo May 22, 2024
7234981
Changed test to reflect change in logic
josercarcamo May 22, 2024
0a927e8
Refactored line of code per code review
josercarcamo May 22, 2024
abb4039
Merge branch 'main' into josercarcamo/7900-tree-single-select
josercarcamo May 22, 2024
0cd9db2
Reverted line of code because not logically equivalent
josercarcamo May 22, 2024
c799667
Rewrote suggested line
josercarcamo May 22, 2024
03a1451
Merge branch 'josercarcamo/7900-tree-single-select' of github.com:Esr…
josercarcamo May 22, 2024
246ad2c
Uncommented test case
josercarcamo May 22, 2024
fac0a89
Uncommented test case
josercarcamo May 22, 2024
b307e44
Uncommented test case
josercarcamo May 22, 2024
c16ec3b
Uncommented test case
josercarcamo May 22, 2024
70b16df
Uncommented test case
josercarcamo May 22, 2024
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
31 changes: 31 additions & 0 deletions packages/calcite-components/src/components/tree/tree.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1316,5 +1316,36 @@ describe("calcite-tree", () => {
},
);
}

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 : true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: this.selectionMode === "single" || !treeItem.selected;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, the two lines are not logically equivalent: The suggested line will return "true" whenever "selectionMode" is "single"; however, the existing line can return true or false when "selectionMode" is "single". The line can be changed to:

this.selectionMode !== "single" || !treeItem.selected"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes look good. 👍 Merge approved

}
});
}
Expand Down
Loading