Skip to content

Commit 18c6dec

Browse files
crisbetoandrewseguin
authored andcommitted
fix: improved key manager typings (#6443)
Makes the FocusKeyManager and ActivedescendantKeyManager generic which avoids having to cast the items all the time (e.g. https://github.com/angular/material2/blob/master/src/lib/autocomplete/autocomplete-trigger.ts#L234).
1 parent 85a6fff commit 18c6dec

12 files changed

+26
-26
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export interface Highlightable extends ListKeyManagerOption {
1818
setInactiveStyles(): void;
1919
}
2020

21-
export class ActiveDescendantKeyManager extends ListKeyManager<Highlightable> {
21+
export class ActiveDescendantKeyManager<T> extends ListKeyManager<Highlightable & T> {
2222

2323
/**
2424
* This method sets the active item to the item at the specified index.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export interface FocusableOption extends ListKeyManagerOption {
1717
focus(): void;
1818
}
1919

20-
export class FocusKeyManager extends ListKeyManager<FocusableOption> {
20+
export class FocusKeyManager<T> extends ListKeyManager<FocusableOption & T> {
2121
/**
2222
* This method sets the active item to the item at the specified index.
2323
* It also adds focuses the newly active item.

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -451,11 +451,11 @@ describe('Key managers', () => {
451451
});
452452

453453
describe('FocusKeyManager', () => {
454-
let keyManager: FocusKeyManager;
454+
let keyManager: FocusKeyManager<FakeFocusable>;
455455

456456
beforeEach(() => {
457457
itemList.items = [new FakeFocusable(), new FakeFocusable(), new FakeFocusable()];
458-
keyManager = new FocusKeyManager(itemList);
458+
keyManager = new FocusKeyManager<FakeFocusable>(itemList);
459459

460460
// first item is already focused
461461
keyManager.setFirstItemActive();
@@ -503,11 +503,11 @@ describe('Key managers', () => {
503503
});
504504

505505
describe('ActiveDescendantKeyManager', () => {
506-
let keyManager: ActiveDescendantKeyManager;
506+
let keyManager: ActiveDescendantKeyManager<FakeHighlightable>;
507507

508508
beforeEach(fakeAsync(() => {
509509
itemList.items = [new FakeHighlightable(), new FakeHighlightable(), new FakeHighlightable()];
510-
keyManager = new ActiveDescendantKeyManager(itemList);
510+
keyManager = new ActiveDescendantKeyManager<FakeHighlightable>(itemList);
511511

512512
// first item is already focused
513513
keyManager.setFirstItemActive();

src/lib/autocomplete/autocomplete-trigger.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ export class MdAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
231231
/** The currently active option, coerced to MdOption type. */
232232
get activeOption(): MdOption | null {
233233
if (this.autocomplete && this.autocomplete._keyManager) {
234-
return this.autocomplete._keyManager.activeItem as MdOption;
234+
return this.autocomplete._keyManager.activeItem;
235235
}
236236

237237
return null;
@@ -439,7 +439,7 @@ export class MdAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
439439
* Clear any previous selected option and emit a selection change event for this option
440440
*/
441441
private _clearPreviousSelectedOption(skip: MdOption) {
442-
this.autocomplete.options.forEach((option) => {
442+
this.autocomplete.options.forEach(option => {
443443
if (option != skip && option.selected) {
444444
option.deselect();
445445
}

src/lib/autocomplete/autocomplete.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ let _uniqueAutocompleteIdCounter = 0;
4343
export class MdAutocomplete implements AfterContentInit {
4444

4545
/** Manages active item in option list based on key events. */
46-
_keyManager: ActiveDescendantKeyManager;
46+
_keyManager: ActiveDescendantKeyManager<MdOption>;
4747

4848
/** Whether the autocomplete panel should be visible, depending on option length. */
4949
showPanel = false;
@@ -66,7 +66,7 @@ export class MdAutocomplete implements AfterContentInit {
6666
constructor(private _changeDetectorRef: ChangeDetectorRef) { }
6767

6868
ngAfterContentInit() {
69-
this._keyManager = new ActiveDescendantKeyManager(this.options).withWrap();
69+
this._keyManager = new ActiveDescendantKeyManager<MdOption>(this.options).withWrap();
7070
}
7171

7272
/**

src/lib/chips/chip-list.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ import {Component, DebugElement, QueryList} from '@angular/core';
33
import {By} from '@angular/platform-browser';
44
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
55
import {MdChipList, MdChipsModule} from './index';
6-
import {FocusKeyManager} from '../core/a11y/focus-key-manager';
6+
import {FocusKeyManager} from '@angular/cdk/a11y';
77
import {createKeyboardEvent} from '@angular/cdk/testing';
88

99
import {MdInputModule} from '../input/index';
1010
import {LEFT_ARROW, RIGHT_ARROW, BACKSPACE, DELETE, TAB} from '../core/keyboard/keycodes';
1111
import {Directionality} from '../core';
1212
import {MdFormFieldModule} from '../form-field/index';
13+
import {MdChip} from './chip';
1314

1415
describe('MdChipList', () => {
1516
let fixture: ComponentFixture<any>;
@@ -18,8 +19,7 @@ describe('MdChipList', () => {
1819
let chipListInstance: MdChipList;
1920
let testComponent: StandardChipList;
2021
let chips: QueryList<any>;
21-
let manager: FocusKeyManager;
22-
22+
let manager: FocusKeyManager<MdChip>;
2323
let dir = 'ltr';
2424

2525
beforeEach(async(() => {

src/lib/chips/chip-list.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
} from '@angular/core';
2222

2323
import {MdChip} from './chip';
24-
import {FocusKeyManager} from '../core/a11y/focus-key-manager';
24+
import {FocusKeyManager} from '@angular/cdk/a11y';
2525
import {BACKSPACE, DELETE, LEFT_ARROW, RIGHT_ARROW, UP_ARROW} from '../core/keyboard/keycodes';
2626
import {Directionality} from '@angular/cdk/bidi';
2727
import {Subscription} from 'rxjs/Subscription';
@@ -77,7 +77,7 @@ export class MdChipList implements AfterContentInit, OnDestroy {
7777
_tabIndex = 0;
7878

7979
/** The FocusKeyManager which handles focus. */
80-
_keyManager: FocusKeyManager;
80+
_keyManager: FocusKeyManager<MdChip>;
8181

8282
/** The chip components contained within this chip list. */
8383
chips: QueryList<MdChip>;
@@ -87,7 +87,7 @@ export class MdChipList implements AfterContentInit, OnDestroy {
8787
}
8888

8989
ngAfterContentInit(): void {
90-
this._keyManager = new FocusKeyManager(this.chips).withWrap();
90+
this._keyManager = new FocusKeyManager<MdChip>(this.chips).withWrap();
9191

9292
// Prevents the chip list from capturing focus and redirecting
9393
// it back to the first chip when the user tabs out.

src/lib/core/a11y/activedescendant-key-manager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export interface Highlightable extends ListKeyManagerOption {
1818
setInactiveStyles(): void;
1919
}
2020

21-
export class ActiveDescendantKeyManager extends ListKeyManager<Highlightable> {
21+
export class ActiveDescendantKeyManager<T> extends ListKeyManager<Highlightable & T> {
2222

2323
/**
2424
* This method sets the active item to the item at the specified index.

src/lib/core/a11y/focus-key-manager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export interface FocusableOption extends ListKeyManagerOption {
1717
focus(): void;
1818
}
1919

20-
export class FocusKeyManager extends ListKeyManager<FocusableOption> {
20+
export class FocusKeyManager<T> extends ListKeyManager<FocusableOption & T> {
2121
/**
2222
* This method sets the active item to the item at the specified index.
2323
* It also adds focuses the newly active item.

src/lib/list/selection-list.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,10 @@ export class MdSelectionList extends _MdSelectionListMixinBase
202202
private _optionDestroyStream: Subscription;
203203

204204
/** The FocusKeyManager which handles focus. */
205-
_keyManager: FocusKeyManager;
205+
_keyManager: FocusKeyManager<MdListOption>;
206206

207207
/** The option components contained within this selection-list. */
208-
@ContentChildren(MdListOption) options;
208+
@ContentChildren(MdListOption) options: QueryList<MdListOption>;
209209

210210
/** options which are selected. */
211211
selectedOptions: SelectionModel<MdListOption> = new SelectionModel<MdListOption>(true);
@@ -223,7 +223,7 @@ export class MdSelectionList extends _MdSelectionListMixinBase
223223
}
224224

225225
ngAfterContentInit(): void {
226-
this._keyManager = new FocusKeyManager(this.options).withWrap();
226+
this._keyManager = new FocusKeyManager<MdListOption>(this.options).withWrap();
227227

228228
if (this.disabled) {
229229
this._tabIndex = -1;

src/lib/menu/menu-directive.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import {AnimationEvent} from '@angular/animations';
2727
import {MenuPositionX, MenuPositionY} from './menu-positions';
2828
import {throwMdMenuInvalidPositionX, throwMdMenuInvalidPositionY} from './menu-errors';
2929
import {MdMenuItem} from './menu-item';
30-
import {FocusKeyManager} from '../core/a11y/focus-key-manager';
30+
import {FocusKeyManager} from '@angular/cdk/a11y';
3131
import {MdMenuPanel} from './menu-panel';
3232
import {Subscription} from 'rxjs/Subscription';
3333
import {transformMenu, fadeInItems} from './menu-animations';
@@ -68,7 +68,7 @@ const MD_MENU_BASE_ELEVATION = 2;
6868
exportAs: 'mdMenu'
6969
})
7070
export class MdMenu implements AfterContentInit, MdMenuPanel, OnDestroy {
71-
private _keyManager: FocusKeyManager;
71+
private _keyManager: FocusKeyManager<MdMenuItem>;
7272
private _xPosition: MenuPositionX = this._defaultOptions.xPosition;
7373
private _yPosition: MenuPositionY = this._defaultOptions.yPosition;
7474
private _previousElevation: string;
@@ -145,7 +145,7 @@ export class MdMenu implements AfterContentInit, MdMenuPanel, OnDestroy {
145145
@Inject(MD_MENU_DEFAULT_OPTIONS) private _defaultOptions: MdMenuDefaultOptions) { }
146146

147147
ngAfterContentInit() {
148-
this._keyManager = new FocusKeyManager(this.items).withWrap();
148+
this._keyManager = new FocusKeyManager<MdMenuItem>(this.items).withWrap();
149149
this._tabSubscription = this._keyManager.tabOut.subscribe(() => this.close.emit('keydown'));
150150
}
151151

src/lib/select/select.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ export class MdSelect extends _MdSelectMixinBase implements AfterContentInit, On
239239
_triggerWidth: number;
240240

241241
/** Manages keyboard events for options in the panel. */
242-
_keyManager: FocusKeyManager;
242+
_keyManager: FocusKeyManager<MdOption>;
243243

244244
/**
245245
* The width of the selected option's value. Must be set programmatically
@@ -737,7 +737,7 @@ export class MdSelect extends _MdSelectMixinBase implements AfterContentInit, On
737737

738738
/** Sets up a key manager to listen to keyboard events on the overlay panel. */
739739
private _initKeyManager() {
740-
this._keyManager = new FocusKeyManager(this.options).withTypeAhead();
740+
this._keyManager = new FocusKeyManager<MdOption>(this.options).withTypeAhead();
741741
this._tabSubscription = this._keyManager.tabOut.subscribe(() => this.close());
742742
}
743743

0 commit comments

Comments
 (0)