diff --git a/src/cdk-experimental/ui-patterns/tree/tree.spec.ts b/src/cdk-experimental/ui-patterns/tree/tree.spec.ts index f1231ab6371c..0ffc2fda466e 100644 --- a/src/cdk-experimental/ui-patterns/tree/tree.spec.ts +++ b/src/cdk-experimental/ui-patterns/tree/tree.spec.ts @@ -501,9 +501,16 @@ 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', () => { @@ -511,9 +518,16 @@ describe('Tree Pattern', () => { 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', () => { @@ -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', () => { diff --git a/src/cdk-experimental/ui-patterns/tree/tree.ts b/src/cdk-experimental/ui-patterns/tree/tree.ts index b348c9c57741..eed86e65434f 100644 --- a/src/cdk-experimental/ui-patterns/tree/tree.ts +++ b/src/cdk-experimental/ui-patterns/tree/tree.ts @@ -244,8 +244,8 @@ export class TreePattern { } 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()) { @@ -275,14 +275,10 @@ export class TreePattern { 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})); } - 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}))