Skip to content

Commit 2361385

Browse files
crisbetojelbourn
authored andcommitted
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.
1 parent fc809ed commit 2361385

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/lib/core/option/option.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ export class MdOption {
149149
_handleKeydown(event: KeyboardEvent): void {
150150
if (event.keyCode === ENTER || event.keyCode === SPACE) {
151151
this._selectViaInteraction();
152+
153+
// Prevent the page from scrolling down and form submits.
154+
event.preventDefault();
152155
}
153156
}
154157

src/lib/select/select.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,26 @@ describe('MdSelect', () => {
277277
expect(panel.classList).toContain('custom-two');
278278
});
279279

280+
it('should prevent the default action when pressing SPACE on an option', () => {
281+
trigger.click();
282+
fixture.detectChanges();
283+
284+
const option = overlayContainerElement.querySelector('md-option');
285+
const event = dispatchKeyboardEvent(option, 'keydown', SPACE);
286+
287+
expect(event.defaultPrevented).toBe(true);
288+
});
289+
290+
it('should prevent the default action when pressing ENTER on an option', () => {
291+
trigger.click();
292+
fixture.detectChanges();
293+
294+
const option = overlayContainerElement.querySelector('md-option');
295+
const event = dispatchKeyboardEvent(option, 'keydown', ENTER);
296+
297+
expect(event.defaultPrevented).toBe(true);
298+
});
299+
280300
});
281301

282302
describe('selection logic', () => {

0 commit comments

Comments
 (0)