Skip to content

Commit a625f70

Browse files
committed
fix(selection-list): improve accessibility of selection list
* Since the selection list is still a `list` that contains some content, there should be a way for screenreader users, to navigate through the options (even if disabled). With this change, if the list is disabled, it's still possible to walk through the options. * Adds a new functionality to the `ListKeyManager` that allows the developer to control the items that can't be focused. `skipPredicate`. (e.g. for the selection list we want to make sure that users can navigate using the arrow keys to disabled items as well) * Testing: Fixes that by default all fake events bubble up the DOM. This works in most of the cases, but it's wrong to always bubble events. e.g. `focus` doesn't bubble. Fixes #9995
1 parent 653d8dc commit a625f70

File tree

8 files changed

+79
-49
lines changed

8 files changed

+79
-49
lines changed

src/cdk/a11y/key-manager/list-key-manager.spec.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ import {Subject} from 'rxjs/Subject';
1111

1212

1313
class FakeFocusable {
14-
constructor(private _label = '') { }
14+
/** Whether the item is disabled or not.*/
1515
disabled = false;
16+
/** Test property that can be used to test the `skipPredicate` functionality. */
17+
skipItem = false;
18+
constructor(private _label = '') {}
1619
focus(_focusOrigin?: FocusOrigin) {}
1720
getLabel() { return this._label; }
1821
}
@@ -480,6 +483,31 @@ describe('Key managers', () => {
480483
});
481484
});
482485

486+
describe('skip predicate', () => {
487+
488+
it('should skip disabled items by default', () => {
489+
itemList.items[1].disabled = true;
490+
491+
expect(keyManager.activeItemIndex).toBe(0);
492+
493+
keyManager.onKeydown(fakeKeyEvents.downArrow);
494+
495+
expect(keyManager.activeItemIndex).toBe(2);
496+
});
497+
498+
it('should be able to skip items with a custom predicate', () => {
499+
keyManager.skipPredicate(item => item.skipItem);
500+
501+
itemList.items[1].skipItem = true;
502+
503+
expect(keyManager.activeItemIndex).toBe(0);
504+
505+
keyManager.onKeydown(fakeKeyEvents.downArrow);
506+
507+
expect(keyManager.activeItemIndex).toBe(2);
508+
});
509+
});
510+
483511
describe('typeahead mode', () => {
484512
const debounceInterval = 300;
485513

src/cdk/a11y/key-manager/list-key-manager.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ export class ListKeyManager<T extends ListKeyManagerOption> {
4747
private _vertical = true;
4848
private _horizontal: 'ltr' | 'rtl' | null;
4949

50+
/**
51+
* Predicate function that can be used to check whether an item should be skipped
52+
* by the key manager. By default, disabled items are skipped.
53+
*/
54+
private _skipPredicateFn = (item: T) => item.disabled;
55+
5056
// Buffer for the letters that the user has pressed when the typeahead option is turned on.
5157
private _pressedLetters: string[] = [];
5258

@@ -72,6 +78,16 @@ export class ListKeyManager<T extends ListKeyManagerOption> {
7278
/** Stream that emits whenever the active item of the list manager changes. */
7379
change = new Subject<number>();
7480

81+
/**
82+
* Predicate function that determines items that should be skipped by the list key manager.
83+
* By default, disabled items are skipped by the key manager.
84+
* @param predicate Function that determines whether the given item should be skipped.
85+
*/
86+
skipPredicate(predicate: (item: T) => boolean): this {
87+
this._skipPredicateFn = predicate;
88+
return this;
89+
}
90+
7591
/**
7692
* Turns on wrapping mode, which ensures that the active item will wrap to
7793
* the other end of list when there are no more items in the given direction.
@@ -128,7 +144,9 @@ export class ListKeyManager<T extends ListKeyManagerOption> {
128144
const index = (this._activeItemIndex + i) % items.length;
129145
const item = items[index];
130146

131-
if (!item.disabled && item.getLabel!().toUpperCase().trim().indexOf(inputString) === 0) {
147+
if (!this._skipPredicateFn(item) &&
148+
item.getLabel!().toUpperCase().trim().indexOf(inputString) === 0) {
149+
132150
this.setActiveItem(index);
133151
break;
134152
}
@@ -274,7 +292,7 @@ export class ListKeyManager<T extends ListKeyManagerOption> {
274292
const index = (this._activeItemIndex + (delta * i) + items.length) % items.length;
275293
const item = items[index];
276294

277-
if (!item.disabled) {
295+
if (!this._skipPredicateFn(item)) {
278296
this.setActiveItem(index);
279297
return;
280298
}
@@ -301,7 +319,7 @@ export class ListKeyManager<T extends ListKeyManagerOption> {
301319
return;
302320
}
303321

304-
while (items[index].disabled) {
322+
while (this._skipPredicateFn(items[index])) {
305323
index += fallbackDelta;
306324

307325
if (!items[index]) {

src/cdk/testing/event-objects.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export function createKeyboardEvent(type: string, keyCode: number, target?: Elem
7474
}
7575

7676
/** Creates a fake event object with any desired event type. */
77-
export function createFakeEvent(type: string, canBubble = true, cancelable = true) {
77+
export function createFakeEvent(type: string, canBubble = false, cancelable = true) {
7878
const event = document.createEvent('Event');
7979
event.initEvent(type, canBubble, cancelable);
8080
return event;

src/lib/list/_list-theme.scss

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,8 @@
2626
background-color: mat-color($background, disabled-list-option);
2727
}
2828

29+
.mat-list-option,
2930
.mat-nav-list .mat-list-item {
30-
outline: none;
31-
32-
&:hover, &.mat-list-item-focus {
33-
background: mat-color($background, 'hover');
34-
}
35-
}
36-
37-
.mat-list-option {
38-
outline: none;
39-
4031
&:hover, &.mat-list-item-focus {
4132
background: mat-color($background, 'hover');
4233
}

src/lib/list/list-option.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<div class="mat-list-item-content"
2-
[class.mat-list-item-content-reverse]="checkboxPosition == 'after'"
3-
[class.mat-list-item-disabled]="disabled">
2+
[class.mat-list-item-content-reverse]="checkboxPosition == 'after'">
43

54
<div mat-ripple
65
class="mat-list-item-ripple"

src/lib/list/list.scss

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -237,24 +237,19 @@ $mat-list-item-inset-divider-offset: 72px;
237237
}
238238
}
239239

240-
241240
.mat-nav-list {
242241
a {
243242
text-decoration: none;
244243
color: inherit;
245244
}
246245

247-
.mat-list-item-content {
246+
.mat-list-item {
248247
cursor: pointer;
249-
250-
&:hover, &.mat-list-item-focus {
251-
outline: none;
252-
}
248+
outline: none;
253249
}
254250
}
255251

256-
.mat-list-option {
257-
&:not(.mat-list-item-disabled) {
258-
cursor: pointer;
259-
}
252+
.mat-list-option:not(.mat-list-item-disabled) {
253+
cursor: pointer;
254+
outline: none;
260255
}

src/lib/list/selection-list.spec.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -251,30 +251,25 @@ describe('MatSelectionList without forms', () => {
251251
});
252252

253253
it('should focus next item when press DOWN ARROW', () => {
254-
let testListItem = listOptions[2].nativeElement as HTMLElement;
255-
let DOWN_EVENT: KeyboardEvent =
256-
createKeyboardEvent('keydown', DOWN_ARROW, testListItem);
257-
let manager = selectionList.componentInstance._keyManager;
254+
const manager = selectionList.componentInstance._keyManager;
258255

259256
dispatchFakeEvent(listOptions[2].nativeElement, 'focus');
260257
expect(manager.activeItemIndex).toEqual(2);
261258

262-
selectionList.componentInstance._keydown(DOWN_EVENT);
263-
259+
selectionList.componentInstance._keydown(createKeyboardEvent('keydown', DOWN_ARROW));
264260
fixture.detectChanges();
265261

266262
expect(manager.activeItemIndex).toEqual(3);
267263
});
268264

269-
it('should focus the first non-disabled item when pressing HOME', () => {
265+
it('should be able to focus the first item when pressing HOME', () => {
270266
const manager = selectionList.componentInstance._keyManager;
271267
expect(manager.activeItemIndex).toBe(-1);
272268

273269
const event = dispatchKeyboardEvent(selectionList.nativeElement, 'keydown', HOME);
274270
fixture.detectChanges();
275271

276-
// Note that the first item is disabled so we expect the second one to be focused.
277-
expect(manager.activeItemIndex).toBe(1);
272+
expect(manager.activeItemIndex).toBe(0);
278273
expect(event.defaultPrevented).toBe(true);
279274
});
280275

@@ -425,7 +420,7 @@ describe('MatSelectionList without forms', () => {
425420
fixture.detectChanges();
426421

427422
expect(selectionList.componentInstance.tabIndex)
428-
.toBe(-1, 'Expected the tabIndex to be set to "-1" if selection list is disabled.');
423+
.toBe(3, 'Expected the tabIndex to be still set to "3".');
429424
});
430425
});
431426

src/lib/list/selection-list.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,18 @@ import {
3232
import {
3333
CanDisable,
3434
CanDisableRipple,
35-
HasTabIndex,
3635
MatLine,
3736
MatLineSetter,
3837
mixinDisabled,
3938
mixinDisableRipple,
40-
mixinTabIndex,
4139
} from '@angular/material/core';
4240
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from '@angular/forms';
4341
import {Subscription} from 'rxjs/Subscription';
4442

4543

4644
/** @docs-private */
4745
export class MatSelectionListBase {}
48-
export const _MatSelectionListMixinBase =
49-
mixinTabIndex(mixinDisableRipple(mixinDisabled(MatSelectionListBase)));
46+
export const _MatSelectionListMixinBase = mixinDisableRipple(mixinDisabled(MatSelectionListBase));
5047

5148
/** @docs-private */
5249
export class MatListOptionBase {}
@@ -294,7 +291,7 @@ export class MatListOption extends _MatListOptionMixinBase
294291
changeDetection: ChangeDetectionStrategy.OnPush
295292
})
296293
export class MatSelectionList extends _MatSelectionListMixinBase implements FocusableOption,
297-
CanDisable, CanDisableRipple, HasTabIndex, AfterContentInit, ControlValueAccessor, OnDestroy {
294+
CanDisable, CanDisableRipple, AfterContentInit, ControlValueAccessor, OnDestroy {
298295

299296
/** The FocusKeyManager which handles focus. */
300297
_keyManager: FocusKeyManager<MatListOption>;
@@ -306,6 +303,9 @@ export class MatSelectionList extends _MatSelectionListMixinBase implements Focu
306303
@Output() readonly selectionChange: EventEmitter<MatSelectionListChange> =
307304
new EventEmitter<MatSelectionListChange>();
308305

306+
/** Tabindex of the selection list. */
307+
@Input() tabIndex: number = 0;
308+
309309
/** The currently selected options. */
310310
selectedOptions: SelectionModel<MatListOption> = new SelectionModel<MatListOption>(true);
311311

@@ -327,7 +327,12 @@ export class MatSelectionList extends _MatSelectionListMixinBase implements Focu
327327
}
328328

329329
ngAfterContentInit(): void {
330-
this._keyManager = new FocusKeyManager<MatListOption>(this.options).withWrap().withTypeAhead();
330+
this._keyManager = new FocusKeyManager<MatListOption>(this.options)
331+
.withWrap()
332+
.withTypeAhead()
333+
// Allow disabled items to be focusable. For accessibility reasons, there must be a way for
334+
// screenreader users, that allows reading the different options of the list.
335+
.skipPredicate(() => false);
331336

332337
if (this._tempValues) {
333338
this._setOptionsFromValues(this._tempValues);
@@ -354,7 +359,7 @@ export class MatSelectionList extends _MatSelectionListMixinBase implements Focu
354359
this._modelChanges.unsubscribe();
355360
}
356361

357-
/** Focus the selection-list. */
362+
/** Focuses the last active list option. */
358363
focus() {
359364
this._element.nativeElement.focus();
360365
}
@@ -392,16 +397,15 @@ export class MatSelectionList extends _MatSelectionListMixinBase implements Focu
392397

393398
/** Passes relevant key presses to our key manager. */
394399
_keydown(event: KeyboardEvent) {
395-
if (this.disabled) {
396-
return;
397-
}
398-
399400
switch (event.keyCode) {
400401
case SPACE:
401402
case ENTER:
402-
this._toggleSelectOnFocusedOption();
403403
// Always prevent space from scrolling the page since the list has focus
404404
event.preventDefault();
405+
406+
if (!this.disabled) {
407+
this._toggleSelectOnFocusedOption();
408+
}
405409
break;
406410
case HOME:
407411
case END:

0 commit comments

Comments
 (0)