Skip to content

Commit

Permalink
fix: improved key manager typings
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
crisbeto committed Aug 13, 2017
1 parent 7f60b09 commit 8e1244f
Show file tree
Hide file tree
Showing 13 changed files with 25 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/cdk/a11y/activedescendant-key-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export interface Highlightable extends ListKeyManagerOption {
setInactiveStyles(): void;
}

export class ActiveDescendantKeyManager extends ListKeyManager<Highlightable> {
export class ActiveDescendantKeyManager<T> extends ListKeyManager<Highlightable & T> {

/**
* This method sets the active item to the item at the specified index.
Expand Down
2 changes: 1 addition & 1 deletion src/cdk/a11y/focus-key-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export interface FocusableOption extends ListKeyManagerOption {
focus(): void;
}

export class FocusKeyManager extends ListKeyManager<FocusableOption> {
export class FocusKeyManager<T> extends ListKeyManager<FocusableOption & T> {
/**
* This method sets the active item to the item at the specified index.
* It also adds focuses the newly active item.
Expand Down
4 changes: 2 additions & 2 deletions src/cdk/a11y/list-key-manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,11 +451,11 @@ describe('Key managers', () => {
});

describe('FocusKeyManager', () => {
let keyManager: FocusKeyManager;
let keyManager: FocusKeyManager<FakeFocusable>;

beforeEach(() => {
itemList.items = [new FakeFocusable(), new FakeFocusable(), new FakeFocusable()];
keyManager = new FocusKeyManager(itemList);
keyManager = new FocusKeyManager<FakeFocusable>(itemList);

// first item is already focused
keyManager.setFirstItemActive();
Expand Down
2 changes: 1 addition & 1 deletion src/demo-app/system-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ System.config({
'@angular/cdk/coercion': 'dist/bundles/cdk-coercion.umd.js',
'@angular/cdk/keycodes': 'dist/bundles/cdk-keycodes.umd.js',
'@angular/cdk/observers': 'dist/bundles/cdk-observers.umd.js',
'@angular/cdk/overlay': 'dist/bundles/cdk-overlay-content.umd.js',
'@angular/cdk/overlay': 'dist/bundles/cdk-overlay.umd.js',
'@angular/cdk/platform': 'dist/bundles/cdk-platform.umd.js',
'@angular/cdk/portal': 'dist/bundles/cdk-portal.umd.js',
'@angular/cdk/rxjs': 'dist/bundles/cdk-rxjs.umd.js',
Expand Down
4 changes: 2 additions & 2 deletions src/lib/autocomplete/autocomplete-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ export class MdAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
/** The currently active option, coerced to MdOption type. */
get activeOption(): MdOption | null {
if (this.autocomplete && this.autocomplete._keyManager) {
return this.autocomplete._keyManager.activeItem as MdOption;
return this.autocomplete._keyManager.activeItem;
}

return null;
Expand Down Expand Up @@ -439,7 +439,7 @@ export class MdAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
* Clear any previous selected option and emit a selection change event for this option
*/
private _clearPreviousSelectedOption(skip: MdOption) {
this.autocomplete.options.forEach((option) => {
this.autocomplete.options.forEach(option => {
if (option != skip && option.selected) {
option.deselect();
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib/autocomplete/autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ let _uniqueAutocompleteIdCounter = 0;
export class MdAutocomplete implements AfterContentInit {

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

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

ngAfterContentInit() {
this._keyManager = new ActiveDescendantKeyManager(this.options).withWrap();
this._keyManager = new ActiveDescendantKeyManager<MdOption>(this.options).withWrap();
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/lib/chips/chip-list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import {Component, DebugElement, QueryList} from '@angular/core';
import {By} from '@angular/platform-browser';
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
import {MdChipList, MdChipsModule} from './index';
import {FocusKeyManager} from '../core/a11y/focus-key-manager';
import {FocusKeyManager} from '@angular/cdk/a11y';
import {createKeyboardEvent} from '@angular/cdk/testing';

import {MdInputModule} from '../input/index';
import {LEFT_ARROW, RIGHT_ARROW, BACKSPACE, DELETE, TAB} from '../core/keyboard/keycodes';
import {Directionality} from '../core';
import {MdFormFieldModule} from '../form-field/index';
import {MdChip} from './chip';

describe('MdChipList', () => {
let fixture: ComponentFixture<any>;
Expand All @@ -18,8 +19,7 @@ describe('MdChipList', () => {
let chipListInstance: MdChipList;
let testComponent: StandardChipList;
let chips: QueryList<any>;
let manager: FocusKeyManager;

let manager: FocusKeyManager<MdChip>;
let dir = 'ltr';

beforeEach(async(() => {
Expand Down
6 changes: 3 additions & 3 deletions src/lib/chips/chip-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
} from '@angular/core';

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

/** The FocusKeyManager which handles focus. */
_keyManager: FocusKeyManager;
_keyManager: FocusKeyManager<MdChip>;

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

ngAfterContentInit(): void {
this._keyManager = new FocusKeyManager(this.chips).withWrap();
this._keyManager = new FocusKeyManager<MdChip>(this.chips).withWrap();

// Prevents the chip list from capturing focus and redirecting
// it back to the first chip when the user tabs out.
Expand Down
2 changes: 1 addition & 1 deletion src/lib/core/a11y/activedescendant-key-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export interface Highlightable extends ListKeyManagerOption {
setInactiveStyles(): void;
}

export class ActiveDescendantKeyManager extends ListKeyManager<Highlightable> {
export class ActiveDescendantKeyManager<T> extends ListKeyManager<Highlightable & T> {

/**
* This method sets the active item to the item at the specified index.
Expand Down
2 changes: 1 addition & 1 deletion src/lib/core/a11y/focus-key-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export interface FocusableOption extends ListKeyManagerOption {
focus(): void;
}

export class FocusKeyManager extends ListKeyManager<FocusableOption> {
export class FocusKeyManager<T> extends ListKeyManager<FocusableOption & T> {
/**
* This method sets the active item to the item at the specified index.
* It also adds focuses the newly active item.
Expand Down
6 changes: 3 additions & 3 deletions src/lib/list/selection-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,10 @@ export class MdSelectionList extends _MdSelectionListMixinBase
private _optionDestroyStream: Subscription;

/** The FocusKeyManager which handles focus. */
_keyManager: FocusKeyManager;
_keyManager: FocusKeyManager<MdListOption>;

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

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

ngAfterContentInit(): void {
this._keyManager = new FocusKeyManager(this.options).withWrap();
this._keyManager = new FocusKeyManager<MdListOption>(this.options).withWrap();

if (this.disabled) {
this._tabIndex = -1;
Expand Down
6 changes: 3 additions & 3 deletions src/lib/menu/menu-directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {AnimationEvent} from '@angular/animations';
import {MenuPositionX, MenuPositionY} from './menu-positions';
import {throwMdMenuInvalidPositionX, throwMdMenuInvalidPositionY} from './menu-errors';
import {MdMenuItem} from './menu-item';
import {FocusKeyManager} from '../core/a11y/focus-key-manager';
import {FocusKeyManager} from '@angular/cdk/a11y';
import {MdMenuPanel} from './menu-panel';
import {Subscription} from 'rxjs/Subscription';
import {transformMenu, fadeInItems} from './menu-animations';
Expand Down Expand Up @@ -68,7 +68,7 @@ const MD_MENU_BASE_ELEVATION = 2;
exportAs: 'mdMenu'
})
export class MdMenu implements AfterContentInit, MdMenuPanel, OnDestroy {
private _keyManager: FocusKeyManager;
private _keyManager: FocusKeyManager<MdMenuItem>;
private _xPosition: MenuPositionX = this._defaultOptions.xPosition;
private _yPosition: MenuPositionY = this._defaultOptions.yPosition;
private _previousElevation: string;
Expand Down Expand Up @@ -145,7 +145,7 @@ export class MdMenu implements AfterContentInit, MdMenuPanel, OnDestroy {
@Inject(MD_MENU_DEFAULT_OPTIONS) private _defaultOptions: MdMenuDefaultOptions) { }

ngAfterContentInit() {
this._keyManager = new FocusKeyManager(this.items).withWrap();
this._keyManager = new FocusKeyManager<MdMenuItem>(this.items).withWrap();
this._tabSubscription = this._keyManager.tabOut.subscribe(() => this.close.emit('keydown'));
}

Expand Down
4 changes: 2 additions & 2 deletions src/lib/select/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ export class MdSelect extends _MdSelectMixinBase implements AfterContentInit, On
_triggerWidth: number;

/** Manages keyboard events for options in the panel. */
_keyManager: FocusKeyManager;
_keyManager: FocusKeyManager<MdOption>;

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

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

Expand Down

0 comments on commit 8e1244f

Please sign in to comment.