Skip to content

Commit c21dfa3

Browse files
authored
fix(cdk-experimental/ui-patterns): enter/space/click in single selection mode should not deselect tree item (angular#31843)
1 parent 9f01fe9 commit c21dfa3

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

src/cdk-experimental/ui-patterns/tree/tree.spec.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -501,19 +501,33 @@ describe('Tree Pattern', () => {
501501

502502
tree.onKeydown(space());
503503
expect(tree.inputs.value()).toEqual(['Item 0']);
504+
});
505+
506+
it('should not deselect an item on Space', () => {
507+
const {tree} = createTree(treeExample, treeInputs);
504508

505509
tree.onKeydown(space());
506-
expect(tree.inputs.value()).toEqual([]);
510+
expect(tree.inputs.value()).toEqual(['Item 0']);
511+
512+
tree.onKeydown(space());
513+
expect(tree.inputs.value()).toEqual(['Item 0']);
507514
});
508515

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

512519
tree.onKeydown(enter());
513520
expect(tree.inputs.value()).toEqual(['Item 0']);
521+
});
522+
523+
it('should not deselect an item on Enter', () => {
524+
const {tree} = createTree(treeExample, treeInputs);
514525

515526
tree.onKeydown(enter());
516-
expect(tree.inputs.value()).toEqual([]);
527+
expect(tree.inputs.value()).toEqual(['Item 0']);
528+
529+
tree.onKeydown(enter());
530+
expect(tree.inputs.value()).toEqual(['Item 0']);
517531
});
518532

519533
it('should only allow one selected item', () => {
@@ -919,10 +933,19 @@ describe('Tree Pattern', () => {
919933
tree.onPointerdown(createClickEvent(item1.element()));
920934
expect(tree.activeItem()).toBe(item1);
921935
expect(tree.inputs.value()).toEqual(['Item 1']);
936+
});
937+
938+
it('should not deselect item on click', () => {
939+
const {tree, allItems} = createTree(treeExample, treeInputs);
940+
const item1 = getItemByValue(allItems(), 'Item 1');
922941

923942
tree.onPointerdown(createClickEvent(item1.element()));
924943
expect(tree.activeItem()).toBe(item1);
925-
expect(tree.inputs.value()).toEqual([]);
944+
expect(tree.inputs.value()).toEqual(['Item 1']);
945+
946+
tree.onPointerdown(createClickEvent(item1.element()));
947+
expect(tree.activeItem()).toBe(item1);
948+
expect(tree.inputs.value()).toEqual(['Item 1']);
926949
});
927950

928951
it('should not change selection when the tree is disabled', () => {

src/cdk-experimental/ui-patterns/tree/tree.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,8 @@ export class TreePattern<V> {
244244
}
245245

246246
if (!this.followFocus() && !this.inputs.multi()) {
247-
manager.on(this.dynamicSpaceKey, () => list.toggleOne());
248-
manager.on('Enter', () => list.toggleOne());
247+
manager.on(this.dynamicSpaceKey, () => list.selectOne());
248+
manager.on('Enter', () => list.selectOne());
249249
}
250250

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

278-
if (!this.multi() && this.followFocus()) {
278+
if (!this.multi()) {
279279
return manager.on(e => this.goto(e, {selectOne: true}));
280280
}
281281

282-
if (!this.multi() && !this.followFocus()) {
283-
return manager.on(e => this.goto(e, {toggle: true}));
284-
}
285-
286282
if (this.multi() && this.followFocus()) {
287283
return manager
288284
.on(e => this.goto(e, {selectOne: true}))

0 commit comments

Comments
 (0)