Skip to content
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
29 changes: 26 additions & 3 deletions src/cdk-experimental/ui-patterns/tree/tree.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -501,19 +501,33 @@ describe('Tree Pattern', () => {

tree.onKeydown(space());
expect(tree.inputs.value()).toEqual(['Item 0']);
});

it('should not deselect an item on Space', () => {
const {tree} = createTree(treeExample, treeInputs);

tree.onKeydown(space());
expect(tree.inputs.value()).toEqual([]);
expect(tree.inputs.value()).toEqual(['Item 0']);

tree.onKeydown(space());
expect(tree.inputs.value()).toEqual(['Item 0']);
});

it('should select an item on Enter', () => {
const {tree} = createTree(treeExample, treeInputs);

tree.onKeydown(enter());
expect(tree.inputs.value()).toEqual(['Item 0']);
});

it('should not deselect an item on Enter', () => {
const {tree} = createTree(treeExample, treeInputs);

tree.onKeydown(enter());
expect(tree.inputs.value()).toEqual([]);
expect(tree.inputs.value()).toEqual(['Item 0']);

tree.onKeydown(enter());
expect(tree.inputs.value()).toEqual(['Item 0']);
});

it('should only allow one selected item', () => {
Expand Down Expand Up @@ -919,10 +933,19 @@ describe('Tree Pattern', () => {
tree.onPointerdown(createClickEvent(item1.element()));
expect(tree.activeItem()).toBe(item1);
expect(tree.inputs.value()).toEqual(['Item 1']);
});

it('should not deselect item on click', () => {
const {tree, allItems} = createTree(treeExample, treeInputs);
const item1 = getItemByValue(allItems(), 'Item 1');

tree.onPointerdown(createClickEvent(item1.element()));
expect(tree.activeItem()).toBe(item1);
expect(tree.inputs.value()).toEqual([]);
expect(tree.inputs.value()).toEqual(['Item 1']);

tree.onPointerdown(createClickEvent(item1.element()));
expect(tree.activeItem()).toBe(item1);
expect(tree.inputs.value()).toEqual(['Item 1']);
});

it('should not change selection when the tree is disabled', () => {
Expand Down
10 changes: 3 additions & 7 deletions src/cdk-experimental/ui-patterns/tree/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,8 @@ export class TreePattern<V> {
}

if (!this.followFocus() && !this.inputs.multi()) {
manager.on(this.dynamicSpaceKey, () => list.toggleOne());
manager.on('Enter', () => list.toggleOne());
manager.on(this.dynamicSpaceKey, () => list.selectOne());
manager.on('Enter', () => list.selectOne());
}

if (this.inputs.multi() && this.followFocus()) {
Expand Down Expand Up @@ -275,14 +275,10 @@ export class TreePattern<V> {
manager.on(Modifier.Shift, e => this.goto(e, {selectRange: true}));
}

if (!this.multi() && this.followFocus()) {
if (!this.multi()) {
return manager.on(e => this.goto(e, {selectOne: true}));
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this just be combined together? I am not sure if I understand why we change this to toggle vs selectOne? Is it because it isn't mulit?

Copy link
Member Author

@ok7sai ok7sai Sep 9, 2025

Choose a reason for hiding this comment

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

No, when it's not multi-selectable clicking on the same tree item shouldn't deselect it https://www.w3.org/WAI/ARIA/apg/patterns/treeview/examples/treeview-1a/ so it should perform selectOne instead of toggle.

The current behavior https://ng-comp-devapp.web.app/cdk-experimental-tree is incorrect.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry - I am still a noob at github - I meant lines 279 is also doing the same thing. So essentially it just would need to check if it is not multi.

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh oops yes you are absolutely correct. It should be combined to

if (!this.multi()) {
  return manager.on(e => this.goto(e, {selectOne: true}));
}

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed! Thanks for catching that redundant code!

}

if (!this.multi() && !this.followFocus()) {
return manager.on(e => this.goto(e, {toggle: true}));
}

if (this.multi() && this.followFocus()) {
return manager
.on(e => this.goto(e, {selectOne: true}))
Expand Down
Loading