Skip to content

Commit

Permalink
fix(select): page scrolling down when selecting option with space (#5192
Browse files Browse the repository at this point in the history
)

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.
  • Loading branch information
crisbeto authored and jelbourn committed Jun 23, 2017
1 parent fc809ed commit 2361385
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/lib/core/option/option.ts
Expand Up @@ -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();
}
}

Expand Down
20 changes: 20 additions & 0 deletions src/lib/select/select.spec.ts
Expand Up @@ -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', () => {
Expand Down

0 comments on commit 2361385

Please sign in to comment.