Skip to content

Commit

Permalink
fix(list): incorrectly selecting items when moving focus using shift …
Browse files Browse the repository at this point in the history
…+ arrow key in single selection mode (#18579)

The functionality that allows selecting items while moving focus using shift + arrow key is currently always enabled, however it should only apply in multi selection mode.
  • Loading branch information
crisbeto committed Mar 12, 2020
1 parent b2d08df commit ee3958d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
38 changes: 38 additions & 0 deletions src/material/list/selection-list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,44 @@ describe('MatSelectionList without forms', () => {
expect(listOptions.every(option => !option.componentInstance.selected)).toBe(true);
});

it('should focus, but not toggle, the next item when pressing SHIFT + UP_ARROW in single ' +
'selection mode', () => {
const manager = selectionList.componentInstance._keyManager;
const upKeyEvent = createKeyboardEvent('keydown', UP_ARROW);
Object.defineProperty(upKeyEvent, 'shiftKey', {get: () => true});

dispatchFakeEvent(listOptions[3].nativeElement, 'focus');
expect(manager.activeItemIndex).toBe(3);

expect(listOptions[1].componentInstance.selected).toBe(false);
expect(listOptions[2].componentInstance.selected).toBe(false);

selectionList.componentInstance._keydown(upKeyEvent);
fixture.detectChanges();

expect(listOptions[1].componentInstance.selected).toBe(false);
expect(listOptions[2].componentInstance.selected).toBe(false);
});

it('should focus, but not toggle, the next item when pressing SHIFT + DOWN_ARROW ' +
'in single selection mode', () => {
const manager = selectionList.componentInstance._keyManager;
const downKeyEvent = createKeyboardEvent('keydown', DOWN_ARROW);
Object.defineProperty(downKeyEvent, 'shiftKey', {get: () => true});

dispatchFakeEvent(listOptions[0].nativeElement, 'focus');
expect(manager.activeItemIndex).toBe(0);

expect(listOptions[1].componentInstance.selected).toBe(false);
expect(listOptions[2].componentInstance.selected).toBe(false);

selectionList.componentInstance._keydown(downKeyEvent);
fixture.detectChanges();

expect(listOptions[1].componentInstance.selected).toBe(false);
expect(listOptions[2].componentInstance.selected).toBe(false);
});

});
});

Expand Down
2 changes: 1 addition & 1 deletion src/material/list/selection-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ export class MatSelectionList extends _MatSelectionListMixinBase implements CanD
}
}

if ((keyCode === UP_ARROW || keyCode === DOWN_ARROW) && event.shiftKey &&
if (this.multiple && (keyCode === UP_ARROW || keyCode === DOWN_ARROW) && event.shiftKey &&
manager.activeItemIndex !== previousFocusIndex) {
this._toggleFocusedOption();
}
Expand Down

0 comments on commit ee3958d

Please sign in to comment.