From 2361385dfc6ce8e8225c2a89b1536e4193874c55 Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Fri, 23 Jun 2017 21:58:35 +0200 Subject: [PATCH] fix(select): page scrolling down when selecting option with space (#5192) Fixes the page being scrolled down if the user presses space to select an option. This is a regression since it used to be handled by the `ListKeyManager`, but it got lost when it was refactored not to disable the default actions. --- src/lib/core/option/option.ts | 3 +++ src/lib/select/select.spec.ts | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/lib/core/option/option.ts b/src/lib/core/option/option.ts index 4cc82b6c9b7..7e0ecf022c7 100644 --- a/src/lib/core/option/option.ts +++ b/src/lib/core/option/option.ts @@ -149,6 +149,9 @@ export class MdOption { _handleKeydown(event: KeyboardEvent): void { if (event.keyCode === ENTER || event.keyCode === SPACE) { this._selectViaInteraction(); + + // Prevent the page from scrolling down and form submits. + event.preventDefault(); } } diff --git a/src/lib/select/select.spec.ts b/src/lib/select/select.spec.ts index 52da5e0bbd5..35a646ec885 100644 --- a/src/lib/select/select.spec.ts +++ b/src/lib/select/select.spec.ts @@ -277,6 +277,26 @@ describe('MdSelect', () => { expect(panel.classList).toContain('custom-two'); }); + it('should prevent the default action when pressing SPACE on an option', () => { + trigger.click(); + fixture.detectChanges(); + + const option = overlayContainerElement.querySelector('md-option'); + const event = dispatchKeyboardEvent(option, 'keydown', SPACE); + + expect(event.defaultPrevented).toBe(true); + }); + + it('should prevent the default action when pressing ENTER on an option', () => { + trigger.click(); + fixture.detectChanges(); + + const option = overlayContainerElement.querySelector('md-option'); + const event = dispatchKeyboardEvent(option, 'keydown', ENTER); + + expect(event.defaultPrevented).toBe(true); + }); + }); describe('selection logic', () => {