Skip to content

Commit 6db3511

Browse files
traviskaufmanjelbourn
authored andcommitted
fix(checkbox): Cancel spacebar keydown events when component focused
This commit ensures that when a checkbox element is focused, spacebar `keydown` events will not bubble up to window, causing side effects like page scrolling. This commit also fixes an issue as part of this change where the component could still be focused even when it was disabled. Finally, some of the test code has been cleaned up and corrected. Fixes #162 Closes #181
1 parent 666cdba commit 6db3511

File tree

2 files changed

+72
-12
lines changed

2 files changed

+72
-12
lines changed

Diff for: src/components/checkbox/checkbox.spec.ts

+61-10
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,9 @@ export function main() {
289289
expect(el.nativeElement.className).toContain('md-checkbox-disabled');
290290
});
291291

292-
it('sets the tabindex to -1 on the host element', function() {
292+
it('removes the tabindex attribute from the host element', function() {
293293
let el = fixture.debugElement.query(By.css('.md-checkbox'));
294-
expect(el.nativeElement.getAttribute('tabindex')).toEqual('-1');
294+
expect(el.nativeElement.hasAttribute('tabindex')).toBe(false);
295295
});
296296

297297
it('sets "aria-disabled" to "true" on the host element', function() {
@@ -307,7 +307,7 @@ export function main() {
307307
tabindexController.isDisabled = true;
308308
fixture.detectChanges();
309309
let el = fixture.debugElement.query(By.css('.md-checkbox'));
310-
expect(el.nativeElement.getAttribute('tabindex')).toEqual('-1');
310+
expect(el.nativeElement.hasAttribute('tabindex')).toBe(false);
311311

312312
tabindexController.isDisabled = false;
313313
fixture.detectChanges();
@@ -334,9 +334,9 @@ export function main() {
334334
}).then(done).catch(done);
335335
});
336336

337-
it('keeps the tabindex at -1', function() {
337+
it('keeps the tabindex removed from the host', function() {
338338
let el = fixture.debugElement.query(By.css('.md-checkbox'));
339-
expect(el.nativeElement.getAttribute('tabindex')).toEqual('-1');
339+
expect(el.nativeElement.hasAttribute('tabindex')).toBe(false);
340340
});
341341

342342
it('uses the newly changed tabindex when re-enabled', function() {
@@ -394,6 +394,48 @@ export function main() {
394394
});
395395
});
396396

397+
describe('when the checkbox is focused', function() {
398+
var fixture: ComponentFixture;
399+
var controller: CheckboxController;
400+
var el: DebugElement;
401+
402+
function dispatchUIEventOnEl(evtName: string) {
403+
var evt: Event;
404+
if (BROWSER_SUPPORTS_EVENT_CONSTRUCTORS) {
405+
evt = new Event(evtName);
406+
} else {
407+
evt = document.createEvent('Event');
408+
evt.initEvent(evtName, true, true);
409+
}
410+
el.nativeElement.dispatchEvent(evt);
411+
fixture.detectChanges();
412+
}
413+
414+
beforeEach(function(done: () => void) {
415+
builder.createAsync(CheckboxController).then(function(f) {
416+
fixture = f;
417+
controller = fixture.componentInstance;
418+
419+
fixture.detectChanges();
420+
el = fixture.debugElement.query(By.css('.md-checkbox'));
421+
}).then(done).catch(done);
422+
});
423+
424+
it('blocks spacebar keydown events from performing their default behavior', function() {
425+
dispatchUIEventOnEl('focus');
426+
427+
var evt = keydown(el, ' ', fixture);
428+
expect(evt.preventDefault).toHaveBeenCalled();
429+
});
430+
431+
it('does not block other keyboard events from performing their default behavior', function() {
432+
dispatchUIEventOnEl('focus');
433+
434+
var evt = keydown(el, 'Tab', fixture);
435+
expect(evt.preventDefault).not.toHaveBeenCalled();
436+
});
437+
});
438+
397439
describe('when a spacebar press occurs on the checkbox', function() {
398440
var fixture: ComponentFixture;
399441
var controller: CheckboxController;
@@ -597,27 +639,36 @@ function click(el: DebugElement, fixture: ComponentFixture) {
597639
}
598640

599641
// TODO(traviskaufman): Reinvestigate implementation of this method once tests in Dart begin to run.
600-
function keyup(el: DebugElement, key: string, fixture: ComponentFixture) {
642+
function keyEvent(name: string, el: DebugElement, key: string, fixture: ComponentFixture): Event {
601643
var kbdEvent: Event;
602644
if (BROWSER_SUPPORTS_EVENT_CONSTRUCTORS) {
603-
kbdEvent = new KeyboardEvent('keyup');
645+
kbdEvent = new KeyboardEvent(name);
604646
} else {
605-
kbdEvent = document.createEvent('Events');
606-
kbdEvent.initEvent('keyup', true, true);
647+
kbdEvent = document.createEvent('Event');
648+
kbdEvent.initEvent(name, true, true);
607649
}
608650
// Hack DOM Level 3 Events "key" prop into keyboard event.
609651
Object.defineProperty(kbdEvent, 'key', {
610-
value: ' ',
652+
value: key,
611653
enumerable: false,
612654
writable: false,
613655
configurable: true
614656
});
657+
spyOn(kbdEvent, 'preventDefault').and.callThrough();
615658
spyOn(kbdEvent, 'stopPropagation').and.callThrough();
616659
el.nativeElement.dispatchEvent(kbdEvent);
617660
fixture.detectChanges();
618661
return kbdEvent;
619662
}
620663

664+
function keydown(el: DebugElement, key: string, fixture: ComponentFixture): Event {
665+
return keyEvent('keydown', el, key, fixture);
666+
}
667+
668+
function keyup(el: DebugElement, key: string, fixture: ComponentFixture): Event {
669+
return keyEvent('keyup', el, key, fixture);
670+
}
671+
621672
@Component({
622673
selector: 'checkbox-controller',
623674
template: `

Diff for: src/components/checkbox/checkbox.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,13 @@ enum TransitionCheckState {
6868
'[class.md-checkbox-checked]': 'checked',
6969
'[class.md-checkbox-disabled]': 'disabled',
7070
'[class.md-checkbox-align-end]': 'align == "end"',
71-
'[tabindex]': 'disabled ? -1 : tabindex',
71+
'[attr.tabindex]': 'disabled ? null : tabindex',
7272
'[attr.aria-label]': 'ariaLabel',
7373
'[attr.aria-labelledby]': 'labelId',
7474
'[attr.aria-checked]': 'getAriaChecked()',
7575
'[attr.aria-disabled]': 'disabled',
7676
'(click)': 'onInteractionEvent($event)',
77+
'(keydown.space)': 'onSpaceDown($event)',
7778
'(keyup.space)': 'onInteractionEvent($event)',
7879
'(blur)': 'onTouched()'
7980
},
@@ -102,7 +103,7 @@ export class MdCheckbox implements ControlValueAccessor {
102103

103104
/**
104105
* The tabindex attribute for the checkbox. Note that when the checkbox is disabled, the attribute
105-
* on the host element will be set to -1, regardless of the actual tabindex value.
106+
* on the host element will be removed. It will be placed back when the checkbox is re-enabled.
106107
*/
107108
@Input() tabindex: number = 0;
108109

@@ -192,6 +193,14 @@ export class MdCheckbox implements ControlValueAccessor {
192193
this.toggle();
193194
}
194195

196+
/**
197+
* Event handler used for (keydown.space) events. Used to prevent spacebar events from bubbling
198+
* when the component is focused, which prevents side effects like page scrolling from happening.
199+
*/
200+
onSpaceDown(evt: Event) {
201+
evt.preventDefault();
202+
}
203+
195204
/** Implemented as part of ControlValueAccessor. */
196205
writeValue(value: any) {
197206
this.checked = !!value;

0 commit comments

Comments
 (0)